Skip to content

Commit 799fc97

Browse files
authored
fix bug in reading hourly timesteps from netcdf files (#268)
* fix bug in reading hourly timesteps from netcdf files * re-run tests
1 parent ce96eea commit 799fc97

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "hf_hydrodata"
3-
version = "1.4.7"
3+
version = "1.4.8"
44
description = "hydroframe tools and utilities"
55
authors = ["William M. Hasling", "Laura Condon", "Reed Maxwell", "George Artavanis", "Amy M. Johnson", "Amy C. Defnet"]
66
license = "MIT"

src/hf_hydrodata/gridded.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,7 +3353,7 @@ def _create_da_indexer(options: dict, entry, data_ds, data_da, file_path: str) -
33533353
)
33543354
elif period == "hourly":
33553355
time_index = int(
3356-
(start_time_value - dimension_start_time).seconds / 3600
3356+
(start_time_value - dimension_start_time).total_seconds() / 3600
33573357
)
33583358
elif period == "monthly":
33593359
time_index = (
@@ -3385,7 +3385,7 @@ def _create_da_indexer(options: dict, entry, data_ds, data_da, file_path: str) -
33853385
end_time_index = (end_time_value - dimension_start_time).days
33863386
elif period == "hourly":
33873387
end_time_index = int(
3388-
(end_time_value - dimension_start_time).seconds / 3600
3388+
(end_time_value - dimension_start_time).total_seconds() / 3600
33893389
)
33903390
elif period == "monthly":
33913391
end_time_index = (

tests/hf_hydrodata/test_gridded.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,3 +2693,29 @@ def test_gridded_file_pfb():
26932693
hf.get_gridded_file(file_path, options)
26942694
data = hf.gridded.read_fast_pfb(file_path)
26952695
assert data.shape == (1, 144, 19, 48)
2696+
2697+
2698+
def test_create_da_indexer_hourly_multiday_time_index():
2699+
"""
2700+
Unit test that _create_da_indexer uses total_seconds() (not .seconds) when
2701+
computing the hourly time index from a netcdf file's time dimension.
2702+
2703+
timedelta.seconds wraps at 86400 (one day), so a start_time that is more
2704+
than 24 hours past the file's first timestep would produce a wrong index.
2705+
For example, 54 hours past the start: .seconds gives 6*3600 → index 6,
2706+
total_seconds() gives 54*3600 → index 54 (correct).
2707+
"""
2708+
dimension_start = datetime.datetime(2005, 10, 1, 0, 0, 0)
2709+
times = [dimension_start + datetime.timedelta(hours=h) for h in range(100)]
2710+
data = np.zeros((100,))
2711+
data_da = xr.DataArray(data, dims=["time"], coords={"time": times}, name="var")
2712+
data_ds = data_da.to_dataset()
2713+
2714+
entry = {"temporal_resolution": "hourly", "grid": None}
2715+
# start_time is 54 hours (2 days + 6 hours) after the dimension start.
2716+
# .seconds would return 6*3600, giving index 6. total_seconds() gives 54.
2717+
options = {"start_time": "2005-10-03 06:00:00"}
2718+
2719+
da_indexers = gr._create_da_indexer(options, entry, data_ds, data_da, "synthetic.nc")
2720+
2721+
assert da_indexers["time"] == 54

0 commit comments

Comments
 (0)