Flag NaN values rearest neighbor with xarray

Viewed 36

I am working on netcdf dataset that sometimes have a large cloud coverage, materialized by NaN. Is there a way to flag the n neighbors of NaN patches ? Namely, go from this:

array([[0.7, 0.3, 0.9, 0.2, 0.6, 0.5, 0.2, 1. ],
       [0.2, 1. , 0.2, 0.2, 0.7, 0. , 1. , 0.2],
       [0.6, 0.4, 0.8, 0.1, 0.2, 0.5, 0.8, 0.7],
       [0.5, 1. , 0.3, 0.8, 0.2, 0.9, 0.2, 0.1],
       [nan, nan, 1. , 0.7, 0.5, 0.7, 0.8, 0.6],
       [nan, nan, 0.6, 0.4, 0.3, 0.8, 0.2, 0.8]])

To this (for n = 2):

array([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
       [1. , 1. , 1. , 1. , 0. , 0. , 0. , 0. ],
       [1. , 1. , 1. , 1. , 0. , 0. , 0. , 0. ],
       [nan, nan, 1. , 1. , 0. , 0. , 0. , 0. ],
       [nan, nan, 1. , 1. , 0. , 0. , 0. , 0. ]])

I tried using xarray.dataarray.rolling() but it does not support np.isnan() (see error bellow).

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Thanks in advance for your help.

1 Answers

In principle, you can do similar thing with NumPy, it does not matter whether you originally used xarray or something else to read in the data.

#!/usr/bin/env ipython
# ---------------------
import numpy as np
from pylab import subplot,pcolormesh,show
# ---------------------
n=1;
ny,nx = 20,20
# ---------------------
# some random matrix:
dataout = np.random.random((ny,nx))

n_nan = 5
# some random places for nan values:
ii = np.array(np.random.random((n_nan,1))*nx,dtype='int32')
jj = np.array(np.random.random((n_nan,1))*ny,dtype='int32')

dataout[jj,ii] = np.nan
nanmask = np.array(np.isnan(dataout),dtype='float32');
# ---------------------------------------------------------
#pcolormesh(dataout);show()
# ---------------------------------------------------------
# let us update the nan-mask:
JJ,II = np.where(np.isnan(dataout)==1);
for inan in range(len(JJ)):
    jp,ip = JJ[inan],II[inan]
    # -----------------------------------------------------
    nanmask[jp-n:jp+n+1,ip-n:ip+n+1] = 1.0
# ---------------------------------------------------------
subplot(121);pcolormesh(dataout);
subplot(122);pcolormesh(nanmask);show()

So, I will detect the mask of nan values and then mark neighbouring locations around each nan point. Not very fancy solution, but seems to work.

Related