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

FIX: Writing errors when trying to save PyDDA grids. #128

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions REQUIREMENTS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pooch
cmweather
cdsapi
xarray
cftime
xarray-datatree
10 changes: 8 additions & 2 deletions pydda/constraints/station_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pyart
import time


from datetime import datetime, timedelta
from six import StringIO

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions pydda/io/read_grid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import xarray as xr
import numpy as np
import cftime

from glob import glob
from datatree import DataTree
Expand Down Expand Up @@ -90,26 +91,24 @@ 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]
else:
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(
Expand Down
4 changes: 3 additions & 1 deletion pydda/tests/test_initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading