Skip to content

Commit

Permalink
Add fallback nondim grid support (#42)
Browse files Browse the repository at this point in the history
* Add nondim grid support

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

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

* Fix bare exception

---------

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 Nov 9, 2023
1 parent 7370ce0 commit de0d420
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
78 changes: 77 additions & 1 deletion xpublish_wms/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,75 @@ def project(self, da: xr.DataArray, crs: str) -> xr.DataArray:
return da


class NonDimensionalGrid(Grid):
def __init__(self, ds: xr.Dataset):
self.ds = ds

@staticmethod
def recognize(ds: xr.Dataset) -> bool:
try:
return len(ds.cf["latitude"].dims) == 2
except Exception:
return False

@property
def name(self) -> str:
return "nondimensional"

@property
def render_method(self) -> RenderMethod:
return RenderMethod.Quad

@property
def crs(self) -> str:
return "EPSG:4326"

def project(self, da: xr.DataArray, crs: str) -> xr.DataArray:
if crs == "EPSG:4326":
da = da.assign_coords({"x": da.cf["longitude"], "y": da.cf["latitude"]})
elif crs == "EPSG:3857":
x, y = to_mercator.transform(da.cf["longitude"], da.cf["latitude"])
x_chunks = (
da.cf["longitude"].chunks if da.cf["longitude"].chunks else x.shape
)
y_chunks = da.cf["latitude"].chunks if da.cf["latitude"].chunks else y.shape

da = da.assign_coords(
{
"x": (
da.cf["longitude"].dims,
dask_array.from_array(x, chunks=x_chunks),
),
"y": (
da.cf["latitude"].dims,
dask_array.from_array(y, chunks=y_chunks),
),
},
)

da = da.unify_chunks()
return da

def sel_lat_lng(
self,
subset: xr.Dataset,
lng,
lat,
parameters,
) -> Tuple[xr.Dataset, list, list]:
"""Select the given dataset by the given lon/lat and optional elevation"""
subset = sel2d(
subset[parameters],
lons=subset.cf["longitude"],
lats=subset.cf["latitude"],
lon0=lng,
lat0=lat,
)
x_axis = [strip_float(subset.cf["longitude"])]
y_axis = [strip_float(subset.cf["latitude"])]
return subset, x_axis, y_axis


class ROMSGrid(Grid):
def __init__(self, ds: xr.Dataset):
self.ds = ds
Expand Down Expand Up @@ -691,7 +760,14 @@ def tessellate(self, da: xr.DataArray) -> np.ndarray:
).triangles


_grid_impls = [HYCOMGrid, FVCOMGrid, SELFEGrid, ROMSGrid, RegularGrid]
_grid_impls = [
HYCOMGrid,
FVCOMGrid,
SELFEGrid,
ROMSGrid,
NonDimensionalGrid,
RegularGrid,
]


def register_grid_impl(grid_impl: Grid, priority: int = 0):
Expand Down
4 changes: 2 additions & 2 deletions xpublish_wms/wms/get_feature_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ def get_feature_info(ds: xr.Dataset, query: dict) -> Response:
lat=y_coord[y],
parameters=parameters,
)
except ValueError:
raise HTTPException(500, f"Unsupported grid type: {ds.gridded.name}")
except ValueError as e:
raise HTTPException(500, f"Error with grid type {ds.gridded.name}: {e}")

# When none of the parameters have data, drop it
time_coord_name = selected_ds.cf.coordinates["time"][0]
Expand Down

0 comments on commit de0d420

Please sign in to comment.