function [clim,resid]=minusclim(fldin,nfld,Lclim)
%--
%function [clim,resid]=minusclim(fldin,nfld,Lclim)
% get climatological and residual after subjtracting out climatology
%input:
% fldin: input field, generally the total time-series from which climatology is derived
% Lclim: either 12 (months) or 365 (days)
% nfld: the number of fields
%output:
% clim:  climatological field, repmat to full length of fldin
% resid: residual, computed as fldin-clim
%--

sz0=size(fldin);
if(sz0(2)==nfld);
  fldin=permute(fldin,[2 1]);
end;

sz=size(fldin);

clim=reshape(fldin,nfld,Lclim,sz(2)/Lclim);
clim=squeeze(nanmean(clim,3));
clim=repmat(clim,[1 1 sz(2)/Lclim]);clim=reshape(clim,nfld,sz(2));


resid=fldin-clim;

if(sz0(2)==nfld);
   clim=permute(clim,[2,1]);
  resid=permute(resid,[2 1]);
end;

return
