From fc963eee326da53c539fbe5a567e808b6fb917c8 Mon Sep 17 00:00:00 2001 From: Omkar Kabde Date: Mon, 27 May 2024 19:37:56 +0530 Subject: [PATCH] Update xarray version to 2023.10 and Add Support for Python 3.9 (#21) * xarray -> v2023.10.0 * xarray v2023.10.0 * | -> typing.Union --- pyproject.toml | 6 +++++- xarray_subset_grid/accessor.py | 6 +++--- xarray_subset_grid/grid.py | 4 ++-- xarray_subset_grid/grids/sgrid.py | 13 +++++++------ xarray_subset_grid/grids/ugrid.py | 4 +++- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index eb0eeaf..801662d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ diff --git a/xarray_subset_grid/accessor.py b/xarray_subset_grid/accessor.py index f6c95a3..4c14a93 100644 --- a/xarray_subset_grid/accessor.py +++ b/xarray_subset_grid/accessor.py @@ -1,7 +1,7 @@ -from typing import Optional +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 @@ -87,7 +87,7 @@ def subset_vars(self, vars: list[str]) -> xr.Dataset: return self._ds def subset_polygon( - self, polygon: list[tuple[float, float]] | ndarray + self, polygon: Union[list[tuple[float, float]], np.ndarray] ) -> Optional[xr.Dataset]: """Subset the dataset to the grid. diff --git a/xarray_subset_grid/grid.py b/xarray_subset_grid/grid.py index 5979177..25396db 100644 --- a/xarray_subset_grid/grid.py +++ b/xarray_subset_grid/grid.py @@ -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): @@ -47,7 +47,7 @@ def subset_vars(self, ds: xr.Dataset, vars: Iterable[str]) -> list[str]: @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 diff --git a/xarray_subset_grid/grids/sgrid.py b/xarray_subset_grid/grids/sgrid.py index 1f6ea27..33ae6ff 100644 --- a/xarray_subset_grid/grids/sgrid.py +++ b/xarray_subset_grid/grids/sgrid.py @@ -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 @@ -55,7 +56,7 @@ def data_vars(self, ds: xr.Dataset) -> list[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 @@ -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 @@ -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) diff --git a/xarray_subset_grid/grids/ugrid.py b/xarray_subset_grid/grids/ugrid.py index 7a86a5c..2d0007b 100644 --- a/xarray_subset_grid/grids/ugrid.py +++ b/xarray_subset_grid/grids/ugrid.py @@ -1,3 +1,5 @@ +from typing import Union + import numpy as np import xarray as xr @@ -85,7 +87,7 @@ def data_vars(self, ds: xr.Dataset) -> list[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]] | 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