function [kappa_ddiff_t,kappa_ddiff_s,dTdz,dSdz]=cvmix_ddiff(T,S,P,lat,str,flag_smooth,Nsmooth)
%-------
%function [kappa_ddiff_t,kappa_ddiff_s]=cvmix_ddiff(T,S,P,lat,str,flag_smooth,Nsmooth)
% copy from cvmix_init_ddiff
%
% Input: 
%   T: either potential temp or insitu temp, size [Lz x Nprof]
%   S: salinity, same size as T
%   P: pressure, size [Lz x Nprof] or size [Nz x 1]
%   lat: latitude, to convert from P to z [m]
%   str: 'ptemp' or 'temp'
%   flag_smooth: do smoothing [1] or not [0]
%   Nsmooth: number of point for running avg along profile
%
% Output:
%   kappa_ddiff_t, size [Nz-1 x Nprof] , at centered location
%   kappa_ddiff_s, size [Nz-1 x Nprof] , at centered location 
%-------

%---- assume z is the first dimension
sz=size(T);if(sz(1)==1&sz(2)>sz(1));T=T';end;
sz=size(S);if(sz(1)==1&sz(2)>sz(1));P=S';end;
sz=size(P);if(sz(1)==1&sz(2)>sz(1));P=P';end;

szT=size(T);
szS=size(S);
szP=size(P);

if(szT~=szS);error('size T and S not the same');end;
if(szT(1)==szP(1) & szT(2)>szP(2));
  P=repmat(P,[1 szT(2)]);
end;
Lz=szT(1);
Nprof=szT(2);

%convert P to z
% a note on z-dir: this code assumes that 
%   dSdz is positive when salty water sits on top of freshwater
%   dTdz is negative when cold water sits on top of warm water
% This implies z-positive is UP, so make sure z is negative for depth, starting at 0 at surface
z=depth(P,lat);
z=-abs(z);

%---- first: convert to potential temperature:
%keyboard

if(strmatch(str,'temp')==1);
  fprintf('converting insitu temp to ptemp');
  T=sw_ptmp(S,T,P,zeros(size(P)));
  str='ptemp';
end;

%---- smoothing if needed:
%if(flag_smooth==1);
%  Tsm=zeros(size(T));
%  Ssm=Tsm;
%  T0=T;
%  S0=S;
%  for iprof=1:Nprof
%    Tsm(:,iprof)=smooth(T(:,iprof),Nsmooth);  
%    Ssm(:,iprof)=smooth(S(:,iprof),Nsmooth);  
%  end;
%  T=Tsm;
%  S=Ssm;
%end;

%---- calculate alpha and beta:
beta  =sw_beta(S,T,P,str);
alpha =sw_alpha(S,T,P,str);

%---- centering:
P_c    =(P(1:Lz-1,:)+P(2:Lz,:))./2;
z_c    =(z(1:Lz-1,:)+z(2:Lz,:))./2;
alpha_c=(alpha(1:Lz-1,:)+alpha(2:Lz,:))./2;
beta_c =(beta(1:Lz-1,:)+beta(2:Lz,:))./2;

%---- define constants:
%values from cvmix assumption:
kappa_s0           = 1e-4;
kappa_t0           = 0.7*kappa_s0;
%values I used for the Arctic:
kappa_s0           = 1e-6;
kappa_t0           = 0.7*kappa_s0;
R_rho0             = 2.55;
nu_molecular       = 1.5e-6;
kappa_ddiff_param1 = 0.909;
kappa_ddiff_param2 = 4.6;
kappa_ddiff_param3 = -0.54;
ddiff_exp1         = 1;
ddiff_exp2         = 3;

%calculate stratification
dTdz=diff(T,1,1)./diff(z,1,1);
dSdz=diff(S,1,1)./diff(z,1,1);

%inspection shows that this dTdz and dSdz are very noisy.
% This is perhaps expected because diffusion is happening on very small
% level as long as there is any instability.
% However, for the purpose of visualization, perhaps we can smooth
% the dTdz and dSdz out to get a first-order understanding of
% how much double diffusion can contribute in addition to the rough
% background guess of 1e-6 ?

if(flag_smooth==1);
  dTdzsm=zeros(size(dTdz));
  dSdzsm=dSdz;
  dTdz0=dTdz;
  dSdz0=dSdz;
  for iprof=1:Nprof
    dTdzsm(:,iprof)=smooth(dTdz(:,iprof),Nsmooth);  
    dSdzsm(:,iprof)=smooth(dSdz(:,iprof),Nsmooth);  
  end;
  dTdz=dTdzsm;
  dSdz=dSdzsm;
end;


%now filtering according to stratification:
%note that because dSdz is in the denominator, can risk infinity if dSdz is too small
% so should define a dSdz_tiny = 1e-4 to cap this?

dSdz_tiny=1e-4;

R_rho=alpha_c.*(dTdz)./(beta_c.*(dSdz));

%---- predefine vertical column
kappa_ddiff_t=kappa_t0.*ones(size(alpha_c));
kappa_ddiff_s=kappa_s0.*ones(size(alpha_c));

%===============================
for iprof=1:Nprof

%----- CASE 1: salt water on top of fresh water -------
  ii=find(1<=R_rho(:,iprof) & R_rho(:,iprof)<=R_rho0 & dSdz(:,iprof)>0);%&abs(dSdz(:,iprof))>dSdz_tiny);
  if(length(ii)>0);
    kappa_ddiff_t(ii,iprof)=kappa_t0.*(1-((R_rho(ii,iprof)-1)./(R_rho0-1)).^ddiff_exp1).^ddiff_exp2;
    kappa_ddiff_s(ii,iprof)=kappa_s0.*(1-((R_rho(ii,iprof)-1)./(R_rho0-1)).^ddiff_exp1).^ddiff_exp2;
  end;

%----- CASE 2: cold water on top of warm water --------
  ij=find(R_rho(:,iprof)>0 & R_rho(:,iprof)<1 & dTdz<0);% & abs(dSdz(:,iprof))>dSdz_tiny);
  if(length(ij)>0);
    kappa_ddiff_t(ij,iprof)=nu_molecular.*kappa_ddiff_param1.*...
            exp(kappa_ddiff_param2.*exp(kappa_ddiff_param3.*(1./R_rho(ij,iprof)-1)));

%two cases for kappa_ddiff_s:
    ik=find(R_rho(ij,iprof)<0.5);
    il=find(R_rho(ij,iprof)>=0.5&R_rho(ij,iprof)<1);

    if(length(ik)>0);
      factor=0.15.*R_rho(ij(ik),iprof);
      kappa_ddiff_s(ij(ik),iprof)=factor.*kappa_ddiff_t(ij(ik),iprof);
    end;
    if(length(il)>0);
      factor=1.85.*R_rho(ij(il),iprof)-0.85;
      kappa_ddiff_s(ij(il),iprof)=factor.*kappa_ddiff_t(ij(il),iprof);
    end;
  end;

end;	%iprof loop
%===============================

return
