function [fldOut,area]=calc_mskmean_T_mod(fldIn,mask,RAC,fldType);
% CALC_MSKMEAN_T(budgIn,mask,RAC,fldType) 
%    computes average over a region (mask) of fldIn (or its fields recursively).
%    If fldType is 'intensive' (default) then fldIn is mutliplies by RAC.
%same as gcmfaces/gcmfaces_calc/calc_mskmean_T.m but using compact format

%gcmfaces_global;

if isempty(who('fldType')); fldType='intensive'; end;

if isa(fldIn,'struct');
  list0=fieldnames(fldIn);
  fldOut=[];
  for vv=1:length(list0);
    tmp1=getfield(fldIn,list0{vv});
%    if isa(tmp1,'gcmfaces');
    if isa(tmp1,'double');
      [tmp2,area]=calc_mskmean_T_mod(tmp1,mask,RAC,fldType);
      fldOut=setfield(fldOut,list0{vv},tmp2);
    end;
  end;
  return;
end;

nr=size(fldIn,3);
nr2=size(mask,3);
if nr2~=nr; mask=repmat(mask,[1 1 nr]); end;
mask(mask==0)=NaN; mask(isnan(fldIn))=NaN;
areaMask=repmat(RAC,[1 1 nr]).*mask;
if strcmp(fldType,'intensive');
  fldOut=nansum(fldIn(:).*areaMask(:))./nansum(areaMask(:));
  area=nansum(areaMask(:));
else;
  fldOut=nansum(fldIn(:).*mask(:))./nansum(areaMask(:));
  area=nansum(areaMask(:));
end;


