UGRID Data Models#
Dataclasses for UGRID topology metadata, mesh variables, and dataset-level metadata summaries.
UgridMetadata is the dataset-level summary: it aggregates one MeshTopologyInfo per mesh and records
the variable inventory as a data_variables: dict[str, str] map (variable name → mesh location). The
MeshVariable records themselves are held by UgridDataset, not by UgridMetadata. A
MeshVariable carries a lazy loader so its data is only materialised on access, and offers
time-slicing helpers:
classDiagram
class UgridMetadata {
data_variables : dict[str,str]
global_attributes
conventions
n_nodes / n_faces / n_edges
}
class MeshTopologyInfo {
mesh_name
topology_dimension
node_x_var / node_y_var
face_node_var / edge_node_var
crs_wkt
}
class MeshVariable {
name · location · mesh_name
shape · nodata · units
+data
+n_time_steps
+sel_time(i)
+sel_time_range(a, b)
+with_data(arr)
}
UgridMetadata o-- MeshTopologyInfo : mesh_topologies
note for UgridMetadata "data_variables is a name → location map, not MeshVariable records"
note for MeshVariable "held by UgridDataset; data is loaded lazily on first access"
pyramids.netcdf.ugrid.MeshTopologyInfo
dataclass
#
Parsed UGRID topology metadata from a NetCDF file.
Represents the structure of a single mesh topology variable, including references to coordinate variables, connectivity arrays, and data variables defined on the mesh.
Attributes:
| Name | Type | Description |
|---|---|---|
mesh_name |
str
|
Name of the topology variable (e.g., "mesh2d"). |
topology_dimension |
int
|
Mesh dimensionality (1=network, 2=surface, 3=volume). |
node_x_var |
str
|
Name of the node x-coordinate variable. |
node_y_var |
str
|
Name of the node y-coordinate variable. |
face_node_var |
str | None
|
Name of the face-node connectivity variable. |
edge_node_var |
str | None
|
Name of the edge-node connectivity variable. |
face_edge_var |
str | None
|
Name of the face-edge connectivity variable. |
face_face_var |
str | None
|
Name of the face-face connectivity variable. |
edge_face_var |
str | None
|
Name of the edge-face connectivity variable. |
boundary_node_var |
str | None
|
Name of the boundary-node connectivity variable. |
face_x_var |
str | None
|
Name of the face center x-coordinate variable. |
face_y_var |
str | None
|
Name of the face center y-coordinate variable. |
edge_x_var |
str | None
|
Name of the edge center x-coordinate variable. |
edge_y_var |
str | None
|
Name of the edge center y-coordinate variable. |
data_variables |
dict[str, str]
|
Mapping of variable name to mesh location (e.g., {"water_level": "face"}). |
crs_wkt |
str | None
|
Well-Known Text representation of the CRS, if available. |
Source code in src/pyramids/netcdf/ugrid/models.py
pyramids.netcdf.ugrid.MeshVariable
dataclass
#
Data variable defined on a mesh location.
Wraps a numpy array of values associated with mesh elements (nodes, faces, or edges). Supports lazy loading via a loader callable that defers reading until data is first accessed.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Variable name in the NetCDF file. |
location |
str
|
Mesh location ("node", "face", or "edge"). |
mesh_name |
str
|
Name of the associated mesh topology variable. |
shape |
tuple[int, ...]
|
Shape of the data array. |
attributes |
dict[str, Any]
|
Dictionary of NetCDF variable attributes. |
nodata |
float | None
|
No-data / fill value for masked elements. |
units |
str | None
|
Physical units string (e.g., "m", "m/s"). |
standard_name |
str | None
|
CF standard name (e.g., "sea_surface_height"). |
active |
ndarray | None
|
Optional boolean active (wet/dry) mask, broadcastable to the variable's
element axis — either |
_data |
ndarray | None
|
Eagerly loaded data array, or None if using lazy loading. |
_loader |
Callable[[], ndarray] | None
|
Callable that returns the data array on first access. |
Source code in src/pyramids/netcdf/ugrid/models.py
394 395 396 397 398 399 400 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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 | |
data
property
#
Return the data array, triggering lazy load if needed.
has_data_source
property
#
True when the variable can produce an array — eager data or a lazy loader.
Metadata-only, so callers can decide whether to read a variable's values without forcing
the load a bare .data access would trigger.
n_elements
property
#
Number of mesh elements (last dimension of shape).
time_index
property
#
Axis of the real time dimension, or None when the variable is not temporal.
Identifies the time axis by name from dimensions (a dimension named time/t, or
time…/…_time/…_time_… per the CF/UGRID convention) rather than assuming any extra axis
is time. The word-boundary match avoids substring false-positives like runtime / lifetime
/ daytime, and a non-temporal multi-dimensional variable such as (n_layers, n_face)
reports None (ARC-19). When dimensions is unknown — e.g. an array-built variable that
carries no dimension names — it falls back to the historical heuristic (leading axis of a
1-D array).
Returns:
| Type | Description |
|---|---|
int | None
|
The 0-based index of the time axis, or |
has_time
property
#
True when the variable has a real time dimension.
Unlike a bare ndim > 1 test, this is driven by time_index, so a non-temporal
(n_layers, n_face) variable is not mis-flagged as temporal (ARC-19).
n_time_steps
property
#
Number of time steps. Returns 0 if the variable has no time dimension.
dtype
property
#
Data type of the variable.
Returns the explicitly set dtype if available, falls back to the loaded data's dtype, and defaults to float64.
active_has_time
property
#
True when the active mask carries its own leading time axis ((n_time, n_element)).
A static mask is (n_element,) and applies to every step; a per-step mask is 2-D with
time on axis 0. Returns False when there is no mask.
Examples:
- A per-step mask has a time axis; a static one does not:
>>> import numpy as np >>> from pyramids.netcdf.ugrid.models import MeshVariable >>> per_step = MeshVariable( ... name="wl", location="face", mesh_name="mesh2d", shape=(2, 3), ... active=np.array([[True, False, True], [True, True, False]]), ... ) >>> per_step.active_has_time True >>> static = MeshVariable( ... name="wl", location="face", mesh_name="mesh2d", shape=(3,), ... active=np.array([True, False, True]), ... ) >>> static.active_has_time False
load_array()
#
Return the full array without memoising it on this variable.
.data caches the loaded array on the instance; load_array reads it for a one-shot
consumer (e.g. writing every variable to a file) without retaining it, so a bulk operation
over many variables need not hold them all resident at once (issue #982). Returns an already
loaded array when present.
Returns:
| Type | Description |
|---|---|
NDArray | None
|
numpy.ndarray | None: The array already held, or the one the loader reads -- which |
NDArray | None
|
is not memoised, so a second call reads again. |
NDArray | None
|
neither data nor a loader, which is a metadata-only variable rather than an error. |
Source code in src/pyramids/netcdf/ugrid/models.py
sel_time(index)
#
Select a single time step, reading only that slab from disk when possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Time step index. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
1D array of values at the given time step. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If index is out of range. |
ValueError
|
If the variable has no time dimension, or has no loaded data array. |
Source code in src/pyramids/netcdf/ugrid/models.py
sel_time_range(start, stop)
#
Select a time range, reading only that slab from disk when possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
int
|
Start time index (inclusive). |
required |
stop
|
int
|
Stop time index (exclusive). |
required |
Returns:
| Type | Description |
|---|---|
MeshVariable
|
New MeshVariable with the selected time range. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the variable has no time dimension, or has no loaded data array. |
Source code in src/pyramids/netcdf/ugrid/models.py
with_data(data, *, active=_KEEP_ACTIVE)
#
Return a copy of this variable carrying data, keeping all other metadata.
The new variable is eager (_data set, no loader); its shape is taken from
data when provided, else the original shape is retained. Used wherever a
derived dataset (time selection, spatial clip, …) replaces a variable's array while
preserving its name / location / mesh / attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray | None
|
The replacement data array, or |
required |
active
|
ndarray | None
|
The active (wet/dry) mask the copy carries. Left unset, the source's mask
is carried through unchanged (right for a shape-preserving derivation such as a
layer selection); pass a sliced mask when the element or time axis changed, or
|
_KEEP_ACTIVE
|
Returns:
| Name | Type | Description |
|---|---|---|
MeshVariable |
MeshVariable
|
The copy carrying |
Source code in src/pyramids/netcdf/ugrid/models.py
pyramids.netcdf.ugrid.UgridMetadata
dataclass
#
Full metadata summary for a UGRID dataset.
Aggregates topology information, data variable inventory, global attributes, and mesh element counts for display and inspection purposes.
Attributes:
| Name | Type | Description |
|---|---|---|
mesh_topologies |
tuple[MeshTopologyInfo, ...]
|
List of parsed mesh topologies in the file. |
data_variables |
dict[str, str]
|
Mapping of variable name to location. |
global_attributes |
dict[str, Any]
|
File-level NetCDF attributes. |
conventions |
str | None
|
Conventions string (e.g., "CF-1.8 UGRID-1.0"). |
n_nodes |
int
|
Total number of mesh nodes. |
n_faces |
int
|
Total number of mesh faces. |
n_edges |
int
|
Total number of mesh edges. |