baspy
baspy copied to clipboard
Ignore missing values when calculating trend
missing_value = scs_full_cube.data.fill_value
slope = np.zeros(cube.data[0,:,:].shape)
for x in range(0,len(lons)):
for y in range(0,len(lats)):
arr =cube.data.data[:,x,y]
ind = np.where(arr < 366)[0]
if len(ind) < len(arr)*0.8: # if 20% of points are missing then set to missing value (ignore it in plot)
slope[x,y] = missing_value
else:
slope[x,y], intercept, r_value, p_value, std_err = stats.linregress(years[ind], arr[ind])
# create new mask and add to cube
new_mask = np.zeros(slope.shape)
new_mask[ slope == missing_value] = 1.
new_mask = new_mask.astype(bool) # convert to boolean array (True/False)
mx = ma.masked_array(slope, mask=new_mask)