disp(['read_co2_', cruise, '_', year]);

% Opens Channel of the Licor 7500
% Inputs data from ASCII data files

z=[];
fclose('all');

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

jd_lic=[];
jax1=1;lic1=[];

for jam=0:23,					    %cycle thru 24 hourly stats files
    if jam<10,
        hr=['0' num2str(jam)];
    else
        hr=num2str(jam);
    end;                            %end if jam
   
    dfl=[way_raw_data_flux, 'day',jd,'\lic0', year(3:4) jd hr '_raw.txt'];
    disp(['Reading Licor file  ',dfl])
    flist=fopen(dfl,'r');
   
    if flist>0,					    %if the file exists, 
            clear temp;
%             temp =textscan(flist,'%f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 2,  'emptyvalue', NaN);
            temp =textscan(flist,'%2f%2f%3f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 2,  'emptyvalue', NaN);
            for ii=1:4
                if length(temp{1,ii})~=3600*flicor
                    temp{1,ii}=[temp{1,ii};ones(3600*flicor-length(temp{1,ii}),1)*NaN];
                end;
            end;  
            for ii=5:8
                if length(temp{1,ii})~=3600*flicor
                    temp{1,ii}=[temp{1,ii};ones(3600*flicor-length(temp{1,ii}),1)*0];
                end;
            end;    
                           
            z=cell2mat(temp)';
%             z(5:9,any(isnan(z),1))=NaN;      %put NaN into any rows containing NaNs (to remove bad lines, gap...)
           
            [nr1,nl1]=size(z);
            lic1(:,jax1:jax1+nl1-1)=z;
            spechum_lic=z(6,:)*18/1000; %g/m3
            jd1=ddd + (jam+((lic1(1,jax1:jax1+nl1-1)+((lic1(2,jax1:jax1+nl1-1)+lic1(3,jax1:jax1+nl1-1)/1000)/60))/60))/24;
            jd_lic = [jd_lic jd1];
            jax1=jax1+nl1;     
        
        if jam==0 | jam==12 | jam==18
            disp(['Calculating licor humidity spectra for ',hr])
            [S_Lic,F]=psd2(detrend(spechum_lic),length(spechum_lic),flicor);
            [S_Lics,Fsl]=specsmoo(S_Lic,flicor);
            
            figure;loglog(Fsl,S_Lics.*Fsl,Fsl(length(Fsl)-31:length(Fsl)),((Fsl(length(Fsl)-31:length(Fsl))).^(-2/3))./(Fsl(length(Fsl)-18).^(-2/3)).*S_Lics(length(Fsl)-18).*Fsl(length(Fsl)-18));
            title(['Licor Hygrometer Spectrum:   Day ',jd,'   Hour ',hr]);
            xlabel('Frequency in Hz');
            ylabel('Spectrum * Frequency');
            print('-djpeg90 ',[way_images_flux 'LicH2O_spectrum' jd hr '.jpg']);
            
            if jam==12,
               specfile=[way_spec_data_flux 'q_sp',jd,hr,'.txt'];
               fspec=fopen(specfile,'w');
               specs=[Fsl;S_Lics];
               fprintf(fspec,'%11.5f %11.5f \r\n',specs);
               fclose(fspec);
               clear fspec specs specfile
            end;
            clear S_Lic F S_Lics Fsl spechum_lic

        end;
    end;                            %end if flist
end;

% % Time in flux data files is the number of 100-nano second intervals since
% % Jan 1, 1601 divided by 10000. It was chosen to maintain compatibility
% % with an HP-UNIX version of the code form long ago
% %  0             'dd-mmm-yyyy HH:MM:SS'  
% %01-Mar-2000 15:45:17
% dates = datestr(datenum(1601,1,1) + datenum(lic1(1,:)*10000*100e-9/86400));
% hh = str2num(dates(:,13:14));
% mm = str2num(dates(:,16:17));
% ss = str2num(dates(:,19:20));
% jd_lic= ddd + (hh+((mm+(ss/60))/60))/24;
% 
% % test to see if first point is from the previous day
% if str2num(dates(1,1:2)) ~= str2num(dates(2,1:2))
%     jd_lic(1) = jd_lic(1)-1;
% end   

Licor_diag =lic1(4,:);
Licor_CO2  =lic1(5,:);%mmol/m^3
Licor_H2O  =lic1(6,:);%mmol/m^3
Licor_Temp =lic1(7,:);%C
Licor_Press=lic1(8,:);%kPa
            
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); %mmol/m3 to umol/mol
Licor_H2O_a=Licor_H2O*18/1000;%mmol/m3 to 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
%1250 seems to be an approximation of the dry air density.
ii=find(~isnan(Licor_H2O_a));

rhoa_lic=(Licor_Press*1000./(Rgas*(Licor_Temp+273.16)))-(0.61*Licor_H2O_a/1000);%air density in licor
Licor_H2O_a=Licor_H2O_a./rhoa_lic;%g/m^3 to g/kg

if plotit
    figure;plot(jd_lic,Licor_H2O_a);title(['Licor specific humidity. ', cruise, year]);xlabel(['JD', year]);ylabel('q (g/kg)');axis([ddd ddd+1 -1 30]);
        print('-djpeg90 ',[way_images_flux '\Licor_spechum_' num2str(ddd) '.jpg']);
    figure;plot(jd_lic,Licor_CO2_a);title(['Licor CO2. ', cruise, year]);xlabel(['JD', year]);ylabel('CO2 (umol/mol)');axis([ddd ddd+1 0 800])
        print('-djpeg90 ',[way_images_flux '\Licor_CO2_' num2str(ddd) '.jpg']);
    figure;plot(jd_lic,Licor_Temp);title(['Licor Temperature [degC]. ', cruise, year]);xlabel(['JD', year]);xlim([ddd ddd+1])
        print('-djpeg90 ',[way_images_flux '\Licor_temp_' num2str(ddd) '.jpg']);    
    figure;plot(jd_lic,Licor_Press);title(['Licor Pressure [kPa]. ', cruise, year]);xlabel(['JD', year]);xlim([ddd ddd+1])
        print('-djpeg90 ',[way_images_flux '\Licor_press_' num2str(ddd) '.jpg']);    
%     figure;plot(jd_lic,Licor_CO2_a);title('Licor CO2 [umol/mol]');
    figure;plot(jd_lic,Licor_H2O_a,jd_pc,qvais,'o');title(['Licor (blue) Vaisala (circles). ', cruise, year]);xlabel(['JD', year]);ylabel('q (g/kg)');axis([ddd ddd+1 0 50])
        print('-djpeg90 ',[way_images_flux '\Licor_spechum_comp_' num2str(ddd) '.jpg']);   
    figure;plot(jd_lic,agc,[ddd ddd+1],[55 55],'r',[ddd ddd+1],[65 65],'r'); title('AGC value. Typical "clean window" values are 55 to 65%');axis([ddd ddd+1 50 100]);
        print('-djpeg90 ',[way_images_flux '\Licor_AGC_' num2str(ddd) '.jpg']);   
end;
