Skip to content

Commit

Permalink
update utils function and unit test to support get time info from tim…
Browse files Browse the repository at this point in the history
…e variable defined as 'date'
  • Loading branch information
gantian127 committed Oct 30, 2024
1 parent e984f35 commit 66ee707
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
54 changes: 41 additions & 13 deletions src/bmi_era5/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from __future__ import annotations

import os.path
from datetime import datetime

import cdsapi
import cftime
import numpy as np
import xarray as xr


Expand Down Expand Up @@ -81,19 +84,44 @@ def get_time_info(self):

# time values are float in BMI time function
if self._data:
time_info = {
"start_time": float(self._data.valid_time.values[0]),
"time_step": 0.0
if len(self._data.valid_time.values) == 1
else float(
self._data.valid_time.values[1] - self._data.valid_time.values[0]
),
"end_time": float(self._data.valid_time.values[-1]),
"total_steps": len(self._data.valid_time.values),
"time_units": self._data.valid_time.units,
"calendar": self._data.valid_time.calendar,
"time_value": self._data.valid_time.values.astype("float"),
}
if "valid_time" in self._data.keys():
time_info = {
"start_time": float(self._data.valid_time.values[0]),
"time_step": 0.0
if len(self._data.valid_time.values) == 1
else float(
self._data.valid_time.values[1]
- self._data.valid_time.values[0]
),
"end_time": float(self._data.valid_time.values[-1]),
"total_steps": len(self._data.valid_time.values),
"time_units": self._data.valid_time.units,
"calendar": self._data.valid_time.calendar,
"time_value": self._data.valid_time.values.astype("float"),
}
elif "date" in self._data.keys():
# convert date time to CF convention values
date_objs = [
datetime.strptime(str(date_value), "%Y%m%d")
for date_value in self._data.date.values
]
time_units = "seconds since 1970-01-01"
calendar = "proleptic_gregorian"
cf_dates = cftime.date2num(
date_objs, units=time_units, calendar=calendar
)

time_info = {
"start_time": float(cf_dates[0]),
"time_step": 0.0
if len(cf_dates) == 1
else float(cf_dates[1] - cf_dates[0]),
"end_time": float(cf_dates[-1]),
"total_steps": len(cf_dates),
"time_units": time_units,
"calendar": calendar,
"time_value": np.array(cf_dates, dtype=float),
}

return time_info

Expand Down
42 changes: 41 additions & 1 deletion tests/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
)
]

parameters2 = [
(
"reanalysis-era5-single-levels-monthly-means",
"monthly_mean.nc",
{
"product_type": ["monthly_averaged_reanalysis"],
"variable": ["2m_dewpoint_temperature"],
"year": ["2022"],
"month": ["01", "02", "03", "04"],
"time": ["00:00"],
"data_format": "netcdf",
"download_format": "unarchived",
"area": [39, -106, 36, -103],
},
)
]


@pytest.mark.parametrize("name, file, era5_req", parameters)
def test_get_data(tmpdir, name, file, era5_req):
Expand Down Expand Up @@ -76,7 +93,8 @@ def test_get_var_info(tmpdir, name, file, era5_req):


@pytest.mark.parametrize("name, file, era5_req", parameters)
def test_get_time_info(tmpdir, name, file, era5_req):
def test_get_time_info_valid_time(tmpdir, name, file, era5_req):
"""Test when time variable is valid_time"""
path = os.path.join(tmpdir, file)

era5 = Era5Data()
Expand All @@ -93,3 +111,25 @@ def test_get_time_info(tmpdir, name, file, era5_req):
assert time_info_2["total_steps"] == 3
assert time_info_2["time_units"] == "seconds since 1970-01-01"
assert time_info_2["calendar"] == "proleptic_gregorian"


@pytest.mark.parametrize("name, file, era5_req", parameters2)
def test_get_time_info_date(tmpdir, name, file, era5_req):
"""Test when time variable is date"""

path = os.path.join(tmpdir, file)

era5 = Era5Data()
time_info_1 = era5.get_time_info()

assert time_info_1 == {}

era5.get_data(name, era5_req, path)
time_info_2 = era5.get_time_info()

assert time_info_2["start_time"] == 1640995200.0
assert time_info_2["end_time"] == 1648771200.0
assert time_info_2["time_step"] == 2678400.0
assert time_info_2["total_steps"] == 4
assert time_info_2["time_units"] == "seconds since 1970-01-01"
assert time_info_2["calendar"] == "proleptic_gregorian"

0 comments on commit 66ee707

Please sign in to comment.