-
Notifications
You must be signed in to change notification settings - Fork 298
Description
🐛 Bug Report
Thanks to @HelenRJohnson for spotting this.
Using iris.util.unify_time_units() on a CubeList where the time units have an integer dtype prevents the CubeList from being concatenated:
iris.exceptions.ConcatenateError: failed to concatenate into a single cube.
Dimension coordinates metadata differ: time != time
Explanation
This is because unify_time_units() converts all time units to match the first one. cf-units will not convert something if it is already equal, so the first time unit does not undergo conversion. Meanwhile all the other time units are coerced to dtype="float64", to avoid problems with partial days etc. So the end result is that the first time unit remains an integer dtype while all the others are floats, and this is something that is checked when preparing for concatenation.
Proposed solution
Force all time coordinates to be dtype=float64 at the start of the unify_time_units() process, and advertise this behaviour in the docstring. We should include tests for the new lines, too. I tested this with the below and it fixed my example.
for cube in cubes:
for time_coord in cube.coords():
if time_coord.units.is_time_reference():
+ time_coord.points = time_coord.core_points().astype("float64")
+ time_coord.bounds = time_coord.core_bounds().astype("float64")
epoch = epochs.setdefault(
time_coord.units.calendar, time_coord.units.origin
)How To Reproduce
Steps to reproduce the behaviour:
- Define a
CubeListwith multipleCubes, which can be concatenated together.
EachCubeshould include a time coordinate, usingdtype=intfor the points.
Make sure the finalCube's time coordinate has a different epoch to the others (prevents successful concatenation). - Apply
iris.util.unify_time_units()to theCubeList. - Attempt
CubeList.concatenate_cube()on thisCubeList.
Expected behaviour
Should produce a single concatenated Cube.
Environment
- OS & Version: Red Hat 7.9
- Iris Version:
v3.6.1