Skip to content

Commit

Permalink
Fix time indexing and dim ordering for regular datasets (#95)
Browse files Browse the repository at this point in the history
* Fix time indexing and dim ordering for regular datasets

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
mpiannucci and pre-commit-ci[bot] authored Oct 29, 2024
1 parent f13bf08 commit 4f6139a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 7 additions & 1 deletion xpublish_wms/grids/regular.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ def project(
# build new y coordinate
coords["y"] = ("y", da.cf["latitude"].values, da.cf["latitude"].attrs)
# build new data array
shape = da.shape
if shape == (len(da.cf["latitude"]), len(da.cf["longitude"])):
dims = ("y", "x")
else:
dims = ("x", "y")

da = xr.DataArray(
data=da,
dims=("y", "x"),
dims=dims,
coords=coords,
name=da.name,
attrs=da.attrs,
Expand Down
4 changes: 2 additions & 2 deletions xpublish_wms/wms/get_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ def get_capabilities(ds: xr.Dataset, request: Request, query_params: dict) -> Re

additonal_coords = ds.gridded.additional_coords(da)
for coord in additonal_coords:
values = da.cf.coords[coord].values
units = da.cf.coords[coord].attrs.get("units", "")
values = da[coord].values
units = da[coord].attrs.get("units", "")
coord_element = ET.SubElement(
layer,
"Dimension",
Expand Down
6 changes: 4 additions & 2 deletions xpublish_wms/wms/get_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,13 @@ def select_custom_dim(self, da: xr.DataArray) -> xr.DataArray:
for dim, value in self.dim_selectors.items():
if dim in da.coords:
dtype = da[dim].dtype
if np.issubdtype(dtype, np.integer):
if "timedelta" in str(dtype):
value = pd.to_timedelta(value)
elif np.issubdtype(dtype, np.integer):
value = int(value)
elif np.issubdtype(dtype, np.floating):
value = float(value)
da = da.sel({dim: value})
da = da.sel({dim: value}, method="nearest")

# Squeeze single value dimensions
da = da.squeeze()
Expand Down

0 comments on commit 4f6139a

Please sign in to comment.