function out = read_top_params(filename)

% this function is meant to read in the 2014 version of .top files, and save
% them into a structure

% filename = 'ha20140820opus-ipp.top'; %example input file
%% Open, read in outline file
fid=fopen(filename,'r');
origtext = textscan(fid, '%s','delimiter','\n'); %reads generic text file into a cell
fclose(fid);

origtext = origtext{1,1};
param_lines = NaN*zeros(length(origtext),1);
for i1 = 1:length(origtext);
    semicolontest = strfind(origtext{i1,1}, ':');
    if isempty(origtext{i1,1})~=1 && (isempty(semicolontest) || semicolontest(1,1)~=1);
        param_lines(i1,1) = 1;
    end
end
param_lines2 = find(~isnan(param_lines));
outtext = origtext; %copy the original text
outtext = outtext(1:(param_lines2(end)-1),1); %removes lines that included just example spectra


parameters_list = {'ifgs_loc_1', 'spec_loc_2', 'save_new_ifgs_3', 'save_new_ifgs_loc_4', ...
    'save_phasecurves_5', 'save_phasecurves_loc_6', 'channels_7', 'freq_lims_8', ...
    'naming_conv_9', 'light_source_ind_10', 'channel_ind_ifg_11', 'channel_ind_spec_12', ...
    'output_form_13', 'ascii_field_sep_14', 'ascii_minmax_pairs_15', 'qc_suntracker_16', ...
    'qc_min_zpd_peaks_17', 'qc_cross_corr_18', 'add_get_utc_19', 'max_scans_20', ...
    'max_ifg_points_21', 'freq_lim_siv_corr_22', 'type_lse_23', 'length_phase_op_24', ...
    'frac_int_use_spec_pt_pco_25', 'amount_processing_26', 'verbosity_27'};

if length(parameters_list) ~= length(param_lines2);
    error('unrecognized .top file format')
end

for i1 = 1:length(param_lines2);
    i1_line_text = origtext(param_lines2(i1));
    i1_line_num = str2num(i1_line_text{1});
    if ~isempty(i1_line_num);
        i1_line_out = i1_line_num;
    else
        i1_line_out = i1_line_text{1};
    end
     
    out.(parameters_list{i1}) = i1_line_out;
end

% out.text = outtext;
% out.param_lines = param_lines2;