Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

391 add function to round timestamp to outer noon for cmems #392

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions dfm_tools/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

30 changes: 30 additions & 0 deletions tests/test_download.py
Original file line number Diff line number Diff line change
@@ -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