#virtemp.cgi by Tim Brice NWS El Paso


#Constants
$Rd = 287.04;
$cp = 1005;

#Sub-routines
sub ctof {
  local($C) = @_;
  $F = (9/5 * $C) + 32;
  return ($F);
}
sub ftoc {
  local($F) = @_;
  $C = 5/9 * ($F - 32);
  return ($C);
}
sub ctok {
  local($C) = @_;
  $K = $C + 273.15;
  return ($K);
}
sub vappress {
  local($ktemp) = @_;
  $expo = 23.832241 - 5.02808*(log($ktemp)/log(10)) - 1.3816*10**-7 *
10**(11.334 - .0303998 * $ktemp);
  $expo = $expo + 8.1328 * 10**-3 * 10**(3.49149 - 1302.8844/$ktemp) -
2949.076/$ktemp;
  $e = 10**($expo);
  return ($e);
}

#Change entries to celsius and millibars
if ($vt{airtemp} =~ /-?(\d+)\.?(\d+)|-?(\d+)/)  {
  if ($vt{corf1} eq Fahrenheit) {
   &ftoc($vt{airtemp});
   $temp = $C;
} else {
   $temp = $vt{airtemp};
  }
} else {
   print "An illegal character was entered.\n";
   die
}

if ($vt{dewtemp} =~ /-?(\d+)\.?(\d+)|-?(\d+)/)  {
 if ($vt{corf2} eq Fahrenheit) {
   &ftoc($vt{dewtemp});
   $dewp = $C;
} else {
  $dewp = $vt{dewtemp};
 }
} else {
 print "An illegal character was entered.\n";
 die
}

if ($vt{press} =~ /(\d+)\.?(\d+)/)  {
 if ($vt{mborin} eq millibars) {
   $press = $vt{press};
} else {
   $press = $vt{press} * 33.8639;
 }
} else {
 print "An illegal character was entered.\n";
 die
}

#calculate the Virtual Temperature

&ctok($dewp);
$kdewp = $K;

&vappress($kdewp);
$esurf = $e;

&ctok($temp);
$ktemp = $K;

$vtemp = $ktemp/ (1 - .379 * ($esurf/$press));
$vtemp1 = $vtemp - 273.15;

&ctof($vtemp1);
$vtempf = $F;

#Output time

$~ = VT1;
write;

format VT1 =
With an air temperature of @###.# degrees @<<<<<<<<<< a dewpoint
                           $vt{airtemp}   $vt{corf1}
temperature of @###.# degrees @<<<<<<<<<< and a station pressure of @###.##
               $vt{dewtemp}   $vt{corf2}                            $vt{press}
@<<<<<<<<<<<<<<<<<:
$vt{mborin}

You get a virtual temperature of @###.## degrees Fahrenheit or@###.## $vtempf $vtemp1 degrees Celsius.

. if ($vt{dewtemp} > $vt{airtemp}) { print "

The dewpoint temperature is greater than the air temperature, this only rarely occurs in the atmosphere. You may want to go back and double check your entries.

"; } print "The virtual temperature is a fictitious temperature that satisfies several meteorlogical equations. Basically the virtual temperature is the temperature dry air would have if its pressure and specific volume were equal to those of a similar sample of moist air. "; print "

"; print "Since water vapor is less dense than air, its presence always increases the specific volume. Hence the virtual temperature is always higher than the observed temperature.";