function [Licor_H2O_a,Licor_CO2_a,Licor_Temp,Licor_Press,agc,jd_lic,Fcs,Scs,Fhs,Shs]=CO2_GASEXIII_read(varargin)
    
% Opens Channel of the Licor 7500
% Inputs data from ASCII data files

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

fname=varargin{1};
ddd=varargin{3};
way_raw_data_flux=varargin{4};
year=varargin{5};
gpname=varargin{2};
hhh=varargin{6};

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

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

for jam=hhh(1):hhh(length(hhh)),					    %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,'\' fname '0', year(3:4) jd hr '_raw.txt']; %read licor by itself from Uconn (Jim)
    disp(['Reading Licor by file  ',dfl])
    flist=fopen(dfl,'r');
    z=[];temp=[];   
    if flist>0,					    %if the file exists, 
        
            clear temp;
            if strcmp(fname,'lis')==1
                temp =textscan(flist,'%2f%2f%3f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN,'treatAsEmpty','Overflow');
            elseif strcmp(fname,'lin')==1
                temp =textscan(flist,'%2f%2f%2f%3f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN,'treatAsEmpty','Overflow');
            elseif strcmp(gpname,'Uconn')==1
                temp =textscan(flist,'%2f%2f%2f%3f %f %f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN,'treatAsEmpty','Overflow');
            end;
         
            z=cell2mat(temp)';
            
            if strcmp(fname,'lis')==1
%                 z(4:8,any(isnan(z),1))=NaN;      %put NaN into any rows containing NaNs (to remove bad lines, gap...)
                ii=find(z(8,:)<50); z(7:8,ii)=z(7:8,ii-1);
                
                [nr1,nl1]=size(z);
                if nl1<36000
                    z=[z ones(8,36000-nl1)*NaN];
                    nl1=36000;
                end;
            elseif strcmp(fname,'lin')==1
%                 z(5:9,any(isnan(z),1))=NaN;      %put NaN into any rows containing NaNs (to remove bad lines, gap...)
                ii=find(z(9,:)<50); z(8:9,ii)=z(8:9,ii-1);
                
                [nr1,nl1]=size(z);
                if nl1<36000
                    z=[z ones(9,36000-nl1)*NaN];
                    nl1=36000;
                end;
            elseif strcmp(gpname,'Uconn')==1
%                 z(5:11,any(isnan(z),1))=NaN;      %put NaN into any rows containing NaNs (to remove bad lines, gap...)
                ii=find(z(11,:)<50); z(10:11,ii)=z(10:11,ii-1);
                
                [nr1,nl1]=size(z);
                if nl1<36000
                    z=[z ones(11,36000-nl1)*NaN];
                    nl1=36000;
                end;
                
            end;          
            
             lic1(:,jax1:jax1+nl1-1)=z;
%             lic1=z;
            
            if strcmp(fname,'lis')==1
                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;
            elseif strcmp(fname,'lin')==1
                jd1=ddd + (lic1(1,jax1:jax1+nl1-1)+((lic1(2,jax1:jax1+nl1-1)+((lic1(3,jax1:jax1+nl1-1)+lic1(4,jax1:jax1+nl1-1)/1000)/60))/60))/24;
            elseif strcmp(gpname,'Uconn')==1
                jd1=ddd + (lic1(1,jax1:jax1+nl1-1)+((lic1(2,jax1:jax1+nl1-1)+((lic1(3,jax1:jax1+nl1-1)+lic1(4,jax1:jax1+nl1-1)/1000)/60))/60))/24;
            end;
            
            jd_lic = [jd_lic jd1];
            jax1=jax1+nl1;     
        
    end;                            %end if flist
end;

if strcmp(fname,'lis')==1
    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
elseif strcmp(fname,'lin')==1
    Licor_diag =lic1(5,:);
    Licor_CO2  =lic1(6,:);%mmol/m^3
    Licor_H2O  =lic1(7,:);%mmol/m^3
    Licor_Temp =lic1(8,:);%C
    Licor_Press=lic1(9,:);%kPa
elseif strcmp(gpname,'Uconn')==1
    Licor_diag =lic1(5,:);
    Licor_CO2  =lic1(7,:);%mmol/m^3
    Licor_H2O  =lic1(9,:);%mmol/m^3
    Licor_Temp =lic1(10,:);%C
    Licor_Press=lic1(11,:);%kPa
end;
            
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>200);jj=find(Licor_Press>0 & Licor_Press<200);
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_CO2>100);Licor_CO2(ii)=NaN;ii=find(Licor_CO2<0);Licor_CO2(ii)=NaN;

% ii=find(Licor_H2O==0 | Licor_H2O>2000);jj=find(Licor_H2O>0 & Licor_H2O<2000);
% Licor_H2O(ii)=median(Licor_H2O(jj));
% ii=find(Licor_H2O>500);Licor_H2O(ii)=NaN;ii=find(Licor_H2O<0);Licor_H2O(ii)=NaN;

      
      
Licor_CO2_a=Licor_CO2*8.314*(nanmean(Licor_Temp)+273.15)/nanmean(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=nanmean(Licor_Press)*1000/(Rgas*(nanmean(Licor_Temp)+273.16)*(1+0.61*nanmean(Licor_H2O_a)/1250));%air density in licor
%1250 seems to be an approximation of the dry air density.
rhoa_lic=(nanmean(Licor_Press)*1000/(Rgas*(nanmean(Licor_Temp)+273.16)))-(0.61*nanmean(Licor_H2O_a)/1000);%air density in licor


Licor_H2O_a=Licor_H2O_a/rhoa_lic;%g/m^3 to g/kg

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ii=find(~isnan(Licor_CO2_a));
specsize=length(Licor_CO2_a(ii));
nover=36000-specsize;
[Sc,Fc]=psd2(detrend(Licor_CO2_a(ii)),specsize,10,hamming(specsize));
[Sh,Fh]=psd2(detrend(Licor_H2O_a(ii)),specsize,10,hamming(specsize));
[Scs,Fcs]=specsmoo(Sc,10);
[Shs,Fhs]=specsmoo(Sh,10);
