diff --git a/REQUIREMENTS.txt b/REQUIREMENTS.txt index 5902dc2f..5e8c659c 100644 --- a/REQUIREMENTS.txt +++ b/REQUIREMENTS.txt @@ -9,4 +9,5 @@ pooch cmweather cdsapi xarray +cftime xarray-datatree diff --git a/pydda/constraints/station_data.py b/pydda/constraints/station_data.py index 793350c0..717c0506 100644 --- a/pydda/constraints/station_data.py +++ b/pydda/constraints/station_data.py @@ -5,6 +5,7 @@ import pyart import time + from datetime import datetime, timedelta from six import StringIO @@ -94,8 +95,13 @@ def get_iem_obs(Grid, window=60.0): ) # Get the timestamp for each request - grid_time = datetime.strptime( - Grid["time"].attrs["units"], "seconds since %Y-%m-%dT%H:%M:%SZ" + grid_time = datetime( + year=Grid["time"][0].dt.year.values, + month=Grid["time"][0].dt.month.values, + day=Grid["time"][0].dt.day.values, + hour=Grid["time"][0].dt.hour.values, + minute=Grid["time"][0].dt.minute.values, + second=Grid["time"][0].dt.second.values, ) start_time = grid_time - timedelta(minutes=window / 2.0) end_time = grid_time + timedelta(minutes=window / 2.0) diff --git a/pydda/io/read_grid.py b/pydda/io/read_grid.py index d4378c89..d7727080 100644 --- a/pydda/io/read_grid.py +++ b/pydda/io/read_grid.py @@ -1,5 +1,6 @@ import xarray as xr import numpy as np +import cftime from glob import glob from datatree import DataTree @@ -90,6 +91,7 @@ def read_from_pyart_grid(Grid): origin_latitude = Grid.origin_latitude origin_longitude = Grid.origin_longitude origin_altitude = Grid.origin_altitude + radar_name = Grid.radar_name if len(list(Grid.fields.keys())) > 0: first_grid_name = list(Grid.fields.keys())[0] @@ -97,19 +99,16 @@ def read_from_pyart_grid(Grid): first_grid_name = "" projection = Grid.get_projparams() new_grid = new_grid.to_xarray() - + new_grid.attrs["radar_name"] = radar_name["data"] new_grid["projection"] = xr.DataArray(1, dims=(), attrs=projection) - + new_grid["time"] = cftime.num2date(Grid.time["data"], Grid.time["units"]).astype( + "datetime64[ns]" + ) if "lat_0" in projection.keys(): new_grid["projection"].attrs["_include_lon_0_lat_0"] = "true" else: new_grid["projection"].attrs["_include_lon_0_lat_0"] = "false" - if "units" not in new_grid["time"].attrs.keys(): - new_grid["time"].attrs["units"] = ( - "seconds since %s" - % new_grid["time"].dt.strftime("%Y-%m-%dT%H:%M:%SZ").values[0] - ) new_grid.attrs["first_grid_name"] = first_grid_name x = radar_latitude.pop("data").squeeze() new_grid["radar_latitude"] = xr.DataArray( diff --git a/pydda/tests/test_initialization.py b/pydda/tests/test_initialization.py index 2970cbc6..5c632e71 100644 --- a/pydda/tests/test_initialization.py +++ b/pydda/tests/test_initialization.py @@ -61,10 +61,12 @@ def test_get_iem_data(): ) fdata3 = np.zeros((20, 20, 20)) Grid.add_field("zero_field", {"data": fdata3, "_FillValue": -9999.0}) + Grid.time["units"] = "seconds since 2024-05-22 15:15:46.934000" Grid = pydda.io.read_from_pyart_grid(Grid) station_obs = pydda.constraints.get_iem_obs(Grid) names = [x["site_id"] for x in station_obs] - assert names == ["P28", "WLD", "WDG", "SWO", "END"] + assert "P28" in names + assert "IAB" in names def test_hrrr_data():