Skip to content

Commit

Permalink
Merge pull request #111 from ACCESS-NRI/110_get_timeinfo_bug
Browse files Browse the repository at this point in the history
Allow `get_timeinfo` to return `subhr` frequencies
  • Loading branch information
dougiesquire authored Aug 10, 2023
2 parents 61013e1 + 5f2dbb2 commit 622d8d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/access_nri_intake/source/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ def _todate(t):
frequency = f"{months}mon"
elif dt.days >= 1:
frequency = f"{dt.days}day"
elif dt.seconds >= 3600:
hours = round(dt.seconds / 3600)
frequency = f"{hours}hr"
else:
frequency = f"{dt.seconds // 3600}hr"
frequency = "subhr"

start_time = ts.strftime(time_format)
end_time = te.strftime(time_format)
Expand Down
45 changes: 44 additions & 1 deletion tests/test_source_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
from pathlib import Path

import pytest
import xarray as xr

from access_nri_intake.source.utils import parse_access_filename, parse_access_ncfile
from access_nri_intake.source.utils import (
get_timeinfo,
parse_access_filename,
parse_access_ncfile,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -298,3 +303,41 @@ def test_parse_access_ncfile(test_data, filename, expected):
file = str(test_data / Path(filename))

assert parse_access_ncfile(file) == expected


@pytest.mark.parametrize(
"start_end, expected",
[
([0.0, 0.00625], ("1900-01-01, 00:00:00", "1900-01-01, 00:09:00", "subhr")),
([0.0, 0.125], ("1900-01-01, 00:00:00", "1900-01-01, 03:00:00", "3hr")),
([0.0, 0.25], ("1900-01-01, 00:00:00", "1900-01-01, 06:00:00", "6hr")),
([0.0, 1.0], ("1900-01-01, 00:00:00", "1900-01-02, 00:00:00", "1day")),
([0.0, 31.0], ("1900-01-01, 00:00:00", "1900-02-01, 00:00:00", "1mon")),
([0.0, 90.0], ("1900-01-01, 00:00:00", "1900-04-01, 00:00:00", "3mon")),
([0.0, 365.0], ("1900-01-01, 00:00:00", "1901-01-01, 00:00:00", "1yr")),
([0.0, 730.0], ("1900-01-01, 00:00:00", "1902-01-01, 00:00:00", "2yr")),
],
)
@pytest.mark.parametrize("bounds", [True, False])
def test_get_timeinfo(start_end, expected, bounds):
if bounds:
time = (start_end[0] + start_end[1]) / 2
ds = xr.Dataset(
data_vars={
"dummy": ("time", [0]),
"time_bounds": (("time", "nv"), [start_end]),
},
coords={"time": [time]},
)
ds["time"].attrs = dict(bounds="time_bounds")
else:
ds = xr.Dataset(
data_vars={"dummy": ("time", [0, 0])},
coords={"time": start_end},
)

ds["time"].attrs |= dict(
units="days since 1900-01-01 00:00:00", calendar="GREGORIAN"
)

assert get_timeinfo(ds) == expected

0 comments on commit 622d8d0

Please sign in to comment.