Skip to content

Commit

Permalink
Merge branch 'main' into ioos_sprint
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBarker-NOAA committed Jun 4, 2024
2 parents 0dc9efe + fc963ee commit 02f6468
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ classifiers = [

dynamic = ["version"]

dependencies = ["numpy", "xarray", "cf_xarray", "dask[complete]"]
dependencies = [
"numpy",
"xarray>=2023.10.0",
"cf_xarray",
"dask[complete]"]

[project.optional-dependencies]
dev = [
Expand Down
11 changes: 6 additions & 5 deletions xarray_subset_grid/accessor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Optional
import warnings
#from typing import Optional, Union

import numpy as np
import xarray as xr
from numpy import ndarray

from xarray_subset_grid.grid import Grid
from xarray_subset_grid.grids import SGrid, UGrid
Expand Down Expand Up @@ -98,10 +99,10 @@ def subset_vars(self, vars: list[str]) -> xr.Dataset:
return self._grid.subset_vars(self._ds, vars)
return self._ds

def subset_polygon(
self, polygon: list[tuple[float, float]] | ndarray
) -> Optional[xr.Dataset]:
"""Subset the dataset to the grid.
def subset_polygon(self, polygon: list[tuple[float, float]] | np.ndarray
) -> Optional[xr.Dataset]:
"""
Subset the dataset to the grid.
This call is forwarded to the grid implementation with the loaded dataset.
Expand Down
4 changes: 2 additions & 2 deletions xarray_subset_grid/grid.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from abc import ABC, abstractmethod
from collections.abc import Iterable
from typing import Union

import numpy as np
import xarray as xr
from numpy import ndarray


class Grid(ABC):
Expand Down Expand Up @@ -55,7 +55,7 @@ def subset_vars(self, ds: xr.Dataset, vars: Iterable[str]) -> xr.Dataset:

@abstractmethod
def subset_polygon(
self, ds: xr.Dataset, polygon: list[tuple[float, float]] | ndarray
self, ds: xr.Dataset, polygon: Union[list[tuple[float, float]], np.ndarray]
) -> xr.Dataset:
"""Subset the dataset to the grid
:param ds: The dataset to subset
Expand Down
13 changes: 7 additions & 6 deletions xarray_subset_grid/grids/sgrid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union

import numpy as np
import xarray as xr
from numpy import ndarray

from xarray_subset_grid.grid import Grid
from xarray_subset_grid.utils import normalize_polygon_x_coords, ray_tracing_numpy
Expand Down Expand Up @@ -55,7 +56,7 @@ def data_vars(self, ds: xr.Dataset) -> set[str]:
return {var for var in ds.data_vars if not set(ds[var].dims).isdisjoint(dims)}

def subset_polygon(
self, ds: xr.Dataset, polygon: list[tuple[float, float]] | ndarray
self, ds: xr.Dataset, polygon: Union[list[tuple[float, float]], np.ndarray]
) -> xr.Dataset:
"""Subset the dataset to the grid
:param ds: The dataset to subset
Expand Down Expand Up @@ -94,9 +95,7 @@ def subset_polygon(
# to match the original dimension shape
x = np.array(lon.flat)
polygon = normalize_polygon_x_coords(x, polygon)
polygon_mask = ray_tracing_numpy(x, lat.flat, polygon).reshape(
lon.shape
)
polygon_mask = ray_tracing_numpy(x, lat.flat, polygon).reshape(lon.shape)

# Adjust the mask to only mask the rows and columns that are completely
# outside the polygon. If the row and column both touch the target polygon
Expand All @@ -117,7 +116,9 @@ def subset_polygon(
)

# Now we can use the mask to subset the data
ds_subset = ds_subset[vars].where(ds_subset.subset_mask, drop=True).drop_encoding()
ds_subset = (
ds_subset[vars].where(ds_subset.subset_mask, drop=True).drop_encoding()
)

# Add the subsetted dataset to the list for merging
ds_out.append(ds_subset)
Expand Down
4 changes: 3 additions & 1 deletion xarray_subset_grid/grids/ugrid.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

import numpy as np
import xarray as xr

Expand Down Expand Up @@ -96,7 +98,7 @@ def data_vars(self, ds: xr.Dataset) -> set[str]:


def subset_polygon(
self, ds: xr.Dataset, polygon: list[tuple[float, float]] | np.ndarray
self, ds: xr.Dataset, polygon: Union[list[tuple[float, float]], np.ndarray]
) -> xr.Dataset:
"""Subset the dataset to the grid
:param ds: The dataset to subset
Expand Down

0 comments on commit 02f6468

Please sign in to comment.