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

int CalcAddWeather(SWSData WS, SMetData pMetData[], BOOL bSupit, SCalcWeather pCW[])

{
float angot;
float daylength;

// Loop over the number of rows that we have retrieved
for (int nDay = 0; nDay < WS.nRows; nDay++)
{
        pCW[nDay].Day = pMetData[nDay].Day;
        pCW[nDay].Station = WS.Station;

        // First the Global Radiation calculation

        // Calculate the Angot radiation and the daylength
       AngotRadiation(WS.Latitude, pMetData[nDay].Day, &angot, &daylength);

        // First determine how we are going to do the calculation
        // If the metdata contains measured global radiation, use that.
        if (pMetData[nDay].MeasuredRad != LongPseudoNull)
                  pCW[nDay].GlobalRad = pMetData[nDay].MeasuredRad;
        else
{
                  // else this depends on the availability of: sunshine duration, cloud cover
                  // and Tmin/Tmax. (See SUPIT, Global Radiation, and KIDD, Estimation of..)
                  // and the model chosen by the user.. If it is Choisnel, derive the angstrom
                  // coefficients from the LATITUDE

                  // Check for angot first. Can be 0 in extreme cases

                  if (angot == 0.0f)
                  {
                            pCW[nDay].GlobalRad = 0l;
                  }
                  else
                   {
                             // If the sunshine duration is known, use the ANGSTROM formula
                             if (pMetData[nDay].Sunshine != FloatPseudoNull)
                                       {
                                       if (bSupit)
                                                     // If we use the interpolated angstrom coefficients
                                                    pCW[nDay].GlobalRad = Angstrom(WS, pMetData[nDay], angot, daylength);
                                         else
                                                    // use the Choisnel method
                                                    pCW[nDay].GlobalRad = Choisnel(WS, pMetData[nDay], angot, daylength);
                                        }
 

                                        // If not Angstrom, check if the temperature data is available
                                       else if ((pMetData[nDay].TempMin != FloatPseudoNull) &&  (pMetData nDay].TempMax != FloatPseudoNull))
                                       {
                                                   // Ok, we've got temperature. But have we got cloudcover as well ?
                                                   if (pMetData[nDay].CloudCover != FloatPseudoNull)
                                                   {
                                                             // yes, cloudcover AND temperature, so use SUPIT
                                                             pCW[nDay].GlobalRad = Supit(WS, pMetData[nDay], angot);
                                                   }
                                                   else
                                                   {
                                                             // Just temperature, so use HARGREAVES
                                                             pCW[nDay].GlobalRad = Hargreaves(WS, pMetData[nDay], angot);
                                                  }
                                         }
                                         else
                                                 // No Global Radiation calculation possible
                                                 pCW[nDay].GlobalRad = LongPseudoNull;
                                         }
                 }

                 // Now we've got the global radiation, angot radiation and daylength, we
                 // can calculate the potential evapotransporation. However, we also need
                 // Tmin, Tmax, and the vapour pressure.
                if ((pMetData[nDay].TempMin != FloatPseudoNull) &&
                           (pMetData[nDay].TempMax != FloatPseudoNull) &&
                           (pMetData[nDay].Wind10 != FloatPseudoNull) &&
                           (pMetData[nDay].VapourPressure != FloatPseudoNull))
                 {
                           PotEvTransp(WS, pMetData[nDay], pCW[nDay].GlobalRad, angot, daylength, &pCW[nDay].PotEvTransp);
                 }
                 else
                 {
                          pCW[nDay].PotEvTransp.E0 = FloatPseudoNull;
                          pCW[nDay].PotEvTransp.ES0 = FloatPseudoNull;
                          pCW[nDay].PotEvTransp.ET0 = FloatPseudoNull;
                   }
}

// and return the number of rows processed
return WS.nRows;
}