function [diagsM,diags,tempT,tempS,tempf,tempHF,tempAr,ixx,iyy]=compute_heatbudgetprof_v3(T,S,UV,A,HF,ix,iy,iz,ts,dt,yrStart,strexe);

%keyboard
% constants
rho0=1029;%1026;%rho(33,0,0)
Cp=4184;  %4.184x10^3 J/kg/C

%take common lat~70N
%lat0=70;
%GRID_25;
%P=pressure(dpt25(iz),lat0);

tps=datevec(ts2dte(ts,dt,yrStart,1,1));
Lexe=size(strexe,2);
diags=nan(1,4*Lexe);
diagsM=nan(1,10);

sz=size(T);

if((length(ix)==1 & length(iy)>1) | (length(iy)==1 & length(ix)>1));
  if(sz(1)==length(ix)|sz(1)==length(iy));	%already section
    tempHF=HF;
    tempT =T;
    tempS =S;
    tempf =UV;
    tempAr=A;
  else;						%aste input
    LL=max(length(ix),length(iy));
    tempHF=squeeze(HF(ix,iy,iz));   
    tempT =squeeze(T(ix,iy,iz)); 
    tempS =squeeze(S(ix,iy,iz)); 
    tempf =squeeze(UV(ix,iy,iz));
    tempAr=squeeze(A(ix,iy,iz));
  end;
  if(length(ix)==1);ixx=ix.*ones(length(iy),length(iz));iyy=repmat(iy',[1 length(iz)]);
  elseif(length(iy)==1);iyy=iy.*ones(length(ix),length(iz));ixx=repmat(ix',[1 length(iz)]);end;
%already take into account tempHF in vel
  tempHF=tempHF(:);
  maskTS=tempHF;maskTS(find(maskTS~=0))=1;
  tempAr=tempAr(:).*maskTS;	
  tempT =tempT(:).*maskTS;tempS =tempS(:).*maskTS;tempf =tempf(:).*tempHF;
  ixx    =ixx(:);
  iyy    =iyy(:);
%  [tempP,dummy] = meshgrid(P,1:LL);tempP=tempP(:).*maskTS;
%  tempR=rho(tempS,tempT,tempP)-1000;

elseif(length(ix)>1 & length(iy)>1 & length(ix)==length(iy));    %use both u and v
  if(sz(1)==length(ix));	%already section
    tempHF=HF;
    tempT =T;
    tempS =S;
    tempf =UV;
    tempAr=A;
  else;				%aste input
    tempT=zeros(length(ix),length(iz));tempS=tempT;tempf=tempT;tempAr=tempT;tempHF=tempT;
    for j=1:length(ix);
      tempHF(j,:)=squeeze(HF(ix(j),iy(j),iz))';
      tempT(j,:) =squeeze(T(ix(j),iy(j),iz))';
      tempS(j,:) =squeeze(S(ix(j),iy(j),iz))';
      tempf(j,:) =squeeze(UV(ix(j),iy(j),iz))';
      tempAr(j,:)=squeeze(A(ix(j),iy(j),iz))';
      %maskTS=tempHF(j,:);maskTS(find(maskTS~=0))=1;
      %tempT(j,:) =squeeze(T(ix(j),iy(j),iz))'.*maskTS;
      %tempS(j,:) =squeeze(S(ix(j),iy(j),iz))'.*maskTS;
      %tempf(j,:) =squeeze(UV(ix(j),iy(j),iz))'.*tempHF(j,:);
      %tempAr(j,:)=squeeze(A(ix(j),iy(j),iz))'.*maskTS;
    end;
  end;

  ixx=0.*tempT;iyy=0.*tempT;
  for j=1:length(ix);
    ixx(j,:)   =ix(j).*ones(1,length(iz));
    iyy(j,:)   =iy(j).*ones(1,length(iz));
  end;
  maskTS=tempHF;maskTS(find(maskTS~=0))=1;
  tempT =tempT.*maskTS;
  tempS =tempS.*maskTS;
  tempf =tempf.*tempHF;
  tempAr=tempAr.*maskTS;

  tempT=tempT(:);tempS=tempS(:);tempf=tempf(:);tempAr=tempAr(:);ixx=ixx(:);iyy=iyy(:);
  
else;
  error('inside compute_heatbudgetprof_v3.m, wrong length of ix and iy');
end;

% now fill in diags:

  diagsM(1:4)=[ts,tps(1:3)];                                    %[yr,mo,day]
%keyboard
for i=1:Lexe
  if(~isempty(strexe{i}));
    clear ii;eval(['ii=find(' strexe{i} ');']);
    if(length(ii)>0);
      diags((i-1)*4+1)=sum(tempf(ii).*tempAr(ii));                            %m^3/s
      diags((i-1)*4+2)=sum(tempf(ii).*tempAr(ii).*tempT(ii)).*rho0.*Cp;       %J/s
      diags((i-1)*4+3)=sum(tempf(ii).*tempAr(ii).*tempS(ii)).*rho0;           %g/s
      if(i==1);
        diagsM(5)=mean(tempS(ii));diagsM(6) =median(tempS(ii));
        diagsM(7)=mean(tempT(ii));diagsM(8) =median(tempT(ii));
        diagsM(9)=mean(tempf(ii));diagsM(10)=median(tempf(ii));
      end;
    end;
    diags((i-1)*4+4)=length(ii);
  end;
end;

return

