From b27c22d8f4c3f9cc340d781e53db82e7f0b8e4e4 Mon Sep 17 00:00:00 2001 From: Bobby Jackson Date: Fri, 2 Aug 2024 11:38:02 -0500 Subject: [PATCH 1/3] FIX: Writing errors when trying to save PyDDA grids. --- pydda/__init__.py | 2 +- pydda/io/read_grid.py | 8 ++------ setup.py | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/pydda/__init__.py b/pydda/__init__.py index 67a9cc9e..e049a167 100644 --- a/pydda/__init__.py +++ b/pydda/__init__.py @@ -12,7 +12,7 @@ from . import constraints from . import io -__version__ = "2.0.0" +__version__ = "2.0.1" print("Welcome to PyDDA %s" % __version__) print("If you are using PyDDA in your publications, please cite:") diff --git a/pydda/io/read_grid.py b/pydda/io/read_grid.py index ed2b37ae..35bed7c2 100644 --- a/pydda/io/read_grid.py +++ b/pydda/io/read_grid.py @@ -91,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] @@ -98,7 +99,7 @@ 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) if "lat_0" in projection.keys(): @@ -106,11 +107,6 @@ def read_from_pyart_grid(Grid): 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/setup.py b/setup.py index fb25dc22..ba2bc73e 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ PLATFORMS = "Linux, Windows, OSX" MAJOR = 2 MINOR = 0 -MICRO = 0 +MICRO = 1 # SCRIPTS = glob.glob('scripts/*') # TEST_SUITE = 'nose.collector' From d9133a9255190888b3ae27e2df6c3cc1e327c4cb Mon Sep 17 00:00:00 2001 From: Bobby Jackson Date: Fri, 2 Aug 2024 12:42:50 -0500 Subject: [PATCH 2/3] FIX: Time in the xarray object and the IEM OBS reader. --- REQUIREMENTS.txt | 1 + pydda/constraints/station_data.py | 10 ++++++++-- pydda/io/read_grid.py | 5 ++++- pydda/tests/test_initialization.py | 4 +++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/REQUIREMENTS.txt b/REQUIREMENTS.txt index cb8a06ce..146c0ed7 100644 --- a/REQUIREMENTS.txt +++ b/REQUIREMENTS.txt @@ -10,3 +10,4 @@ cmweather cdsapi xarray datatree +cftime 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 35bed7c2..f87cc92f 100644 --- a/pydda/io/read_grid.py +++ b/pydda/io/read_grid.py @@ -1,6 +1,7 @@ import xarray as xr import xradar as xd import numpy as np +import cftime from glob import glob from datatree import DataTree @@ -101,7 +102,9 @@ def read_from_pyart_grid(Grid): 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: 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(): From 15e912ed67df0427a9bfa14057f24e55907dc914 Mon Sep 17 00:00:00 2001 From: Bobby Jackson Date: Fri, 2 Aug 2024 12:46:23 -0500 Subject: [PATCH 3/3] Update REQUIREMENTS.txt --- REQUIREMENTS.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REQUIREMENTS.txt b/REQUIREMENTS.txt index d3f371a5..5e8c659c 100644 --- a/REQUIREMENTS.txt +++ b/REQUIREMENTS.txt @@ -10,4 +10,4 @@ cmweather cdsapi xarray cftime -xarray-datatree \ No newline at end of file +xarray-datatree