2

I have some curvilinear gridded data, and I'm trying to interpolate this data to a new grid. The data has unrealistically high variables for the masked data, so I tried masking using:

temp = np.ma.masked_where(temp > 500, temp)

if I do this then:

>>> temp.max()
28.174417

However, when I run:

temp = RectBivariateSpline(lon, lat,
                           np.swapaxes(temp, 0, 1))
temp = temp.ev(newgrid_lon, newgrid_lat)

I end up with the unrealistically high mask values again:

>>> temp.max()
1.541312613999187e+30

scipy.interpolate is obviously not accepting my mask. Does anyone know how to set a mask for scipy.interpolate or know any workarounds? Thanks.

Hadi
  • 36,233
  • 13
  • 65
  • 124
user14241
  • 727
  • 1
  • 8
  • 27
  • 3
    Fundamentally you can't do structured interpolation with missing data ([see here](http://stackoverflow.com/q/15485343/1461210)). You could either treat your data as unstructured (e.g. exclude the masked values and use `SmoothBivariateSpline` instead of `RectBivariateSpline`), or else replace your masked values with some sort of approximation (the mean of their nearest neighbours? a constant?) before doing your interpolation. – ali_m Nov 23 '15 at 00:31
  • Thanks for the help. I may try setting the masked values to a constant as you suggest. I will see how that makes the data look. If that doesn't work, I might have to look into `SmoothBivariateSpline`. I just hope its not as slow as `griddata`. – user14241 Nov 23 '15 at 00:49
  • It worked well enough for my purposes. Thanks. – user14241 Nov 23 '15 at 01:25
  • 1
    `np.ma` just adds the mask array without actually changing the data array. Only the `np.ma` methods/functions take the mask into account. Other `numpy` and `scipy` code just uses the data without awareness of the mask. – hpaulj Nov 23 '15 at 03:08
  • I found a great solution using `pyresample.kd_tree.resample_gauss`. It is mask aware, customizable, and very fast. I will be using this for all my grid interpolation in the future. – user14241 Nov 23 '15 at 05:21

0 Answers0