% Define the scalars that predict the seasonality of leaf shedding
% and root decay, based on changes in LAI. This needs improvement
%
% See: Randerson, J. T., M. V. Thompson, C. M. Malmstrom, C. B. Field 
% and I. Y. Fung, 1996, Substrate limitations for heterotrophs: 
% Implications for models that estimate the seasonal cycle of 
% atmospheric CO2. Global Biogeochemical Cycles, 10 (4), 585-602.

warning off MATLAB:divideByZero

MINLAI      = 12*ones(numberOfGridCells,1);
MAXLAI      = zeros(numberOfGridCells,1);
SUMLAI      = zeros(numberOfGridCells,1);
AVELAI      = zeros(numberOfGridCells,1);
LTVARSUM    = zeros(numberOfGridCells,1);

SUMLAI = sum(lais(:,2:13),2);    

SUMLAInew(:,month) = SUMLAI;
for i=13:-1:2
    io = lais(:,i) > lais(:,i-1);
    fid = lais(:,i) - lais(:,i-1);
    LTVARSUM(io) = LTVARSUM(io) + fid(io);
    
    io = lais(:,i) > MAXLAI;
    fid = lais(:,i);
    MAXLAI(io) = fid(io);
    
    io = lais(:,i) < MINLAI;
    fid = lais(:,i);
    MINLAI(io) = fid(io);
end

AVELAI = SUMLAI ./ 12;

LTCON = MINLAI ./ AVELAI;
LTCON(AVELAI <= 0)=0;

io  = lais(:,2) - lais(:,1) > 0;
fid = lais(:,2) - lais(:,1);
LTVAR(io) = fid(io);
LTVAR(~io) = 0;

fid = (LTCON ./ 12) + (1 - LTCON) .* (LTVAR ./ LTVARSUM);
fid(LTVARSUM <= 0) = 0;
litterscalar(:,month) = fid;

fid = 0.7 * ((litterscalar(:,month) + LAI(:,month) ./ SUMLAI ) ./ 2) + 0.3./12;
fid(SUMLAI <= 0) = 0;
rootlitscalar(:,month) = fid;

warning on MATLAB:divideByZero

hlitterscalar(:,month) = 0.5 + (0.5 - a(:,month)./2);     % scalar for herbaceous leaf and froot turnover TEMPORARY


