//writen by: E. van der Goot and Giorgio Cifani
//JRC, I-21020 Ispra, Italy
//date: 1996

//based on the original FORTRAN/SQL CGMS and WOFOST code
//produced by Tamme van der Wal, Kees van Diepen and Daniel van Kralingen
//Alterra, P.O. Box 6700 AA, Wageningen, The Netherlands

void CEvTra::CommonEvTra(float fLAI, float fE0, float fES0, float fET0)
{
// Maximum evaporation and transpiration rates
float fEKL = (float)exp(-(0.75f * g_dbTable.m_param.w.KDIF) * fLAI);

m_fEvWMax  = fE0 * fEKL;
m_fEvSMax  = max(   0.0f, fES0 * fEKL);
m_fTraMax  = max(0.0001f, fET0 * (1.0f - fEKL));
}
 

// Potential calculation
//
void CEvTra::Calculate(SGridWeather GW, float fLAI)
{
// NOTE: convert units
float fE0  = GW.fE0  / 10.0f;
float fES0 = GW.fES0 / 10.0f;
float fET0 = GW.fET0 / 10.0f;

fET0 *= g_dbTable.m_param.w.CFET;

CommonEvTra(fLAI, fE0, fES0, fET0);

m_fTra = m_fTraMax;
}
 

// Water limited calculation
//
void CEvTra::Calculate(SGridWeather GW, SSoilPhysicalGroup* pSPG,
                                           float fSoilMoisture, float fLAI)
{
// NOTE: convert units
float fE0  = GW.fE0  / 10.0f;
float fES0 = GW.fES0 / 10.0f;
float fET0 = GW.fET0 / 10.0f;

fET0 *= g_dbTable.m_param.w.CFET;

CommonEvTra(fLAI, fE0, fES0, fET0);

// Local variables without persistence
float fSWDepl;   // Soil water depletion fraction as a function of
                         // potential evapotranspiration
float fRFWS;     // Reduction factor due to water shortage
float fSMCr;     // Critical soil moisture content

// Curve for DEPNR 5, and other curves at fixed distance below it
fSWDepl = 1.0f / (0.76f + (1.5f * fET0)) -
          (5.0f - g_dbTable.m_param.w.DEPNR) * 0.1f;

// Correction for lower curves (DEPNR less than 3)
if (g_dbTable.m_param.w.DEPNR < 3.0f)
    {
    fSWDepl += (fET0 - 0.6f) /
                (g_dbTable.m_param.w.DEPNR *
                (g_dbTable.m_param.w.DEPNR + 3.0f)
               );
    }

// Calculation critical soil moisture content
fSWDepl = Limit(0.1f, 0.95f, fSWDepl);

fSMCr = ((1.0f - fSWDepl) *
         (pSPG->fSoilMoistureContentFC - pSPG->fSoilMoistureContentWP) +
         pSPG->fSoilMoistureContentWP);

// Reduction in transpiration in case of water shortage
fRFWS = Limit(0.0f, 1.0f, (fSoilMoisture - pSPG->fSoilMoistureContentWP) /
              (fSMCr - pSPG->fSoilMoistureContentWP));
 

m_fTra = fRFWS * m_fTraMax;
}