Skip to content

Commit

Permalink
added example with bounds -- fixed assign_ugrid_topology
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBarker-NOAA committed Oct 19, 2024
1 parent ca5b65a commit 32b70f3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
Binary file added docs/examples/example_data/tris_and_bounds.nc
Binary file not shown.
6 changes: 3 additions & 3 deletions pixi.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["setuptools>=61", "wheel", "setuptools_scm[toml]>=6.2"]
requires = ["setuptools>=72", "wheel", "setuptools_scm[toml]>=8.0"]
build-backend = "setuptools.build_meta"

[project]
Expand All @@ -8,7 +8,7 @@ authors = [
{ name = "Matthew Iannucci", email = "matt.iannucci@tetratech.com" },
{ name = "Christopher H. Barker", email = "chris.barker@noaa.gov" },
]
description = "Subset Xarray datasets in space"
description = "Subset Xarray datasets in time and space"
readme = "README.md"
requires-python = ">=3.10"
keywords = ["xarray"]
Expand All @@ -31,7 +31,7 @@ dynamic = ["version"]

dependencies = [
"numpy",
"xarray>=2023.10.0",
"xarray>=2024.6",
"cf_xarray",
"cftime",
"dask[complete]",
Expand Down Expand Up @@ -99,7 +99,7 @@ default = { solve-group = "default" }
dev = { features = ["dev"], solve-group = "default" }
examples = { features = ["examples"], solve-group = "default" }
all = { features = ["dev", "examples"], solve-group = "default" }
test310 = ["dev", "py310"] # implicit: test310 = ["dev", "py310", "default"]
test310 = ["dev", "py310"] # implicit: test310 = ["dev", "py310", "default"]
test311 = ["dev", "py311"]
test312 = ["dev", "py312"]

Expand All @@ -109,8 +109,8 @@ test312 = ["dev", "py312"]
[tool.pixi.dependencies]
python = ">=3.10"
numpy = "*"
xarray = ">=2023.10.0"
pandas = ">=2.2.2"
xarray = ">=2024.6"
pandas = "*"
cf_xarray = "*"
dask = "*"
fsspec = "*"
Expand Down
40 changes: 37 additions & 3 deletions tests/test_grids/test_ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
lonc:standard_name = "longitude" ;
lonc:long_name = "zonal longitude" ;
lonc:units = "degree_east" ;
float latc(nele) ;
latc:standard_name = "latitude" ;
latc:long_name = "zonal latitude" ;
Expand All @@ -60,7 +61,7 @@
face_face_connectivity:
int nbe(three, nele) ;
nbe:long_name = "elements surrounding anch element" ;
nbe:long_name = "elements surrounding each element" ;
The depth coordinates (xarray is not sure what to do with these ...)
Expand All @@ -87,10 +88,8 @@
time:format = "defined reference date" ;
time:time_zone = "UTC" ;
Data on the grid:
float h(node) ;
h:standard_name = "sea_floor_depth_below_geoid" ;
h:long_name = "Bathymetry" ;
Expand Down Expand Up @@ -265,7 +264,11 @@ def test_assign_ugrid_topology_min():
assert mesh["face_coordinates"] == "lonc latc"
assert mesh["face_face_connectivity"] == "nbe"
assert mesh["face_dimension"] == "nele"
assert mesh["start_index"] == 1

# There should be no empty attributes
for key, val in mesh.items():
assert val

def test_assign_ugrid_topology_dict():
"""
Expand All @@ -292,6 +295,36 @@ def test_assign_ugrid_topology_dict():
assert mesh["face_face_connectivity"] == "nbe"
assert mesh["face_dimension"] == "nele"

def test_assign_ugrid_topology_no_face_coords():
"""
test for no face coords
"""
# this example has triangles and bounds -- no face coordinates
ds = xr.open_dataset(EXAMPLE_DATA / "tris_and_bounds.nc")
with pytest.raises(KeyError):
ds["mesh"]

ds = ugrid.assign_ugrid_topology(ds, face_node_connectivity='tris',
node_coordinates = ('lon lat'),
boundary_node_connectivity = 'bounds'
)

# 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"] == "tris"
assert mesh["face_dimension"] == "tris"

assert mesh.keys() == {'cf_role',
'topology_dimension',
'face_node_connectivity',
'boundary_node_connectivity',
'node_coordinates',
'face_dimension',
'start_index',
}

def test_assign_ugrid_topology_existing_mesh_var():
"""
Expand Down Expand Up @@ -351,6 +384,7 @@ def test_assign_ugrid_topology_start_index_zero_infer():
assert ds["mesh_boundary_nodes"].attrs["start_index"] == 0



# NOTE: these tests are probably not complete -- but they are something.
# we really should have a complete UGRID example to test with.
def test_grid_vars():
Expand Down
8 changes: 4 additions & 4 deletions xarray_subset_grid/grids/ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,11 @@ def assign_ugrid_topology(
raise ValueError(
"face_node_connectivity is a required parameter if it is not in the mesh_topology variable" # noqa: E501
)

if mesh.face_coordinates is None:
try:
mesh.face_coordinates = ds[mesh.face_node_connectivity].cf.coordinates
face_coordinates = ds[mesh.face_node_connectivity].cf.coordinates
mesh.face_coordinates = " ".join(f"{coord[0]}"
for coord in mesh.face_coordinates.values())
for coord in face_coordinates.values())
except AttributeError:
mesh.face_coordinates = None

Expand Down Expand Up @@ -487,7 +486,8 @@ def assign_ugrid_topology(

# push the non-None mesh attributes back into the variable
for key, val in mesh.__dict__.items():
if val is not None:
# can't use truthiness, as 0 is false
if not ((val is None) or (val == "")):
mesh_attrs[key] = val

return ds

0 comments on commit 32b70f3

Please sign in to comment.