@@ -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