function [reworking, Imagesum] = meandering_fluvial_surface_reworking(meandering_grid, grid_length, grid_width, starting_iteration, dt_yrs)
%% Fluvial Surface Reworking from Grid
% This generates a curve, data and map of the degree of fluvial surface
% reworking directly from the model grid. Allows me to cirucmvent turning
% everything into a photo and then doing the analyses
starting = starting_iteration;
% The length of converting the river centerline into a grid makes me have
% to generate the grid in steps. The map step here should be the same step
% that I used for my make_grid step
nfiles = numel(meandering_grid); % length of the grid
iterations = [1:2:nfiles-starting]; % X-axis for the reworking scale. Model years or iterations
reworking = zeros(length(iterations),number_of_bootstraps);
%% Bulk reworking
Image_previous = meandering_grid(starting).map; % for generating first timestep
F = sum( meandering_grid(starting).map(:)>=1) ; % number of cells with value greater than 1 F = sum(IB(:)>=1)
CB = sum( meandering_grid(starting).map(:)==0); % number of cells with value of 0 (unworked)
A = F+CB; % total number of cells
% Initializing the table for surface reworking
reworking = zeros(length(iterations),2);
reworking(1,2)=(1-(CB/A));
reworking(:,1)=iterations;
cell_position = 2;
step = 2*(1/dt_yrs);
for ii = starting+1:step:numel(meandering_grid);
%meandering_grid(ii).map(meandering_grid(ii).map == 0) = 1;
%meandering_grid(ii).map(isnan(meandering_grid(ii).map)) = 0;
% adding subsequent steps to the map
Imagesum = single(Image_previous + meandering_grid(ii).map);
% Finding number of worked and unworked cells
worked = sum(Imagesum(:)>=1);
%unworked = sum(Imagesum(:)==0);
reworking(cell_position,2) = single((worked/A));
cell_position = cell_position + 1;
Image_previous = Imagesum;
end
for ii = 1: length(reworking(:,2))
if ii > 1
A = reworking(ii-1,1);
B = reworking(ii,1);
if A == (B)
break
end
end
end
end