/*
 * SUMMARY:      GetMetData.c - Read new station meteorological data
 * USAGE:        Part of DHSVM
 *
 * AUTHOR:       Bart Nijssen
 * ORG:          University of Washington, Department of Civil Engineering
 * E-MAIL:       nijssen@u.washington.edu
 * ORIG-DATE:    Apr-96
 * Last Change: Wed Jan 27 23:45:25 1999 by Bart Nijssen <nijssen@u.washington.edu>
 * DESCRIPTION:  Read new station meteorological data
 * DESCRIP-END.
 * FUNCTIONS:    GetMetData()
 * COMMENTS:     
 */

#ifndef lint
static char vcid[] = "$Id: GetMetData.c,v 1.2 2001/03/31 00:10:24 waichler Exp $";
#endif /* lint */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "settings.h"
#include "data.h"
#include "DHSVMerror.h"
#include "functions.h"
#include "constants.h"
#include "rad.h"

#define BEAM_THRESHOLD 25.

/*****************************************************************************
  GetMetData()
*****************************************************************************/
void GetMetData(OPTIONSTRUCT *Options, TIMESTRUCT *Time, int NSoilLayers, 
                int NStats, float SunMax, METLOCATION *Stat, MAPSIZE *Radar, 
                RADARPIX **RadarMap, char *RadarFileName)
{
  int i;			/* counter */

  if (DEBUG)
    printf("Reading all met data for current timestep\n");

  for (i = 0; i < NStats; i++)
    ReadMetRecord(Options, &(Time->Current), NSoilLayers, &(Stat[i].MetFile), 
                  Stat[i].IsWindModelLocation, &(Stat[i].Data), 
                  Stat[i].NPrecipLapse, Stat[i].NTempLapse);
  
  if (Options->PrecipType == RADAR)
    ReadRadarMap(&(Time->Current), &(Time->StartRadar), Time->Dt, Radar,
		 RadarMap, RadarFileName);
  
  for (i = 0; i < NStats; i++) {
    /* The condition (Stat[i].Data.SinBeamMod > BEAM_THRESHOLD) is a hack 
       to ensure that no abnormally high radiation values occur when using 
       IPW.  The problem is the following:  At certain timesteps, 
       especially the first and last timestep of the day, the radiation 
       value modeled by IPW can be really low, while the measured value is 
       still quite high. This can result in really high ratios of modeled 
       to observed, which after interpolation can result in very high 
       radiation values at pixels where there is no measured shortwave.  
       One way to get around this is to say that IPW does not do a 
       particularly good job below a certain threshold, and in that case 
       assign all the incoming shortwave to the diffuse.  Since the diffuse 
       radiation fields are much smoother, this should not give rise to 
       the same problem */
    
    /* Sun is above horizon, split radiation into beam and diffuse */
    if (SunMax > 0.0 && Stat[i].Data.SinBeamMod > BEAM_THRESHOLD) {
      Stat[i].Data.ClearIndex = Stat[i].Data.Sin/SunMax;
      SeparateRadiation(Stat[i].Data.Sin, Stat[i].Data.ClearIndex, 
                        &(Stat[i].Data.SinBeamObs), 
                        &(Stat[i].Data.SinDiffuseObs));
      
      if (Stat[i].Data.SinBeamMod > 0.0)
        Stat[i].Data.BeamRatio    = 
          Stat[i].Data.SinBeamObs/Stat[i].Data.SinBeamMod;
      else
        Stat[i].Data.BeamRatio    = 0.0;
      if (Stat[i].Data.SinDiffuseMod > 0.0)
        Stat[i].Data.DiffuseRatio = 
          Stat[i].Data.SinDiffuseObs/Stat[i].Data.SinDiffuseMod;
      else
        Stat[i].Data.DiffuseRatio = 0.0;
    }
    else {
      /* Sun is below horizon, all radiation is diffuse */
      Stat[i].Data.SinBeamObs    = 0.0;
      Stat[i].Data.SinDiffuseObs = Stat[i].Data.Sin;
      Stat[i].Data.BeamRatio     = 0.0;
      if (Stat[i].Data.SinDiffuseMod > 0.0)
        Stat[i].Data.DiffuseRatio = 
          Stat[i].Data.SinDiffuseObs/Stat[i].Data.SinDiffuseMod;
      else
        Stat[i].Data.DiffuseRatio = 1.0;
    }
  }
  /*   for (i = 0; i < NStats; i++) */
  /*          printf("%d\t%g\t%g\t%g\t%g\t%g\t%g\n", i, */
  /*                 Stat[i].Data.SinBeamObs, Stat[i].Data.SinBeamMod,  */
  /*                 Stat[i].Data.BeamRatio, */
  /*                 Stat[i].Data.SinDiffuseObs, Stat[i].Data.SinDiffuseMod,  */
  /*                 Stat[i].Data.DiffuseRatio); */  
}
