Skip to content

Commit 238f3ce

Browse files
Merge pull request #153 from aodn/fix_hourly_interval
Fix centering of hourly interval
2 parents ab01d2a + 96f8040 commit 238f3ce

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

aodntools/timeseries_products/hourly_timeseries.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,22 @@ def PDresample_by_hour(df, function_dict, function_stats):
305305
:param df: pandas dataframe with ancillary variables and coords removed but with TIME as index
306306
:return: pandas dataframe
307307
"""
308-
## back the index 30min
309-
df.index = df.index - pd.Timedelta(30, units='m')
310308

311309
varnames = df.columns
312-
df_data = pd.DataFrame()
310+
df_data = pd.DataFrame(index=pd.DatetimeIndex([]))
313311
for variable in varnames:
314312
ds_var = df[variable]
315-
ds_var_mean = ds_var.resample('1H').apply(function_dict[variable]).astype(np.float32)
313+
ds_var_resample = ds_var.resample('1H', base=0.5) # shift by half hour to centre bin on the hour
314+
ds_var_mean = ds_var_resample.apply(function_dict[variable]).astype(np.float32)
316315
df_data = pd.concat([df_data, ds_var_mean], axis=1, sort=False)
317316
for stat_method in function_stats:
318-
ds_var_stat = ds_var.resample('1H').apply(stat_method).astype(np.float32)
317+
ds_var_stat = ds_var_resample.apply(stat_method).astype(np.float32)
319318
ds_var_stat = ds_var_stat.rename("_".join([variable, stat_method]))
320319
df_data = pd.concat([df_data, ds_var_stat], axis=1, sort=False)
321320

322-
##forward the index 30min
323-
df_data.index = df_data.index + pd.Timedelta(30, units='m')
321+
##forward the index 30min so the timestamps are on the hour
322+
df_data.index += pd.to_timedelta('30min')
323+
324324
return df_data
325325

326326

test_aodntools/timeseries_products/IMOS_ANMN-NRS_STZ_20181213_NRSROT_FV02_hourly-timeseries_END-20190523_C-20220404.nc renamed to test_aodntools/timeseries_products/IMOS_ANMN-NRS_STZ_20181213_NRSROT_FV02_hourly-timeseries_END-20190523_C-20220428.nc

387 KB
Binary file not shown.

test_aodntools/timeseries_products/test_hourly_timeseries.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
]
2222
INPUT_PATHS = [os.path.join(TEST_ROOT, f) for f in INPUT_FILES]
2323
EXPECTED_OUTPUT_FILE = os.path.join(
24-
TEST_ROOT, 'IMOS_ANMN-NRS_STZ_20181213_NRSROT_FV02_hourly-timeseries_END-20190523_C-20220404.nc'
24+
TEST_ROOT, 'IMOS_ANMN-NRS_STZ_20181213_NRSROT_FV02_hourly-timeseries_END-20190523_C-20220428.nc'
2525
)
2626

2727
INST_VARIABLES = {'instrument_id', 'source_file', 'LONGITUDE', 'LATITUDE', 'NOMINAL_DEPTH'}
@@ -89,6 +89,7 @@ def test_hourly_aggregator(self):
8989

9090
# check variable values
9191
expected = Dataset(EXPECTED_OUTPUT_FILE)
92+
self.assertEqual(len(expected['TIME']), len(dataset['TIME']))
9293
compare_vars = ('TIME', 'NOMINAL_DEPTH', 'instrument_index',
9394
'TEMP', 'TEMP_count', 'TEMP_min', 'TEMP_max')
9495
non_match_vars = [var for var in compare_vars

0 commit comments

Comments
 (0)