function out = read_atf_wnhdr(ascii_fname)

% This function reads in ascii files with numbers at the beginning of the
% headers specifiying 1) how many header rows and 2) how many columns of
% data, assuming that the first column is of strings (e.g. spectral names)
% ascii_fname = '../dn20160825.aft';

%% get headers
fid = fopen(ascii_fname); %open the setting file
row1 = textscan(fgetl(fid), '%d');
row1 = row1{1}; %pulls double out of cell
for i1=2:row1(1); %for each header row
  row_i1 = fgetl(fid);
end
hdr_row = textscan(row_i1, '%s'); %last row is column headers
hdr_row = hdr_row{1};

out.filename = ascii_fname;


% data_raw = textscan(fid, repmat('%s',1,length(hdr_row)), 'delimiter', {' '}, 'MultipleDelimsAsOne', 1); %more generic for reading, (less errors) but slower
if isequal(hdr_row{1},'Spectrum_File_Name');
  data_raw = textscan(fid, ['%s', repmat('%f',1,length(hdr_row)-1)]);
else
  data_raw = textscan(fid, repmat('%f',1,length(hdr_row)));
end


for i1=1:length(hdr_row); %for each header
  out.(hdr_row{i1}) = data_raw{i1};
  
  %%% This section detects if columns are numeric and converts when
  %%% possible. However, it is very slow...
%   num_i1 = str2double(data_raw{i1}); %convert strings to double
%   if ~all(isnan(num_i1)); %if data was converted successfully to numerical
%     out.(hdr_row{i1}) = num_i1;
%   else
%     out.(hdr_row{i1}) = data_raw{i1};
%   end
end


fclose(fid);

