code: def median_fltr(dem, fsize=7, origmask=False):
      
      print("Applying median filter with size %s" % fsize)
      from scipy.ndimage.filters import median_filter
      dem_filt_med = median_filter(dem.filled(np.nan), fsize)
      #Now mask all nans
      out = np.ma.fix_invalid(dem_filt_med, copy=False, fill_value=dem.fill_value)
      if origmask:
          out = np.ma.array(out, mask=dem.mask, fill_value=dem.fill_value)
      out.set_fill_value(dem.fill_value)
      return out
docs: Scipy.ndimage median filter
  
      Does not properly handle NaN