disp('eval_licor_rhb_07')
% Inputs data from ASCII data files
%  Note:  hhh=hour

z=zeros(6,flicor.*3600); 

fclose('all'); 
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];

Tmean=avgTvais;
qmean=avgQvais;
tref=Tmean; %15;
Rgas=287.1;
rhoa=1010*100/(Rgas*(Tmean+273.16)*(1+0.61*qmean/1000));%air density

%###########    Open LICOR file    ##################################
dfl=[way_raw_data, 'day',dfx1,'\P1_',dfx];       
disp(dfl);

flist=fopen(dfl,'r');

if flist >0  %File exists  DEW 5/7/2007
    
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;

%###########    Licor diagnostic decoding ##################################
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
rhoa_lic=mean(Licor_Press)*1000/(Rgas*(mean(Licor_Temp)+273.16)*(1+0.61*mean(Licor_H2O_a)/1250));%air density in licor

ttvs=(0:length(qvais)-1)*60+30;
%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]'); ylim([0 max(Licor_CO2_a)+100]);
        print('-djpeg90 ',[way_images '\CO2_' num2str(ddd) '_' num2str(hhh) '.jpg']);
    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]);
        print('-djpeg90 ',[way_images '\SpecificHumidity_' num2str(ddd) '_' num2str(hhh) '.jpg']);
    % LiCor pressure isn't very exciting to plot DEW
        %figure;plot(tlicor,Licor_Press*10);title('Licor Pressure');xlabel('Time (s)');ylabel('P (mb)');axis([0 3600 1000 1020]);
        %print('-djpeg90 ',[way_images '\LicorP_' num2str(ddd) '_' num2str(hhh) '.jpg']);

         
    figure;plot(tlicor,agc,[0 max(tlicor)],[55 55],'r',[0 max(tlicor)],[65 65],'r');
    title('AGC value. Typical "clean window" values are 55 to 65%');axis([0 max(tlicor) 50 100]);
end;

spechum_l=Licor_H2O_a;
[Lic,F]=psd2(detrend(spechum_l),length(spechum_l),flicor);
[Lics,Fsl]=specsmoo(Lic,flicor);%2006 AMMA cruise, licor set to 10 hz


if hhh==12,
   specfile=[way_spec_data 'q_sp',num2str(ddd),num2str(hhh),'.txt'];
   fspec=fopen(specfile,'w');
   specs=[Fsl Lics];
   fprintf(fspec,'%11.5f %11.5f \r\n',specs);
   fclose(fspec);
   clear fspec specs specfile
end;

if plot_f
    figure;loglog(Fsl,Lics.*Fsl,Fsl(length(Fsl)-31:length(Fsl)),((Fsl(length(Fsl)-31:length(Fsl))).^(-2/3))./(Fsl(length(Fsl)-18).^(-2/3)).*Lics(length(Fsl)-18).*Fsl(length(Fsl)-18));
    title(['Licor Hygrometer Spectrum:   Day ',num2str(ddd),'   Hour ',num2str(hhh)]);
    xlabel('Frequency in Hz');
    ylabel('Spectrum * Frequency (g/kg)^2)');
end;

end %File exists DEW 5/7/2007
