/*
 * SUMMARY:      ReadMetRecord.c - Read 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: Thu Aug 15 12:20:53 2002 by Scott Waichler <waichler@tuff.pnl.gov>
 * DESCRIPTION:  Read station meteorological data
 * DESCRIP-END.
 * FUNCTIONS:    ReadMetRecord()
 * COMMENTS:     
 */

#ifndef lint
static char vcid[] = "$Id: ReadMetRecord.c,v 1.2 2001/03/31 00:10:32 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"

#define MAXMETVARS    20	/* Maximum Number of meteorological variables to
                                   read.  Hack to be replaced by something
                                   better */  

/*****************************************************************************
  ReadMetRecord()
*****************************************************************************/
void ReadMetRecord(OPTIONSTRUCT *Options, DATE *Current, int NSoilLayers, 
                   FILES *InFile, unsigned char IsWindModelLocation, 
                   MET *MetRecord, int NPrecipLapse, int NTempLapse) 
{
  DATE MetDate;			/* Date of meteorological record */
  float Array[MAXMETVARS];	/* Temporary storage of met variables */
  int i, j;
  int NMetVars;                 /* Number of meteorological variables to 
                                   read */

  NMetVars = 5 + NSoilLayers;
  if (Options->PrecipType == STATION)
    NMetVars++;
  /*  SRW adds HANFORD option for precip lapsing 08/15/02 */
  if (Options->PrecipLapse == VARIABLE || Options->PrecipLapse == HANFORD)
    NMetVars += NPrecipLapse;
  if (Options->TempLapse == VARIABLE)
    NMetVars += NTempLapse;
  if (IsWindModelLocation)
    NMetVars++;

  if (DEBUG) 
    printf("Reading met record from file %s\n", InFile->FileName);

  /* The data in the met files are supposed to be in the order:
     Month/Day/Year-Hour, Temperature (C), Windspeed (m/s), Humidity (%), 
     Shortwave (W/m2), Longwave (W/m2), Clearness index, Soil Temperature(1),
     Soil Temperature (2), Soil Temperature (3) */

  if (!ScanDate(InFile->FilePtr, &MetDate))
    ReportError(InFile->FileName, 23);

  while (!IsEqualTime(&MetDate, Current) && !feof(InFile->FilePtr)) {
    if (ScanFloats(InFile->FilePtr, Array, NMetVars) != NMetVars)
      ReportError(InFile->FileName, 5);
    if (!ScanDate(InFile->FilePtr, &MetDate))
      ReportError(InFile->FileName, 23);
  }
  
  if (!IsEqualTime(&MetDate, Current)) {
    if (DEBUG) {
      printf("Metfile: ");
      PrintDate(&MetDate, stdout);
      printf("Current: ");
      PrintDate(Current, stdout);
    }
    ReportError(InFile->FileName, 28);
  }
  
  if (ScanFloats(InFile->FilePtr, Array, NMetVars) != NMetVars)
    ReportError(InFile->FileName, 5);

  MetRecord->Tair = Array[0];
  MetRecord->Wind = Array[1];
  MetRecord->Rh   = Array[2];
  if (MetRecord->Rh<0.0 || MetRecord->Rh>100.0) {
    printf("error: RH out of bounds: %s\n",InFile->FileName);
    exit(-1);
  }
  MetRecord->Sin  = Array[3];
  if (MetRecord->Sin>1380.0) {
    printf("error: Shortwave out of bounds: %s\n",InFile->FileName);
    exit(-1);
  }
  if (MetRecord->Sin<0.0) {
    printf("Warning: Negative Shortwave, setting to zero: %s\n",InFile->FileName);
  }
  MetRecord->Lin  = Array[4];
  if (MetRecord->Lin<50.0 || MetRecord->Lin>1800.0) {
    printf("error: Longwave out of bounds: %s\n",InFile->FileName);
    exit(-1);
  }

  j = 5;

  for (i = 0; i < NSoilLayers; i++) {
    MetRecord->Tsoil[i] = Array[5+i];
    j++;
  }

  if (Options->PrecipType == STATION) {
    MetRecord->Precip = Array[j];
    if(MetRecord->Precip<0) {
      printf("warning: negative precip, setting to zero: %s \n",InFile->FileName);
      MetRecord->Precip=0.0;
    }
    j++;
    
  }
  else {
    MetRecord->Precip = 0.0;
    MetRecord->PrecipLapse[0] = 0.0;
  }

  /*  SRW adds HANFORD option for precip lapsing 08/15/02 */
  if (Options->PrecipLapse == VARIABLE || Options->PrecipLapse == HANFORD) {
    for(i = 0; i < NPrecipLapse; i++ ){
      MetRecord->PrecipLapse[i] = Array[j];
      j++;
    }
  }
  else
    MetRecord->PrecipLapse[0] = PRECIPLAPSE;

  if (Options->TempLapse == VARIABLE) {
    for(i = 0; i < NTempLapse; i++ ){
      MetRecord->TempLapse[i] = Array[j];
      j++;
    }
  }
  else
    MetRecord->TempLapse[0] = TEMPLAPSE;

  if (IsWindModelLocation) 
    MetRecord->WindDirection = (int) Array[j];
  else
    MetRecord->WindDirection = NOT_APPLICABLE;
}
		   
     
