function [agc_0,Licor_CO2,Licor_H2O,Licor_Temp,Licor_Press] = read_7500co2_HiWinGs_2013(dfl)
% reads variables from raw LI7500 PSD data files for HiWinGS
% file path string 'dfl' must be defined prior to call

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

%% initialize
disp('read_co2_HiWinGs_2013.m')
Licrate = 10;               % data rate in Hz
Lenlic = 3600*Licrate;      % data points in 1 hr
Nlenlic = floor(Lenlic/6);  % data points in 10 min segment
fclose('all');
disp(dfl);
flist=fopen(dfl,'r');

z = NaN+zeros(9,36000);     % flicor = 10Hz --> 864000 samples
Licor_CO2=[];
Licor_H2O=[];
Licor_Press=[];
Licor_Temp=[];
temp=[];
z=[];
Licor_diag_0 =[];

%% read data file
if flist>0
   ncol =6;
   while feof(flist)==0,  
       try  
            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                   
           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;
   fclose(flist);
else    % return NaN arrays if no file found
     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;

% 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;

clear temp z y b4 b5 b6 b7


