
licor_prefix='li0';
disp(['read_co2_7200: ', licor_prefix]);

Licrate=10;%data rate in Hz
Lenlic=3600*Licrate;
Nlenlic=floor(Lenlic/6);
ncol=11;

z=[];zz=[];temp=[];
% z=NaN+zeros(ncol,36000); % flicor=10Hz -->864000 samples
fclose('all');

for jam=0:23
    hr=sprintf('%02i',jam);
    jd=sprintf('%03i',ddd);

    dfx=[jd hr];

    dfl=[way_raw_data_flux, yyyy(3:4),jd,'\', 'li00' yyyy(3:4) jd hr '_raw.txt'];
  

    flist=fopen(dfl,'r');
     zz=[];temp=[];
    if flist>0
          disp(dfl);
% (Data (DiagVal 8191)(DiagVal2 0)(CO2Raw 0.130853)(H2ORaw 0.0912389)(CO2D 25.0466
% )(H2OD 693.592)(Temp 32.5851)(Pres 100.943)(APres 101.015)(DPres -0.0717261)(Coo
% ler 2.19796)(CO2MF 630.709)(CO2MFd 641.921)(H2OMF 17.4657)(H2OMFd 17.7762)(DewPt
%  15.468)(AvgTemp 32.6053)(TempIn 32.5984)(TempOut 32.607)(AvgSS 101.976)(CO2SS 1
% 01.861)(H2OSS 102.09)(DeltaSS -0.228477)(H2OAW 36302.5)(H2OAWO 39748.5)(CO2AW 30
% 851)(CO2AWO 32694))
       while feof(flist)==0,  
           try    %VOCALS 2008 is lic, and has 2 data formats!...      
                temp=textscan(flist,'%2f%2f%3f %f %*f %*f:%*f:%*f:%*f %*f %*f %*f %*f %*f %*f %*f %f %*f %f %f %*f %*f %f %*f %f %f %*f %*f %*f %*f %*f %*f %*f %*f %*[^\n]','delimiter', ', ', 'headerlines', 1, 'emptyvalue', NaN,'treatAsEmpty','Overflow');%          temp =textscan(flist,'%2f%2f%3f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
               temp{1,length(temp)+1}=jam*ones(length(temp{end}),1);%temp{1,length(temp)+1}=jam*ones(length(temp{1}),1);
%                zz=single(cell2mat(temp));  do not single time!
                zz=cell2mat(temp);  
               zy=[zz(:,1:4) single([zz(:,5:end-1)]) zz(:,end)]';
               z=[z zy];
           catch
                 for ll=1:ncol
                     [l8,c8]=size(temp{1,ncol});
                     if length(temp{1,ll})~=l8
                         temp{1,ll}(l8+1)=[];
                     end;
                 end;
                 if ~isempty(temp{1,1})
                     %z=[z cell2mat(temp)'];
                        % zz=single(cell2mat(temp));  do not single time!
                        zz=cell2mat(temp);  
                        zy=[zz(:,1:4) single([zz(:,5:end-1)]) zz(:,end)]';;%zy=[zz(:,1:4) single([zz(:,7:8) zz(:,10:12) zz(:,14:18) zz(:,20:21)]) zz(:,26)]';
                        z=[z zy];
                 end;
            end;     
   
       end;    

       fclose(flist);   
    end
%    z(:,any(isnan(z),1))=[];
end
% Data Outputs: Ndx | Time | Date | CO2Raw | H2ORaw | DiagVal | CO2D | H2OD | Temp |
% AvgTemp | TempIn | TempOut | Pres | APres | DPres | FlowPressure | MeasFlowRate |
% VolFlowRate | FlowPower | FlowDrive | Aux | Aux2 | Aux3 | Aux4 | Cooler | CO2MF |
% CO2MFd | H2OMF | H2OMFd | DewPt | H2OAW | H2OAWO | CO2AW | CO2AWO

%CO2Raw --> CO2 absorptance
%CO2D --> CO2 density (mmol m-3)
%CO2MF --> CO2 mol fraction (?mol/mol)
%CO2MFd --> CO2 mol fraction dry (?mol/mol)
%H2OD --> H2O density (mmol m-3)
%H2OMF --> H2O mol fraction (mmol/mol)
% Temp --> Block temperature, °C
% AvgTemp --> Weighted average temperature of tin and tout, °C
% TempIn --> Inlet temperature, °C
% TempOut --> Outlet temperature, °C
% Pres --> Total pressure, kPa
% Apres --> Absolute pressure (LI-7550),kPa
% Dpres --> Differential pressure (sensor head), kPa
% H2OAW --> H2O source
% H2OAWO --> H2O reference
% CO2AW --> CO2 source
% CO2AWO --> CO2 reference
% Cooler --> Detector cooler (Volts)

% 4614811 (Data (DiagVal 8183)(CO2Raw 0.087695)(H2ORaw 0.0429437)(CO2D 15.5907)(H2OD 285.381)(Temp 22.3643)(Pres 82.4158)(APres 82.5536)(DPres -0.137818)(Cooler 2.059)(CO2MF 464.7)(CO2MFd 468.687)(H2OMF 8.50612)(H2OMFd 8.5791)(DewPt 1.85432)(AvgTemp 22.3796)(TempIn 22.3715)(TempOut 22.3816)(H2OAW 42432.5)(H2OAWO 44162)(CO2AW 35737)(CO2AWO 36105))
 
chk=size(z);
   if chk(1)==ncol,
      t7200= z(1,:)*60+z(2,:)+z(3,:)/1000; %time in s
      jd7200=ddd + (z(end,:) + t7200/3600)/24;
      LI7200_agc=single(z(4,:));  %diagnostic value
%       LI7200_CO2raw=z(5,:); %CO2 absorptance
%       LI7200_H2Oraw=z(6,:); %H2O absorptance
%       LI7200_CO2d=single(z(5,:)); %CO2 density (mmol m-3)
%       LI7200_H2Od=single(z(6,:)); %H2O density (mmol m-3)
%      LI7200_Tblock=z(9,:); %Temperature at IRGA block, °C
%       LI7200_Press=single(z(7,:)); %Total pressure, kPa
%       LI7200_APress=single(z(8,:)); % Absolute pressure (LI-7550),kPa
      LI7200_Headpress=single(z(5,:)); % Differential pressure (sensor head), kPa
 %     LI7200_cooler=z(13,:); %Detector cooler (Volts)
      LI7200_CO2mf=single(z(6,:)); %CO2 mol fraction (umol/mol)
      LI7200_CO2mfd=single(z(7,:));  %CO2 mol fraction dry (umol/mol)
%       LI7200_H2Omf=single(z(12,:));  %H2O mol fraction (mmol/mol)
%       LI7200_H2Omfd=single(z(13,:)); %H2O mol fraction dry (mmol/mol)?
      LI7200_Tdew=single(z(8,:)); %Dewpoint temperature (degC)
%      LI7200_Tmean=z(19,:); %Weighted average temperature of tin (20%) and tout(80%), °C
      LI7200_Tin=single(z(9,:)); %Inlet temperature, °C
      LI7200_Tout=single(z(10,:)); %Outlet temperature, °C
%       LI7200_H2OAW=z(22,:); %H2O source
%       LI7200_H2OAWO=z(23,:); % H2O reference
%       LI7200_CO2AW=z(24,:); % CO2 source
%       LI7200_CO2AWO=z(25,:); % CO2 reference


% AGC= LI7200agc(LI7200_agc,'AGConly');
% AGC=(LI7200_agc-(bin2dec('1111111110000')))*6.25+6.25; %Assumes all other flags are OK
AGC=dec2bin(LI7200_agc,16); %
AGC=bin2dec(AGC(:,end-3:end))*6.67;
% 100 is clean, 80 is
% relatively dirty, 50 is very dirty (the equivalent of an AGC value of 100 in
% earlier versions of the LI-7200 software), and 0 would mean no signal at all.
clear z zz zy temp
   end;

%reads in the Mass Flow meters
% read_MFM_day_DYNAMO_2011;
read_MFS_day_HiWinGS_2013;

skpfg=1;
if  exist('LI7200_CO2mf','var') && ~isempty(LI7200_CO2mf)
if skpfg
    ij=1:20:length(jd7200);
    ik=1:20:length(jdMFS);
    figure;
    annotation(gcf,'textbox',[0.072 0.85 0.8498 0.12462],'String',sprintf('%s (%04i-%02i-%02i, DOY%03i).  LI7200 parameters ',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontSize',12,'FitBoxToText','off','LineStyle','none','FontWeight','Bold','Interpreter','none'); 

    subplot(2,2,1)
    plot(jd7200(ij),LI7200_Tin(ij),'b',jd7200(ij),LI7200_Tout(ij),'r',jd7200(ij),-LI7200_Tdew(ij),'g')
    legend('In', 'Out','-Tdew','Location','South');
    xlabel('Hour (UTC)');ylabel('Temperature (degC)');
%     title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  LI7200 control parameters ',cruise,Vdate(1),Vdate(2),Vdate(3),ddd),'FontWeight','Bold','Interpreter','none')
%     axis([ddd ddd+1 -45 30]); 
    axis([ddd ddd+1 0 50]); 
    set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
    xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))
    grid
    
    subplot(2,2,2)
    plot(jd7200(ij),LI7200_Headpress(ij),'b',[jd7200(1) jd7200(end)],[-10 -10],'r--','Linewidth',2)
    xlabel('Hour (UTC)');ylabel('Diff. Pressure (kPa)'); axis([ddd ddd+1 -11 -5]); 
    set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
    xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
    grid
    
%     subplot(2,2,3)
%     plot(jdMFM,MFMmassflow,'b',jdMFM,MFMmassflow_setpt,'r--','Linewidth',2)
%     xlabel('Hour (UTC)');ylabel('Main Mass flow (slpm)'); axis([ddd ddd+1 25 35]); 
%     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
%     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
%     grid
%     
%     subplot(2,2,4)
%     plot(jdMFS,MFSmassflow,'b',jdMFS,MFSmassflow_setpt,'r--','Linewidth',2)
%     xlabel('Hour (UTC)');ylabel('Subline Mass flow (slpm)'); axis([ddd ddd+1 3 5]); 
%     set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
%     xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
%     grid
    subplot(2,2,3)
    plot(jdMFS(ik),MFSmassflow(ik),'r','Linewidth',2)
%     legend('Main', 'Sub','Location','best');
    xlabel('Hour (UTC)');ylabel('Mass flow (slpm)'); axis([ddd ddd+1 0 10]); 
    set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
    xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
    grid
    
    subplot(2,2,4)
    plot(jd7200(ij),AGC(ij),'b',[ddd ddd+1],[50 50],'r',[ddd ddd+1],[80 80],'r--','Linewidth',2)
    xlabel('Hour (UTC)');ylabel('AGC (percent)'); axis([ddd ddd+1 0 105]); 
    set(gca(gcf),'XTick',ddd:2/24:ddd+1);datetick('x','HH:MM','keepticks');
    xax=get(gca(gcf),'XTickLabel');xax(end,:)='24:00';set(gca(gcf),'XTickLabel',xax(:,1:2))    
    grid
    annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none'); 
    if prtit; print('-djpeg90 ',[way_images_flux cruise '_LI7200_control_parameters_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); end
end;
    figure;plot(jd7200(ij),LI7200_CO2mf(ij),'g',jd7200(ij),LI7200_CO2mfd(ij),'k');
    legend('measured', 'corrected to dry','Location','South');
    title(sprintf('%s (%04i-%02i-%02i, DOY%03i).  Licor CO2 (unit:%s)',cruise,Vdate(1),Vdate(2),Vdate(3),ddd,licor_prefix),'FontWeight','Bold','Interpreter','none')
    xlabel('Hour (UTC)');ylabel('CO2 (\mumol/mol)');
    axis([ddd ddd+1 median(LI7200_CO2mf(~isnan(LI7200_CO2mf)))-20 median(LI7200_CO2mf(~isnan(LI7200_CO2mf)))+20])
%     xlim([jdMFM(1) jdMFM(end)]);
    datetick('x','HH:MM','keepticks');
    grid
    annotation(gcf,'textbox',[0.007154 0.01077 0.4498 0.02462],'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},'FontSize',6,'FitBoxToText','off','LineStyle','none');
    if prtit; 
        print(graphdevice,[way_images_flux, cruise '_PSD_LI7200_CO2_' sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ddd) graphformat]); 
    end

    %close('all'); 
    jd_ref=ddd+(0:1439)/60/24;
    LI7200_CO2mfi=intavg(jd7200,LI7200_CO2mf,jd_ref); %g/kg   
end;

% clear LI7200_agc LI7200_CO2d LI7200_H2Od LI7200_Press LI7200_APress LI7200_Tin LI7200_Tout LI7200_H2Omf LI7200_H2Omfd
% clear  t7200 tMFS  tPSD  tPSDGPS jdMFS  jd7200 hh 

