Topology Detection and I/O#
UGRID topology detection from NetCDF files using GDAL's Multidimensional API, and UGRID-compliant NetCDF writing.
pyramids.netcdf.ugrid.io.parse_ugrid_topology(rg)
#
Detect and parse all UGRID mesh topologies from a GDAL root group.
Detection strategy (handles diverse real-world files):
- Primary: Scan all MDArrays for
cf_role = "mesh_topology"attribute. - Fallback: Scan for variables with
topology_dimensionANDnode_coordinatesattributes (older files without cf_role). - Scalar check: GDAL may filter 0-dimensional arrays from
GetMDArrayNames(). Explicitly tryOpenMDArray(name)for variable names found in other variables'meshattributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rg
|
Group
|
GDAL root group from a multidimensional NetCDF file. |
required |
Returns:
| Type | Description |
|---|---|
list[MeshTopologyInfo]
|
List of MeshTopologyInfo, one per mesh found in the file. |
list[MeshTopologyInfo]
|
Most files have exactly one mesh. Empty list if no UGRID topology. |
Source code in src/pyramids/netcdf/ugrid/io.py
pyramids.netcdf.ugrid.io.write_ugrid_topology(rg, mesh, mesh_name='mesh2d', crs_wkt=None)
#
Write UGRID topology to a GDAL root group.
Creates the topology variable, node coordinate arrays, connectivity arrays, and optional face/edge center coordinates. Uses cf.write_attributes_to_md_array for all attribute writing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rg
|
Group
|
GDAL root group to write into. |
required |
mesh
|
Mesh2d
|
Mesh2d instance. |
required |
mesh_name
|
str
|
Name for the topology variable. |
'mesh2d'
|
crs_wkt
|
str | None
|
WKT CRS string (optional). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict mapping dimension names to GDAL dimension objects, |
dict[str, Any]
|
for use when writing data variables. |
Source code in src/pyramids/netcdf/ugrid/io.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
pyramids.netcdf.ugrid.io.write_ugrid_data_variable(rg, var, mesh_name, dims)
#
Write a single data variable to the GDAL group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rg
|
Group
|
GDAL root group. |
required |
var
|
MeshVariable
|
MeshVariable instance. |
required |
mesh_name
|
str
|
Name of the mesh topology variable. |
required |
dims
|
dict[str, Any]
|
Dict mapping dimension names to GDAL dimension objects. |
required |