Skip to content

UGRID NetCDF — unstructured grids in netCDF (.nc) ​

The UGRID conventions for unstructured meshes in netCDF: what Delft3D-FM, D-Flow FM, SCHISM, FVCOM and xugrid write, and QGIS's mesh layer reads (roadmap 2.4.3.2). Both engines read and write it (formats/ugrid_nc.cpp on the shared netCDF helpers; meshioplusplus.ugrid_nc through netCDF4). The AFLR3 ugrid format keeps its own name.

Format nameugrid_nc
Extensions.nc
Read / Write✓ / ✓ (1-D, 2-D, and 3-D topologies; time-series writer)
Extra dependenciesnetCDF (MESHIOPLUSPLUS_WITH_NETCDF) in the core; netCDF4 for the Python reference

Reading ​

python
import meshioplusplus

mesh = meshioplusplus.read("model_map.nc")                       # the first step
last = meshioplusplus.ugrid_nc.read("model_map.nc", time_step=-1)
times = meshioplusplus.ugrid_nc.time_values("model_map.nc")
  • A variable on a dimension netCDF named itself (phony_dim_<n>) is refused by name. netCDF-C gives a dataset whose dimension scales are missing or corrupt such a dimension, and when its DIMENSION_LIST names datasets that are no dimension scales it leaves the variable's dimensions NULL, so reading it crashed inside the library (found by fuzzing; tests/fuzz/regressions/ugrid_nc/dimension-list-names-no-scale). A plain HDF5 file without dimension scales is therefore not read as UGRID, which resolves its node, volume, face and edge dimensions by name.
  • The first variable with cf_role = "mesh_topology" is the mesh (1-D, 2-D, or 3-D); an Exodus file (num_nodes, num_dim) is told to read as exodus.
  • node_coordinates gives x, y and an optional third coordinate (z is 0 otherwise). Spherical lon/lat coordinates are kept as they are.
  • volume_node_connectivity (with optional volume_shape_type: 0 tetrahedron, 1 wedge, 2 hexahedron, 3 pyramid, or inferred from node count 4/5/6/8) becomes tetra, pyramid, wedge, and hexahedron blocks in that order.
  • face_node_connectivity: start_index (0 or 1) is honoured, the _FillValue padding of mixed polygons is dropped, and an array stored (max_nodes, faces) is read through the topology's face_dimension. Faces become a triangle, a quad and one polygon block per larger node count, in that order, each in the file's face order.
  • edge_node_connectivity becomes a trailing line block.
  • Data variables naming the mesh (mesh) with location node, volume, face or edge become point and cell data (a trailing dimension becomes components). A variable defined on a subset of topology dimensions is padded with NaN (0 for an integer array) on the other blocks; a variable carrying meshioplusplus_name is read under that name, which is how written <name>_face and <name>_edge variables merge back.
  • A leading time dimension is the step axis: time_step (0 the first, negatives from the end; ReadOptions::mTimeStep in C++) picks one, its value is field_data["meshio:time"], and time_values (the metadata reader in the core) lists them all. Variables laid out otherwise are skipped with a warning.

Writing ​

python
meshioplusplus.write("out.nc", mesh)
with meshioplusplus.ugrid_nc.SeriesWriter("series.nc") as writer:
    writer.write_step(0.0, mesh_t0)
    writer.write_step(1.0, mesh_t1)

A netCDF-4 file with one topology mesh (3-D when volume cells are present, 2-D for faces, or 1-D for lines): mesh_node_x, mesh_node_y (and mesh_node_z for 3-D topologies or when any z is non-zero), mesh_volume_nodes and mesh_volume_shape_type from tetra, pyramid, wedge, and hexahedron blocks, mesh_face_nodes from triangle, quad, and polygon blocks (_FillValue -1, start_index 0), and mesh_edge_nodes from line blocks. Point data are node variables, cell data volume/face/edge variables (disambiguated with _face / _edge and meshioplusplus_name when mixed). Names are made netCDF-safe (the original kept in meshioplusplus_name). Unsupported cell types are refused by name. Multi-step files are written through ugrid_nc.SeriesWriter (UgridNcSeriesWriter in C++) or write_sequence(..., file_format="ugrid_nc"), appending steps along an unlimited time dimension while preserving the mesh's CRS.

Georeferencing ​

The CF grid_mapping variable the node coordinates name is read as field_data["meshio:crs"] (units and CRS): its crs_wkt or spatial_ref, else epsg_code/EPSG_code text, else an integer epsg as EPSG:<code>. A write of a mesh with a CRS adds a scalar mesh_crs grid-mapping variable (crs_wkt for WKT; epsg_code and epsg for an EPSG code) and names it in every coordinate variable's grid_mapping. Both engines.

Verification ​

xugrid (0.15) opens a file meshio++ wrote, and meshio++ reads the file xugrid writes back (tests/python/test_ugrid_nc.py); the mixed, transposed, 1-based and time-dependent layouts are built in the tests with netCDF4. No Delft3D-FM _map.nc has been read yet.

Released under the MIT License.