iris
iris copied to clipboard
Aggregating a masked auxiliary coordinate removes mask
🐛 Bug Report
Aggregating (Cube.aggregated_by or Cube.collapsed) a masked auxiliary coordinate removes the mask of the coordinate (i.e., a regular numpy array is returned). Cube.rolling_window seems to work fine.
How To Reproduce
Steps to reproduce the behaviour:
import numpy as np
import iris
import iris.analysis
from iris.coords import AuxCoord
from iris.cube import Cube
print("Iris version:", iris.__version__)
year_coord = AuxCoord([2000, 2000, 2001], long_name="year")
p_coord = AuxCoord(np.ma.masked_invalid([np.nan, np.nan, np.nan]), var_name="p")
# p_coord = AuxCoord(np.ma.masked_invalid([1, 2, 3]), var_name="p") # this works as expected (i.e., values are averaged)
cube = Cube([1, 2, 3], aux_coords_and_dims=[(year_coord, 0), (p_coord, 0)])
agg_cube = cube.aggregated_by("year", iris.analysis.MEAN)
col_cube = cube.collapsed("year", iris.analysis.MEAN)
rol_cube = cube.rolling_window("year", iris.analysis.MEAN, 2)
print("before:", cube.coord("p").points)
print("after aggregated_by:", agg_cube.coord("p").points)
print("after collapsed:", col_cube.coord("p").points)
print("after rolling_window:", rol_cube.coord("p").points)
gives
Iris version: 3.12.2
before: [-- -- --]
after aggregated_by: [nan nan]
after collapsed: [0.]
after rolling_window: [-- --]
Expected behaviour
Iris version: 3.12.2
before: [-- -- --]
after aggregated_by: [-- --]
after collapsed: [--]
after rolling_window: [-- --]
Environment
- OS & Version: Linux
- Iris Version: 3.12.2
From @SciTools/peloton: worth looking for any occurrences of np.asarray() or np.array() - should ideally be np.asanyarray()
Possibly related to the comment here?