Source code for opendrift.readers.interpolation.interpolators

import logging
import numpy as np
from scipy.ndimage import map_coordinates, uniform_filter
import logging; logging.captureWarnings(True)
from scipy.interpolate import interp1d, LinearNDInterpolator

logger = logging.getLogger(__name__)  # using common logger

[docs] def expand_numpy_array(data): if isinstance(data, np.ma.MaskedArray): logger.warning('Converting masked array to numpy array before interpolating') data = np.ma.filled(data, fill_value=np.nan) if not np.isfinite(data).any(): logger.warning('Only NaNs, returning') return # Fill each NaN cell with the MEAN of its valid 3x3 neighbours (was # grey_dilation, a MAX filter -- biased for signed fields like u/v). mask = ~np.isfinite(data) valid = np.isfinite(data).astype(np.float64) filled = np.where(mask, 0.0, data).astype(np.float64) num = uniform_filter(filled, size=3, mode='constant', cval=0.0) den = uniform_filter(valid, size=3, mode='constant', cval=0.0) fillable = mask & (den > 1e-12) with np.errstate(invalid='ignore', divide='ignore'): neighbour_mean = num / den data[fillable] = neighbour_mean[fillable].astype(data.dtype)
# cells with no valid neighbour stay NaN ########################### # 2D interpolator classes ###########################
[docs] class Nearest2DInterpolator(): def __init__(self, xgrid, ygrid, x, y): self.x = x self.y = y # N points span (N-1) cell gaps, not N. self.xi = (x - xgrid.min())/(xgrid.max()-xgrid.min())*(len(xgrid)-1) self.yi = (y - ygrid.min())/(ygrid.max()-ygrid.min())*(len(ygrid)-1) self.xi = np.round(self.xi).astype(np.uint32) self.yi = np.round(self.yi).astype(np.uint32) self.xi[self.xi >= len(xgrid)] = len(xgrid)-1 self.yi[self.yi >= len(ygrid)] = len(ygrid)-1
[docs] def __call__(self, array2d): return array2d[self.yi, self.xi]
[docs] class NDImage2DInterpolator(): def __init__(self, xgrid, ygrid, x, y): self.x = x self.y = y self.xi = (x - xgrid.min())/(xgrid.max()-xgrid.min())*len(xgrid) self.yi = (y - ygrid.min())/(ygrid.max()-ygrid.min())*len(ygrid)
[docs] def __call__(self, array2d): try: array2d = np.ma.array(array2d, mask=array2d.mask) array2d[array2d.mask] = np.nan # Gives holes except: pass return np.ma.masked_invalid( map_coordinates(array2d, [self.yi, self.xi], cval=np.nan, order=0))
[docs] class LinearND2DInterpolator(): def __init__(self, xgrid, ygrid, x, y): self.block_x, self.block_y = np.meshgrid(xgrid, ygrid) self.block_x = self.block_x.ravel() self.block_y = self.block_y.ravel() self.x = x self.y = y
[docs] def __call__(self, array2d): array_ravel = array2d.ravel() valid = np.isfinite(array_ravel) #if isinstance(array2d.mask, np.ndarray): # valid = ~array2d.ravel().mask #elif array2d.mask == False: # valid = np.ones(array_ravel.shape, dtype=bool) #elif array2d.mask == True: # valid = np.zeros(array_ravel.shape, dtype=bool) if hasattr(self, 'interpolator'): if not np.array_equal(valid, self.interpolator.valid): logger.debug('Cannot reuse interpolator - validity of ' 'array is different from original.') if hasattr(self, 'interpolator') and (np.array_equal( valid, self.interpolator.valid)): # Reuse stored interpolator with new data self.interpolator.values[:, 0] = \ (array_ravel[valid]) else: # Make new interpolator for given x,y self.interpolator = LinearNDInterpolator( (self.block_y[valid], self.block_x[valid]), array_ravel[valid]) # Store valid array, to determine if can be used again self.interpolator.valid = valid # Call interpolator to avoid threading-problem: # https://github.com/scipy/scipy/issues/8856 self.interpolator((0,0)) return self.interpolator(self.y, self.x)
[docs] class Linear2DInterpolator(): def __init__(self, xgrid, ygrid, x, y): self.x = x self.y = y self.xi = (x - xgrid[0])/(xgrid[-1]-xgrid[0])*(len(xgrid)-1) self.yi = (y - ygrid[0])/(ygrid[-1]-ygrid[0])*(len(ygrid)-1)
[docs] def __call__(self, array2d): if isinstance(array2d,np.ma.MaskedArray): logger.debug('Converting masked array to numpy array for interpolation') array2d = np.ma.filled(array2d, fill_value=np.nan) if not np.isfinite(array2d).any(): logger.warning('Only NaNs input to linearNDFast - returning') return np.nan*np.ones(len(self.xi)) # Fill NaN-values with nearby real values interp = map_coordinates(array2d, [self.yi, self.xi], cval=np.nan, order=1) missing = np.where(~np.isfinite(interp))[0] if len(missing) == 0: return interp # Expand the data in place (kept for later calls on the same block), # over the whole block while many elements are missing, then only in # windows around the tiles that still have missing elements. After i # expansions a cell depends only on cells within i of it, so windows with # a margin of the remaining expansions + 2 give the same values as # expanding the whole block (to rounding). max_iter, tile = 10, 32 ny, nx = array2d.shape data = array2d windows = None try_windows_below = len(missing) + 1 # tile again once missing has halved i=0 while len(missing) > 0: i += 1 if i > max_iter: logger.warning('Still NaN-values after %i iterations, exiting!' % max_iter) break logger.debug('Linear2DInterpolator informational: NaN values for %i elements, expanding data %i' % (len(missing), i)) if windows is None and len(missing) < try_windows_below: try_windows_below = len(missing) // 2 margin = max_iter - i + 3 ty = np.clip(np.floor(self.yi[missing]), 0, ny - 1).astype(int) // tile tx = np.clip(np.floor(self.xi[missing]), 0, nx - 1).astype(int) // tile tiles, which = np.unique(ty * nx + tx, return_inverse=True) if len(tiles) * (tile + 2 * margin)**2 < data.size / 2: windows = [] for k, (y, x) in enumerate(zip(tiles // nx, tiles % nx)): y0, x0 = max(0, y * tile - margin), max(0, x * tile - margin) sl = (slice(y0, (y + 1) * tile + margin + 1), slice(x0, (x + 1) * tile + margin + 1)) windows.append([sl, data[sl].copy(), missing[which.ravel() == k], 0]) if windows is None: expand_numpy_array(data) interp[missing] = map_coordinates( data, [self.yi[missing], self.xi[missing]], cval=np.nan, order=1, mode='nearest') else: for w in windows: sl, window, wmissing, _ = w if len(wmissing) == 0: continue if not np.isfinite(window).any(): # Too far from valid data to be filled within max_iter w[2] = wmissing[:0] continue expand_numpy_array(window) w[3] += 1 interp[wmissing] = map_coordinates( window, [self.yi[wmissing] - sl[0].start, self.xi[wmissing] - sl[1].start], cval=np.nan, order=1, mode='nearest') w[2] = wmissing[~np.isfinite(interp[wmissing])] missing = np.where(~np.isfinite(interp))[0] # Copy the windows back into the block, except near their inner edges for sl, window, _, rounds in windows or []: if rounds > 0: inner = tuple(slice(0 if s.start == 0 else rounds + 1, m if s.start + m >= n else m - rounds - 1) for s, m, n in zip(sl, window.shape, (ny, nx))) block = data[sl][inner] fill = ~np.isfinite(block) block[fill] = window[inner][fill] return interp
horizontal_interpolation_methods = { 'nearest': Nearest2DInterpolator, 'ndimage': NDImage2DInterpolator, 'linearND': LinearND2DInterpolator, 'linearNDFast': Linear2DInterpolator} ########################### # 1D interpolator classes ###########################
[docs] class Nearest1DInterpolator(): def __init__(self, zgrid, z): # Truncating above and below z[z < zgrid.min()] = zgrid.min() z[z > zgrid.max()] = zgrid.max() # Interpolator zgrid -> index if zgrid[1] > zgrid[0]: # increasing z_interpolator = interp1d(zgrid, range(len(zgrid))) else: # decreasing values, must flip for interpolator z_interpolator = interp1d(zgrid[::-1], range(len(zgrid))[::-1]) # Indices corresponding to nearest value in zgrid self.zi = np.round(z_interpolator(z)).astype(np.uint8) self.zi[self.zi < 0] = 0 self.zi[self.zi >= len(zgrid)] = len(zgrid) - 1
[docs] def __call__(self, array2d): return array2d[self.zi, range(len(self.zi))]
[docs] class Linear1DInterpolator(): def __init__(self, zgrid, z): # Truncating above and below z[z < zgrid.min()] = zgrid.min() z[z > zgrid.max()] = zgrid.max() # Interpolator zgrid -> index if zgrid[1] > zgrid[0]: # increasing z_interpolator = interp1d(zgrid, range(len(zgrid))) else: # decreasing values, must flip for interpolator z_interpolator = interp1d(zgrid[::-1], range(len(zgrid))[::-1]) z_interpolator(z[0]) # to prevent threading issues # Indices corresponding to layers above and below interp_zi = z_interpolator(z) self.index_above = np.floor(interp_zi).astype(np.int8) self.index_above[self.index_above < 0] = 0 self.index_below = np.minimum(self.index_above + 1, len(zgrid) - 1) self.weight_above = 1 - (interp_zi - self.index_above) self.xi = range(len(z))
[docs] def __call__(self, array2d): return array2d[self.index_above, self.xi]*self.weight_above + \ array2d[self.index_below, self.xi]*(1 - self.weight_above)
vertical_interpolation_methods = { 'nearest': Nearest1DInterpolator, 'linear': Linear1DInterpolator}
[docs] def fill_NaN_towards_seafloor(array): """Extrapolate NaN-values (missing) towards seafloor""" filled = False for i in range(1, array.shape[0]): mask = np.isnan(array[i,:,:]) if np.sum(mask) > 0: array[i, mask] = array[i-1, mask] filled = True return filled