Skip to content

Commit d516cc9

Browse files
used outside time buffer in cmems_nc_to_ini(), expanded testbank (#1087)
* used outside time buffer in cmems_nc_to_ini() * expanded testbank * updated whatsnew
1 parent a20993e commit d516cc9

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

dfm_tools/modelbuilder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from dfm_tools.interpolate_grid2bnd import (ext_add_boundary_object_per_polyline,
1111
open_prepare_dataset,
1212
ds_apply_conversion_dict,
13+
_ds_sel_time_outside,
1314
)
1415

1516
__all__ = [
@@ -110,10 +111,9 @@ def cmems_nc_to_ini(ext_old, dir_output, list_quantities, tstart, dir_pattern, c
110111

111112
tstart_pd = pd.Timestamp(tstart)
112113
tstart_str = tstart_pd.strftime("%Y-%m-%d_%H-%M-%S")
114+
# tstop_pd is slightly higher than tstart_pd to ensure >1 timesteps in all cases
115+
tstop_pd = tstart_pd + pd.Timedelta(hours=1)
113116

114-
# FM needs two timesteps, so convert timestamp to two surrounding timestamps
115-
tstart_round = pd.Timestamp(tstart).floor('1d')
116-
tstop_round = (pd.Timestamp(tstart) + pd.Timedelta(hours=24)).ceil('1d')
117117
for quan_bnd in list_quantities:
118118

119119
if quan_bnd in ["temperaturebnd","uxuyadvectionvelocitybnd"]:
@@ -145,7 +145,7 @@ def cmems_nc_to_ini(ext_old, dir_output, list_quantities, tstart, dir_pattern, c
145145

146146
# subset two times. interp to tstart would be the proper way to do it,
147147
# but FM needs two timesteps for nudge_salinity_temperature and initial waq vars
148-
data_xr = data_xr.sel(time=slice(tstart_round, tstop_round))
148+
data_xr = _ds_sel_time_outside(ds=data_xr, tstart=tstart_pd, tstop=tstop_pd)
149149

150150
# assert that there are at least two timesteps in the resulting dataset
151151
# delft3dfm will crash if there is only one timestep

docs/whats-new.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## UNRELEASED
44

5+
### Feat
6+
- usage of outside time buffer in `dfmt.cmems_nc_to_ini()` so noon-centered or monthly timestamps are also supported in [#1087](https://github.com/Deltares/dfm_tools/pull/1087)
7+
58
### Fix
69
- made p-drive paths for tide models and gesla3 work on linux also in [#1083](https://github.com/Deltares/dfm_tools/pull/1083) and [#1085](https://github.com/Deltares/dfm_tools/pull/1085)
710

tests/test_modelbuilder.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,20 @@ def test_get_ncvarname_list():
3838
assert "quantity 'nonexistingbnd' not in conversion_dict" in str(e.value)
3939

4040

41+
@pytest.mark.parametrize("timecase", [pytest.param(x, id=x) for x in ['midnight','noon','monthly']])
4142
@pytest.mark.systemtest
42-
def test_cmems_nc_to_ini_midnight_centered(tmp_path):
43+
def test_cmems_nc_to_ini(tmp_path, timecase):
44+
"""
45+
tests for midnight-centered data, noon-centered data and monthly timestamped data
46+
"""
4347
ds1 = cmems_dataset_4times().isel(time=slice(None,2))
44-
ds1["time"] = ds1["time"] + pd.Timedelta(hours=12)
4548
ds2 = cmems_dataset_4times().isel(time=slice(2,None))
46-
ds2["time"] = ds2["time"] + pd.Timedelta(hours=12)
49+
if timecase == "midnight":
50+
ds1["time"] = ds1["time"] + pd.Timedelta(hours=12)
51+
ds2["time"] = ds2["time"] + pd.Timedelta(hours=12)
52+
elif timecase == "monthly":
53+
ds1["time"] = [pd.Timestamp("2019-11-01"), pd.Timestamp("2019-12-01")]
54+
ds2["time"] = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]
4755

4856
dir_pattern = os.path.join(tmp_path, "temp_cmems_2day_*.nc")
4957
file_nc1 = dir_pattern.replace("*","so_p1")
@@ -67,8 +75,13 @@ def test_cmems_nc_to_ini_midnight_centered(tmp_path):
6775

6876
file_expected = tmp_path / "nudge_salinity_temperature_2020-01-01_00-00-00.nc"
6977

70-
times_expected = ['2020-01-01 00:00:00', '2020-01-02 00:00:00']
71-
78+
if timecase == "midnight":
79+
times_expected = ['2020-01-01 00:00:00', '2020-01-02 00:00:00']
80+
elif timecase == "noon":
81+
times_expected = ['2019-12-31 12:00:00', '2020-01-01 12:00:00']
82+
elif timecase == "monthly":
83+
times_expected = ['2020-01-01 00:00:00', '2020-02-01 00:00:00']
84+
7285
assert os.path.exists(file_expected)
7386
ds_out = xr.open_dataset(file_expected)
7487

0 commit comments

Comments
 (0)