#densityalt.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 ($da{airtemp} =~ /-?(\d+)\.?(\d+)|-?(\d+)/)  {
  if ($da{corf1} eq Fahrenheit) {
   &ftoc($da{airtemp});
   $temp = $C;
} else {
   $temp = $da{airtemp};
  }
} else {
   print "An illegal character was entered.\n";
   die
}

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

if ($da{press} =~ /(\d+)\.?(\d+)/)  {
 if ($da{mborin} eq millibars) {
   $press = $da{press};
} else {
   $press = $da{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));

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

$da = 145366 * (1 - ((.28424 * $press)/$vtemp)**.235);
$dam = $da * .3048;

#Output time

$~ = DA1;
write;

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

You get a density altitude of @####.## feet or @####.## meters $da $dam

. print "Density altitude is a meteorological variable that is important to pilots, especially during the summer. The density altitude is the altitude in a standard atmosphere where the density is the same as the given atmospheric density. During a hot muggy summer day a pilot begins to take her plane off at an airport that is at an elevation of 2500 feet, but because of the warm temperature and the moisture in the air, the airplane has to work as if it was taken off at an airport at an elevation of 6000 feet. The result is that the plane needs more power and a longer roll down the runway to take off.";