function [xs_return, min_t_r, max_t_r, t_steps] = ...
    qpeak_xs_from_his_chap2(file_num, filename, file_loc, chan_dist_max,...
    min_t, max_t, max_Q_fi, base_Q, upstream_peak)
% Function extracts maximum values at each cross-section
fnameloc = cell(file_num,1);

for i = 1:file_num
    fnameloc(i) = {strcat(file_loc, filename(i))};
end

tq_raw = cell(file_num,2);

for i = 1:file_num    
    tq_raw(i,1) = {ncread(char(fnameloc{i}), 'time')};
    tq_raw(i,2) = {ncread(char(fnameloc{i}), 'cross_section_discharge')};
end

for x = 1:file_num
    if x == 1
        time_flow(:,1) = cell2mat(tq_raw(x,1));
        discharge = cell2mat(tq_raw(x,2));
        [xs_count,~] = size(discharge);
        time_flow(:,2:xs_count+1) = discharge';
        tq_hold = 0;
        [t_count, ~] = size(time_flow);
    elseif x > 1
        clear tq_hold
        tq_hold(:,1) = cell2mat(tq_raw(x,1));
        q_hold = cell2mat(tq_raw(x,2));
        tq_hold(:,2:xs_count+1) = q_hold';
        [tsteps_hold,~] = size(tq_hold);
        for j = 1:tsteps_hold
            if tq_hold(j,1) > time_flow(t_count,1)
                time_flow(t_count+1:t_count+tsteps_hold-j+1,:)...
                    = tq_hold(j:tsteps_hold,:);
                [t_count, ~] = size(time_flow);
                break
            end
        end
            
    end
end

%time_flow = abs(time_flow);
min_time = min(time_flow(:,1));
max_time = max(time_flow(:,1));

max_flows = zeros(xs_count, 3);
max_flows(:,1) = 1:xs_count;

for i = 1:xs_count

        max_flows(i,3) = max(time_flow(:,i+1));
        [~,r] = max(time_flow(:,i+1));
        max_flows(i,2) = time_flow(r,1);
        %max_flows(i,4) = max_flows(i,3) / upstream_peak;
        
%         if i == xs_count
%             max_flows(i,1) = 17.5;
%         end
        
        %if i > 1
        %    max_flows(i,4) = 1000 / (max_flows(i,2) - max_flows(i-1,2));
        %else
        %    max_flows(i,4) = NaN;
        %end
        
end

max_xs_num = chan_dist_max/250*3; % Extracts data from X km downstream
xs_time = time_flow(:,1); % set time array
[min_t_r,~] = find(xs_time(:,1)==min_t); % index of set minimum time
[max_t_r,~] = find(xs_time(:,1)==max_t); % index of set maximum time
[t_steps,~] = size(xs_time); % total time steps in model run
% xs_xy_Xkm = xs_xy(2:(max_xs_num+1),:); % Coordinates of XS
xs_flow_Xkm = time_flow(:, 2:(max_xs_num+1)); % flow data for upper 27 km
xs_max_Q_Xkm = max_flows(1:max_xs_num,:); % Extracts maximum Q
xs_dist = xs_max_Q_Xkm(:,1) * 250; % sets XS distances
% calculates flow above base
xs_above_base = xs_max_Q_Xkm(:,3) - base_Q(1:max_xs_num,1);

% calculates attenuation
max_flows_totals = zeros(chan_dist_max/250,3);
all_xs_flow = zeros(max_t_r,chan_dist_max/250);
max_flows_totals(:,1) = 1:chan_dist_max/250;
a = 0;
for j = 3:3:xs_count
    
    a = a + 1;
    
    all_xs_flow(:,a) = time_flow(:,j+1)+ time_flow(:,j-1)+ time_flow(:,j);
    max_flows_totals(a,3) = max(all_xs_flow(:,a));
    [~,r] = max(all_xs_flow(:,a));
    max_flows_totals(a,2) = time_flow(r,1);
        
end
xs_rarq = max_flows_totals(:,3)/upstream_peak;

xs_return = cell(1,5);
xs_return(:,1) = {xs_time};
xs_return(:,2) = {'no cross-section xy'};
xs_return(:,3) = {xs_flow_Xkm};
xs_return(:,4) = {xs_max_Q_Xkm};
xs_return(:,5) = {xs_dist};
xs_return(:,6) = {all_xs_flow};
xs_return(:,7) = {max_flows_totals};
xs_return(:,8) = {xs_rarq};
xs_return(:,9) = {xs_above_base};

end