%------ read_7200_HiWinGS_2013_all.m

%	reads raw LI-7200 data files for HiWinGS from PSD data 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

%	See comments at end of script for variable LI7200_* descriptions

%% initialize
licor_prefix='li0';
disp(['read_co2_7200: ', licor_prefix]);

Licrate = 10;                   % data rate in Hz
Lenlic = 3600*Licrate;          % samples per hour
Nlenlic = floor(Lenlic/6);      % samples per 10 min
ncol = 30;                      % total columns of data
z=[];zz=[];temp=[];
fclose('all');

dfl=[way_raw_data_flux, yyyy(3:4),jd,'/', 'li00' yyyy(3:4) jd hr '_raw.txt'];
flist=fopen(dfl,'r');


%% read file
if flist>0
	disp(dfl);
    while feof(flist)==0,  
        try 
            % temp is 1x29 cell array
            temp = textscan(flist,'%2f%2f%3f %f %*f %*f:%*f:%*f:%*f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %*[^\n]','delimiter', ', ', 'headerlines', 1, 'emptyvalue', NaN,'treatAsEmpty','Overflow');
            temp{1,length(temp)+1} = hhh*ones(length(temp{end}),1);   % add 30th cell with hour
            zz = cell2mat(temp);                                      % zz is 36000x30
            zy = [zz(:,1:4) single([zz(:,5:end-1)]) zz(:,end)]';      % zy is 30x36000, variables 5-20 single precision
            z = [z zy];
        catch
            for ll=1:ncol  % length of last cell reflects missing values from any/all fields
                [l8,c8]=size(temp{1,ncol});
                if length(temp{1,ll})~=l8
                    temp{1,ll}(l8+1)=[];  % truncate length of all cells to length(temp{1,ncol})
                end;
            end;
            if ~isempty(temp{1,1})  % catch error on first line, no data yet
                zz = cell2mat(temp);  
                zy = [zz(:,1:4) single([zz(:,5:end-1)]) zz(:,end)]';
                z = [z zy];
            end;
        end;
    end;
    fclose(flist);
else
    z = ones(30,36000)*NaN; % no data file found
end;

%% select data
chk=size(z);
LI7200_CO2mf=[];
if chk(1)==ncol,
    t7200 = z(1,:)*60+z(2,:)+z(3,:)/1000;       % time in s
    jd7200 = ddd + (z(end,:) + t7200/3600)/24;  % decimal day of year
    LI7200_agc = z(4,:);                        % diagnostic value
    LI7200_CO2raw = z(5,:);                     % CO2 absorptance
    LI7200_H2Oraw = z(6,:);                     % H2O absorptance
    LI7200_CO2d = z(7,:);                       % CO2 density (mmol m-3)
    LI7200_H2Od = z(8,:);                       % H2O density (mmol m-3)
%    LI7200_Tblock = z(9,:);                     % Temperature at IRGA block, °C
    LI7200_Press = z(10,:);                     % Total pressure, kPa
    LI7200_APress = z(11,:);                    % Absolute pressure (LI-7550),kPa
%    LI7200_Headpress = z(12,:);                 % Differential pressure (sensor head), kPa
    LI7200_cooler = z(13,:);                    % Detector cooler (Volts)
    LI7200_CO2mf = z(14,:);                     % CO2 mol fraction (umol/mol)
    LI7200_CO2mfd = z(15,:);                    % CO2 mol fraction dry (umol/mol)
    LI7200_H2Omf = z(16,:);                     % H2O mol fraction (mmol/mol)
    LI7200_H2Omfd = z(17,:);                    % H2O mol fraction dry (mmol/mol)?
%    LI7200_Tdew = z(18,:);                     % Dewpoint temperature (degC)
    LI7200_Tmean = z(19,:);                     % Weighted average temperature of tin (20%) and tout(80%), °C
%    LI7200_Tin = z(20,:);                       % Inlet temperature, °C
%    LI7200_Tout = z(21,:);                      % Outlet temperature, °C
%    LI7200_AvgSS = z(22,:);
%    LI7200_CO2SS = z(23,:);
%    LI7200_H2OSS = z(24,:);
%    LI7200_DeltaSS = z(25,:);
    LI7200_H2OAW = z(26,:);                     % H2O source
    LI7200_H2OAWO = z(27,:);                    % H2O reference
    LI7200_CO2AW = z(28,:);                     % CO2 source
    LI7200_CO2AWO = z(29,:);                    % CO2 reference
end;

AGC=dec2bin(LI7200_agc,16); %
AGC=bin2dec(AGC(:,end-3:end))*6.67;             % compute AGC value
LI7200_agc=AGC';
% 100 is clean, 80 is
% relatively dirty, 50 is very dirty (the equivalent of an AGC value of 100 in
% earlier versions of the LI-7200 software), and 0 would mean no signal at all.

clear z zz zy temp AGC
