function Af = medfiltJR(A,fsize);
%% To median filter images without distortion around edges and coastlines
%%	fsize is the size of the 2-D box to be used.
%%  	Assumes that land is masked as NaN (no value output to these).
%%	Will change zero to NaN;

idx = find(A==0); A(idx) = NaN*idx;

Af = A; [R C] = size(A); buffer = floor(fsize/2);

for r = 1+buffer:R-buffer;
 for c = 1+buffer:C-buffer;
   a = A([r-buffer:r+buffer],[c-buffer:c+buffer]); a([isnan(a)|a==0])=[];
   if ~isempty(a); Af(r,c)=median(a(:)); end
 end
end
  
  
