function [distance_return] = ...
    distance_calcs(lb_cell,rb_cell, segment250pts)
% Organizes or calculates various distances for the channel-floodplain
% interface elements

% Extract left and right boundary midpoint coordinates
lb_xy = cell2mat(lb_cell(5,1));
rb_xy = cell2mat(rb_cell(5,1));

% Import cumulative distance downstream
lb_downdist = cell2mat(lb_cell(11,1));
rb_downdist = cell2mat(rb_cell(11,1));

% Extract distances upstream and downstream from each point
lb_updown = cell2mat(lb_cell(10,1));
rb_updown = cell2mat(rb_cell(10,1));

% Get size of left and right boundaries
[num_lb,~] = size(lb_xy);
[num_rb,~] = size(rb_xy);

%% Adjust downstream distances to center of river distances
% This needs to be done in order to relate discharge across a
% cross-section to the fluxes along the banks. Without this portion of
% the script, the banks are much different distances than the center
% channel. This uses a ratio within each 250 segment of river to
% proportionally decrease bank length to center channel length

max_xs_num = 27000/250;

% Determine which xs_polygon each element is in
lb_seg_id = zeros(num_lb,1);
rb_seg_id = zeros(num_rb,1);

for i250 = 1:max_xs_num
    for j250 = 1:num_lb
       if inpolygon(lb_xy(j250,1),lb_xy(j250,2),...
               segment250pts(segment250pts(:,3)==i250,1),...
               segment250pts(segment250pts(:,3)==i250,2)) == 1
           lb_seg_id(j250,1) = i250;
       end
    end

    for k250 = 1:num_rb
       if inpolygon(rb_xy(k250,1),rb_xy(k250,2),...
               segment250pts(segment250pts(:,3)==i250,1),...
               segment250pts(segment250pts(:,3)==i250,2)) == 1
           rb_seg_id(:,1) = i250;
       end
    end
end

lb_center_dist(:,1) = lb_downdist;
rb_center_dist(:,1) = rb_downdist;
lb_center_dist(1,2) = lb_center_dist(1,1);
rb_center_dist(1,2) = rb_center_dist(1,1);

for d250 = 2:num_lb
    lb_center_dist(d250,2) = lb_center_dist(d250,1) - ...
        lb_center_dist(d250-1,1);
end
for d250 = 2:num_rb
    rb_center_dist(d250,2) = rb_center_dist(d250,1) - ...
        rb_center_dist(d250-1,1);
end
lb_center_dist(:,3) = lb_seg_id;
rb_center_dist(:,3) = rb_seg_id;

for a250 = 1:max_xs_num
    lb_segdist = sum(lb_center_dist(lb_center_dist(:,3)==a250,2));
    rb_segdist = sum(rb_center_dist(rb_center_dist(:,3)==a250,2));

    for b250 = 1:num_lb
        if lb_center_dist(b250,3) == a250;
            lb_center_dist(b250,3) = lb_segdist;
            lb_center_dist(b250,4) = lb_center_dist(b250,2) / ...
                lb_center_dist(b250,3);
            lb_center_dist(b250,5) = lb_center_dist(b250,4) * 250;
        end
    end

    for c250 = 1:num_rb
        if rb_center_dist(c250,3) == a250;
            rb_center_dist(c250,3) = rb_segdist;
            rb_center_dist(c250,4) = rb_center_dist(c250,2) / ...
                lb_center_dist(c250,3);
            rb_center_dist(c250,5) = rb_center_dist(c250,4) * 250;
        end
    end
end

lb_center_dist(:,6) = cumsum(lb_center_dist(:,5));
rb_center_dist(:,6) = cumsum(rb_center_dist(:,5));

distance_return = cell(1,8);

distance_return(1,1) = {lb_xy};
distance_return(1,2) = {rb_xy};
distance_return(1,3) = {lb_downdist};
distance_return(1,4) = {rb_downdist};
distance_return(1,5) = {lb_updown};
distance_return(1,6) = {rb_updown};
distance_return(1,7) = {lb_center_dist};
distance_return(1,8) = {rb_center_dist};
end