Skip to content

Commit fb632d3

Browse files
authored
Read UGRID files with transposed connectivity (i.e. fesom.mesh.diag) (#1025)
* read fesom diag files * Remove exo file * remove print
1 parent 933bfc5 commit fb632d3

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

test/grid_geoflow.exo

-567 KB
Binary file not shown.

test/test_fesom.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import uxarray as ux
2+
3+
import os
4+
from pathlib import Path
5+
6+
current_path = Path(os.path.dirname(os.path.realpath(__file__)))
7+
8+
fesom_ugrid_diag_file= current_path / "meshfiles" / "ugrid" / "fesom" / "fesom.mesh.diag.nc"
9+
10+
11+
def test_open_fesom_ugrid():
12+
uxgrid = ux.open_grid(fesom_ugrid_diag_file)
13+
uxgrid.validate()

uxarray/grid/connectivity.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def close_face_nodes(face_node_connectivity, n_face, n_max_face_nodes):
5858

5959

6060
def _replace_fill_values(grid_var, original_fill, new_fill, new_dtype=None):
61-
"""Replaces all instances of the the current fill value (``original_fill``)
62-
in (``grid_var``) with (``new_fill``) and converts to the dtype defined by
61+
"""Replaces all instances of the current fill value (``original_fill``) in
62+
(``grid_var``) with (``new_fill``) and converts to the dtype defined by
6363
(``new_dtype``)
6464
6565
Parameters
@@ -82,6 +82,7 @@ def _replace_fill_values(grid_var, original_fill, new_fill, new_dtype=None):
8282
# locations of fill values
8383
if original_fill is not None and np.isnan(original_fill):
8484
fill_val_idx = np.isnan(grid_var)
85+
grid_var[fill_val_idx] = 0.0 # todo?
8586
else:
8687
fill_val_idx = grid_var == original_fill
8788

uxarray/io/_ugrid.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ def _read_ugrid(ds):
1111
"""Parses an unstructured grid dataset and encodes it in the UGRID
1212
conventions."""
1313

14-
# Grid Topology
14+
# Extract grid topology attributes
1515
grid_topology_name = list(ds.filter_by_attrs(cf_role="mesh_topology").keys())[0]
1616
ds = ds.rename({grid_topology_name: "grid_topology"})
1717

18-
# Coordinates
19-
2018
# get the names of node_lon and node_lat
2119
node_lon_name, node_lat_name = ds["grid_topology"].node_coordinates.split()
2220
coord_dict = {
@@ -37,7 +35,6 @@ def _read_ugrid(ds):
3735
coord_dict[face_lat_name] = ugrid.FACE_COORDINATES[1]
3836

3937
ds = ds.rename(coord_dict)
40-
# Connectivity
4138

4239
conn_dict = {}
4340
for conn_name in ugrid.CONNECTIVITY_NAMES:
@@ -56,25 +53,38 @@ def _read_ugrid(ds):
5653
dim_dict = {}
5754

5855
# Rename Core Dims (node, edge, face)
59-
if "node_dimension" in ds["grid_topology"]:
56+
if "node_dimension" in ds["grid_topology"].attrs:
6057
dim_dict[ds["grid_topology"].node_dimension] = ugrid.NODE_DIM
6158
else:
6259
dim_dict[ds["node_lon"].dims[0]] = ugrid.NODE_DIM
6360

64-
if "face_dimension" in ds["grid_topology"]:
61+
if "face_dimension" in ds["grid_topology"].attrs:
6562
dim_dict[ds["grid_topology"].face_dimension] = ugrid.FACE_DIM
6663
else:
6764
dim_dict[ds["face_node_connectivity"].dims[0]] = ugrid.FACE_DIM
6865

69-
if "edge_dimension" in ds["grid_topology"]:
66+
if "edge_dimension" in ds["grid_topology"].attrs:
7067
# edge dimension is not always provided
7168
dim_dict[ds["grid_topology"].edge_dimension] = ugrid.EDGE_DIM
7269
else:
7370
if "edge_lon" in ds:
7471
dim_dict[ds["edge_lon"].dims[0]] = ugrid.EDGE_DIM
7572

73+
for conn_name in conn_dict.values():
74+
# Ensure grid dimension (i.e. 'n_face') is always the first dimension
75+
da = ds[conn_name]
76+
dims = da.dims
77+
78+
for grid_dim in dim_dict.keys():
79+
if dims[1] == grid_dim:
80+
ds[conn_name] = da.T
81+
7682
dim_dict[ds["face_node_connectivity"].dims[1]] = ugrid.N_MAX_FACE_NODES_DIM
7783

84+
for dim in ds.dims:
85+
if ds.sizes[dim] == 2:
86+
dim_dict[dim] = "two"
87+
7888
ds = ds.swap_dims(dim_dict)
7989

8090
return ds, dim_dict
@@ -149,7 +159,9 @@ def _standardize_connectivity(ds, conn_name):
149159
if "start_index" in ds[conn_name].attrs:
150160
new_conn[new_conn != INT_FILL_VALUE] -= INT_DTYPE(ds[conn_name].start_index)
151161
else:
152-
new_conn[new_conn != INT_FILL_VALUE] -= INT_DTYPE(new_conn.min())
162+
fill_value_indices = new_conn != INT_FILL_VALUE
163+
start_index = new_conn[fill_value_indices].min()
164+
new_conn[fill_value_indices] -= INT_DTYPE(start_index)
153165

154166
# reassign data to use updated connectivity
155167
ds[conn_name].data = new_conn

0 commit comments

Comments
 (0)