|
14 | 14 |
|
15 | 15 | """Operators to perform various kind of collapse on either 1 or 2 dimensions.""" |
16 | 16 |
|
| 17 | +import datetime |
17 | 18 | import warnings |
18 | 19 |
|
19 | 20 | import iris |
20 | 21 | import iris.analysis |
21 | 22 | import iris.coord_categorisation |
| 23 | +import iris.coords |
22 | 24 | import iris.cube |
23 | 25 | import iris.util |
24 | 26 |
|
@@ -264,8 +266,24 @@ def collapse_by_validity_time( |
264 | 266 | new_cubelist = iris.cube.CubeList( |
265 | 267 | cube.slices_over(["forecast_period", "forecast_reference_time"]) |
266 | 268 | ) |
267 | | - # Remove forecast_period and forecast_reference_time coordinates. |
268 | 269 | for sub_cube in new_cubelist: |
| 270 | + # Reconstruct the time coordinate if it is missing. |
| 271 | + if "time" not in [coord.name() for coord in sub_cube.coords()]: |
| 272 | + ref_time_coord = sub_cube.coord("forecast_reference_time") |
| 273 | + ref_units = ref_time_coord.units |
| 274 | + ref_time = ref_units.num2date(ref_time_coord.points) |
| 275 | + period_coord = sub_cube.coord("forecast_period") |
| 276 | + period_units = period_coord.units |
| 277 | + # Given how we are slicing there will only be one point. |
| 278 | + period_seconds = period_units.convert(period_coord.points[0], "seconds") |
| 279 | + period_duration = datetime.timedelta(seconds=period_seconds) |
| 280 | + time = ref_time + period_duration |
| 281 | + time_points = ref_units.date2num(time) |
| 282 | + time_coord = iris.coords.AuxCoord( |
| 283 | + points=time_points, standard_name="time", units=ref_units |
| 284 | + ) |
| 285 | + sub_cube.add_aux_coord(time_coord) |
| 286 | + # Remove forecast_period and forecast_reference_time coordinates. |
269 | 287 | sub_cube.remove_coord("forecast_period") |
270 | 288 | sub_cube.remove_coord("forecast_reference_time") |
271 | 289 | # Create new CubeList by merging with unique = False to produce a validity |
|
0 commit comments