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

Licrate=10;%data rate in Hz
Lenlic=3600*Licrate;
Nlenlic=floor(Lenlic/6);
fname='lin';

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

if hhh<10,
   hr=['0',num2str(hhh)];
else
   hr=num2str(hhh);
end;

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

Licor_CO2=[];
Licor_H2O=[];
Licor_Press=[];
Licor_Temp=[];

dfl=['C:\Data\AMMA\day',jd,'\' fname '008', jd hr '_raw.txt']; 
disp(['Reading Licor by file  ',dfl])
flist=fopen(dfl,'r');
z=[];temp=[]; time_licor=[];  
if flist>0,					    %if the file exists, 
   while feof(flist)==0,
        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(fname,'li1')==1 | strcmp(fname,'li2')==1 | strcmp(fname,'li3')==1
            temp =textscan(flist,'%2f%2f%2f%3f %f %f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1,  'emptyvalue', NaN,'treatAsEmpty','Overflow');
        end;

        try
            z=[z cell2mat(temp)'];
        catch
            [nr1,nl1]=size(temp);
            for ll=1:nl1
                 [l10,c10]=size(temp{1,nl1});
                 if length(temp{1,ll})~=l10
                     temp{1,ll}(l10+1)=[];
                 end;
             end;
         if isempty(temp{1,1});
             temp=[];
         end;
            z=[z cell2mat(temp)'];
        end;  %end of catch/try
   end; %end of while feof

        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(fname,'li1')==1 | strcmp(fname,'li2')==1 | strcmp(fname,'li3')==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;
         
        if strcmp(fname,'lis')==1
            time_licor=lic1(1,:)*60+lic1(2,:)+lic1(3,:)/1000; %real raw time in s
        elseif strcmp(fname,'lin')==1
            time_licor=lic1(2,:)*60+lic1(3,:)+lic1(4,:)/1000; %real raw time in s
        elseif strcmp(fname,'li1')==1 | strcmp(fname,'li2')==1 | strcmp(fname,'li3')==1
            time_licor=lic1(2,:)*60+lic1(3,:)+lic1(4,:)/1000; %real raw time in s
        end;
%    end;
else
%    Licor_CO2=  ones(1,36000)*NaN;
%    Licor_H2O=  ones(1,36000)*NaN;
%    Licor_Temp= ones(1,36000)*NaN;
%    Licor_Press=ones(1,36000)*NaN;
        if strcmp(fname,'lis')==1
                lic1=ones(8,36000)*NaN;
        elseif strcmp(fname,'lin')==1
                lic1=ones(9,36000)*NaN;
        elseif strcmp(fname,'li1')==1 | strcmp(fname,'li2')==1 | strcmp(fname,'li3')==1
                lic1=ones(11,36000)*NaN;
        end;
    time_licor=tref;
end;                            %end if flist

if strcmp(fname,'lis')==1
    Licor_diagi =lic1(4,:);
    Licor_CO2i  =lic1(5,:);%mmol/m^3
    Licor_H2Oi  =lic1(6,:);%mmol/m^3
    Licor_Tempi =lic1(7,:);%C
    Licor_Pressi=lic1(8,:);%kPa
elseif strcmp(fname,'lin')==1
    Licor_diagi =lic1(5,:);
    Licor_CO2i  =lic1(6,:);%mmol/m^3
    Licor_H2Oi  =lic1(7,:);%mmol/m^3
    Licor_Tempi =lic1(8,:);%C
    Licor_Pressi=lic1(9,:);%kPa
elseif strcmp(fname,'li1')==1 | strcmp(fname,'li2')==1 | strcmp(fname,'li3')==1
    Licor_diagi =lic1(5,:);
    Licor_CO2i  =lic1(7,:);%mmol/m^3
    Licor_H2Oi  =lic1(9,:);%mmol/m^3
    Licor_Tempi =lic1(10,:);%C
    Licor_Pressi=lic1(11,:);%kPa
end;

ii=find(isnan(Licor_CO2i));
if length(ii)==36000
    Licor_CO2=  ones(1,36000)*NaN;
    Licor_H2O=  ones(1,36000)*NaN;
    Licor_Temp= ones(1,36000)*NaN;
    Licor_Press=ones(1,36000)*NaN;
else
    try
        Licor_CO2=spline(time_licor,Licor_CO2i,tref);
        Licor_H2O=spline(time_licor, Licor_H2Oi,tref);
        Licor_Temp=spline(time_licor,Licor_Tempi,tref);
        Licor_Press=spline(time_licor,Licor_Pressi,tref);
    catch
        if length(Licor_CO2i)>36000
            Licor_CO2=  Licor_CO2i(1,1:36000);
            Licor_H2O= Licor_H2Oi(1,1:36000);
            Licor_Temp= Licor_Tempi(1,1:36000);
            Licor_Press=Licor_Pressi(1,1:36000);
        else
            Licor_CO2=  [Licor_CO2i ones(1,36000-length(Licor_CO2i))*NaN];
            Licor_H2O=  [Licor_H2Oi ones(1,36000-length(Licor_H2Oi))*NaN];
            Licor_Temp= [Licor_Tempi ones(1,36000-length(Licor_Tempi))*NaN];
            Licor_Press=[Licor_Pressi ones(1,36000-length(Licor_Pressi))*NaN];
       end;
    end;
end;

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