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

Allow get_timeinfo to return subhr frequencies #111

Merged
merged 2 commits into from
Aug 10, 2023
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
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
Loading