Skip to content

Commit

Permalink
Added examples and tests for passing in a dict of all the topology va…
Browse files Browse the repository at this point in the history
…riables.
  • Loading branch information
ChrisBarker-NOAA committed May 28, 2024
1 parent 1343419 commit 2049847
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
29 changes: 28 additions & 1 deletion tests/test_grids/test_ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def test_assign_ugrid_topology_no_connectivity():
assert True



def test_assign_ugrid_topology():
"""
FVCOM (SFBOFS):
Expand All @@ -42,5 +41,33 @@ def test_assign_ugrid_topology():
assert mesh['face_node_connectivity'] == 'nv'
assert mesh['face_coordinates'] == 'lonc latc'

def test_assign_ugrid_topology_dict():
"""
FVCOM (SFBOFS):
Test passing a dict in of the topology.
"""
ds = xr.open_dataset(EXAMPLE_DATA / "SFBOFS_subset1.nc")

# make sure it's not there to start with
with pytest.raises(KeyError):
ds['mesh']

grid_topology = {'node_coordinates': ('lon', 'lat'),
'face_node_connectivity': 'nv',
'node_coordinates': ('lon', 'lat'),
'face_coordinates': ('lonc', 'latc'),
}

ds = ugrid.assign_ugrid_topology(ds, **grid_topology)

# there are others, but these are the ones that really matter.
mesh = ds['mesh'].attrs
assert mesh['cf_role'] == 'mesh_topology'
assert mesh['node_coordinates'] == 'lon lat'
assert mesh['face_node_connectivity'] == 'nv'
assert mesh['face_coordinates'] == 'lonc latc'
assert mesh['node_coordinates'] == 'lon lat'


27 changes: 23 additions & 4 deletions xarray_subset_grid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ def assign_ugrid_topology(ds: xr.Dataset,
face_face_connectivity: str = None,
node_coordinates: str = None,
face_coordinates: str = None,
**attrs) -> xr.Dataset:
"""Assign the UGRID topology to the dataset
) -> xr.Dataset:
"""
Assign the UGRID topology to the dataset
Only the face_node_connectivity attribute is required. The face_face_connectivity attribute is optional.
Only the face_node_connectivity parameter is required. The face_face_connectivity attribute is optional.
If the variable for face_node_connectivity is named nv, the function call should look like this:
```
Expand All @@ -97,7 +98,24 @@ def assign_ugrid_topology(ds: xr.Dataset,
Args:
ds (xr.Dataset): The dataset to assign the UGRID topology to
**attrs: The attributes to assign to the datasets mesh topology metadata (see function description for more details)
face_node_connectivity (str): THe variable name of the face definitions
face_face_connectivity: str = None,
node_coordinates: str = None,
face_coordinates: str = None,
(See the UGRID conventions for descriptions of these)
You can pass a dict in with all the grid topology variables:
```
grid_topology = {'node_coordinates': ('lon', 'lat'),
'face_node_connectivity': 'nv',
'node_coordinates': ('lon', 'lat'),
'face_coordinates': ('lonc', 'latc'),
}
```
"""
# Get the variable name for the face_node_connectivity
# face_node_connectivity = attrs.get("face_node_connectivity", None)
Expand All @@ -108,6 +126,7 @@ def assign_ugrid_topology(ds: xr.Dataset,
# Get the longitude and latitude coordinate variable names
# node_coords = attrs.get("node_coordinates", None)
# face_coords = attrs.get("face_coordinates", None)

node_coords = node_coordinates
face_coords = face_coordinates

Expand Down

0 comments on commit 2049847

Please # to comment.