#!/usr/local/bin/perl
#--------------------
#
# Program Name:archive24.cgi
#
# Created by: Eric J. Horst
# Millersville University
#
#
# Activation Date: June 1998
# Modified April 1999
# Copyright 1998; all rights reserved.
#--------------------
#
# This program is designed to take weather data collected
# via RainWise equipment and archeive last 24-hours of hourly
# data. To be run by cron each day at midnight.
#
#--------------------IMPORTANT---------------------
# Two modifications need to be performed each year:
# 1.) create new year directory in Archive (ie. mkdir 2000)
# 2.) create new monthly subdirectories (ie. Jan, Feb...) in 2000 dir
#
#------
# 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;
chop($mospell = `date '+%b'`);
chop($month = `date '+%m'`);
chop($year = `date '+%Y'`);
chop($today = `date '+%d'`);
# OUTFILE names below need changed once per year
# ALSO, the directory in which the CGI runs must be increased for new year
if ($today == 1) {
if ($month == 1) {
$outfile = "31Dec98.html";
}
elsif ($month == 2) {
$outfile = "31Jan98.html";
}
elsif ($month == 3) {
$outfile = "28Feb98.html";
}
elsif ($month == 4) {
$outfile = "31Mar98.html";
}
elsif ($month == 5) {
$outfile = "30Apr98.html";
}
elsif ($month == 6) {
$outfile = "31May98.html";
}
elsif ($month == 7) {
$outfile = "30Jun98.html";
}
elsif ($month == 8) {
$outfile = "31Jul98.html";
}
elsif ($month == 9) {
$outfile = "31Aug98.html";
}
elsif ($month == 10) {
$outfile = "30Sep98.html";
}
elsif ($month == 11) {
$outfile = "31Oct98.html";
}
else {
$outfile = "30Nov98.html";
}
}
else {
$yday = $today - 1;
chop($outfile = `date '+$yday%b%y.html'`); # Setup output file
}
open(OUTPUT, "> archive/$year/$mospell/$outfile"); # output to proper dir
#------
# Print header info
#------
printf OUTPUT "\n
Hourly Observations at
MU\n";
printf OUTPUT "\n\n";
printf OUTPUT "\nM.U. Hourly Observations file:
\n\n";
printf OUTPUT "$outfile";
printf OUTPUT "\n";
printf OUTPUT "\n";
printf OUTPUT
"
| Date | Time | Temp. (oF) | Dew Point (oF) | Rel. Hum. (%) | Press. (in.) | Wind Dir. (deg.) | Wind Speed (mph) | 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 == "26") { # 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;
&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
printf OUTPUT "
\n\n\n";
printf OUTPUT "\n";
close OUTPUT;
`chmod 744 archive/$year/$mospell/$outfile`;
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_rain - $element_rain;
}
else {
$hrrain = "n/a";
}
# calculations for dewpoint
$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));
&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
printf OUTPUT "\n";
printf OUTPUT "%s | %s | %s | %s | %s | %s | %2.2f | %s | %s | %2.2f | \n
\n\n",
$base_obs48, $date, $time, $base_outtemp, $dew_point, $base_rh, $base_barometer, $base_winddir, $base_windspd, $hrrain;
} # End of print_data subroutine
sub missing_data {
printf OUTPUT "\n";
printf OUTPUT "$base_obs48 | miss | miss | miss | miss | miss | miss | miss | miss
| \n
\n\n";
} # End of missing_data subroutine