function out = readzenofiles(filename)

% purpose of this function is to read in expected Zeno files
% site with the extension.  This reader is more specialized for these file
% types.  Apr 9 2014

% v - 2017-Nov-11 - JKH

% example filename
% filename = '../../../../../../oco2-data/tccon/spectra/caltech_em27/i2s_infiles/20140628/ci_cmb_zeno.zno';

fileID = fopen(filename);

header_int = cell(20,1); %pre-allocates for speed on the guess that there won't be more than 20 lines of header

data_check = zeros(size(header_int));
i1 = 1; %initialize while loop
i1a = 1; %second (starting) index
rws_num = 10;
while sum(data_check(i1a:i1)) ~= rws_num; %assumes that if there has been 10 rows of numbers then data has started, 
%     note that this has the disadvantage of requiring at least 10 rows
    
    header_int{i1} = fgetl(fileID);
    
    line_i1 = textscan(header_int{i1}, '%f', 'delimiter', {',','/',':',' ','\t','^M'});
    
    data_check(i1) = ~isempty(line_i1{1}); %if it is empty, it's likely header text
    
    i1 = i1+1;
    if i1 <= rws_num;
        i1a = i1;
    else
        i1a = i1- rws_num;
    end
end
% fileID = fclose('all'); %close file (so it can be reopened and re-read)
fclose('all');

header = textscan(header_int{i1a-1}, '%s', 'delimiter', {',','/',':',' ','\t','^M'}, 'multipleDelimsAsOne', 1);
header = header{1,1}'; %creates line of data units header

header_int = header_int(1:(i1a-2),1);

scan_format = repmat('%f',1,length(line_i1{1,1})-1); %sets-up to read in text as floating point string.  This should likeely be %f repeated 19 times
if isequal(header{end}, 'Bit')
    scan_format = [scan_format, '%s']; %makes last column (bit number) a string
else
    scan_format = [scan_format, '%f'];
end
    
fileID = fopen(filename);
for i1 = 1:(i1a-1); %retrims off first rows of file that contain header without removing the data
    fgetl(fileID);
end


% 4275,14/04/09,21:20:38,0.0,0,0.0,0.0,33.3,6,915.8,986.5,0,0,13.6,180000,
% scan_format(
filedata_raw = textscan(fileID, scan_format, 'delimiter', {',','/',':',' ','\t','^M', '\n', '\r'}, 'multipleDelimsAsOne', 1); % filedataraw_a = filedata_raw;
fdr2 = filedata_raw;
%out.fdr2 = fdr2;
fdr_size = length(fdr2);
if isequal(header{end}, 'Bit')
    filedata_raw = cell2mat(filedata_raw(1:(fdr_size-1)));
else
    filedata_raw = cell2mat(filedata_raw(1:(fdr_size)));
end

filedata_raw(filedata_raw == -9999) = NaN; %removes bad data values, sets as NaN
filedata_raw(filedata_raw == -10000) = NaN;

out.filename = filename;
out.info = header_int;
out.header = header;
out.data = filedata_raw;

utc_lcl = 0; %default is neither
if any(not(cellfun(@isempty, strfind(header, 'CompDate')))) && any(not(cellfun(@isempty, strfind(header, 'CompTime'))))%if computer time is avaliable (first choice)
  utc_lcl=2; %local
elseif any(not(cellfun(@isempty, strfind(header, 'UTCDate')))) && any(not(cellfun(@isempty, strfind(header, 'UTCTime'))))%
  utc_lcl=1; %utc
end
out.utc_lcl=utc_lcl; %output what type of time parameter

i1_ind = 1;
for i1 = 1:(length(header)-1);
%     idx = find(strcmp([C{:}], 'a'))
    if all(cellfun(@isempty, strfind({'UTCDate','UTCTime','CompDate','CompTime'},header{i1})))  %if header does not match any of these strings
        out.(header{i1}) = filedata_raw(:, i1_ind);
        i1_ind = i1_ind+1;
    elseif (isequal(header{i1}, 'UTCDate') && utc_lcl==1) || (isequal(header{i1}, 'CompDate') && utc_lcl==2)
        out.year = filedata_raw(:, i1_ind);
        if out.year(1) < 100
            out.year = out.year + 2000;
        end
        out.month = filedata_raw(:, i1_ind+1);
        out.day = filedata_raw(:, i1_ind+2);
        i1_ind = i1_ind+3;
    elseif (isequal(header{i1}, 'UTCTime') && utc_lcl==1) || (isequal(header{i1}, 'CompTime') && utc_lcl==2)
        out.hour = filedata_raw(:, i1_ind);
        out.min = filedata_raw(:, i1_ind+1);
        out.sec = filedata_raw(:, i1_ind+2);
        i1_ind = i1_ind+3;
    end
    
end

if isequal(header{end}, 'Bit')
    out.(header{end}) = fdr2{fdr_size};
else
    out.(header{end}) = filedata_raw(:, end);
end

% out.likelyheader = {'unitnum', 'year','month','day','hour','min','sec','windspd','winddir','sigtheta','gust','temp','RH','slrflux','pres','precip','leafwet','Battery','Bit'};
% if (length(out.likelyheader)-1) ~= (size(out.data,2)); %takings off one for bit
%     disp('Warning, possibly unknown data in Zeno input file')
% end
fclose('all');
%% Compare size of file with what's been read in.  If they do not match, there's an error.
fid = fopen(filename, 'rb');
% Get file size.
fseek(fid, 0, 'eof');
fileSize = ftell(fid);
frewind(fid);
% Read the whole file.
data = fread(fid, fileSize, 'uint8');
% Count number of line-feeds.
numLines = sum(data == 10);
fclose(fid);

length_read = (i1a-1) + length(out.year); %rows of header + rows of data
if abs(numLines-length_read) > 1;
 warning('Zeno file does not appear to be read in correctly')
end






