Iris meshes are not pickle safe

Open
#7,028 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
62/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python
Domain
data

Research direction

Start with iris/mesh/components.py, especially the mesh connectivity manager shown in the traceback, and reproduce the failure using test_data/NetCDF/ugrid/21_triangle_example.nc. Ensure a pickled and reloaded cube retains usable mesh connectivities, then rerun the demonstrated connectivity check to confirm it succeeds.

Written by the indexing model from the issue text.

Description

Type: Bug

🐛 Bug Report

Highlighting this behaviour with the iris UGRID 21_triangle_example.nc test data, which loads 5 cubes with meshes, but let's just focus on one of them:

>>> import iris
>>>
>>> fname = "test_data/NetCDF/ugrid/21_triangle_example.nc"
>>> cube = iris.load_cube(fname, "volume flux between cells")
>>> print(cube)

Yields:

volume flux between cells / (m^3/s) (-- : 41)
    Mesh coordinates:
        latitude                        x
        longitude                       x
    Mesh:
        name                        Topology data of 2D unstructured mesh
        location                    edge
    Attributes:
        Conventions                 'UGRID-0.9'

Let's see the available connectivities attached to the mesh:

>>> from pprint import pprint
>>>
>>> pprint(cube.mesh.connectivities)
[<Connectivity: Maps every triangular face to its three corner nodes. / (unknown)  [...]  shape(21, 3)>,
 <Connectivity: Maps every edge to the two nodes that it connects. / (unknown)  [...]  shape(41, 2)>,
 <Connectivity: Maps every boundary segment to the two nodes that it connects. / (unknown)  [...]  shape(19, 2)>]

Now let's serialize the cube (along with its mesh et al) using pickle:

>>> import pickle
>>>
>>> with open("flux.pkl", "wb") as fh:
...     pickle.dump(cube, fh)
...

Now load the pickle back and recheck the connectivities again:

>>> with open("flux.pkl", "rb") as fh:
...     flux = pickle.load(fh)
...
>>> print(flux)
volume flux between cells / (m^3/s) (-- : 41)
    Mesh coordinates:
        latitude                        x
        longitude                       x
    Mesh:
        name                        Topology data of 2D unstructured mesh
        location                    edge
    Attributes:
        Conventions                 'UGRID-0.9'

Seems fine so far ...

>>> pprint(flux.mesh.connectivities())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/apps/sss/environments/default-2025_11_26/lib/python3.12/site-packages/iris/mesh/components.py", line 1433, in connectivities
    result = self._connectivity_manager.filters(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/apps/sss/environments/default-2025_11_26/lib/python3.12/site-packages/iris/mesh/components.py", line 2617, in filters
    supports_faces = any(["face" in role for role in self.ALL])
                                                     ^^^^^^^^
AttributeError: '_Mesh2DConnectivityManager' object has no attribute 'ALL'

💣 Oppsie!

We need to add additional __setstate__ and __getstate__ methods to provide full pickling coverage for meshes.

Ping @jrackham-mo

Dominant language
Python
Stars
724
Forks
317
Avg merge
3d 20h
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SciTools/iris

All issues in SciTools/iris

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.