#mixratio.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);
}
sub mixratio {
  local($etemp,$press) = @_;
  $mixr = 621.97 * $etemp/($press - $etemp);
  return ($mixr);
}

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


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


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

#Start the math

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

  &vappress($ktemp);
  $etemp = $E;

  &mixratio($etemp,$press);
  $smixr = $mixr;  
}

if ($dewp !~ null) {
  &ctok($dewp);
  $kdewp = $K;

  &vappress($kdewp);
  $edewp = $E;

  &mixratio($edewp,$press);
  $mixr = $mixr;
}

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 @<<<<<<<<<< and a station
                               $mr{dewtemp}   $mr{corf2}
pressure of @###.## @<<<<<<<<<<<<<<<<<:
        $mr{press}  $mr{mborin}

You get an actual mixing ratio of @##.## grams per kilograms. $mixr . format VP3 = With an air temperature of @###.# degrees @<<<<<<<<< and a station $mr{airtemp} $mr{corf1} pressure of @###.## @<<<<<<<<<<<<<<<<<: $mr{press} $mr{mborin}

You get a saturated mixing ratio of @##.## grams per kilograms. $smixr . format VP5 = With an air temperature of @###.# degrees @<<<<<<<<< a dewpoint of $mr{airtemp} $mr{corf1} @###.# degrees @<<<<<<<<< and a station pressure of @###.## $mr{dewtemp} $mr{corf2} $mr{press} @<<<<<<<<<<<<<<<<<: $mr{mborin}

You get an actual mixing ratio of @##.## and a saturated mixing ratio of $mixr @##.##, both grams per kilogram. You also get a relative humidity of $smixr @##.# percent. $rh . if ($mr{dewtemp} > $mr{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.

"; } }