Skip to content

_encode_ugrid mutates BASE_GRID_TOPOLOGY_ATTRS #1750

Description

@rajeeja

uxarray/io/_ugrid.py:95 binds the module-level dict without copying and then adds keys to it, so every to_xarray("UGRID") / to_netcdf call in a process permanently grows the shared constant. Any grid encoded afterwards advertises variables and dimensions it does not have.

import numpy as np, uxarray as ux
from uxarray.conventions import ugrid

hpgrid = ux.Grid.from_healpix(zoom=1)
hpgrid.edge_lon; hpgrid.face_edge_connectivity
hpgrid.to_xarray("UGRID")
print(len(ugrid.BASE_GRID_TOPOLOGY_ATTRS))  # 11, was 6

minimal = ux.Grid.from_topology(
    node_lon=np.array([0., 1., 1., 0.]),
    node_lat=np.array([0., 0., 1., 1.]),
    face_node_connectivity=np.array([[0, 1, 2, 3]]),
)
ds = minimal.to_xarray("UGRID")
print("n_edge" in ds.dims, ds.grid_topology.attrs["edge_dimension"])  # False n_edge

Fix is simply grid_topology = dict(ugrid.BASE_GRID_TOPOLOGY_ATTRS) plus a regression test.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions