From 2818bcbe4daaaff82470a8900527a345ebafff91 Mon Sep 17 00:00:00 2001 From: Baptiste Vandecrux <35140661+BaptisteVandecrux@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:32:07 +0200 Subject: [PATCH] more instructive error messages + some deprecation fixes --- .../postprocess/real_time_utilities.py | 2 +- src/pypromice/process/L2toL3.py | 19 +++++++++++++++---- tests/unit/qc/test_persistence.py | 18 +++++++++--------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/pypromice/postprocess/real_time_utilities.py b/src/pypromice/postprocess/real_time_utilities.py index 952a69d8..22f4c5fc 100644 --- a/src/pypromice/postprocess/real_time_utilities.py +++ b/src/pypromice/postprocess/real_time_utilities.py @@ -68,7 +68,7 @@ def get_latest_data( # Apply smoothing to z_boom_u # require at least 2 hourly obs? Sometimes seeing once/day data for z_boom_u - df_limited = rolling_window(df_limited, "z_boom_u", "72H", 2, 1) + df_limited = rolling_window(df_limited, "z_boom_u", "72h", 2, 1) # limit to single most recent valid row (convert to series) s_current = df_limited.loc[last_valid_index] diff --git a/src/pypromice/process/L2toL3.py b/src/pypromice/process/L2toL3.py index 292f2d04..e9d9fb1d 100755 --- a/src/pypromice/process/L2toL3.py +++ b/src/pypromice/process/L2toL3.py @@ -100,7 +100,9 @@ def toL3(L2, station_config={}, T_0=273.15): else: logger.info('t_l, p_l or rh_l_cor missing, cannot calulate tubrulent heat fluxes') - # Smoothing and inter/extrapolation of GPS coordinates + if len(station_config)==0: + logger.warning('\n***\nThe station configuration file is missing or improperly passed to pypromice. Some processing steps might fail.\n***\n') + # Smoothing and inter/extrapolation of GPS coordinates for var in ['gps_lat', 'gps_lon', 'gps_alt']: ds[var.replace('gps_','')] = ('time', gps_coordinate_postprocessing(ds, var, station_config)) @@ -108,12 +110,21 @@ def toL3(L2, station_config={}, T_0=273.15): try: ds = process_surface_height(ds, station_config) except Exception as e: - logger.error("Error processing surface height at %s"%station_config['stid']) + logger.error("Error processing surface height at %s"%L2.attrs['station_id']) logging.error(e, exc_info=True) # making sure dataset has the attributes contained in the config files - ds.attrs['project'] = station_config['project'] - ds.attrs['location_type'] = station_config['location_type'] + if 'project' in station_config.keys(): + ds.attrs['project'] = station_config['project'] + else: + logger.error('No project info in station_config. Using \"PROMICE\".') + ds.attrs['project'] = "PROMICE" + + if 'location_type' in station_config.keys(): + ds.attrs['location_type'] = station_config['location_type'] + else: + logger.error('No project info in station_config. Using \"ice sheet\".') + ds.attrs['location_type'] = "ice sheet" return ds diff --git a/tests/unit/qc/test_persistence.py b/tests/unit/qc/test_persistence.py index d343b0bc..0707a9dd 100644 --- a/tests/unit/qc/test_persistence.py +++ b/tests/unit/qc/test_persistence.py @@ -23,10 +23,10 @@ def _test_1_hour_repeat(self, index: int): start="2023-01-26", end="2023-01-27", freq="h", tz="utc", inclusive="left" ) input_series = pd.Series(index=time_range, data=np.arange(0, len(time_range))) - input_series[index + 1] = input_series[index] + input_series.iloc[index + 1] = input_series.iloc[index] min_repeats = 1 expected_output = input_series.map(lambda _: False) - expected_output[index + 1] = True + expected_output.iloc[index + 1] = True persistent_mask = find_persistent_regions( input_series, min_repeats=min_repeats, max_diff=0.001 @@ -62,7 +62,7 @@ def test_persistent_period_longer_than_period_threshold(self): expected_filter_start = 27 expected_filter_end = 33 input_series = pd.Series(index=time_range, data=np.arange(0, len(time_range))) - input_series[index_start:index_end] = input_series[index_start] + input_series.iloc[index_start:index_end] = input_series.iloc[index_start] expected_output = input_series.map(lambda _: False) expected_output[expected_filter_start:expected_filter_end] = True @@ -82,7 +82,7 @@ def test_period_threshold_longer_than_persistent_period(self): index_end = 27 min_repeats = 10 input_series = pd.Series(index=time_range, data=np.arange(0, len(time_range))) - input_series[index_start:index_end] = input_series[index_start] + input_series.iloc[index_start:index_end] = input_series.iloc[index_start] expected_output = input_series.map(lambda _: False) persistent_mask = find_persistent_regions( @@ -101,7 +101,7 @@ def test_persistent_period_at_the_end(self): min_repeats = 4 expected_filter_start = 27 input_series = pd.Series(index=time_range, data=np.arange(0, len(time_range))) - input_series[index_start:] = input_series[index_start] + input_series.iloc[index_start:] = input_series.iloc[index_start] expected_output = input_series.map(lambda _: False) expected_output[expected_filter_start:] = True @@ -121,10 +121,10 @@ def test_dont_filter_nan_values(self): index=time_range, data=np.zeros_like(time_range, dtype="float") ) min_repeats = 4 - input_series[:] = np.nan - input_series[9] = -11 - input_series[10:12] = -10 - input_series[15] = -9 + input_series.iloc[:] = np.nan + input_series.iloc[9] = -11 + input_series.iloc[10:12] = -10 + input_series.iloc[15] = -9 # There are >=4 repeats if the nan values are forward filled. [10:15] == -10 # The output mask shouldn't filter nan values. expected_output = input_series.map(lambda _: False)