//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
 

float AFGen(SParameterValues* pTable, int nTabLen, float fValue)
{
// Argument less or equal to first X in table
if (pTable[0].XValue >= fValue)
    return pTable[0].YValue;

for (int nIndex = 1; nIndex < nTabLen; nIndex++)
     {
     // Argument between first and last X in table, interpolation
     if (pTable[nIndex].XValue >= fValue)
        {
        float fSlope = (pTable[nIndex].YValue - pTable[nIndex - 1].YValue)
  /
                (pTable[nIndex].XValue - pTable[nIndex - 1].XValue);

        return (pTable[nIndex - 1].YValue + (fValue - pTable[nIndex -1].XValue) * fSlope);
       }

      // Table partly filled, argument larger then last X in table
      if (pTable[nIndex].XValue < pTable[nIndex - 1].XValue)
         return pTable[nIndex - 1].YValue;
      }

// Table fully filled, argument larger then last X in table
return pTable[nTabLen - 1].YValue;
}