Skip to content

Commit 33e0268

Browse files
authored
Update units for parsed distances from MPAS (#1179)
* update units when parsing distances from mpas and docstring * add test, add warning to SpatialHash * correct phrasing * add periodic element note * update note * update test
1 parent 2154ea8 commit 33e0268

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

test/test_mpas.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import uxarray as ux
55
import xarray as xr
66
from pathlib import Path
7+
import numpy.testing as nt
78
from uxarray.constants import INT_DTYPE, INT_FILL_VALUE
89
from uxarray.io._mpas import _replace_padding, _replace_zeros, _to_zero_index, _read_mpas
910

@@ -110,3 +111,14 @@ def test_face_area():
110111

111112
assert "face_areas" in uxgrid_primal._ds
112113
assert "face_areas" in uxgrid_dual._ds
114+
115+
116+
def test_distance_units():
117+
xrds = xr.open_dataset(mpas_ocean_mesh)
118+
uxgrid = ux.open_grid(mpas_ocean_mesh)
119+
120+
assert "edge_node_distances" in uxgrid._ds
121+
assert "edge_face_distances" in uxgrid._ds
122+
123+
nt.assert_array_almost_equal(uxgrid['edge_node_distances'].values, (xrds['dvEdge'].values / xrds.attrs['sphere_radius']))
124+
nt.assert_array_almost_equal(uxgrid['edge_face_distances'].values, (xrds['dcEdge'].values / xrds.attrs['sphere_radius']))

uxarray/grid/grid.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ def node_face_connectivity(self, value):
13871387

13881388
@property
13891389
def edge_node_distances(self):
1390-
"""Distances between the two nodes that surround each edge in degrees.
1390+
"""Distances between the two nodes that surround each edge in radians.
13911391
13921392
Dimensions ``(n_edge, )``
13931393
"""
@@ -1404,7 +1404,7 @@ def edge_node_distances(self, value):
14041404
@property
14051405
def edge_face_distances(self):
14061406
"""Distances between the centers of the faces that saddle each edge in
1407-
degrees.
1407+
radians.
14081408
14091409
Dimensions ``(n_edge, )``
14101410
"""
@@ -1782,6 +1782,10 @@ def get_spatial_hash(
17821782
self._spatialhash : grid.Neighbors.SpatialHash
17831783
SpatialHash instance
17841784
1785+
Note
1786+
----
1787+
Does not currently support queries on periodic elements.
1788+
17851789
Examples
17861790
--------
17871791
Open a grid from a file path:

uxarray/grid/neighbors.py

+27-8
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,10 @@ class SpatialHash:
792792
Source grid used to construct the hash grid and hash table
793793
reconstruct : bool, default=False
794794
If true, reconstructs the spatial hash
795+
796+
Note
797+
----
798+
Does not currently support queries on periodic elements.
795799
"""
796800

797801
def __init__(
@@ -806,11 +810,21 @@ def __init__(
806810

807811
# Hash grid size
808812
self._dh = self._hash_cell_size()
813+
809814
# Lower left corner of the hash grid
810-
self._xmin = np.deg2rad(self._source_grid.node_lon.min().to_numpy()) - self._dh
811-
self._ymin = np.deg2rad(self._source_grid.node_lat.min().to_numpy()) - self._dh
812-
self._xmax = np.deg2rad(self._source_grid.node_lon.max().to_numpy()) + self._dh
813-
self._ymax = np.deg2rad(self._source_grid.node_lat.max().to_numpy()) + self._dh
815+
lon_min = np.deg2rad(self._source_grid.node_lon.min().to_numpy())
816+
lat_min = np.deg2rad(self._source_grid.node_lat.min().to_numpy())
817+
lon_max = np.deg2rad(self._source_grid.node_lon.max().to_numpy())
818+
lat_max = np.deg2rad(self._source_grid.node_lat.max().to_numpy())
819+
820+
self._xmin = lon_min - self._dh
821+
self._ymin = lat_min - self._dh
822+
self._xmax = lon_max + self._dh
823+
self._ymax = lat_max + self._dh
824+
825+
print(self._xmin, self._xmax)
826+
print(self._ymin, self._ymax)
827+
814828
# Number of x points in the hash grid; used for
815829
# array flattening
816830
Lx = self._xmax - self._xmin
@@ -866,10 +880,15 @@ def _initialize_face_hash_table(self):
866880
)
867881
i2, j2 = self._hash_index2d(coords)
868882

869-
for eid in range(self._source_grid.n_face):
870-
for j in range(j1[eid], j2[eid] + 1):
871-
for i in range(i1[eid], i2[eid] + 1):
872-
index_to_face[i + self._nx * j].append(eid)
883+
try:
884+
for eid in range(self._source_grid.n_face):
885+
for j in range(j1[eid], j2[eid] + 1):
886+
for i in range(i1[eid], i2[eid] + 1):
887+
index_to_face[i + self._nx * j].append(eid)
888+
except IndexError:
889+
raise IndexError(
890+
"list index out of range. This may indicate incorrect `edge_node_distances` values."
891+
)
873892

874893
return index_to_face
875894

uxarray/io/_mpas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def _parse_edge_faces(in_ds, out_ds, mesh_type):
403403

404404
def _parse_edge_node_distances(in_ds, out_ds):
405405
"""Parses ``edge_node_distances``"""
406-
edge_node_distances = in_ds["dvEdge"]
406+
edge_node_distances = in_ds["dvEdge"] / in_ds.attrs["sphere_radius"]
407407

408408
out_ds["edge_node_distances"] = edge_node_distances.assign_attrs(
409409
descriptors.EDGE_NODE_DISTANCES_ATTRS
@@ -412,7 +412,7 @@ def _parse_edge_node_distances(in_ds, out_ds):
412412

413413
def _parse_edge_face_distances(in_ds, out_ds):
414414
"""Parses ``edge_face_distances``"""
415-
edge_face_distances = in_ds["dcEdge"]
415+
edge_face_distances = in_ds["dcEdge"] / in_ds.attrs["sphere_radius"]
416416

417417
out_ds["edge_face_distances"] = edge_face_distances.assign_attrs(
418418
descriptors.EDGE_FACE_DISTANCES_ATTRS

0 commit comments

Comments
 (0)