% getAgeClassBF, get the Burned fraction for the running age class

% the idea is that the oldest part of the gridcell burns first, because
% these parts contain the highest fuel loads

if year==1 & month==1 & ageClass==1
    ageClassIndex   = zeros(numberOfGridCells,numberOfAgeClasses);  % matrix that defines the age of the gridcells in each ageClass
    BFallClasses    = BF; 
end

if ageClass == 1
    BFleftCurrentMonth = BFallClasses(:,month);     % for the first (oldest) age class, assign the total burned area
end

BFtemp = BFleftCurrentMonth;                        % assign the current burned fraction
io  = BFtemp > 1/numberOfAgeClasses;                % check where the burned fraction exceeds the class size
BFtemp(io) = 1/numberOfAgeClasses;                  % where BF > class size, assign the class size
BFleftCurrentMonth  = BFleftCurrentMonth - BFtemp;  % set BF for next age class
BFtemp = BFtemp * numberOfAgeClasses;               % correct for class size

ageCurrentClass = ageClassIndex(:,ageClass);
	ageCurrentClass = ageCurrentClass + 1;          % add age [months]
	io  = BFtemp>0;                                 % check in which gridcells there was a fire (rejuvenile)
	ageCurrentClass(io) = 1 - BFtemp(io);           % reset age to 0 (completely burned) or a value between 0 and 1 (partly burned)
ageClassIndex(:,ageClass) = ageCurrentClass;

BF(:,month) = BFtemp;
