Skip to content

Commit

Permalink
Add raster support for fvcom layers without nearest neighbor metadata (
Browse files Browse the repository at this point in the history
…#70)

* Breakout grids to module

* Get all grids working for getmap

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

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

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

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

* satisfy black

* Fix GFI for fvcom

* [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 Apr 17, 2024
1 parent 2c8efb7 commit 9574a71
Show file tree
Hide file tree
Showing 12 changed files with 2,021 additions and 1,827 deletions.
1,769 changes: 0 additions & 1,769 deletions xpublish_wms/grid.py

This file was deleted.

150 changes: 150 additions & 0 deletions xpublish_wms/grids/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from typing import Optional, Sequence, Tuple, Union

import cf_xarray # noqa
import numpy as np
import xarray as xr

from xpublish_wms.grids.fvcom import FVCOMGrid
from xpublish_wms.grids.grid import Grid, RenderMethod
from xpublish_wms.grids.hycom import HYCOMGrid
from xpublish_wms.grids.irregular import IrregularGrid
from xpublish_wms.grids.regular import RegularGrid
from xpublish_wms.grids.roms import ROMSGrid
from xpublish_wms.grids.selfe import SELFEGrid

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


def register_grid_impl(grid_impl: Grid, priority: int = 0):
"""
Register a new grid implementation.
:param grid_impl: The grid implementation to register
:param priority: The priority of the implementation. Highest priority is 0. Default is 0.
"""
_grid_impls.insert(grid_impl, priority)


def grid_factory(ds: xr.Dataset) -> Optional[Grid]:
for grid_impl in _grid_impls:
if grid_impl.recognize(ds):
return grid_impl(ds)

return None


@xr.register_dataset_accessor("gridded")
class GridDatasetAccessor:
_ds: xr.Dataset
_grid: Optional[Grid]

def __init__(self, ds: xr.Dataset):
self._ds = ds
self._grid = grid_factory(ds)

@property
def grid(self) -> Grid:
if self._grid is None:
return None
else:
return self._grid

@property
def name(self) -> str:
if self._grid is None:
return "unsupported"
else:
return self.grid.name

@property
def render_method(self) -> RenderMethod:
if self._grid is None:
return RenderMethod.Quad
else:
return self._grid.render_method

@property
def crs(self) -> str:
if self._grid is None:
return None
else:
return self.grid.crs

def bbox(self, var) -> Tuple[float, float, float, float]:
if self._grid is None:
return None
else:
return self._grid.bbox(var)

def has_elevation(self, da: xr.DataArray) -> bool:
if self._grid is None:
return False
else:
return self._grid.has_elevation(da)

def elevation_units(self, da: xr.DataArray) -> Optional[str]:
if self._grid is None:
return None
else:
return self._grid.elevation_units(da)

def elevation_positive_direction(self, da: xr.DataArray) -> Optional[str]:
if self._grid is None:
return None
else:
return self._grid.elevation_positive_direction(da)

def elevations(self, da: xr.DataArray) -> Optional[xr.DataArray]:
if self._grid is None:
return None
else:
return self._grid.elevations(da)

def select_by_elevation(
self,
da: xr.DataArray,
elevations: Optional[Sequence[float]],
) -> xr.DataArray:
if self._grid is None:
return None
else:
return self._grid.select_by_elevation(da, elevations)

def mask(
self,
da: Union[xr.DataArray, xr.Dataset],
) -> Union[xr.DataArray, xr.Dataset]:
if self._grid is None:
return None
else:
return self._grid.mask(da)

def project(self, da: xr.DataArray, crs: str) -> xr.DataArray:
if self._grid is None:
return None
else:
return self._grid.project(da, crs)

def tessellate(self, da: Union[xr.DataArray, xr.Dataset]) -> np.ndarray:
if self._grid is None:
return None
else:
return self._grid.tessellate(da)

def sel_lat_lng(
self,
subset: xr.Dataset,
lng,
lat,
parameters,
) -> Tuple[xr.Dataset, list, list]:
if self._grid is None:
return None
else:
return self._grid.sel_lat_lng(subset, lng, lat, parameters)
Loading

0 comments on commit 9574a71

Please sign in to comment.