function array_out = cell_partitioning(array_in, stack_size, pad_radius)
% TODO - description
% utility function used to partition the array into cells in order to make
% it easier to process in parfor

if isempty(stack_size) || stack_size <= 2*pad_radius
    warning('The supplied stack_size was too small. Setting to the default of 64')
    stack_size = 64;
end
num_pics = size(array_in, 3);
num_ranges = ceil(num_pics / (stack_size - 2*pad_radius)); % minus 2*rad because it will be padded

ranges = floor(linspace(1, num_pics, num_ranges+1)); % the code tends to crash when the array is too big
ranges(end) = ranges(end) + 1;

array_out = cell(1, length(ranges)-1);
for k = 1:length(ranges)-1
    array_out{k} = array_in(:, :, ranges(k):ranges(k+1)-1);
end

end

