From 7b5c58f32a38137dcb092bfdde871478b93f6e57 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick Date: Fri, 6 Sep 2024 11:55:01 -0500 Subject: [PATCH 1/9] Deprecated remapping without accessor --- docs/user_api/index.rst | 22 ++++++++++++--- uxarray/core/dataarray.py | 59 --------------------------------------- uxarray/core/dataset.py | 59 --------------------------------------- 3 files changed, 18 insertions(+), 122 deletions(-) diff --git a/docs/user_api/index.rst b/docs/user_api/index.rst index ac521c048..9cff549bb 100644 --- a/docs/user_api/index.rst +++ b/docs/user_api/index.rst @@ -54,9 +54,16 @@ Remapping --------- .. autosummary:: :toctree: generated/ + :template: autosummary/accessor.rst + + UxDataArray.remap + +.. autosummary:: + :toctree: generated/ + :template: autosummary/accessor.rst - UxDataset.nearest_neighbor_remap - UxDataset.inverse_distance_weighted_remap + UxDataset.remap.nearest_neighbor_remap + UxDataset.remap.inverse_distance_weighted_remap Plotting -------- @@ -132,9 +139,16 @@ Remapping --------- .. autosummary:: :toctree: generated/ + :template: autosummary/accessor.rst + + UxDataArray.remap + +.. autosummary:: + :toctree: generated/ + :template: autosummary/accessor_method.rst - UxDataArray.nearest_neighbor_remap - UxDataArray.inverse_distance_weighted_remap + UxDataArray.remap.nearest_neighbor_remap + UxDataArray.remap.inverse_distance_weighted_remap UxDataArray.nodal_average Plotting diff --git a/uxarray/core/dataarray.py b/uxarray/core/dataarray.py index b6f80cb73..d39d50535 100644 --- a/uxarray/core/dataarray.py +++ b/uxarray/core/dataarray.py @@ -285,65 +285,6 @@ def to_dataset( return uxds - def nearest_neighbor_remap( - self, - destination_obj: Union[Grid, UxDataArray, UxDataset], - remap_to: str = "nodes", - coord_type: str = "spherical", - ): - """Nearest Neighbor Remapping between a source (``UxDataArray``) and - destination.`. - - Parameters - --------- - destination_obj : Grid, UxDataArray, UxDataset - Destination for remapping - remap_to : str, default="nodes" - Location of where to map data, either "nodes" or "face centers" - coord_type : str, default="spherical" - Indicates whether to remap using on spherical or cartesian coordinates - """ - warn( - "This usage of remapping will be deprecated in a future release. It is advised to use uxds.remap.nearest_neighbor() instead.", - DeprecationWarning, - ) - - return self.remap.nearest_neighbor(destination_obj, remap_to, coord_type) - - def inverse_distance_weighted_remap( - self, - destination_obj: Union[Grid, UxDataArray, UxDataset], - remap_to: str = "nodes", - coord_type: str = "spherical", - power=2, - k=8, - ): - """Inverse Distance Weighted Remapping between a source - (``UxDataArray``) and destination.`. - - Parameters - --------- - destination_obj : Grid, UxDataArray, UxDataset - Destination for remapping - remap_to : str, default="nodes" - Location of where to map data, either "nodes" or "face centers" - coord_type : str, default="spherical" - Indicates whether to remap using on spherical or cartesian coordinates - power : int, default=2 - Power parameter for inverse distance weighting. This controls how local or global the remapping is, a higher - power causes points that are further away to have less influence - k : int, default=8 - Number of nearest neighbors to consider in the weighted calculation. - """ - warn( - "This usage of remapping will be deprecated in a future release. It is advised to use uxds.remap.inverse_distance_weighted() instead.", - DeprecationWarning, - ) - - return self.remap.inverse_distance_weighted( - destination_obj, remap_to, coord_type, power, k - ) - def integrate( self, quadrature_rule: Optional[str] = "triangular", order: Optional[int] = 4 ) -> UxDataArray: diff --git a/uxarray/core/dataset.py b/uxarray/core/dataset.py index 9a2f522a0..b4c69dce4 100644 --- a/uxarray/core/dataset.py +++ b/uxarray/core/dataset.py @@ -337,62 +337,3 @@ def to_array(self) -> UxDataArray: xarr = super().to_array() return UxDataArray(xarr, uxgrid=self.uxgrid) - - def nearest_neighbor_remap( - self, - destination_obj: Union[Grid, UxDataArray, UxDataset], - remap_to: str = "nodes", - coord_type: str = "spherical", - ): - """Nearest Neighbor Remapping between a source (``UxDataset``) and - destination.`. - - Parameters - --------- - destination_obj : Grid, UxDataArray, UxDataset - Destination for remapping - remap_to : str, default="nodes" - Location of where to map data, either "nodes", "edge centers", or "face centers" - coord_type : str, default="spherical" - Indicates whether to remap using on spherical or cartesian coordinates - """ - warn( - "This usage of remapping will be deprecated in a future release. It is advised to use uxds.remap.nearest_neighbor() instead.", - DeprecationWarning, - ) - - return self.remap.nearest_neighbor(destination_obj, remap_to, coord_type) - - def inverse_distance_weighted_remap( - self, - destination_obj: Union[Grid, UxDataArray, UxDataset], - remap_to: str = "nodes", - coord_type: str = "spherical", - power=2, - k=8, - ): - """Inverse Distance Weighted Remapping between a source (``UxDataset``) - and destination.`. - - Parameters - --------- - destination_obj : Grid, UxDataArray, UxDataset - Destination for remapping - remap_to : str, default="nodes" - Location of where to map data, either "nodes", "edge centers", or "face centers" - coord_type : str, default="spherical" - Indicates whether to remap using on spherical or cartesian coordinates - power : int, default=2 - Power parameter for inverse distance weighting. This controls how local or global the remapping is, a higher - power causes points that are further away to have less influence - k : int, default=8 - Number of nearest neighbors to consider in the weighted calculation. - """ - warn( - "This usage of remapping will be deprecated in a future release. It is advised to use uxds.remap.inverse_distance_weighted() instead.", - DeprecationWarning, - ) - - return self.remap.inverse_distance_weighted( - destination_obj, remap_to, coord_type, power, k - ) From b9e9bb204f2cd91b5ef575d0e20de4c83cd4f0b2 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick Date: Fri, 6 Sep 2024 11:55:54 -0500 Subject: [PATCH 2/9] Pre-commit --- uxarray/core/dataarray.py | 4 +--- uxarray/core/dataset.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/uxarray/core/dataarray.py b/uxarray/core/dataarray.py index d39d50535..8f46e4220 100644 --- a/uxarray/core/dataarray.py +++ b/uxarray/core/dataarray.py @@ -4,7 +4,7 @@ import numpy as np -from typing import TYPE_CHECKING, Optional, Union, Hashable, Literal +from typing import TYPE_CHECKING, Optional, Hashable, Literal from uxarray.formatting_html import array_repr @@ -20,8 +20,6 @@ from xarray.core.utils import UncachedAccessor -from warnings import warn - from uxarray.core.gradient import ( _calculate_grad_on_edge_from_faces, diff --git a/uxarray/core/dataset.py b/uxarray/core/dataset.py index b4c69dce4..23843f9ee 100644 --- a/uxarray/core/dataset.py +++ b/uxarray/core/dataset.py @@ -5,7 +5,7 @@ import sys -from typing import Optional, IO, Union +from typing import Optional, IO from uxarray.grid import Grid from uxarray.core.dataarray import UxDataArray From 67e90bfdbf18b2d9b1e51b91dddaad38a380cd20 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick <95507181+aaronzedwick@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:34:23 -0500 Subject: [PATCH 3/9] Update docs/user_api/index.rst Co-authored-by: Philip Chmielowiec <67855069+philipc2@users.noreply.github.com> --- docs/user_api/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user_api/index.rst b/docs/user_api/index.rst index de595a5e0..47882390b 100644 --- a/docs/user_api/index.rst +++ b/docs/user_api/index.rst @@ -62,7 +62,7 @@ Remapping :toctree: generated/ :template: autosummary/accessor.rst - UxDataset.remap.nearest_neighbor_remap + UxDataset.remap.nearest_neighbor UxDataset.remap.inverse_distance_weighted_remap Plotting From f7ed1670896a550518c334c758fc1cbd917f3580 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick <95507181+aaronzedwick@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:34:51 -0500 Subject: [PATCH 4/9] Update docs/user_api/index.rst Co-authored-by: Philip Chmielowiec <67855069+philipc2@users.noreply.github.com> --- docs/user_api/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user_api/index.rst b/docs/user_api/index.rst index 47882390b..7a90380bf 100644 --- a/docs/user_api/index.rst +++ b/docs/user_api/index.rst @@ -63,7 +63,7 @@ Remapping :template: autosummary/accessor.rst UxDataset.remap.nearest_neighbor - UxDataset.remap.inverse_distance_weighted_remap + UxDataset.remap.inverse_distance_weighted Plotting -------- From 2f63c6539035e8d1634c0ec25358db07d0840d89 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick <95507181+aaronzedwick@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:34:57 -0500 Subject: [PATCH 5/9] Update docs/user_api/index.rst Co-authored-by: Philip Chmielowiec <67855069+philipc2@users.noreply.github.com> --- docs/user_api/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user_api/index.rst b/docs/user_api/index.rst index 7a90380bf..75ca37e61 100644 --- a/docs/user_api/index.rst +++ b/docs/user_api/index.rst @@ -147,7 +147,7 @@ Remapping :toctree: generated/ :template: autosummary/accessor_method.rst - UxDataArray.remap.nearest_neighbor_remap + UxDataArray.remap.nearest_neighbor UxDataArray.remap.inverse_distance_weighted_remap UxDataArray.nodal_average From dd26f742c030aeb69e10ab4f5342c2d910e0f498 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick <95507181+aaronzedwick@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:35:03 -0500 Subject: [PATCH 6/9] Update docs/user_api/index.rst Co-authored-by: Philip Chmielowiec <67855069+philipc2@users.noreply.github.com> --- docs/user_api/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user_api/index.rst b/docs/user_api/index.rst index 75ca37e61..ff8a400c6 100644 --- a/docs/user_api/index.rst +++ b/docs/user_api/index.rst @@ -148,7 +148,7 @@ Remapping :template: autosummary/accessor_method.rst UxDataArray.remap.nearest_neighbor - UxDataArray.remap.inverse_distance_weighted_remap + UxDataArray.remap.inverse_distance_weighted UxDataArray.nodal_average Plotting From b93a6e3b5cfbf3fec95e19d52ec036a70f49a186 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick Date: Tue, 10 Sep 2024 09:29:51 -0500 Subject: [PATCH 7/9] Removed Nodal Average --- docs/user_api/index.rst | 1 - test/test_dataarray.py | 26 -------------------------- test/test_plot.py | 3 +-- uxarray/core/dataarray.py | 16 +--------------- 4 files changed, 2 insertions(+), 44 deletions(-) diff --git a/docs/user_api/index.rst b/docs/user_api/index.rst index ff8a400c6..6a4ee1611 100644 --- a/docs/user_api/index.rst +++ b/docs/user_api/index.rst @@ -149,7 +149,6 @@ Remapping UxDataArray.remap.nearest_neighbor UxDataArray.remap.inverse_distance_weighted - UxDataArray.nodal_average Plotting -------- diff --git a/test/test_dataarray.py b/test/test_dataarray.py index 9d0408411..7cc438a83 100644 --- a/test/test_dataarray.py +++ b/test/test_dataarray.py @@ -113,29 +113,3 @@ def test_geodataframe_caching(self): # override will recompute the grid assert gdf_start is not gdf_end - - def test_nodal_average(self): - - # test on a node-centered dataset - uxds = ux.open_dataset(gridfile_geoflow, dsfile_v1_geoflow) - - v1_nodal_average = uxds['v1'].nodal_average() - - # final dimension should match number of faces - self.assertEqual(v1_nodal_average.shape[-1], uxds.uxgrid.n_face) - - # all other dimensions should remain unchanged - self.assertEqual(uxds['v1'].shape[0:-1], v1_nodal_average.shape[0:-1]) - - # test on a sample mesh with 4 verts - verts = [[[-170, 40], [180, 30], [165, 25], [-170, 20]]] - data = [1, 2, 3, 4] - - uxgrid = ux.open_grid(verts, latlon=True) - - uxda = ux.UxDataArray(uxgrid=uxgrid, data=data, dims=('n_node')) - - uxda_nodal_average = uxda.nodal_average() - - # resulting data should be the mean of the corner nodes of the single face - self.assertEqual(uxda_nodal_average, np.mean(data)) diff --git a/test/test_plot.py b/test/test_plot.py index 9a503c8ea..c46806e18 100644 --- a/test/test_plot.py +++ b/test/test_plot.py @@ -80,7 +80,7 @@ def test_node_centered_data(self): uxds['v1'][0][0].plot.points(backend=backend) - uxds['v1'][0][0].nodal_average().plot.polygons(backend=backend) + uxds['v1'][0][0].topological_mean(destination='face').plot.polygons(backend=backend) def test_clabel(self): @@ -104,7 +104,6 @@ def test_dataset(self): # plot.hist() is an xarray method assert hasattr(uxds['v1'].plot, 'hist') - def test_dataarray(self): """Tests whether a Xarray Dataset method can be called through the UxDataset plotting accessor.""" diff --git a/uxarray/core/dataarray.py b/uxarray/core/dataarray.py index 8f46e4220..62536ce38 100644 --- a/uxarray/core/dataarray.py +++ b/uxarray/core/dataarray.py @@ -338,20 +338,6 @@ def integrate( return uxda - def nodal_average(self): - """Computes the Nodal Average of a Data Variable, which is the mean of - the nodes that surround each face. - - Can be used for remapping node-centered data to each face. - """ - - warnings.warn( - "This function will be deprecated in a future release. Please use uxda.mean(destination=`face`) instead.", - DeprecationWarning, - ) - - return self.topological_mean(destination="face") - def topological_mean( self, destination: Literal["node", "edge", "face"], @@ -825,7 +811,7 @@ def gradient( Face-centered variable >>> uxds['var'].gradient() Node-centered variable - >>> uxds['var'].nodal_average().gradient() + >>> uxds['var'].topological_mean(destination="face").gradient() """ if not self._face_centered(): From f30f59479da77cc2a4dd2d87bedc9924d716bb49 Mon Sep 17 00:00:00 2001 From: Aaron Zedwick Date: Wed, 11 Sep 2024 16:08:29 -0500 Subject: [PATCH 8/9] Removing destination_obj --- uxarray/remap/dataarray_accessor.py | 54 ++++----------------- uxarray/remap/dataset_accessor.py | 56 ++++------------------ uxarray/remap/inverse_distance_weighted.py | 15 ++---- 3 files changed, 20 insertions(+), 105 deletions(-) diff --git a/uxarray/remap/dataarray_accessor.py b/uxarray/remap/dataarray_accessor.py index 4a5f21dfe..a7e925c87 100644 --- a/uxarray/remap/dataarray_accessor.py +++ b/uxarray/remap/dataarray_accessor.py @@ -31,8 +31,7 @@ def __repr__(self): def nearest_neighbor( self, - destination_grid: Optional[Grid] = None, - destination_obj: Optional[Grid, UxDataArray, UxDataset] = None, + destination_grid: Grid, remap_to: str = "face centers", coord_type: str = "spherical", ): @@ -43,38 +42,19 @@ def nearest_neighbor( --------- destination_grid : Grid Destination Grid for remapping - destination_obj : Grid, UxDataArray, UxDataset - Optional destination for remapping, deprecating remap_to : str, default="nodes" Location of where to map data, either "nodes" or "face centers" coord_type : str, default="spherical" Indicates whether to remap using on spherical or cartesian coordinates """ - if destination_grid is not None and destination_obj is not None: - raise ValueError( - "Only one destination allowed, " - "please remove either `destination_grid` or `destination_obj`." - ) - elif destination_grid is None and destination_obj is None: - raise ValueError("Destination needed for remap.") - if destination_grid is not None: - return _nearest_neighbor_uxda( - self.uxda, destination_grid, remap_to, coord_type - ) - elif destination_obj is not None: - warn( - "destination_obj will be deprecated in a future release. Please use destination_grid instead.", - DeprecationWarning, - ) - return _nearest_neighbor_uxda( - self.uxda, destination_obj, remap_to, coord_type - ) + return _nearest_neighbor_uxda( + self.uxda, destination_grid, remap_to, coord_type + ) def inverse_distance_weighted( self, - destination_grid: Optional[Grid] = None, - destination_obj: Optional[Grid, UxDataArray, UxDataset] = None, + destination_grid: Grid, remap_to: str = "face centers", coord_type: str = "spherical", power=2, @@ -87,8 +67,6 @@ def inverse_distance_weighted( --------- destination_grid : Grid Destination Grid for remapping - destination_obj : Grid, UxDataArray, UxDataset - Optional destination for remapping, deprecating remap_to : str, default="nodes" Location of where to map data, either "nodes" or "face centers" coord_type : str, default="spherical" @@ -99,23 +77,7 @@ def inverse_distance_weighted( k : int, default=8 Number of nearest neighbors to consider in the weighted calculation. """ - if destination_grid is not None and destination_obj is not None: - raise ValueError( - "Only one destination allowed, " - "please remove either `destination_grid` or `destination_obj`." - ) - elif destination_grid is None and destination_obj is None: - raise ValueError("Destination needed for remap.") - if destination_grid is not None: - return _inverse_distance_weighted_remap_uxda( - self.uxda, destination_grid, remap_to, coord_type, power, k - ) - elif destination_obj is not None: - warn( - "destination_obj will be deprecated in a future release. Please use destination_grid instead.", - DeprecationWarning, - ) - return _inverse_distance_weighted_remap_uxda( - self.uxda, destination_obj, remap_to, coord_type, power, k - ) + return _inverse_distance_weighted_remap_uxda( + self.uxda, destination_grid, remap_to, coord_type, power, k + ) diff --git a/uxarray/remap/dataset_accessor.py b/uxarray/remap/dataset_accessor.py index d5a9edf3f..bafe52934 100644 --- a/uxarray/remap/dataset_accessor.py +++ b/uxarray/remap/dataset_accessor.py @@ -31,8 +31,7 @@ def __repr__(self): def nearest_neighbor( self, - destination_grid: Optional[Grid] = None, - destination_obj: Optional[Grid, UxDataArray, UxDataset] = None, + destination_grid: Grid, remap_to: str = "face centers", coord_type: str = "spherical", ): @@ -43,39 +42,19 @@ def nearest_neighbor( --------- destination_grid : Grid Destination Grid for remapping - destination_obj : Grid, UxDataArray, UxDataset - Optional destination for remapping, deprecating remap_to : str, default="nodes" Location of where to map data, either "nodes", "edge centers", or "face centers" coord_type : str, default="spherical" Indicates whether to remap using on spherical or cartesian coordinates """ - if destination_grid is not None and destination_obj is not None: - raise ValueError( - "Only one destination allowed, " - "please remove either `destination_grid` or `destination_obj`." - ) - elif destination_grid is None and destination_obj is None: - raise ValueError("Destination needed for remap.") - - if destination_grid is not None: - return _nearest_neighbor_uxds( - self.uxds, destination_grid, remap_to, coord_type - ) - elif destination_obj is not None: - warn( - "destination_obj will be deprecated in a future release. Please use destination_grid instead.", - DeprecationWarning, - ) - return _nearest_neighbor_uxds( - self.uxds, destination_obj, remap_to, coord_type - ) + return _nearest_neighbor_uxds( + self.uxds, destination_grid, remap_to, coord_type + ) def inverse_distance_weighted( self, - destination_grid: Optional[Grid] = None, - destination_obj: Optional[Grid, UxDataArray, UxDataset] = None, + destination_grid: Grid, remap_to: str = "face centers", coord_type: str = "spherical", power=2, @@ -88,8 +67,6 @@ def inverse_distance_weighted( --------- destination_grid : Grid Destination Grid for remapping - destination_obj : Grid, UxDataArray, UxDataset - Optional destination for remapping, deprecating remap_to : str, default="nodes" Location of where to map data, either "nodes", "edge centers", or "face centers" coord_type : str, default="spherical" @@ -101,23 +78,6 @@ def inverse_distance_weighted( Number of nearest neighbors to consider in the weighted calculation. """ - if destination_grid is not None and destination_obj is not None: - raise ValueError( - "Only one destination allowed, " - "please remove either `destination_grid` or `destination_obj`." - ) - elif destination_grid is None and destination_obj is None: - raise ValueError("Destination needed for remap.") - - if destination_grid is not None: - return _inverse_distance_weighted_remap_uxds( - self.uxds, destination_grid, remap_to, coord_type, power, k - ) - elif destination_obj is not None: - warn( - "destination_obj will be deprecated in a future release. Please use destination_grid instead.", - DeprecationWarning, - ) - return _inverse_distance_weighted_remap_uxds( - self.uxds, destination_obj, remap_to, coord_type, power, k - ) + return _inverse_distance_weighted_remap_uxds( + self.uxds, destination_grid, remap_to, coord_type, power, k + ) diff --git a/uxarray/remap/inverse_distance_weighted.py b/uxarray/remap/inverse_distance_weighted.py index 219ed0fb2..05f9d8451 100644 --- a/uxarray/remap/inverse_distance_weighted.py +++ b/uxarray/remap/inverse_distance_weighted.py @@ -159,7 +159,7 @@ def _inverse_distance_weighted_remap( def _inverse_distance_weighted_remap_uxda( source_uxda: UxDataArray, - destination_obj: Union[Grid, UxDataArray, UxDataset], + destination_grid: Grid, remap_to: str = "face centers", coord_type: str = "spherical", power=2, @@ -171,8 +171,8 @@ def _inverse_distance_weighted_remap_uxda( --------- source_uxda : UxDataArray Source UxDataArray for remapping - destination_obj : Grid, UxDataArray, UxDataset - Destination for remapping + destination_grid : Grid + Destination grid for remapping remap_to : str, default="nodes" Location of where to map data, either "nodes", "edge centers", or "face centers" coord_type : str, default="spherical" @@ -206,14 +206,7 @@ def _inverse_distance_weighted_remap_uxda( destination_dims = list(source_uxda.dims) destination_dims[-1] = destination_dim - if isinstance(destination_obj, Grid): - destination_grid = destination_obj - elif isinstance( - destination_obj, - (uxarray.core.dataarray.UxDataArray, uxarray.core.dataset.UxDataset), - ): - destination_grid = destination_obj.uxgrid - else: + if not isinstance(destination_grid, Grid): raise ValueError("TODO: Invalid Input") # perform remapping From 3fc852520e12ec7461915dff853402a608cfdaab Mon Sep 17 00:00:00 2001 From: Aaron Zedwick Date: Thu, 12 Sep 2024 09:53:07 -0500 Subject: [PATCH 9/9] Updated test cases for `destination_grid` --- test/test_remap.py | 70 +++------------------- uxarray/remap/dataarray_accessor.py | 8 +-- uxarray/remap/dataset_accessor.py | 8 +-- uxarray/remap/inverse_distance_weighted.py | 38 +++--------- uxarray/remap/nearest_neighbor.py | 50 +++------------- 5 files changed, 27 insertions(+), 147 deletions(-) diff --git a/test/test_remap.py b/test/test_remap.py index b4dc27d56..e3b6aacf3 100644 --- a/test/test_remap.py +++ b/test/test_remap.py @@ -92,7 +92,7 @@ def test_nn_remap(self): uxgrid = ux.open_grid(gridfile_ne30) uxda = uxds['v1'] - out_da = uxda.remap.nearest_neighbor(destination_obj=uxgrid, remap_to="nodes") + out_da = uxda.remap.nearest_neighbor(destination_grid=uxgrid, remap_to="nodes") # Assert the remapping was successful and the variable is populated self.assertTrue(len(out_da) != 0) @@ -112,20 +112,6 @@ def test_remap_return_types(self): assert isinstance(remap_uxda_to_grid, UxDataArray) - remap_uxda_to_uxda = source_uxds['v1'].remap.nearest_neighbor( - destination_uxds['psi']) - - # Dataset with two vars: original "psi" and remapped "v1" - assert isinstance(remap_uxda_to_uxda, UxDataset) - assert len(remap_uxda_to_uxda.data_vars) == 2 - - remap_uxda_to_uxds = source_uxds['v1'].remap.nearest_neighbor( - destination_uxds) - - # Dataset with two vars: original "psi" and remapped "v1" - assert isinstance(remap_uxda_to_uxds, UxDataset) - assert len(remap_uxda_to_uxds.data_vars) == 2 - remap_uxds_to_grid = source_uxds.remap.nearest_neighbor( destination_uxds.uxgrid) @@ -133,20 +119,6 @@ def test_remap_return_types(self): assert isinstance(remap_uxds_to_grid, UxDataset) assert len(remap_uxds_to_grid.data_vars) == 3 - remap_uxds_to_uxda = source_uxds.remap.nearest_neighbor( - destination_uxds['psi']) - - # Dataset with four vars: original "psi" and remapped "v1, v2, v3" - assert isinstance(remap_uxds_to_uxda, UxDataset) - assert len(remap_uxds_to_uxda.data_vars) == 4 - - remap_uxds_to_uxds = source_uxds.remap.nearest_neighbor( - destination_uxds) - - # Dataset with four vars: original "psi" and remapped "v1, v2, v3" - assert isinstance(remap_uxds_to_uxds, UxDataset) - assert len(remap_uxds_to_uxds.data_vars) == 4 - def test_edge_centers_remapping(self): """Tests the ability to remap on edge centers using Nearest Neighbor Remapping.""" @@ -155,11 +127,11 @@ def test_edge_centers_remapping(self): source_grid = ux.open_dataset(gridfile_geoflow, dsfile_v1_geoflow) destination_grid = ux.open_dataset(mpasfile_QU, mpasfile_QU) - remap_to_edge_centers = source_grid['v1'].remap.nearest_neighbor(destination_obj=destination_grid, + remap_to_edge_centers = source_grid['v1'].remap.nearest_neighbor(destination_grid=destination_grid.uxgrid, remap_to="edge centers") # Assert the data variable lies on the "edge centers" - self.assertTrue(remap_to_edge_centers['v1']._edge_centered()) + self.assertTrue(remap_to_edge_centers._edge_centered()) def test_overwrite(self): """Tests that the remapping no longer overwrites the dataset.""" @@ -169,7 +141,7 @@ def test_overwrite(self): destination_dataset = ux.open_dataset(gridfile_geoflow, dsfile_v1_geoflow) # Perform remapping - remap_to_edge_centers = source_grid['v1'].remap.nearest_neighbor(destination_obj=destination_dataset, + remap_to_edge_centers = source_grid['v1'].remap.nearest_neighbor(destination_grid=destination_dataset.uxgrid, remap_to="nodes") # Assert the remapped data is different from the original data @@ -254,20 +226,6 @@ def test_remap_return_types(self): assert isinstance(remap_uxda_to_grid, UxDataArray) assert len(remap_uxda_to_grid) == 1 - remap_uxda_to_uxda = source_uxds['v1'].remap.inverse_distance_weighted( - destination_uxds['psi'], power=3, k=10) - - # Dataset with two vars: original "psi" and remapped "v1" - assert isinstance(remap_uxda_to_uxda, UxDataset) - assert len(remap_uxda_to_uxda.data_vars) == 2 - - remap_uxda_to_uxds = source_uxds['v1'].remap.inverse_distance_weighted( - destination_uxds, power=3, k=10) - - # Dataset with two vars: original "psi" and remapped "v1" - assert isinstance(remap_uxda_to_uxds, UxDataset) - assert len(remap_uxda_to_uxds.data_vars) == 2 - remap_uxds_to_grid = source_uxds.remap.inverse_distance_weighted( destination_uxds.uxgrid) @@ -275,20 +233,6 @@ def test_remap_return_types(self): assert isinstance(remap_uxds_to_grid, UxDataset) assert len(remap_uxds_to_grid.data_vars) == 3 - remap_uxds_to_uxda = source_uxds.remap.inverse_distance_weighted( - destination_uxds['psi']) - - # Dataset with four vars: original "psi" and remapped "v1, v2, v3" - assert isinstance(remap_uxds_to_uxda, UxDataset) - assert len(remap_uxds_to_uxda.data_vars) == 4 - - remap_uxds_to_uxds = source_uxds.remap.inverse_distance_weighted( - destination_uxds) - - # Dataset with four vars: original "psi" and remapped "v1, v2, v3" - assert isinstance(remap_uxds_to_uxds, UxDataset) - assert len(remap_uxds_to_uxds.data_vars) == 4 - def test_edge_remapping(self): """Tests the ability to remap on edge centers using Inverse Distance Weighted Remapping.""" @@ -299,11 +243,11 @@ def test_edge_remapping(self): # Perform remapping to the edge centers of the dataset - remap_to_edge_centers = source_grid['v1'].remap.inverse_distance_weighted(destination_obj=destination_grid, + remap_to_edge_centers = source_grid['v1'].remap.inverse_distance_weighted(destination_grid=destination_grid.uxgrid, remap_to="edge centers") # Assert the data variable lies on the "edge centers" - self.assertTrue(remap_to_edge_centers['v1']._edge_centered()) + self.assertTrue(remap_to_edge_centers._edge_centered()) def test_overwrite(self): """Tests that the remapping no longer overwrites the dataset.""" @@ -313,7 +257,7 @@ def test_overwrite(self): destination_dataset = ux.open_dataset(gridfile_geoflow, dsfile_v1_geoflow) # Perform Remapping - remap_to_edge_centers = source_grid['v1'].remap.inverse_distance_weighted(destination_obj=destination_dataset, + remap_to_edge_centers = source_grid['v1'].remap.inverse_distance_weighted(destination_grid=destination_dataset.uxgrid, remap_to="nodes") # Assert the remapped data is different from the original data diff --git a/uxarray/remap/dataarray_accessor.py b/uxarray/remap/dataarray_accessor.py index a7e925c87..3ea5f78db 100644 --- a/uxarray/remap/dataarray_accessor.py +++ b/uxarray/remap/dataarray_accessor.py @@ -1,6 +1,5 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional -from warnings import warn +from typing import TYPE_CHECKING from uxarray.remap.nearest_neighbor import _nearest_neighbor_uxda from uxarray.remap.inverse_distance_weighted import ( @@ -8,7 +7,6 @@ ) if TYPE_CHECKING: - from uxarray.core.dataset import UxDataset from uxarray.core.dataarray import UxDataArray from uxarray.grid import Grid @@ -48,9 +46,7 @@ def nearest_neighbor( Indicates whether to remap using on spherical or cartesian coordinates """ - return _nearest_neighbor_uxda( - self.uxda, destination_grid, remap_to, coord_type - ) + return _nearest_neighbor_uxda(self.uxda, destination_grid, remap_to, coord_type) def inverse_distance_weighted( self, diff --git a/uxarray/remap/dataset_accessor.py b/uxarray/remap/dataset_accessor.py index bafe52934..dbf58e4ad 100644 --- a/uxarray/remap/dataset_accessor.py +++ b/uxarray/remap/dataset_accessor.py @@ -1,6 +1,5 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional -from warnings import warn +from typing import TYPE_CHECKING from uxarray.remap.nearest_neighbor import _nearest_neighbor_uxds from uxarray.remap.inverse_distance_weighted import ( @@ -9,7 +8,6 @@ if TYPE_CHECKING: from uxarray.core.dataset import UxDataset - from uxarray.core.dataarray import UxDataArray from uxarray.grid import Grid @@ -48,9 +46,7 @@ def nearest_neighbor( Indicates whether to remap using on spherical or cartesian coordinates """ - return _nearest_neighbor_uxds( - self.uxds, destination_grid, remap_to, coord_type - ) + return _nearest_neighbor_uxds(self.uxds, destination_grid, remap_to, coord_type) def inverse_distance_weighted( self, diff --git a/uxarray/remap/inverse_distance_weighted.py b/uxarray/remap/inverse_distance_weighted.py index 05f9d8451..92cf1602f 100644 --- a/uxarray/remap/inverse_distance_weighted.py +++ b/uxarray/remap/inverse_distance_weighted.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Union +from typing import TYPE_CHECKING if TYPE_CHECKING: from uxarray.core.dataset import UxDataset @@ -206,9 +206,6 @@ def _inverse_distance_weighted_remap_uxda( destination_dims = list(source_uxda.dims) destination_dims[-1] = destination_dim - if not isinstance(destination_grid, Grid): - raise ValueError("TODO: Invalid Input") - # perform remapping destination_data = _inverse_distance_weighted_remap( source_uxda.uxgrid, @@ -227,26 +224,14 @@ def _inverse_distance_weighted_remap_uxda( dims=destination_dims, uxgrid=destination_grid, ) - # add remapped variable to existing UxDataset - if isinstance(destination_obj, uxarray.core.dataset.UxDataset): - uxds = destination_obj.copy() - uxds[source_uxda.name] = uxda_remap - return uxds - - # construct a UxDataset from remapped variable and existing variable - elif isinstance(destination_obj, uxarray.core.dataset.UxDataArray): - uxds = destination_obj.copy().to_dataset() - uxds[source_uxda.name] = uxda_remap - return uxds # return UxDataArray with remapped variable - else: - return uxda_remap + return uxda_remap def _inverse_distance_weighted_remap_uxds( source_uxds: UxDataset, - destination_obj: Union[Grid, UxDataArray, UxDataset], + destination_grid: Grid, remap_to: str = "face centers", coord_type: str = "spherical", power=2, @@ -258,7 +243,7 @@ def _inverse_distance_weighted_remap_uxds( --------- source_uxds : UxDataset Source UxDataset for remapping - destination_obj : Grid, UxDataArray, UxDataset + destination_grid : Grid Destination for remapping remap_to : str, default="nodes" Location of where to map data, either "nodes", "edge centers", or "face centers" @@ -270,20 +255,11 @@ def _inverse_distance_weighted_remap_uxds( k : int, default=8 Number of nearest neighbors to consider in the weighted calculation. """ - - if isinstance(destination_obj, Grid): - destination_uxds = uxarray.core.dataset.UxDataset(uxgrid=destination_obj) - elif isinstance(destination_obj, uxarray.core.dataset.UxDataArray): - destination_uxds = destination_obj.to_dataset() - elif isinstance(destination_obj, uxarray.core.dataset.UxDataset): - destination_uxds = destination_obj - else: - raise ValueError - + destination_uxds = uxarray.UxDataset(uxgrid=destination_grid) for var_name in source_uxds.data_vars: - destination_uxds = _inverse_distance_weighted_remap_uxda( + destination_uxds[var_name] = _inverse_distance_weighted_remap_uxda( source_uxds[var_name], - destination_uxds, + destination_grid, remap_to, coord_type, power, diff --git a/uxarray/remap/nearest_neighbor.py b/uxarray/remap/nearest_neighbor.py index 744f974a3..1923bd1b7 100644 --- a/uxarray/remap/nearest_neighbor.py +++ b/uxarray/remap/nearest_neighbor.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Union +from typing import TYPE_CHECKING if TYPE_CHECKING: from uxarray.core.dataset import UxDataset @@ -150,7 +150,7 @@ def _nearest_neighbor( def _nearest_neighbor_uxda( source_uxda: UxDataArray, - destination_obj: Union[Grid, UxDataArray, UxDataset], + destination_grid: Grid, remap_to: str = "face centers", coord_type: str = "spherical", ): @@ -160,7 +160,7 @@ def _nearest_neighbor_uxda( --------- source_uxda : UxDataArray Source UxDataArray for remapping - destination_obj : Grid, UxDataArray, UxDataset + destination_grid : Grid Destination for remapping remap_to : str, default="nodes" Location of where to map data, either "nodes", "edge centers", or "face centers" @@ -180,16 +180,6 @@ def _nearest_neighbor_uxda( destination_dims = list(source_uxda.dims) destination_dims[-1] = destination_dim - if isinstance(destination_obj, Grid): - destination_grid = destination_obj - elif isinstance( - destination_obj, - (uxarray.core.dataarray.UxDataArray, uxarray.core.dataset.UxDataset), - ): - destination_grid = destination_obj.uxgrid - else: - raise ValueError("TODO: Invalid Input") - # perform remapping destination_data = _nearest_neighbor( source_uxda.uxgrid, destination_grid, source_uxda.data, remap_to, coord_type @@ -202,26 +192,13 @@ def _nearest_neighbor_uxda( dims=destination_dims, uxgrid=destination_grid, ) - # add remapped variable to existing UxDataset - if isinstance(destination_obj, uxarray.core.dataset.UxDataset): - uxds = destination_obj.copy() - uxds[source_uxda.name] = uxda_remap - return uxds - - # construct a UxDataset from remapped variable and existing variable - elif isinstance(destination_obj, uxarray.core.dataset.UxDataArray): - uxds = destination_obj.copy().to_dataset() - uxds[source_uxda.name] = uxda_remap - return uxds - # return UxDataArray with remapped variable - else: - return uxda_remap + return uxda_remap def _nearest_neighbor_uxds( source_uxds: UxDataset, - destination_obj: Union[Grid, UxDataArray, UxDataset], + destination_grid: Grid, remap_to: str = "face centers", coord_type: str = "spherical", ): @@ -231,26 +208,17 @@ def _nearest_neighbor_uxds( --------- source_uxds : UxDataset Source UxDataset for remapping - destination_obj : Grid, UxDataArray, UxDataset + destination_grid : Grid Destination for remapping remap_to : str, default="nodes" Location of where to map data, either "nodes", "edge centers", or "face centers" coord_type : str, default="spherical" Indicates whether to remap using on Spherical or Cartesian coordinates """ - - if isinstance(destination_obj, Grid): - destination_uxds = uxarray.core.dataset.UxDataset(uxgrid=destination_obj) - elif isinstance(destination_obj, uxarray.core.dataset.UxDataArray): - destination_uxds = destination_obj.to_dataset() - elif isinstance(destination_obj, uxarray.core.dataset.UxDataset): - destination_uxds = destination_obj - else: - raise ValueError - + destination_uxds = uxarray.UxDataset(uxgrid=destination_grid) for var_name in source_uxds.data_vars: - destination_uxds = _nearest_neighbor_uxda( - source_uxds[var_name], destination_uxds, remap_to, coord_type + destination_uxds[var_name] = _nearest_neighbor_uxda( + source_uxds[var_name], destination_grid, remap_to, coord_type ) return destination_uxds