function [agc_0,Licor_CO2,Licor_H2O,Licor_Temp,Licor_Press] = read_7500co2_HiWinGs_2013(dfl)


%outputs
%       Licor_CO2  ;%mmol/m^3
%       Licor_H2O ;%mmol/m^3
%       Licor_Temp ;%C
%       Licor_Press ;%kPa


disp('read_co2_HiWinGs_2013.m')
% Opens Channel #1 , which is the SDM protocol from the Licor 7500
% Inputs data from ASCII data files
licor_prefix='lic';
Licrate=10;%data rate in Hz
Lenlic=3600*Licrate;
Nlenlic=floor(Lenlic/6);


ppp=0;
% z=zeros(9,36000);
z=NaN+zeros(9,36000); % flicor=10Hz -->864000 samples
fclose('all');

% hr=sprintf('%02i',hhh);
% jd=sprintf('%03i',ddd);

% dfx=[jd hr];

% dfl=[way_raw_data_flux, year(3:4),jd,'\' licor_prefix '0', year(3:4) jd hr '_raw.txt'];
disp(dfl);

flist=fopen(dfl,'r');

Licor_CO2=[];
Licor_H2O=[];
Licor_Press=[];
Licor_Temp=[];
temp=[];z=[];
Licor_diag_0 =[];

if flist>0
    %how long is the data message?
    % if ncol==6 then message is:
    % mmsslll status CO2[mmol/m3] H2O[mmol/m3] Temperature[C] pressure[kPa]
    % if ncol==8:
    % mmssll status CO2[umol/mol] CO2[mmol/m3] H2O[mmol/mol] H2O[mmol/m3] Temperature[C] pressure[kPa]
%         fgetl(flist);
%         ncol=length(sscanf(fgetl(flist),'%f'));
%         frewind(flist);
ncol =6;
   while feof(flist)==0,  
       try    %VOCALS 2008 is lic, and has 2 data formats!...      
            if ncol==6
                temp=textscan(flist,'%2f%2f%3f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1, 'emptyvalue', NaN,'treatAsEmpty','Overflow');
            elseif ncol==8 % ignore data positions 5 and 7 
                temp=textscan(flist,'%2f%2f%3f %f %*f %f %*f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1, 'emptyvalue', NaN,'treatAsEmpty','Overflow');
            end                   
%          temp =textscan(flist,'%2f%2f%3f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN);
           z=[z cell2mat(temp)'];
       catch
             for ll=1:8
                 [l8,c8]=size(temp{1,8});
                 if length(temp{1,ll})~=l8
                     temp{1,ll}(l8+1)=[];
                 end;
             end;
             if ~isempty(temp{1,1})
                 z=[z cell2mat(temp)'];
             end;
        end;     
   end;    
   
   z(:,any(isnan(z),1))=[];
   
   chk=size(z);
   if chk(1)==8,
      Licor_diag_0 =[Licor_diag_0   z(4,:)];%
      Licor_CO2  =[Licor_CO2   z(5,:)];%mmol/m^3
      Licor_H2O  =[Licor_H2O   z(6,:)];%mmol/m^3
      Licor_Temp =[Licor_Temp  z(7,:)];%C
      Licor_Press=[Licor_Press z(8,:)];%kPa
   end;
else
     z=ones(8,36000).*NaN;  
     Licor_diag_0 =z(4,:);
     Licor_CO2  =z(5,:);%mmol/m^3
     Licor_H2O  =z(6,:);%mmol/m^3
     Licor_Temp =z(7,:);%C
     Licor_Press=z(8,:);%kPa
end;

if length(Licor_CO2)<Lenlic,
   Licor_diag_0=  [Licor_diag_0   (1:Lenlic-length(Licor_diag_0)).*0+Licor_diag_0(length(Licor_diag_0))      ]; 
   Licor_CO2=  [Licor_CO2   (1:Lenlic-length(Licor_CO2)).*0+Licor_CO2(length(Licor_CO2))      ];
   Licor_H2O=  [Licor_H2O   (1:Lenlic-length(Licor_H2O)).*0+Licor_H2O(length(Licor_H2O))      ];
   Licor_Temp= [Licor_Temp  (1:Lenlic-length(Licor_Temp)).*0+Licor_Temp(length(Licor_Temp))   ];
   Licor_Press=[Licor_Press (1:Lenlic-length(Licor_Press)).*0+Licor_Press(length(Licor_Press))];
end;

if length(Licor_CO2)>Lenlic,
   Licor_diag_0=Licor_diag_0(1:Lenlic);
   Licor_CO2=Licor_CO2(1:Lenlic);
   Licor_H2O=Licor_H2O(1:Lenlic);
   Licor_Temp=Licor_Temp(1:Lenlic);
   Licor_Press=Licor_Press(1:Lenlic);
end;


% Press_1=mean(Licor_Press(1:1*Nlenlic));
% Press_2=mean(Licor_Press(1*Nlenlic+1:2*Nlenlic));
% Press_3=mean(Licor_Press(2*Nlenlic+1:3*Nlenlic));
% Press_4=mean(Licor_Press(3*Nlenlic+1:4*Nlenlic));
% Press_5=mean(Licor_Press(4*Nlenlic+1:5*Nlenlic));
% Press_6=mean(Licor_Press(5*Nlenlic+1:6*Nlenlic));
% 
% std_Press_1=std(Licor_Press(1:1*Nlenlic));
% std_Press_2=std(Licor_Press(1*Nlenlic+1:2*Nlenlic));
% std_Press_3=std(Licor_Press(2*Nlenlic+1:3*Nlenlic));
% std_Press_4=std(Licor_Press(3*Nlenlic+1:4*Nlenlic));
% std_Press_5=std(Licor_Press(4*Nlenlic+1:5*Nlenlic));
% std_Press_6=std(Licor_Press(5*Nlenlic+1:6*Nlenlic));

%disp('pause');pause

% % convert units from mol/m3 to mol/mol
% Rgas=287.1;
% Rgas_universal=8.314472; % Pa m3 /K /mol
% Licor_CO2_a=(Licor_CO2*1e-3).*Rgas_universal.*(Licor_Temp+273.15)./(Licor_Press*1e3); %mmol/m3 to mol/mol

% interpret status diagnostic
xx=Licor_diag_0;
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_0=(xx-y)*6.25;
