From c058194ff2e41591ced2be3f06cb731852034913 Mon Sep 17 00:00:00 2001 From: Kristen Thyng Date: Tue, 18 Jul 2023 09:57:40 -0500 Subject: [PATCH 1/6] added another kwargs --- uxarray/core/api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/uxarray/core/api.py b/uxarray/core/api.py index a17f4ad3e..a2386ad1b 100644 --- a/uxarray/core/api.py +++ b/uxarray/core/api.py @@ -167,7 +167,8 @@ def open_dataset(grid_filename_or_obj: str, vertices=vertices, islatlon=islatlon, isconcave=isconcave, - use_dual=use_dual) + use_dual=use_dual, + **kwargs) ## UxDataset ds = xr.open_dataset(filename_or_obj, decode_times=False, From f8fea0e40a1bfe09045cdd1ccd135a895ac44930 Mon Sep 17 00:00:00 2001 From: Philip Chmielowiec Date: Tue, 18 Jul 2023 20:23:22 -0500 Subject: [PATCH 2/6] add grid_kwargs --- uxarray/core/api.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/uxarray/core/api.py b/uxarray/core/api.py index a2386ad1b..374a34b09 100644 --- a/uxarray/core/api.py +++ b/uxarray/core/api.py @@ -105,6 +105,7 @@ def open_dataset(grid_filename_or_obj: str, islatlon: Optional[bool] = False, isconcave: Optional[bool] = False, use_dual: Optional[bool] = False, + grid_kwargs: Optional[Dict[str, Any]] = None, **kwargs: Dict[str, Any]) -> UxDataset: """Wraps ``xarray.open_dataset()``, given a grid topology definition with a single dataset file or object with corresponding data. @@ -140,6 +141,12 @@ def open_dataset(grid_filename_or_obj: str, use_dual: bool, optional Specify whether to use the primal (use_dual=False) or dual (use_dual=True) mesh if the file type is mpas + grid_kwargs : Dict[str, Any] + Additional arguments passed on to ``xarray.open_dataset`` when opening up a Grid File. Refer to the + [xarray + docs](https://xarray.pydata.org/en/stable/generated/xarray.open_dataset.html) + for accepted keyword arguments. + **kwargs : Dict[str, Any] Additional arguments passed on to ``xarray.open_dataset``. Refer to the [xarray @@ -159,6 +166,7 @@ def open_dataset(grid_filename_or_obj: str, >>> import uxarray as ux >>> ux_ds = ux.open_dataset("grid_filename.g", "grid_filename_vortex.nc") + :param grid_kwargs: """ ## Grid definition @@ -168,7 +176,7 @@ def open_dataset(grid_filename_or_obj: str, islatlon=islatlon, isconcave=isconcave, use_dual=use_dual, - **kwargs) + **grid_kwargs) ## UxDataset ds = xr.open_dataset(filename_or_obj, decode_times=False, @@ -186,6 +194,7 @@ def open_mfdataset(grid_filename_or_obj: str, islatlon: Optional[bool] = False, isconcave: Optional[bool] = False, use_dual: Optional[bool] = False, + grid_kwargs: Optional[Dict[str, Any]] = None, **kwargs: Dict[str, Any]) -> UxDataset: """Wraps ``xarray.open_mfdataset()``, given a single grid topology file with multiple dataset paths with corresponding data. @@ -218,7 +227,13 @@ def open_mfdataset(grid_filename_or_obj: str, Path or URL to the source grid file. For diagnostic/reporting purposes only. use_dual: bool, optional - Specify whether to use the primal (use_dual=False) or dual (use_dual=True) mesh if the file type is mpas + Specify whether to use the primal (use_dual=False) or dual (use_dual=True) mesh if the file type is mpas + + grid_kwargs : Dict[str, Any] + Additional arguments passed on to ``xarray.open_dataset`` when opening up a Grid File. Refer to the + [xarray + docs](https://xarray.pydata.org/en/stable/generated/xarray.open_dataset.html) + for accepted keyword arguments. **kwargs : Dict[str, Any] Additional arguments passed on to ``xarray.open_mfdataset``. Refer to the @@ -248,15 +263,16 @@ def open_mfdataset(grid_filename_or_obj: str, >>> ux_ds = ux.open_mfdataset("grid_filename.g", "grid_filename_vortex_*.nc") """ - ## Grid definition + # Grid definition uxgrid = open_grid(grid_filename_or_obj, gridspec=gridspec, vertices=vertices, islatlon=islatlon, isconcave=isconcave, - use_dual=use_dual) + use_dual=use_dual, + **grid_kwargs) - ## UxDataset + # UxDataset ds = xr.open_mfdataset(paths, decode_times=False, **kwargs) # type: ignore uxds = UxDataset(ds, uxgrid=uxgrid, source_datasets=str(paths)) From 5617e90b190628c38aae3eb3a7278b687c572183 Mon Sep 17 00:00:00 2001 From: Philip Chmielowiec Date: Wed, 19 Jul 2023 11:42:30 -0500 Subject: [PATCH 3/6] default value should be an empty dict, not none --- uxarray/core/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uxarray/core/api.py b/uxarray/core/api.py index 374a34b09..78a2d5fe8 100644 --- a/uxarray/core/api.py +++ b/uxarray/core/api.py @@ -105,7 +105,7 @@ def open_dataset(grid_filename_or_obj: str, islatlon: Optional[bool] = False, isconcave: Optional[bool] = False, use_dual: Optional[bool] = False, - grid_kwargs: Optional[Dict[str, Any]] = None, + grid_kwargs: Optional[Dict[str, Any]] = {}, **kwargs: Dict[str, Any]) -> UxDataset: """Wraps ``xarray.open_dataset()``, given a grid topology definition with a single dataset file or object with corresponding data. @@ -194,7 +194,7 @@ def open_mfdataset(grid_filename_or_obj: str, islatlon: Optional[bool] = False, isconcave: Optional[bool] = False, use_dual: Optional[bool] = False, - grid_kwargs: Optional[Dict[str, Any]] = None, + grid_kwargs: Optional[Dict[str, Any]] = {}, **kwargs: Dict[str, Any]) -> UxDataset: """Wraps ``xarray.open_mfdataset()``, given a single grid topology file with multiple dataset paths with corresponding data. From b6ac44228661908f8c1096e757b0442f014643f4 Mon Sep 17 00:00:00 2001 From: Philip Chmielowiec Date: Wed, 19 Jul 2023 12:11:29 -0500 Subject: [PATCH 4/6] start test --- test/test_api.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/test_api.py b/test/test_api.py index e6b05698c..c41ce3a04 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -4,6 +4,8 @@ import numpy.testing as nt import uxarray as ux +import xarray as xr +import numpy as np try: import constants @@ -14,7 +16,6 @@ class TestAPI(TestCase): - geoflow_data_path = current_path / "meshfiles" / "ugrid" / "geoflow-small" gridfile_geoflow = current_path / "meshfiles" / "ugrid" / "geoflow-small" / "grid.nc" geoflow_data_v1 = geoflow_data_path / "v1.nc" @@ -128,3 +129,7 @@ def test_copy_dataarray(self): # Check that the deep copy is a deep copy assert (v1_uxdata_array_copy_deep.uxgrid == v1_uxdata_array.uxgrid) assert (v1_uxdata_array_copy_deep.uxgrid is not v1_uxdata_array.uxgrid) + + def test_open_dataset_grid_kwargs(self): + + pass From 973ca7c8e2ec6056d32ce70f8504264f9ed0bc78 Mon Sep 17 00:00:00 2001 From: Philip Chmielowiec Date: Thu, 20 Jul 2023 15:27:51 -0500 Subject: [PATCH 5/6] test case --- test/test_api.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/test_api.py b/test/test_api.py index c41ce3a04..879551073 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -131,5 +131,12 @@ def test_copy_dataarray(self): assert (v1_uxdata_array_copy_deep.uxgrid is not v1_uxdata_array.uxgrid) def test_open_dataset_grid_kwargs(self): - - pass + """Drops ``Mesh2_face_nodes`` from the inputted grid file using + ``grid_kwargs``""" + + with self.assertRaises(KeyError): + # attempt to open a dataset after dropping face nodes should raise a KeyError + uxds = ux.open_dataset( + self.gridfile_ne30, + self.dsfile_var2_ne30, + grid_kwargs={"drop_variables": "Mesh2_face_nodes"}) From 91be74f656b956e0ed7cd001079e94b6253ed278 Mon Sep 17 00:00:00 2001 From: Philip Chmielowiec Date: Thu, 20 Jul 2023 15:32:30 -0500 Subject: [PATCH 6/6] clean up --- uxarray/core/api.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/uxarray/core/api.py b/uxarray/core/api.py index e08477036..e4f096562 100644 --- a/uxarray/core/api.py +++ b/uxarray/core/api.py @@ -142,7 +142,7 @@ def open_dataset(grid_filename_or_obj: str, use_dual: bool, optional Specify whether to use the primal (use_dual=False) or dual (use_dual=True) mesh if the file type is mpas - grid_kwargs : Dict[str, Any] + grid_kwargs : Dict[str, Any], optional Additional arguments passed on to ``xarray.open_dataset`` when opening up a Grid File. Refer to the [xarray docs](https://xarray.pydata.org/en/stable/generated/xarray.open_dataset.html) @@ -170,7 +170,7 @@ def open_dataset(grid_filename_or_obj: str, :param grid_kwargs: """ - ## Grid definition + # Grid definition uxgrid = open_grid(grid_filename_or_obj, gridspec=gridspec, vertices=vertices, @@ -179,10 +179,9 @@ def open_dataset(grid_filename_or_obj: str, use_dual=use_dual, **grid_kwargs) - ## UxDataset + # UxDataset ds = xr.open_dataset(filename_or_obj, decode_times=False, **kwargs) # type: ignore - uxds = UxDataset(ds, uxgrid=uxgrid, source_datasets=str(filename_or_obj)) return uxds @@ -230,7 +229,7 @@ def open_mfdataset(grid_filename_or_obj: str, use_dual: bool, optional Specify whether to use the primal (use_dual=False) or dual (use_dual=True) mesh if the file type is mpas - grid_kwargs : Dict[str, Any] + grid_kwargs : Dict[str, Any], optional Additional arguments passed on to ``xarray.open_dataset`` when opening up a Grid File. Refer to the [xarray docs](https://xarray.pydata.org/en/stable/generated/xarray.open_dataset.html) @@ -275,7 +274,6 @@ def open_mfdataset(grid_filename_or_obj: str, # UxDataset ds = xr.open_mfdataset(paths, decode_times=False, **kwargs) # type: ignore - uxds = UxDataset(ds, uxgrid=uxgrid, source_datasets=str(paths)) return uxds