FATE simulationR/PRE_FATE.params_PFGlight.R
    PRE_FATE.params_PFGlight.RdThis script is designed to create parameter files containing
light-related parameters for each PFG (one file for each of them) used in
the light module of FATE.
PRE_FATE.params_PFGlight(
  name.simulation,
  mat.PFG.light,
  mat.PFG.tol = NULL,
  opt.folder.name = NULL
)a string corresponding to the main directory
or simulation name of the FATE simulation
a data.frame with 2 to 6 columns : 
PFG,
type (or shade_factor)
type, (or active_germ_low,
  active_germ_medium, active_germ_high) (or
  strategy_ag)
type, light_need
(see Details)
(optional) 
a data.frame with 2 to 4 columns : 
PFG,
lifeStage, resources, tolerance
  (or strategy_tol)
(see Details)
(optional) 
 a string corresponding
to the name of the folder that will be created into the
name.simulation/DATA/PFGS/LIGHT/ directory to store the results
A .txt file per PFG into the
name.simulation/DATA/PFGS/LIGHT/ directory with the following
parameters :
name of the PFG
light value or strategy of the PFG
index of shade quantity to weight PFG abundance and transform it into light resources
germination rates depending on light conditions
  
(integer between 0 and 100%)
light tolerance table (in a single row). 
  This is a vector of 9 numbers corresponding to the ability of the PFG to
  survive or not :
at different life stages (Germinant (Ge), Immature
    (Im), Mature (Ma))
under different light conditions (Low (L), Medium
    (M) or High (H)).
These parameters should be given in this order : GeL, GeM, GeH, ImL,
  ImM, ImH, MaL, MaM, MaH
  
(integer between 0 and 100%).
  
A LIGHT_COMPLETE_TABLE.csv file summarizing information for all
groups into the name.simulation/DATA/PFGS/ directory.
If the opt.folder.name has been used, the files will be into the
folder name.simulation/DATA/PFGS/LIGHT/opt.folder.name/.
The light module allows the user to add the effect of
light interaction within a primary vegetation succession. 
Several parameters, given within mat.PFG.light or mat.PFG.tol,
are required for each PFG in order to set up this light interaction :
the concerned plant functional group 
or life-form, based on Raunkier. 
 It should be either
  H (herbaceous), C (chamaephyte) or P (phanerophyte)
  for now 
a value corresponding to an index of shade
  quantity to weight PFG abundance and transform it into light resources
  (e.g. if two PFG have shade factors of 1 and 5
  respectively, for the same abundances, the second PFG will produce 5
  times more shade than the first one) 
an integer between 0 and
  100 corresponding to the proportion of seeds that will germinate for
  Low light condition
an integer between 0 and
  100 corresponding to the proportion of seeds that will germinate for
  Medium light condition
an integer between 0 and
  100 corresponding to the proportion of seeds that will germinate for
  High light condition
a string to choose the germination
  strategy : light_lover, indifferent, shade_lover
  
an integer between 0 and 5
  corresponding to the light preference of the PFG (e.g. from Flora
  Indicativa)
the concerned life stage (Germinant,
  Immature, Mature)
the concerned light condition (Low,
  Medium, High)
an integer between 0 and 100
  corresponding to the proportion of surviving individuals
a string to choose the tolerance
  strategy : full_light, pioneer, ubiquist,
  semi_shade, undergrowth 
These values will allow to calculate or define a set of characteristics for each PFG :
index of shade quantity to weight PFG abundance and
  transform it into light resources 
  Two methods to define these proportions are available :
from predefined rules (using type) :
for H (herbaceous) : 1
for C (chamaephyte) : 5
for P (phanerophyte) : 100
from user data : 
with the values contained within the shade_factor column,
    if provided 
proportion of seeds that will germinate for each light
  condition (Low, Medium, High) 
  Three methods to define these proportions are available :
from predefined scenarios (using strategy_ag) : | _L_ _M_ _H_ | _______________ | 90% 80% 50% | shade_lover | 90% 90% 90% | indifferent | 50% 80% 90% | light_lover 
from predefined rules (using type) :
for H (herbaceous) : 50%, 80%, 90%
for C (chamaephyte) or P (phanerophyte):
      90%, 90%, 90%
from user data : 
with the values contained within the active_germ_low,
    active_germ_medium and active_germ_high columns, if
    provided 
defined for each life stage (Germinant,
  Immature, Mature) 
 and each soil condition (Low,
  Medium, High) 
  Three methods to define these tolerances are available :
from predefined scenarios (using
    strategy_tol) : 
. means Not tolerant, 1 means
        Tolerant (100%)
with g: Germinant, i: Immature, m: Mature
with L: low light, M: medium light, H:
        high light 
| _ g _ | _ i _ | _ m _ | | L M H | L M H | L M H | _________________________ | 1 1 . | 1 1 . | 1 1 . | undergrowth | 1 1 . | 1 1 . | 1 1 1 | semi_shade | 1 1 1 | 1 1 1 | 1 1 1 | ubiquist | 1 1 1 | . 1 1 | . 1 1 | pioneer | 1 1 1 | . . 1 | . . 1 | full_light 
from predefined rules (using type and
    light_need):
PFG are tolerant to Low light if light <= 2
PFG are tolerant to Medium light if
        2 <= light <= 4
PFG are tolerant to High light if
        light >= 3
all germinants are assumed to be tolerant to Low
        light
all mature trees or chamaephytes are assumed to be
        tolerant to Medium and High light conditions
all immature trees that grow in the
        penultimate stratum are assumed to be tolerant to High light
        !! desactivated !!
. means Not tolerant
A, B, C, D mean Tolerant (100%) according
        to one of the rule defined above
with g: Germinant, i: Immature, m: Mature
with L: low light, M: medium light, H:
        high light 
| _ g _ | _ i _ | _ m _ | | L M H | L M H | L M H | _________________________ | A . . | A . D | A C C | 1 | A A . | A A D | A A C | 2 | B A . | . A D | . A C | 3 | B A A | . A A | . A A | 4 | B . A | . . A | . C A | 5 
from user data : 
with the values contained within the lifeStage,
      resources and tolerance columns, if provided
## Create a skeleton folder with the default name ('FATE_simulation')
PRE_FATE.skeletonDirectory()
## Create PFG light parameter files (with strategies) ----------------------------------------
mat.ag = data.frame(PFG = paste0('PFG', 1:6)
                    , type = c('C', 'C', 'H', 'H', 'P', 'P')
                    , strategy_ag = c('shade_lover', 'indifferent'
                                      , 'indifferent', 'shade_lover'
                                      , 'light_lover', 'light_lover'))
mat.tol = data.frame(PFG = paste0('PFG', 1:6)
                     , strategy_tol = c('undergrowth', 'ubiquist'
                                        , 'ubiquist', 'semi_shade'
                                        , 'pioneer', 'full_light'))
PRE_FATE.params_PFGlight(name.simulation = 'FATE_simulation'
                         , mat.PFG.light = mat.ag
                         , mat.PFG.tol = mat.tol)
                                                        
## Create PFG light parameter files (with all values) ----------------------------------------
mat.ag = data.frame(PFG = paste0('PFG', 1:6)
                    , shade_factor = c(5, 3, 1, 1, 12, 18)
                    , active_germ_low = c(90, 80, 80, 80, 50, 50)
                    , active_germ_medium = rep(80, 6)
                    , active_germ_high = c(40, 80, 80, 50, 90, 90))
mat.tol = expand.grid(resources = c('Low', 'Medium', 'High')
                      , lifeStage = c('Germinant', 'Immature', 'Mature')
                      , PFG = paste0('PFG', 1:6))
mat.tol$tolerance = c(100, 100, 0, 100, 0, 0, 100, 0, 0
                      , rep(100, 9)
                      , rep(100, 9)
                      , 100, 100, 100, 100, 100, 100, 100, 0, 0
                      , 100, 100, 100, 0, 100, 100, 0, 0, 100
                      , 100, 100, 100, 0, 0, 100, 0, 0, 100)
PRE_FATE.params_PFGlight(name.simulation = 'FATE_simulation'
                         , mat.PFG.light = mat.ag
                         , mat.PFG.tol = mat.tol)
                                                        
                                                        
## -------------------------------------------------------------------------------------------
## Load example data
Champsaur_params = .loadData('Champsaur_params', 'RData')
## Create a skeleton folder
PRE_FATE.skeletonDirectory(name.simulation = 'FATE_Champsaur')
## PFG traits for light
tab.light = Champsaur_params$tab.LIGHT
str(tab.light)
## Create PFG light parameter files ----------------------------------------------------------
PRE_FATE.params_PFGlight(name.simulation = 'FATE_Champsaur'
                         , mat.PFG.light = tab.light[, c('PFG', 'type')]
                         , mat.PFG.tol = tab.light[, c('PFG', 'strategy_tol')])