diff --git a/dfm_tools/download.py b/dfm_tools/download.py index 7440a3a5a..5e44a14a6 100644 --- a/dfm_tools/download.py +++ b/dfm_tools/download.py @@ -297,3 +297,16 @@ def download_OPeNDAP(dataset_url, print(f'xarray writing netcdf file: {name_output}') data_xr_var_seltime.to_netcdf(os.path.join(dir_output,name_output)) #TODO: add chunks={'time':1} or only possible with opening? data_xr_var_seltime.close() + + +def round_timestamp_to_outer_noon(date_min, date_max): + """ + Since the CMEMS dataset only contains noon-values, we often need to round to the previous or next noon timestep to download enough data. + + """ + + td_12h = pd.Timedelta(hours=12) + date_min = (pd.Timestamp(date_min) + td_12h).floor('1d') - td_12h + date_max = (pd.Timestamp(date_max) - td_12h).ceil('1d') + td_12h + return date_min, date_max + diff --git a/tests/test_download.py b/tests/test_download.py new file mode 100644 index 000000000..fc38a3615 --- /dev/null +++ b/tests/test_download.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +""" +Created on Thu Jun 22 09:41:49 2023 + +@author: veenstra +""" + +import pytest +import pandas as pd +import numpy as np +from dfm_tools.download import round_timestamp_to_outer_noon + + +@pytest.mark.unittest +def test_round_timestamp_to_outer_noon(): + date_min_mod_list = pd.date_range('2020-01-01','2020-01-02',freq='30min') + date_max_mod_list = pd.date_range('2020-01-03','2020-01-04',freq='30min') + + for date_min_mod, date_max_mod in zip(date_min_mod_list, date_max_mod_list): + + date_min, date_max = round_timestamp_to_outer_noon(date_min_mod, date_max_mod) + + assert date_min.hour == 12 + assert date_min.minute == 0 + assert date_max.hour == 12 + assert date_max.minute == 0 + assert date_min <= pd.Timestamp(date_min_mod) + assert date_max >= pd.Timestamp(date_max_mod) + assert np.abs((date_min - pd.Timestamp(date_min_mod)).total_seconds()) < 24*3600 + assert np.abs((date_max - pd.Timestamp(date_max_mod)).total_seconds()) < 24*3600 \ No newline at end of file