Skip to content

Cube units dropped after pickle #6378

@cpelley

Description

@cpelley

🐛 Bug Report

Could be a cf_units bug (I am as of yet unsure), but since my example uses iris cubes for now, I'll open here.
The issue is that, on pickling the cube, its units (cf_units) disappears.
This bug is exposed with use of multiprocessing, since that is reliant on pickling for serialisation.

How To Reproduce

Example where the units disappear after multiplying our cube by another with unit of 1:

>>> import pickle
>>> import iris.cube
>>> res = iris.cube.Cube(0, units='K') * iris.cube.Cube(0, units=1)
>>> res
<iris 'Cube' of unknown / (K) (scalar cube)>
>>> pickle.dump(res, open("/tmp/res.pkl", "wb")); pickle.load(open("/tmp/res.pkl", "rb"))
<iris 'Cube' of unknown / (unknown) (scalar cube)>

Where we don't multiply our cube by another with unit 1 (everything turns out OK):

>>> res = iris.cube.Cube(0, units='K')
>>> res
<iris 'Cube' of unknown / (K) (scalar cube)>
>>> pickle.dump(res, open("/tmp/res.pkl", "wb")); pickle.load(open("/tmp/res.pkl", "rb"))
<iris 'Cube' of unknown / (K) (scalar cube)>

Environment

as per sss/environments/default-2024_11_26:

Package/app version
cf_units 3.2.0
iris 3.11.0
Python 3.12.7

Workaround (of sorts)

cube.units = str(cube.units)

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions