function out = read_aeronet_v3(filename)

% function to read in Aeronet version 3 data
% 
% example inputs
% filename = '20160101_20161231_CalTech.tot_lev15';

%% initialize file
fid = fopen(filename); %initial opening

out.AN_version = fgetl(fid); %get version, line 1
out.site = fgetl(fid); %get site name, line 2
out.fv_type = fgetl(fid); %get file/version type, line 3
out.file_notes = fgetl(fid); %get notes on file, line 4
out.contact_info = fgetl(fid); %get contact info on file, line 5
out.web_info = fgetl(fid); %more info, line 6
header_list = textscan(fgetl(fid), '%s', 'delimiter', {','}); %read in header list, line 7
header_list = header_list{1};
info_lines = 7;

%% change header names to acceptable structure names
stc_hdr_list = header_list; 
for i1=1:length(header_list);
  stc_hdr_list{i1} = strrep(stc_hdr_list{i1}, '(', '_');
  stc_hdr_list{i1} = strrep(stc_hdr_list{i1}, ')', '_');
  stc_hdr_list{i1} = strrep(stc_hdr_list{i1}, ':', '_');
  stc_hdr_list{i1} = strrep(stc_hdr_list{i1}, '[', '_');
  stc_hdr_list{i1} = strrep(stc_hdr_list{i1}, ']', '_');
  stc_hdr_list{i1} = strrep(stc_hdr_list{i1}, '-', '_');
  stc_hdr_list{i1} = strrep(stc_hdr_list{i1}, ' ', '_'); %replace spaces with underscores
  if ismember(stc_hdr_list{i1}(1),'0123456789');
    stc_hdr_list{i1} = ['i_', stc_hdr_list{i1}]; %adds a beginning letter to make it a valid field name
  end
end


%% read in the remaining data
line_pre = fgetl(fid); % pre-line to check number of delimiters
ndelim = length(strfind(line_pre,',')) + length(strfind(line_pre,':')); %count the number of delimters

lineformat = repmat('%f', 1, ndelim+1); %base line format
str_ind = 1; %string index
for i1=1:length(stc_hdr_list);
  if isequal(stc_hdr_list{i1}, 'Date_dd_mm_yyyy_') || isequal(stc_hdr_list{i1}, 'Time_hh_mm_ss_') || ...
      isequal(stc_hdr_list{i1}, 'Last_Date_Processed');
    str_ind = str_ind+2;
  end
  if isequal(stc_hdr_list{i1}, 'Data_Quality_Level'); %convert this to a string
    lineformat(str_ind*2) = 's'; 
  end
  
  str_ind = str_ind+1;
end
fclose(fid); %close file


fid = fopen(filename); %initial opening
remain_data = textscan(fid, lineformat, 'delimiter', {',', ':'}, 'HeaderLines', info_lines);
fclose(fid); %close file

%% convert the remaining data into a structure format for easier accessibility
col_n = 1; %column number
for i1=1:length(stc_hdr_list);
  if isequal(stc_hdr_list{i1}, 'Date_dd_mm_yyyy_');
    out.day = remain_data{col_n};
    out.month = remain_data{col_n+1};
    out.year = remain_data{col_n+2};
    col_n = col_n+2;
  elseif isequal(stc_hdr_list{i1}, 'Time_hh_mm_ss_');
    out.hour = remain_data{col_n};
    out.min = remain_data{col_n+1};
    out.sec = remain_data{col_n+2};
    out.UTC_time = datenum(out.year, out.month, out.day, out.hour, out.min, out.sec); %saves date number
    col_n = col_n+2;
  elseif isequal(stc_hdr_list{i1}, 'Last_Date_Processed');
    out.proc_day = remain_data{col_n};
    out.proc_mon = remain_data{col_n+1};
    out.proc_year = remain_data{col_n+2};
    col_n = col_n+2;
  elseif isequal(stc_hdr_list{i1}, 'Exact_Wavelengths_of_AOD_um_'); %should be the last
    out.(stc_hdr_list{i1}) = cell2mat(remain_data(col_n-1:end));
  else
    out.(stc_hdr_list{i1}) = remain_data{col_n};
  end
  
  col_n = col_n+1;
end











