function [data] = importALFAm(filename, verbose)
%IMPORTALFAM Import m files generated by the Alfa software
% author: nils
% created: Aug 18, 2018

% filename = '/Users/nils/Data/EXPORTS/InLine/raw/ALFA011/ALFA011_20180811_023900_m.txt';
% verbose = true;

% if nargin < 2; verbose = false; end
foo = strsplit(filename, '/');

% Init parser
n_column = 38;
i_str = [27, 28, 30:37];
parser = repmat('%f',1,n_column);
parser(i_str*2) = 's';

% Open file
fid=fopen(filename);
if fid==-1
  error('Unable to open file: %s', filename);
end

% Skip header line
fgetl(fid);

% Read data
t = textscan(fid, parser, 'delimiter','\t');
% Close file
fclose(fid);

if verbose && isempty(t{1})
  fprintf('Cannot load %s ... ignored\n', foo{end})
  data = table();
  return
elseif verbose
  fprintf('Importing %s ... ', foo{end});
end

% Build table
data = table(datenum(cell2mat(horzcat(t{27:28})), 'yyyy-mm-ddHH:MM:SS'),...
             t{1}, t{2}, t{3}, t{4}, t{5}, t{6}, t{7}, t{8}, t{9}, t{10},...
             t{11}, t{12}, t{13}, t{14}, t{15}, t{16}, t{17}, t{18}, t{19},...
             t{20}, t{21}, t{22}, t{23}, t{24}, t{25}, t{26},...
             'VariableNames',...
             {'dt', 'Chlb', 'CFRb', 'WLCFb', 'CDOMRb', 'R613Rb',...
             'R625Rb', 'R642Rb', 'R662Rb', 'Chlg', 'CFRg', 'WLCFg',...
             'PE1Rg', 'PE2Rg', 'PE3Rg', 'R642Rg', 'R662Rg', 'PE1CFg',...
             'PE2CFg', 'PE3CFg', 'PE12Rg', 'PE12CFg', 'WLPE12g',...
             'FvFm', 'FvFmC', 'FvFmG', 'FvFmCG'});

if verbose; fprintf('Done\n'); end
end
