Skip to content

Loss of mask when slicing/extracting  #2046

@matthew-mizielinski

Description

@matthew-mizielinski

When extracting a region, or slicing a dimension, from a cube with masked data the data type of the returned cube is not consistent with the source. Consider the following example;

import iris, numpy as np

print iris, iris.__version__
print np, np.__version__

masked_array = np.ma.MaskedArray([[1.,1.],[1.,100000.]], mask = [[False,False],[False,True]])
lat = iris.coords.DimCoord([-10,10],'latitude')
lon = iris.coords.DimCoord([0,180],'longitude')
cube = iris.cube.Cube(masked_array, dim_coords_and_dims = [(lat,0),(lon,1)])

cube_lat_slices = list(cube.slices_over('latitude'))

assert type(cube_lat_slices[0].data) == type(cube_lat_slices[1].data)

I would expect the cubes returned by slices_over to inherit the data type from cube, but when I run this code I get

<module 'iris' from ......./site-packages/iris/__init__.py'> 1.9.2
<module 'numpy' from '...../site-packages/numpy/__init__.pyc'> 1.8.2
Traceback (most recent call last):
  File "loss_of_masking.py", line 14, in <module>
    assert type(cube_lat_slices[0].data) == type(cube_lat_slices[1].data)
AssertionError

The first slice of cube has type numpy.ndarray, while the second has numpy.ma.core.MaskedArray.

The same issue arises when extracting a region from a masked cube that has no masked data (which is where the issue was first identified).

I view this behaviour as a (minor) bug and would suggest that the behaviour of iris should be made consistent with numpy in this situation; subsetting a MaskedArray returns a MaskedArray.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions