<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <vicNl.h>

static char vcid[] = "$Id$";

void rad_and_vpd (atmos_data_struct *atmos, 
		  soil_con_struct    soil_con,
		  int                nrecs, 
		  dmy_struct        *dmy,
		  double           **yearly_prec,
		  double           **yearly_epot)
/**********************************************************************
  rad_and_vpd           <NAME> and <NAME>            1995

  This subroutine calculates net radation and the vapor pressure 
  deficit at the current time step for the current grid cell.  The
  vapor pressure subroutine uses a regression curve and subroutine
  developed by <NAME> 1995 (references noted in aurad.c).  Since
  this method derives questionable shortwave values, a different method
  to calculate the incoming shortwave is used for calculations of 
  net radiation.

  UNITS:	kPa
	
  Modifications:
  5/18/96	Removed first shortwave variable from global usage,
	and added comments.					KAC
  9/4/98  Modified to apply Kimball et. al.'s published regression
          for estimating the daily dew point temperature:
          <NAME>., <NAME>, <NAME>, An Improved
          method for estimating surface humidity from Daily 
          Minimum Temperature, Agricultural and Forest Hydrology, 
          Vol. 85, no. 1-2, June 1997, pg 87-98.               KAC

**********************************************************************/
{
  extern option_struct options;
  extern debug_struct debug;
  extern param_set_struct param_set;

  int    i, no_of_years;
  double deltat, tair, trans_clear, qdp;
  double shortwave;
  double day_len_hr;

  no_of_years = dmy[nrecs-1].year - dmy[0].year + 1;

  yearly_prec[0] = (double*) calloc(no_of_years, sizeof(double));
  yearly_epot[0] = (double*) calloc(no_of_years, sizeof(double));

  /** Compute Priestly Evaporation **/
  if(!options.FULL_ENERGY) {
    for (i = 0; i < nrecs; i++) {
      if ( i == (nrecs-1) )
	deltat = atmos[i].tmax - atmos[i].tmin;
      else
	deltat = atmos[i].tmax - (atmos[i].tmin + atmos[i+1].tmin)/2.0;
      deltat = (deltat < 0) ? -deltat : deltat;
      deltat = (deltat==0) ?  (atmos[i].tmax - atmos[i+1].tmin) : deltat;
      atmos[i].trans = calc_trans(deltat, soil_con.elevation);
      shortwave = calc_netshort(atmos[i].trans, dmy[i].day_in_year, 
				soil_con.lat,&day_len_hr);
      tair = (atmos[i].tmax + atmos[i].tmin) / 2.0;
      atmos[i].priest = priestley(tair, shortwave);
    }
  }
  
  for (i = 0 ; i < nrecs; i++) {
    yearly_prec[0][dmy[i].year-dmy[0].year] += atmos[i].prec;
    yearly_epot[0][dmy[i].year-dmy[0].year] += atmos[i].priest;
  }
  trans_clear = A1_TRANS + A2_TRANS * soil_con.elevation;
  
  for ( i = 0 ; i < nrecs ; i++) {
    tair = (atmos[i].tmax + atmos[i].tmin) / 2.0;
    qdp = svp(estimate_dew_point(dmy,yearly_epot[0],yearly_prec[0],
				 atmos[i].tmin,atmos[i].tmax,atmos[i].priest,
				 day_len_hr*3600.,i));
    if(!param_set.SPEC_HUMID) {
      atmos[i].vpd = (svp(tair) - qdp);
      atmos[i].vp = qdp;
      atmos[i].rel_humid = qdp / svp(tair) * 100.;
    }

    if(!options.FULL_ENERGY) {
      atmos[i].shortwave = /* (1.0 - atmos[i].albedo) *  */
	in_shortwave(soil_con.lat,dmy[i].day_in_year,atmos[i].trans);
      atmos[i].longwave = -net_out_longwave(atmos[i].trans, trans_clear, tair,
					    qdp*1000.0,&atmos[i].tskc);
      atmos[i].rad = atmos[i].shortwave + atmos[i].longwave;
    }
  }
}

double estimate_dew_point(dmy_struct *dmy,
			  double     *yearly_epot,
			  double     *yearly_prec,
			  double      tmin,
			  double      tmax,
			  double      priest,
			  double      day_len,
			  int         day) {
/*******************************************************************
  As described in Kimball et. al. 1997.  See reference above.  KAC  
*******************************************************************/

  double Tdew;
  double EF;

  EF = ((priest / RHO_W) * day_len) / yearly_prec[dmy[day].year-dmy[0].year];
  Tdew = (tmin + KELVIN) * ( -0.127 + 1.121 * (1.003 - 1.444 * EF
					       + 12.312 * EF * EF
					       -32.766 * EF * EF * EF) 
			     + 0.0006 * (tmax - tmin) );

  return(Tdew-KELVIN);
}<commit_msg>fixed units in estimate_dew_point, and removed unused routine rad_and_vpd<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <vicNl.h>

static char vcid[] = "$Id$";

double estimate_dew_point(dmy_struct *dmy,
			  double     *yearly_prec,
			  double      tmin,
			  double      tmax,
			  double      priest,
			  double      day_len,
			  int         day) {
/*******************************************************************
  estimate_dew_point        <NAME>      November 11, 1998

  This routine was written to replace the pre-publication version
  of dew point temperature estimation routine from Kimball et. al., 
  with the version that was published.  The set-up code for this
  routine which was orginally in rad_and_vpd.c has been relocated 
  into the initialize_atmos.c routines which now handle initializing
  all of the atmospheric variables.  

  As described in Kimball, <NAME>., <NAME>, and <NAME>, "An
  improved method for estimating surface humidity from daily minimum
  temperature", Agricultural and Forest Engineering, 85(1997) 87-98.

  Variables:
  dmy_struct *dmy          N/A  Structure containing date information
  double     *yearly_prec  mm/year  Precipitation per year
  double      tmin         C        Minimum daily temperature
  double      tmax         C        Maximum daily temperature 
  double      priest       kg-m^2/s Priestly potential evaporation for the day
  double      day_len      s        Length of the current day in seconds
  int         day          N/A      Day in simulation

*******************************************************************/

  double Tdew;
  double EF;

  EF = ((priest) * day_len) / (yearly_prec[dmy[day].year-dmy[0].year]);
  Tdew = (tmin + KELVIN) * ( -0.127 + 1.121 * (1.003 - 1.444 * EF
					       + 12.312 * EF * EF
					       -32.766 * EF * EF * EF) 
			     + 0.0006 * (tmax - tmin) );

  return(Tdew-KELVIN);
}
{'fraction_non_alphanumeric': 0.17732128184059162, 'fraction_numerical': 0.025472473294987676, 'mean_word_length': 2.7895392278953923, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2702414', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2079, 'n_words': 525}