#vaporpress.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 

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

if ($vp{airtemp} =~ /-?(\d+)\.?(\d+)|-?(\d+)/) {
  if ($vp{corf1} eq Fahrenheit) {
  &ftoc($vp{airtemp}); 
  $temp = $C;
} else {
  $temp = $vp{airtemp};
  }
} elsif ($vp{airtemp} eq //) {
  $temp = null;
} else {
 print "An illegal character was entered.\n";
 die
}

#Start the math

if ($temp !~ null) {
  &ctok($temp);
  $ktemp = $K;

  &vappress($ktemp);
  $etemp = $E;
  $etemp1 = $etemp * .0295300;
}
if ($dewp !~ null) {
  &ctok($dewp);
  $kdewp = $K;

  &vappress($kdewp);
  $edewp = $E;
  $edewp1 = $edewp * .0295300;
}
if ($temp =~ null) {
  $~ = VP1;
  write;
} elsif ($dewp =~ null) {
  $~ = VP3;
  write;
} else {
  $rh = 100*($edewp/$etemp);
  $~ = VP5;
  write;
  }



#let's make some pretty output
format VP1 =
With a dewpoint temperature of @###.# degrees @<<<<<<<<<<:
                               $vp{dewtemp}   $vp{corf2}

You get an actual vapor pressure of @###.## millibars or @.#### $edewp $edewp1 inches of Mercury over water. . format VP3 = With an air temperature of @###.# degrees @<<<<<<<<<: $vp{airtemp} $vp{corf1}

You get a saturated vapor pressure of @###.## millibars or @.#### $etemp $etemp1 inches of Mercury over water. . format VP5 = With an air temperature of @###.# degrees @<<<<<<<<< and a dewpoint of $vp{airtemp} $vp{corf1} @###.# degrees @<<<<<<<<<: $vp{dewtemp} $vp{corf2}

You get a vapor pressure of @###.## millibars or @.#### inches of $edewp $edewp1 Mercury and a saturated vapor pressure of @###.## millibars or $etemp @.#### inches of Mercury all over water. You also get a relative $etemp1 humidity of @##.# percent. $rh . if ($vp{dewtemp} > $vp{airtemp}) { if ($temp =~ null) { } else { 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.

"; } }