function Out = CriticalArea(melt)
%Determine if percolation occurs at each sampled level set, and the
%Area Fraction at each level set
%
%INPUTS
%  melt = r x c, level sets, size r rows by c columns
%         r = 500, c = 500 for PDE model data
%
%OUTPUTS
%   Out = A z x k array which details the Area Fraction, the binary 
%         variable PathExist which indicates the presence of percolation
%         for each level set taken in melt, the number of objects (melt 
%         ponds) detected, and the size of each of those melt ponds. 
%
%By: Anthony Cheng (Mar 2015)
%===================================================================

[r, c] = size(melt);
tic;

%Calculate the Area Fraction by counting number of non-empty "pixels" 
AreaFraction = nnz(melt)/(r*c); 

%Determine the presence of percolation
CC = bwconncomp(rot90(melt),4);
PixelIdxList = CC.PixelIdxList;

PathExist = 0;
for li = 1:numel(PixelIdxList)
    top = find(PixelIdxList{li} <= r);
    bottom = find(PixelIdxList{li} >= r*(c-1));
    if ~isempty(top) && ~isempty(bottom)
        PathExist = 1; 
        break
    else
        continue
    end
end

Out = [AreaFraction,PathExist];
toc

end
