tdk=273.16;
Rgas=287.1;
grav=9.83;
kon=0.4;%von karman constant
cpa=1004.67;%air specific heat
Le=2.5e6;

ddds='017';
way_raw_files=['E:\DATA\ATOMIC\P3\sonde\' ddds '\data\'];
sonde=load([way_raw_files 'ATOMIC_P3_sonde_' ddds '.mat']);

barpress=sonde.Phh;
t=sonde.tahh;
q=sonde.qahh*1e-3;%kg/kg
zhhG=sonde.zhh;
jdxhG=sonde.jdxh;
rhoa = barpress*100./(Rgas*(t+tdk).*(1+0.61*q));
figure;plot(rhoa,zhhG,'.');
Q=q.*rhoa*1e3;%g/m^3

ij=1;
zzx=1e-3*zhhG(:,ij);
z=zhhG(:,ij);tt=t(:,ij);qt=q(:,ij)*1e3;p=barpress(:,ij);
ii=find(jdxhG>16 & jdxhG<30);
rhoam=nanmeanx(rhoa(:,ii)');rhovm=nanmeanx(Q(:,ii)');ttm=nanmeanx(t(:,ii)');
qtm=nanmeanx(q(:,ii)');
ii=find(isnan(tt) & zzx<6);tt(ii)=ttm(ii);
ii=find(isnan(qt) & zzx<6);qt(ii)=qtm(ii);


[atteno,attenq,Iatten, rhoa] = attenCalc(z,tt,qt,p,95);
rhoaf=rhoa;rhovf=rhoa.*qt;
% figure;plot(rhoam,zzx,rhoaf,zzx);
% figure;plot(rhovm,zzx,rhovf,zzx);
[attenox,attenqx,Iattenx, rhoax] = attenCalc(z,ttm',qtm',p,95);
gammav=Iatten;
gammax=-0.215+1.3*rhovm/20+0.25./sqrt(1+0.5*rhovm);%water vapor absorption coeff
% figure;plot(gammav,zzx);

flt_alt=5;
h=flt_alt-zzx;
dz=-diff(h);
ik=find(h<0);nk=ik(1);
Gv=0;pwv=0;
for i=2:nk-1
    Gv(i)=Gv(i-1)+2*gammav(nk-i)*dz(nk-i);
    pwv(i)=pwv(i-1)+1E3*rhovf(nk-i)*dz(nk-i);
end;

% ht=2.9-zzx;
% dzt=-diff(h);nk=0;
%  ik=find(ht<0);nkt=ik(1);
% Gvt=0;pwvt=0;
% for i=2:nkt-1
%     Gvt(i)=Gvt(i-1)+2*gammav(nkt-i)*dz(nkt-i);
%     pwvt(i)=pwvt(i-1)+1e3*rhovm(nkt-i)*dz(nkt-i);
% end;

figure;plot(Gv,h(1:nk-1),pwv*1e-4,h(1:nk-1),'.');ylim([0,flt_alt]);%pwv in cm

%cx=zeros(5,10)*NaN;
iz=[1 102  151  197 286 387 464 545 698 848];
ij=find(iz<nk);
j=5;
cx(j,ij)=Gv(nk-iz(ij))';

save('GVz', 'iz','cx');

  