disp('read_co2_rhb_07.m')
% Opens Channel #1 , which is the SDM protocol from the Licor 7500
% Inputs data from ASCII data files

%ppp=0;
z=zeros(6,36005);  %Licor running at 10Hz for AMMA cruise 2006
fclose('all');
hhh = 10;
if hhh<10,
   dfx2=['0',num2str(hhh)];
else,
   dfx2=num2str(hhh);
end;

if ddd<10,
   dfx1=['00',num2str(ddd)];
elseif ddd<100,
   dfx1=['0',num2str(ddd)];
else,
   dfx1=num2str(ddd);
end;

dfx=[dfx1 dfx2];

%   Open LICOR file
dfl=['f:\AMMA_2007\RHB\flux\raw\day',dfx1,'\P1_',dfx];       
%  dfl=['d:\data\day',dfx1,'\P1_',dfx];       
disp(dfl);

flist=fopen(dfl,'r');
dmp1=fgetl(flist);
dmp2=fgetl(flist);

t1=[];
t2=[];
Licor_CO2=[];
Licor_H2O=[];
Licor_Press=[];
Licor_Temp=[];
Licor_diag=[];

while ~feof(flist)
    z=fscanf(flist,'%g,%g%g%g%g%g',[6,inf]);
    if isempty(z);
        fgetl(flist);
    end;
    [chk nochk]=size(z);
    if chk==6,
        z=z(:,1:nochk-1);
        t1=[t1 z(1,:)];
        Licor_diag=[Licor_diag z(2,:)];
        Licor_CO2  =[Licor_CO2 z(3,:)];%mmol/m^3
        Licor_H2O  =[Licor_H2O z(4,:)];%mmol/m^3
        Licor_Press=[Licor_Press z(6,:)];%pascal
        Licor_Temp =[Licor_Temp z(5,:)];%C
    end;
end;

t2a=(t1(1,:)-t1(1,1))/1000;
tlicor=t2a;
xx=Licor_diag;
b7=floor(xx/128);
y=b7*128;
b6=floor((xx-y)/64);
y=y+b6*64;
b5=floor((xx-y)/32);
y=y+b5*32;
b4=floor((xx-y)/16);
y=y+b4*16;
agc=(xx-y)*6.25;

ii=find(Licor_Press==0 | Licor_Press>1000);jj=find(Licor_Press>0 & Licor_Press<1000);
Licor_Press(ii)=median(Licor_Press(jj));
ii=find(Licor_Temp==0 | Licor_Temp>1000);jj=find(Licor_Temp>0 & Licor_Temp<1000);
Licor_Temp(ii)=median(Licor_Temp(jj));
ii=find(Licor_CO2==0 | Licor_CO2>1000);jj=find(Licor_CO2>0 & Licor_CO2<1000);
Licor_CO2(ii)=median(Licor_CO2(jj));
ii=find(Licor_H2O==0 | Licor_H2O>2000);jj=find(Licor_H2O>0 & Licor_H2O<2000);
Licor_H2O(ii)=median(Licor_H2O(jj));
      
      
Licor_CO2_a=Licor_CO2*8.314*(mean(Licor_Temp)+273.15)/mean(Licor_Press);
Licor_H2O_a=Licor_H2O*18/1000;%g/m^3
Rgas=287.1;
rhoa_lic=mean(Licor_Press)*1000/(Rgas*(mean(Licor_Temp)+273.16)*(1+0.61*mean(Licor_H2O_a)/1250));%air density in licor

fudg_lic=1.00;
if ddd+hhh/24<257+18.5/24;fudg_lic=0.90;end;
Licor_H2O_a=Licor_H2O_a/rhoa_lic*fudg_lic;%g/kg

ttvs=(0:length(qvais)-1)*60+30;
%qvais=15;
plot_f=1;
if plot_f
%    figure;plot(Licor_Temp);title('Licor Temperature [degC]');
%    figure;plot(Licor_Press);title('Licor Pressure [kPa]');
    figure;plot(Licor_CO2_a);title('Licor CO2 [umol/mol]');
%    figure;plot(tlicor,Licor_H2O_a,tophir,spechum,ttvs,qvais,'o');title(['Licor (blue)  ophir (grn) Vaisala (circles); day  ',num2str(ddd),'   Hour ',num2str(hhh)]);xlabel('Time (s)');ylabel('q (g/kg)');%axis([0 3600 5 25]);
    figure;plot(tlicor,Licor_H2O_a,ttvs,qvais,'o');title(['Licor (blue) Vaisala (circles); day  ',num2str(ddd),'   Hour ',num2str(hhh)]);xlabel('Time (s)');ylabel('q (g/kg)');%axis([0 3600 5 25]);

%[St,Ft]=psd2(detrend(Licor_Temp),length(Licor_Temp),20);
%[Sp,Fp]=psd2(detrend(Licor_Press),length(Licor_Press),20);
%[Sc,Fc]=psd2(detrend(Licor_CO2),length(Licor_CO2),20);
%[Sh,Fh]=psd2(detrend(Licor_H2O),length(Licor_H2O),20);
%[Sts,Fts]=specsmoo(St,20);
%[Sps,Fps]=specsmoo(Sp,20);
%[Scs,Fcs]=specsmoo(Sc,20);
%[Shs,Fhs]=specsmoo(Sh,20);
%figure;loglog(Fts,Fts.*Sts,Fts(15:39),(Fts(15:39).^(-2/3))./(Fts(20).^(-2/3)).*Fts(20).*Sts(20));title('temperature')
%figure;loglog(Fps,Fps.*Sps,Fps(15:39),(Fps(15:39).^(-2/3))./(Fps(20).^(-2/3)).*Fps(20).*Sps(20));title('pressure')
%figure;loglog(Fhs,Fhs.*Shs,Fhs(15:39),(Fhs(15:39).^(-2/3))./(Fhs(20).^(-2/3)).*Fhs(20).*Shs(20));title('H2O')
%figure;loglog(Fcs,Fcs.*Scs,Fcs(15:39),(Fcs(15:39).^(-2/3))./(Fcs(20).^(-2/3)).*Fcs(20).*Scs(20));title('CO2')
end;
