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

Temporal res #7

Merged
merged 2 commits into from
Dec 5, 2022
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
21 changes: 12 additions & 9 deletions c3s_eqc_data_checker/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,23 +296,26 @@ def check_temporal_resolution(
self,
min: datetime.date | datetime.datetime | str | None,
max: datetime.date | datetime.datetime | str | None,
resolution: str | None,
frequency: str | None,
name: str | None,
) -> dict[str, Any]: # noqa: D205, D400
"""
[temporal_resolution]
# Check temporal resolution.
#
# See pandas frequency aliases:
# https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases
#
# Arguments:
# * min: first time (optional)
# * max: last time (optional)
# * resolution: time resolution (optional)
# * frequency: time frequency (optional).
# * name: name of time dimension (optional, default: "time")
#
# Example:
min = 1900-01-01
max = 1900-01-02
resolution = "1 days"
frequency = "1D"
name = "time"
"""
if name is None:
Expand All @@ -334,12 +337,12 @@ def check_temporal_resolution(
if (actual_max := time.max()) != pd.to_datetime(max):
errors["max"] = str(actual_max.values)

if resolution is not None:
res_td = pd.to_timedelta(resolution)
if time.size <= 1 and res_td != pd.to_timedelta(0):
errors["resolution"] = "0"
elif ((actual_res := time.diff(name)) != res_td).any():
errors["resolution"] = {str(value) for value in actual_res.values}
if frequency is not None:
expected_time = pd.date_range(
time.min().values, time.max().values, freq=frequency
)
if time.size != expected_time.size or not (time == expected_time).all():
errors["frequency"] = {str(value) for value in time.diff(name).values}

return errors

Expand Down
12 changes: 7 additions & 5 deletions tests/test_10_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,21 @@ def test_cf_compliance(tmp_path: pathlib.Path) -> None:


def test_temporal_resolution(tmp_path: pathlib.Path) -> None:
for date in pd.date_range("1900-01-01", "1900-01-02", freq="1D"):
for date in pd.date_range("1900-01-01", "1900-02-01", freq="1MS"):
da = xr.DataArray(date, name="time")
da.to_netcdf(tmp_path / f"{date}.nc")

checker = Checker(str(tmp_path / "*.nc"), files_format="NETCDF")
actual = checker.check_temporal_resolution("1900-01-01", "1900-01-02", "1D", "time")
actual = checker.check_temporal_resolution(
"1900-01-01", "1900-02-01", "1MS", "time"
)
assert actual == {}

actual = checker.check_temporal_resolution("2000-01-01", "2000-01-02", "2D", "time")
actual = checker.check_temporal_resolution("2000-01-01", "2000-02-01", "1D", "time")
expected = {
"min": "1900-01-01T00:00:00.000000000",
"max": "1900-01-02T00:00:00.000000000",
"resolution": {"86400000000000 nanoseconds"},
"max": "1900-02-01T00:00:00.000000000",
"frequency": {"2678400000000000 nanoseconds"},
}
assert actual == expected

Expand Down
3 changes: 1 addition & 2 deletions tests/test_20_grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_cf_compliance(tmp_path: pathlib.Path, grib_path: pathlib.Path) -> None:
def test_temporal_resolution(grib_path: pathlib.Path) -> None:
checker = Checker(str(grib_path / "GRIB2.tmpl"), files_format="GRIB")
actual = checker.check_temporal_resolution(
"2007-03-23T12", "2007-03-23T12", "0", None
"2007-03-23T12", "2007-03-23T12", None, None
)
assert actual == {}

Expand All @@ -81,7 +81,6 @@ def test_temporal_resolution(grib_path: pathlib.Path) -> None:
expected = {
"max": "2007-03-23T12:00:00.000000000",
"min": "2007-03-23T12:00:00.000000000",
"resolution": "0",
}
assert actual == expected

Expand Down