Topology Detection and I/O#
UGRID topology detection from NetCDF files using GDAL's Multidimensional API, and UGRID-compliant NetCDF writing.
On read, parse_ugrid_topology scans the root group for mesh topology variables and returns one
MeshTopologyInfo per mesh, which Mesh2d.from_gdal_group turns into topology. On write, the topology
and each data variable are emitted back as CF/UGRID-compliant arrays:
flowchart LR
F[(".nc root group")] -->|parse_ugrid_topology| TI["list[MeshTopologyInfo]"]
TI -->|"Mesh2d.from_gdal_group"| M["Mesh2d"]
M -->|write_ugrid_topology| F
V["MeshVariable"] -->|write_ugrid_data_variable| F
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=DEFAULT_MESH_NAME, 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. |
DEFAULT_MESH_NAME
|
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
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |
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 |