% Extraction of QAR for distributed roughness

clear

%% INPUTS for 2010 lidar model

chan_dist_max = 27000; % km
max_Q = [180; 300; 180; 300; 180; 300]; % m^3/s
site_num = 15; % number of sites
min_t = 7200; % plots start at 2 hour
max_t = 96 * 3600; % hours * seconds in an hour - gives seconds
fpath_root = ['/Users/Colin/Documents/MATLAB/DFlow/abq_models/'...
    'abq2010add2012bath/'];

% Define folder with *his.nc files
fpath = {'run3034_s180pback/';'run3035_s300pback/';...
    'run3036_m180pback/';'run3037_m300pback/';...
    'run3038_l180pback/';'run3039_l300pback/'}; % file path

folder_num = numel(fpath(:,1));

xs_fpdist_cell = cell(folder_num,7);

for fi = 1:folder_num
    
    xs_fpdist_cell(fi,1) = {fpath(fi,:)};

    max_Q_fi = max_Q(fi,1);
    
    fpath_fi = strcat(fpath_root,fpath{fi,:});
    
    fnames = struct2cell(dir(strcat(fpath_fi,'*his.nc')))'; 
    [fnum,~] = size(fnames); % number of files

    %% Extract cross-sectional information
    % Returns cell array structured as the following
    % xs_cell
    % Column 1 - name of model run (assigned earlier)
    % Column 2 - Model time
    % Column 3 - XS x and y coordinates
    % Column 4 - All XS measured flow at all times
    % Column 5 - XS #, Time of max discharge, Maximum discharge
    % Column 6 - XS distance
    % Column 7 - Fraction of upstream peak discharge
    
    [xs_fpdist_cell(fi,2:7), min_t_r, max_t_r, t_steps] = ...
        qpeak_xs_from_his(fnum, fnames, fpath_fi, chan_dist_max, min_t, ...
        max_t, max_Q_fi);

end

%% Calculate rates of attenuation

max_xs_id = chan_dist_max / 250;
dist_qar_rates = zeros(max_xs_id,6);
dist_qar_proportional = zeros(max_xs_id,6);

for i = 1:6
    
    flood_qar = xs_fpdist_cell{i,7};
    total_qar = 1 - flood_qar(max_xs_id,1);
    
    for j = 1:max_xs_id
        
        if j == 1
            dist_qar_rates(j,i) = 1 - flood_qar(j,1);
        else
            dist_qar_rates(j,i) = flood_qar(j-1,1) - flood_qar(j,1);
        end
        dist_qar_proportional(j,i) = dist_qar_rates(j,i) / total_qar;
        
    end
    
end