/*
 * SUMMARY:      LapseT.c - Lapse temperature with elevation
 * 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-MOD: Wed Sep 18 11:18:17 2002 by Scott Waichler <waichler@tuff.pnl.gov>
 * DESCRIPTION:  Lapse temperature with elevation
 * DESCRIP-END.
 * FUNCTIONS:    LapseT()
 * COMMENTS:     
 */

#ifndef lint
static char vcid[] = "nil";
#endif /* lint */

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

/*****************************************************************************
  Function name: LapseT()

  Purpose      : Lapse temperature with elevation

  Required     :
    float Temp       - Air temperature at FromElev elevation (C)
    float FromElev   - Elevation to lapse from (m)
    float ToElev     - Elevation to lapse to (m)
    float LapseRate  - Lapse rate in (m/m)

  Returns      :
    float LapsedTemp - Air temperature at ToElev elevation (C)

  Modifies     : None

  Comments     :
*****************************************************************************/
float LapseT(float Temp, float FromElev, float ToElev, int NLapse,
			 float LapseRate1, float LapseRate2, float BreakElev, int OptionsTempLapse)
{
  const char *Routine = "LapseT";
  float LapsedTemp;

  if( NLapse < 2 ){
	LapsedTemp = Temp + (ToElev - FromElev) * LapseRate1;
  }
  else{
	if( ToElev >= BreakElev ){
	  LapsedTemp = Temp + (ToElev - FromElev) * LapseRate2;
	}
	else{
	  LapsedTemp = Temp + (BreakElev - FromElev) * LapseRate2 
						+ (ToElev - BreakElev) * LapseRate1;
	}
  }

  return LapsedTemp;
}

/*****************************************************************************
  Function name: LapsePrecip

  Purpose      : Lapse precipitation with elevation

  Required     : 
    float Precip       - Precipitation at FromElev (m/timestep)
    float FromElev     - Elevation to lapse from (m)
    float ToElev       - Elevation to lapse to (m)

  Returns      :
    float LapsedPrecip - Precipitation at ToElev (m/timestep)

  Modifies     : None

  Comments     : Used to lapse precip with elevation.
*****************************************************************************/
float LapsePrecip(float Precip, float FromElev, float ToElev, 
				  int NLapse, float PrecipLapse1, float PrecipLapse2, float BreakElev,
          int OptionsPrecipLapse)
{
  const char *Routine = "LapsePrecip";
  float LapsedPrecip;           /* Precipitation at ToElev (m/timestep) */
  float StationAdjust = 1.0;


  /*  original code */
  if( NLapse < 2 ){
    if( OptionsPrecipLapse == CONSTANT )
      LapsedPrecip = Precip * ( 1.0 + PrecipLapse1*(ToElev - FromElev) );
    else
      LapsedPrecip = Precip * StationAdjust + PrecipLapse1*(ToElev - FromElev);
  } else {

    if(OptionsPrecipLapse == HANFORD) {
    /* SRW 05/14/02 for Hanford project.  Instead of lapsing using the explicit
       elevation difference, compute and use factors to multiply precipitation of
       the base station.
       Here PrecipLapse1 is the intercept and PrecipLapse2 is the slope of the
       regression line thru Dinicola's factors vs. elevation.
       For more info, see /projects/cold_creek/reports/rep.pdf.  */

      LapsedPrecip = Precip * (PrecipLapse1 + (PrecipLapse2 * ToElev));
    } else {  /* do the normal lapsing involving two rates and a breakpoint */

	  if( ToElev >= BreakElev ){
	    LapsedPrecip = Precip * StationAdjust  + PrecipLapse2*(ToElev - FromElev);
	  }
	  else{
	    LapsedPrecip = Precip * StationAdjust + PrecipLapse2*(BreakElev - FromElev) 
						   + PrecipLapse1*(ToElev - BreakElev);
	  }
    }
  }
 

  /* LapsedPrecip = Precip * ( 1.0 + PrecipLapse1*(ToElev - FromElev) ); */

  if( LapsedPrecip < 0.0 ) LapsedPrecip = 0.0;
  
  return LapsedPrecip;
}
