#dewpoint.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 thetae {
local($ktemp,$press,$mixr1,$klcl) = @_;
$thetae1 = $ktemp * (1000/$press)**(($Rd/$cp)*(1 - .28 * $mixr1)) *
exp((3376/$klcl - 2.54) * $mixr1 * (1 + .81 * $mixr1));
return ($thetae1);
}
#change entries to celsius and millibars
if ($dp{airtemp} =~ /-?(\d+)\.?(\d+)|-?(\d+)/) {
if ($dp{corf1} eq Fahrenheit) {
&ftoc($dp{airtemp});
$temp = $C;
} else {
$temp = $dp{airtemp};
}
} else {
print "An illegal character was entered.\n";
die
}
if ($dp{dewtemp} =~ /-?(\d+)\.?(\d+)|-?(\d+)/) {
if ($dp{corf2} eq Fahrenheit) {
&ftoc($dp{dewtemp});
$dewp = $C;
} else {
$dewp = $dp{dewtemp};
}
} else {
print "An illegal character was entered.\n";
die
}
if ($dp{press} =~ /(\d+)\.?(\d+)/) {
if ($dp{mborin} eq millibars) {
$press = $dp{press};
} else {
$press = $dp{press} * 33.8639;
}
} else {
print "An illegal character was entered.\n";
die
}
#calculate the TLCL and PLCL
$tlcl = $dewp - (.212 + .001571 * $dewp - .000436 * $temp) * ($temp -
$dewp);
&ctok($tlcl);
$klcl = $K;
&ctok($temp);
$ktemp = $K;
$plcl = $press * ($klcl/$ktemp)**(1/($Rd/$cp));
#calculate the vapor press and mixing ratio for the surface parcel
&ctok($dewp);
$kdewp = $K;
&vappress($ktemp);
$esat = $E;
&vappress($kdewp);
$esurf = $E;
$mixr = 621.97 * $esurf/($press - $esurf);
#calculate the potential and equivalent potential temperatures
#for the surface parcel
$theta = $ktemp * (1000/$press)**($Rd/$cp);
$mixr1 = $mixr/1000;
&thetae($ktemp,$press,$mixr1,$klcl);
$thetae = $thetae1;
#let's start the guessing game
$tguess = 0;
$previsign = 1;
$incr = 10;
$thetaediff = 1;
$estthetae = 1;
while (abs($thetaediff) > .05) {
&ctok($tguess);
$ktguess = $K;
$esttheta = $ktguess * (1000/$press)**($Rd/$cp);
&vappress($ktguess);
$eguess = $E;
$mixrguess = 621.97 * $eguess/($press - $eguess);
$mixrguess1 = $mixrguess/1000;
&thetae($ktguess,$press,$mixrguess1,$klcl);
$estthetae = $thetae1;
$thetaediff = $thetae - $estthetae;
$cursign = $thetae <=> $estthetae;
if ($cursign != $prevsign) {
$prevsign = $cursign;
$incr = $incr/2;
}
$tguess = $tguess + $incr * $prevsign;
}
#calculate the relative humidity
$rh = ($esurf/$esat) * 100;
#let's make some pretty output
$~ = DP1;
write;
format DP1 =
With an air temperature of @###.# degrees @<<<<<<<<<< a dewpoint
$dp{airtemp} $dp{corf1}
temperature of @###.# degrees @<<<<<<<<<< and a station pressure of
$dp{dewtemp} $dp{corf2}
@###.## @<<<<<<<<<<<<<<<<<:
$dp{press}$dp{mborin}
.
$~ = DP5;
write;
format DP5 =
You get a relative humidity of @##.# percent and a wet-bulb temperature of
$rh
.
if ($dp{corf1} eq Fahrenheit) {
&ctof($tguess);
$tguessf = $F;
$~ = DP2;
write;
} else {
$~ = DP3;
write;
}
format DP2 =
@###.# degrees Fahrenheit.
$tguessf
.
format DP3 =
@###.# degrees Celsius.
$tguess
.
if ($dp{dewtemp} > $dp{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.
";
}