diff --git a/src/analysis/forecast_twl.py b/src/analysis/forecast_twl.py index 70edcab..5298c9c 100644 --- a/src/analysis/forecast_twl.py +++ b/src/analysis/forecast_twl.py @@ -316,8 +316,15 @@ def slope_from_profile( """ # Need all data to get the slope - if any([x is None for x in [profile_x, profile_z, top_elevation, btm_elevation]]): - return None + # Check validity of profile arrays and return None if data is not good + for profile in [profile_x, profile_z]: + if all(np.isnan(profile)) or all(x is None for x in profile): + return None + + # Check validity of elevation values + for ele in [top_elevation, btm_elevation]: + if np.isnan(ele) or ele is None: + return None end_points = {"top": {"z": top_elevation}, "btm": {"z": btm_elevation}} @@ -377,6 +384,11 @@ def slope_from_profile( True if end_points["top"]["x"] < pts < end_points["btm"]["x"] else False for pts in profile_x ] + + # Need at least two points to do linear regression + if sum(profile_mask) < 2: + return None + slope_x = np.array(profile_x)[profile_mask].tolist() slope_z = np.array(profile_z)[profile_mask].tolist() slope, _, _, _, _ = stats.linregress(slope_x, slope_z)