#!/usr/local/bin/perl
#--------------------
#
# Program Name: 48hour-spr.cgi
#
# IMPORTANT NOTE: There are three different 48hour.cgi propgrams. This
# version has no WIND CHILL or HEAT INDEX, and is for use in the spring
# and fall months. HTML code on the CWS Observation page and LNI (Lanc.
# Newspapers) page must be revised to point to this CGI.
#
# Created by: Steven Fierro & Laurie Know
#
# For use by the Meteorology Department of MU
#
# Activation Date: May 1997
#
# Revised: In 1998 by Eric Horst to handle wind chill and heat index.
#
# Modified 12/99 by David Fitzgerald to read data from the WLS8000
#
# Copyright 1997; all rights reserved.
#--------------------
#
# This program is designed to take weather data collected
# via RainWise equipment and software and display the last
# 48 hours through a WWW browser in tabular form.
#
#--------------------
#
#------
# Handle the web interface
#------
# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
# Un-Webify plus signs and %-encoding
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
#----- Here's where the fun begins -----#
# Variable initialization and program setup
#
@DATA = `tail -3001 /var/data/wls8000/weather.TXT | head -3000`;
$obs48 = 1;
$switch = "on";
$datamiss = "false";
$base = "";
# test variables
$dew_point = 1;
$wind_chill = 2;
#------
# Print header info
#------
print "\n";
print "\n";
print "\n";
print "\n";
print "\n\n";
print "\n
48 Hour Observations at MU\n";
print "\n\n";
print "\nHourly Observations
for Last 48 Hours
\n\n";
print "\n";
print "\n";
print "
| Date | Time | Temp. (oF) | Dew Point (oF) | Rel. Hum. (%) | Press. (in.) | Wind Dir.
| Wind Speed (mph) | Wind Chill | Hourly Rainfall (in.) | \n
\n\n";
@REVDATA = reverse(@DATA); # Reverse the order of the array
$counter = `date '+%H'`; # Start my counter based on current hour
chop($counter);
foreach $element (@REVDATA) {
if ($counter == "00") { # Keep with the 24 hour clock
$counter = 24;
}
if ($obs48 == "50") { # Total number of obs to take + 2
last;
}
if($element ne "\n"){
($placeholder, $time2, $date2, $winddir, $windspd, $solar, $intemp, $outtemp, $rh, $barometer, $rainday, $rainmon, $raintot) = split (/ +/,$element);
$ptend = substr($barometer, -1);
# strip off the units of each variable
substr($windspd, -3) = "";
chop $solar;
chop $intemp;
chop $outtemp;
chop $rh;
chop $barometer;
substr($rainday, -2) = "";
substr($rainmon, -2) = "";
substr($raintot, -4) = "";
# take year off the date
substr($date2, -3) = "";
($hour, $min) = split(/:/, $time2);
chomp($min);
#$hour2 = substr($hour,1);
$hour2 = $hour;
if($hour2 =="00"){$hour2=24;}
if ($hour2 == $counter) {
if ($min eq "00") { # Pick out only the hourly data; ignore the rest
&process_data();
$counter--;
$obs48++;
} # End of hourly data loop
} # End of hour=counter loop
else {
while ($counter - $hour2 >= 2) {
$datamiss = "true";
&process_data();
$counter--;
if ($counter == "00") {
$counter = 24;
}
$obs48++;
}
if ($min ge "55") {
&process_data();
$counter--;
if ($counter == "00") {
$counter = 24;
}
$obs48++;
}
else {
if ($datamiss eq "true") {
$base = "invalid";
&process_data();
}
else {
$datamiss = "true";
&process_data();
}
$counter--;
if ($counter == "00") {
$counter = 24;
}
$obs48++;
}
} # End of missing data line
} #end of blank data line
} # End of foreach loop
print "
\n\n\n";
print "\n";
sub process_data {
if ($base ne "miss") {
if ($switch eq "off") {
($placeholder, $element_time2, $element_date2, $element_winddir, $element_windspd, $element_solar, $element_intemp, $element_outtemp, $element_rh, $element_barometer, $element_rainday, $element_rainmon, $element_raintot) = split(/ +/, $element);
substr($element_windspd, -3) = "";
chop $element_solar;
chop $element_intemp;
chop $element_outtemp;
chop $element_rh;
chop $element_barometer;
substr($element_rainday, -2) = "";
substr($element_rainmon, -2) = "";
substr($element_raintot, -4) = "";
substr($element_date2, -3) = "";
($placeholder, $base_time2, $base_date2, $base_winddir, $base_windspd, $base_solar, $base_intemp, $base_outtemp, $base_rh,$base_barometer, $base_rainday, $base_rainmon, $base_raintot) = split(/ +/, $base);
substr($base_windspd, -3) = "";
chop $base_solar;
chop $base_intemp;
chop $base_outtemp;
chop $base_rh;
chop $base_barometer;
substr($base_rainday, -2) = "";
substr($base_rainmon, -2) = "";
substr($base_raintot, -4) = "";
substr($base_date2, -3) = "";
if ($datamiss eq "false") {
$hrrain = $base_rainday - $element_rainday;
}
else {
$hrrain = "n/a";
}
$constant = 461.51;
$temp_kel = (($base_outtemp - 32) * 5/9)+273.15;
$lat_heat = (2.5019 - (.00254 * ($temp_kel - 273.15))) * 10**6;
$terma_value = $temp_kel * $constant/$lat_heat;
$termb_value = -1 * (log($base_rh/100));
$dew_kel = $temp_kel/(($terma_value * $termb_value) + 1);
$dew_point = int(($dew_kel - 273.15) * 9/5 + 32);
# calculations for wind chill
if($base_windspd < 4)
{
$K_value = 4096;
}
elsif($base_windspd >41)
{
$K_value = 6480;
}
else
{
$K_value = 2670.13458+(1047.3735*(log($base_windspd)));
}
$denom = (91.4 - $base_outtemp) * $K_value;
$wind_chill = int(91.4 - ($denom/4096));
if($outtemp < 50)
{
$wind_chill = $wind_chill;
}
else
{
$wind_chill = " ** ";
}
&print_data();
$base_obs48 = $obs48;
if ($datamiss eq "true") {
$base = "invalid";
$switch = on;
}
else {
$base = $element;
}
}
else { # Switch is "ON"
if ($datamiss eq "true") {
if ($base eq "invalid") {
&missing_data();
$base_obs48 = $obs48;
$datamiss = "false";
$base = $element;
$switch = off;
}
else {
$base = "invalid";
$base_obs48 = $obs48;
}
}
elsif ($datamiss eq "false" && $base eq "invalid") {
&missing_data();
$base_obs48 = $obs48;
$base = $element;
$switch = off;
}
else {
$base_obs48 = $obs48;
$base = $element;
$switch = off;
}
}
} # End of $base ne miss
else { # $base=miss
&missing_data();
$switch = on;
}
} # End of process_data subroutine
sub print_data {
# chop($base_rh); # Get rid of annoying ^M
# chop($base_rh); # Get rid of annoying ^N
# chop($time = substr($base_time2,1)); # Get rid of "" on the time
# chop($date = substr($base_date2,1)); # Get rid of "" on the date
$date = $base_date2;
$time = $base_time2;
# make temp. dewpoint, rh, and wind speed integers to get rid of leading 0 if below 100.
$base_outtemp = int($base_outtemp);
$base_rh = int($base_rh);
$dewpoint = int($dewpoint);
$base_windspd = int($base_windspd);
print "\n";
printf "%s | %s | %s | %s | %s | %s | %2.2f | %s | %s | %s | %2.2f | \n
\n\n",$base_obs48, $date, $time, $base_outtemp, $dew_point, $base_rh,$base_barometer, $base_winddir, $base_windspd,$wind_chill, $hrrain;
#print "DATA = $base_obs48 $date $time $base_outtemp $dew_point $base_rh $base_barometer $base_winddir $base_windspd $wind_chill $hrrain\n";
} # End of print_data subroutine
sub missing_data {
#-> print "\n";
#-> print "$base_obs48 | miss | miss | miss | miss | miss | miss | miss | miss
print "missing data\n";
#-> | \n
\n\n";
} # End of missing_data subroutine