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

Add fallback nondim grid support #42

Merged
merged 4 commits into from
Nov 9, 2023
Merged
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
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
Loading