% organizeAgeClasses, this routine reorganizes the pools according to the
% age classes so that the oldest comes first (and will run and burn first)

% this routine should only run if the the model is run with ageClasses, it
% takes a lot of processor-time and makes the model much slower (nested loop) 
% than running it withhout ageclasses. When not interested in fires, don't run
% the ageclasses (set ageClasses to '1' in 'defineConstants')

[x,y] = sort(ageClassIndex,2);  % sort the age classes (x = values, y = index in matrix)
y = fliplr(y);                  % flip left to right so that oldest values come first

% % elegant code, but horrenduously slow
% temp = zeros(1,numberOfAgeClasses);
% for gridCell = 1:numberOfGridCells
%     if BFallClasses(gridCell,month) > 0 % things only change after a fire!
%         for ageClass = 1:numberOfAgeClasses
%             temp(1,ageClass) = ageClassIndex(gridCell,y(gridCell,ageClass));
%         end
%         ageClassIndex(gridCell,:) = temp;
%        
% 		for pool = 1:numberOfWoodPools
%             eval(['fid = ',woodPoolNames{pool},'s(gridCell,:);'])
%             for ageClass = 1:numberOfAgeClasses
%                 temp(1,ageClass) = fid(1,y(gridCell,ageClass));
%             end
%             eval([woodPoolNames{pool},'s(gridCell,:) = temp;'])
%         end
% 		
% 		for pool = 1:numberOfHerbPools
%             eval(['fid = ',herbPoolNames{pool},'s(gridCell,:);'])
%             for ageClass = 1:numberOfAgeClasses
%                 temp(1,ageClass) = fid(1,y(gridCell,ageClass));
%             end
%             eval([herbPoolNames{pool},'s(gridCell,:) = temp;'])
%         end
%     end
% end


% horrible code, but fast.....
temp = zeros(1,numberOfAgeClasses);
for gridCell = 1:numberOfGridCells
    if BFallClasses(gridCell,month) > 0 % things only change if there was a fire!
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = ageClassIndex(gridCell,y(gridCell,ageClass));
        end
        ageClassIndex(gridCell,:) = temp;
        
        % organize woodpools
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = abovewoodpools(gridCell,y(gridCell,ageClass));
        end
        abovewoodpools(gridCell,:) = temp;
        
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = belowwoodpools(gridCell,y(gridCell,ageClass));
        end
        belowwoodpools(gridCell,:) = temp;
        
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = leafpools(gridCell,y(gridCell,ageClass));
        end
        leafpools(gridCell,:) = temp;
        
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = frootpools(gridCell,y(gridCell,ageClass));
        end
        frootpools(gridCell,:) = temp;
        
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = cwdpools(gridCell,y(gridCell,ageClass));
        end
        cwdpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = surfstrpools(gridCell,y(gridCell,ageClass));
        end
        surfstrpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = surfmetpools(gridCell,y(gridCell,ageClass));
        end
        surfmetpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = surfmicpools(gridCell,y(gridCell,ageClass));
        end
        surfmicpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = soilstrpools(gridCell,y(gridCell,ageClass));
        end
        soilstrpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = soilmetpools(gridCell,y(gridCell,ageClass));
        end
        soilmetpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = soilmicpools(gridCell,y(gridCell,ageClass));
        end
        soilmicpools(gridCell,:) = temp;
        
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = slowpools(gridCell,y(gridCell,ageClass));
        end
        slowpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = armoredpools(gridCell,y(gridCell,ageClass));
        end
        armoredpools(gridCell,:) = temp;
        
        % organize herbaceous pools
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hleafpools(gridCell,y(gridCell,ageClass));
        end
        hleafpools(gridCell,:) = temp;
        
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hfrootpools(gridCell,y(gridCell,ageClass));
        end
        hfrootpools(gridCell,:) = temp;
        
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hsurfstrpools(gridCell,y(gridCell,ageClass));
        end
        hsurfstrpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hsurfmetpools(gridCell,y(gridCell,ageClass));
        end
        hsurfmetpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hsurfmicpools(gridCell,y(gridCell,ageClass));
        end
        hsurfmicpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hsoilstrpools(gridCell,y(gridCell,ageClass));
        end
        hsoilstrpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hsoilmetpools(gridCell,y(gridCell,ageClass));
        end
        hsoilmetpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hsoilmicpools(gridCell,y(gridCell,ageClass));
        end
        hsoilmicpools(gridCell,:) = temp;
        
        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = hslowpools(gridCell,y(gridCell,ageClass));
        end
        hslowpools(gridCell,:) = temp;

        for ageClass = 1:numberOfAgeClasses
            temp(1,ageClass) = harmoredpools(gridCell,y(gridCell,ageClass));
        end
        harmoredpools(gridCell,:) = temp;
    end
end
