%------ read_co2_HiWinGS_2013.m

%	reads 10 Hz licor data files from PSD flux system

%	These variables must exist in the current workspace
%		hhh: numerical value of current hour (00-23)
%		ddd: numerical value of current day of year (0-365)
%		yyyy: string variable of current year (e.g.'2014')
%		way_raw_data_flux: string path to data folder
%       clearit: sets clear flag

%	Script creates these 10Hz arrays:
%		Licor_diag_0, Licor_CO2, Licor_H2O, Licor_Tmp,
%       Licor_Press, Licor_CO2a

% Data file format:
% if ncol==6 then message is:
% mmsslll status CO2[mmol/m3] H2O[mmol/m3] Temperature[C] pressure[kPa]
% if ncol==8:
% mmsslll status CO2[umol/mol] CO2[mmol/m3] H2O[mmol/mol] H2O[mmol/m3] Temperature[C] pressure[kPa]

%% initialize
disp('read_co2_hiwings2013.m')

fclose all;
licor_prefix='lic';
hr=sprintf('%02i',hhh);
jd=sprintf('%03i',ddd);

Licrate = 10;               % data rate in Hz
Lenlic = 3600*Licrate;      % samples per hr @ 10Hz
Nlenlic = floor(Lenlic/6);  % samples per 10min

% Full path & file name string for Windows computer
% dfl=[way_raw_data_flux, yyyy(3:4),jd,'\' licor_prefix '0', yyyy(3:4) jd hr '_raw.txt'];

% Full path & file name string for Mac
dfl=[way_raw_data_flux, yyyy(3:4),jd,'/' licor_prefix '0', yyyy(3:4) jd hr '_raw.txt'];

disp(dfl);
flist=fopen(dfl,'r');

temp={};
z=[];
good_flag = 1;

%% load raw text files
if flist>0
    % find length of line if necessary
%   fgetl(flist);
%   ncol=length(sscanf(fgetl(flist),'%f'));
%   frewind(flist);
    ncol =6;
    while feof(flist)==0,  
        try    % read file into cell array. temp is 1x8 cell array
            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 ii=1:8  % length of last cell reflects missing values from any/all fields
                if length(temp{1,ii})~=length(temp{1,8})
                    temp{1,ii}(length(temp{1,8})+1)=[]; %truncate length of all cells to length(temp{1,8})
                end;
            end;
            if ~isempty(temp{1,1})
                z=[z cell2mat(temp)'];
            end;
        end;     
    end;    
   
    z(:,any(isnan(z),1))=[]; % kill lines with NaN

    chk=size(z);
    if chk(1)==8,
        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;
    fclose(flist);
else  % no data default
    z=ones(8,36000).*NaN;  
    Licor_diag_0 =z(4,:);
    Licor_CO2  =z(5,:);
    Licor_H2O  =z(6,:);
    Licor_Temp =z(7,:);
    Licor_Press=z(8,:);
    good_flag = 0;
end;

%% process motion data unless no file found
if good_flag
    %% copy out data segments
    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;


    %% convert units from mol/m3 to best estimate of mol/mol
    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;
    Licor_diag_0=(xx-y)*6.25;
end;

%% cleanup
if clearit
    clear jd hr Lenlic licor_prefix dfl flist temp z ncol ii
    clear Rgas_universal b7 b6 b5 b4 chk xx y good_flag
end;

