Created
January 17, 2017 11:50
-
-
Save dennissergeev/f047ff84cfe41d682d3c1cab3f8ce849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import iris | |
import gridfill | |
import numpy as np | |
from scipy import interpolate | |
def remask_cube(cube, new_mask, interp_method='nearest'): | |
ma_cube = cube.copy() | |
data = cube.data | |
if np.ma.is_masked(data): | |
x = np.arange(data.shape[1]) | |
y = np.arange(data.shape[0]) | |
xx, yy = np.meshgrid(x, y) | |
non_ma_x = xx[~data.mask] | |
non_ma_y = yy[~data.mask] | |
non_ma_data = data[~data.mask] | |
if method in ('linear', 'nearest', 'cubic'): | |
data = interpolate.griddata((non_ma_x, non_ma_y), | |
non_ma_data.ravel(), | |
(xx, yy), | |
method=method) | |
elif method == 'poisson': | |
# use gridfill package | |
data = gridfill.fill_cube(ma_cube, 0.01).data | |
else: | |
raise ValueError("Unknown interpolation method '{}'") | |
ma_cube.data = np.ma.masked_where(new_mask, data) | |
return ma_cube |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment