Skip to content

iris.analysis.interpolate.linear breaks masked array #106

Closed
rhattersley opened this Issue Sep 21, 2012 · 3 comments

2 participants

@rhattersley
SciTools member

The problem seems to require all of: the data to not be square; the latitude coordinate to be monotonically decreasing; the longitude coordinate to be circular; and cube.data.sharedmask to be False!

The problem was originally triggered by a netCDF4 file with the all the characteristics listed above.

import iris
import iris.tests.stock as stock

cube = stock.realistic_4d_w_missing_data()
# Workaround - slicing a cube removes the mask!
data = cube.data
cube = cube[0, 2, 18::-1]
data = data[0, 2, 18::-1]
cube.data = data
cube.coord('grid_longitude').circular = True
cube.data.unshare_mask()

print 'Before'
print cube.data.data.shape
print cube.data.mask.shape
s = str(cube.data)

sample = [('grid_longitude',0), ('grid_latitude',0)]
inter = iris.analysis.interpolate.linear(cube, sample)

print 'After'
print cube.data.data.shape
print cube.data.mask.shape
s = str(cube.data)

gives (with NumPy 1.6.1):

Before
(19, 20)
(19, 20)
After
(19, 20)
(20, 19)
Traceback (most recent call last):
  File "foo", line 26, in <module>
    s = str(cube.data)
  File ".../python2.7/site-packages/numpy/ma/core.py", line 3531, in __str__
    res[m] = f
IndexError: index (19) out of range (0<=index<18) in dimension 0
@bblay
bblay commented Oct 2, 2012

Isolated

import numpy
import numpy.ma


def simple_ma():
    data = numpy.arange(20).reshape(4,5)
    data[:2, :2] = -1
    data = numpy.ma.masked_less(data, 0)
    return data


# Create a simple masked array
data = simple_ma()
print "Before\n------"
print "data", data.shape, "\n", data, "\n"
print "mask", data.mask.shape, "\n", data.mask, "\n" 

# Create a new array from the masked array
result = numpy.append(data, data[:, :1], axis=1)
print "After\n-----"
print "data", data.shape, "\n", data, "\n"
print "mask", data.mask.shape, "\n", data.mask, "\n" 

# Workaround
data = simple_ma()
new_data = numpy.append(data.data, data.data[:, :1], axis=1)
new_mask = numpy.append(data.mask, data.mask[:, :1], axis=1)
data = numpy.ma.array(new_data, mask=new_mask)
print "Workaround\n----------"
print "data", data.shape, "\n", data, "\n"
print "mask", data.mask.shape, "\n", data.mask, "\n" 

prints

Before
------
data (4, 5) 
[[-- -- 2 3 4]
 [-- -- 7 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]] 

mask (4, 5) 
[[ True  True False False False]
 [ True  True False False False]
 [False False False False False]
 [False False False False False]] 

After
-----
data (4, 5) 
[[-- -- 2 3 4]
 [-1 -- -- 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]] 

mask (5, 4) 
[[ True  True False False]
 [False  True  True False]
 [False False False False]
 [False False False False]
 [False False False False]] 

Workaround
----------
data (4, 6) 
[[-- -- 2 3 4 --]
 [-- -- 7 8 9 --]
 [10 11 12 13 14 10]
 [15 16 17 18 19 15]] 

mask (4, 6) 
[[ True  True False False False  True]
 [ True  True False False False  True]
 [False False False False False False]
 [False False False False False False]] 
@bblay
bblay commented Oct 2, 2012

Related to http://projects.scipy.org/numpy/ticket/1623
Raised a new numpy issue on guthub: numpy/numpy#478

@bblay bblay was assigned Oct 2, 2012
@bblay
bblay commented Oct 16, 2012

merged

@bblay bblay closed this Oct 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.