#!/usr/bin/perl #-------------------- # # Program Name: dailysum.cgi # # Created by: Steven Fierro # Unix and Networking Specialist # Millersville University # # For use by the Meteorology Department of MU # # Activation Date: April 1997 # Revised: April 30, 1997 # May 02, 1997 # May 05, 1997 # May 09, 1997 # May 12, 1997 # May 16, 1997 # June 02, 1997 # June 03, 1997 # June 04, 1997 # June 11, 1997 # June 23, 1997 # February 1, 1999 -dbf # January 3, 2005 -dbf # Updated for new web server # # Copyright 1997; all rights reserved. #-------------------- # # This program is designed to take weather data collected # via RainWise equipment and software and create a daily # summary for display through a WWW browser in tabular form. # This program will run via cron, but can be run at anytime. # # Due to the need to deal with the local Lancaster newspaper, # two copies of this program have been created. This one is # used for the mid-mid max and min, the other is used to # allow the paper to get the info in time for them to go to # press. # # This program will be run just after midnight. # # modified 12/99 by david fitzgerald to use the wls8000 weather data #-------------------- #------ Here's where the fun begins ------# use Date::Manip; #------ # Variable initialization #------ #@DATA = `tail -2500 /var/data/tower/weather.dat`; # Obtain the data # have to skip last line in wls8000 data file as it is often incomplete to to buffereing @DATA = `tail -2501 /data/wls8000/weather.TXT | head -2500`; $start = ""; # used to obtain starting rain value. $last = ""; # used to obtain ending rain value. $basemax = -99; # used as base to determine max temp. $basemin = 99; # used as base to determine min temp. $basewind = 0; # used as base to determine peak wind speed. $today = `date +%Y%m%d`; chop($year = `date '+%Y'`); # Determine the current year chop($monthnum = `date '+%m'`); # Determine the current month number chop($testday = `date '+%d'`); # Determine the current day of the month # Array used to obtain the month name for year-to-date max/min temps %MONTHS = ( '01', 'January', '02', 'February', '03', 'March', '04', 'April', '05', 'May', '06', 'June', '07', 'July', '08', 'August', '09', 'September', '10', 'October', '11', 'November', '12', 'December', ); #------ # Since this program gets run the next day; we need to determine the date # of the day before #------ #if ($testday == 1) { # if ($monthnum eq "04" || $monthnum eq "06" || $monthnum eq "09" || $monthnum eq "11") { # $day = 30; # } # if ($monthnum eq "02") { # $day = 28; # } # else { # $day = 31; # } #} # modified 990201 for logic errors in determining previous days daynumber # since this script is run the day AFTER the day the data is for. -dbf #if ($testday == 1) { # if the previous day was in Feb and we have to figure out if it was leap year. # if($monthnum eq "03") { # $rem = $year%4; # yes its leap year and previous day was the 29th. # if($rem == 0) { # $day = 29; # } # no its not leap year and previous day was the 28th. # else { # $day = 28; # } # } # elsif($monthnum eq "05" || $monthnum eq "07" || $monthnum eq "10" || $monthnum eq "12") { # in this case the previous day was in either April, June, Sept, or November. # $day = 30; # } # else { # $day = 31; # } #} # if not the first day of the month we can simply subtract one from day. #else { # $day = $testday - 1; #} $yesterday = DateCalc($today,"-1 day",\$err); #------ # Since we are using the date before the current; we need to account for # when the month changes as well. #------ #if ($testday == 1) { # if ($monthnum == 1) { # $monthnum = 12; # } # else { # $monthnum--; # if ($monthnum < 10) { # $monthnum = "0$monthnum"; # } # } #} $day = substr($yesterday,6,2); $monthnum = substr($yesterday,4,2); #------ # Now that we figured out what day, month, etc to use lets prepare the files #------ chop($rainfile = `date '+$monthnum%y-cumrain.txt'`); # Determine rainfall file chop($yearfile = `date '+%Y-cumrain.txt'`); # Determine yearly rain file chop($outfile = `date '+$monthnum%y.html'`); # Setup output file chop($yearrain = `cat $yearfile.tmp`); # Get the current yearly rainfall chop($cumrain = `cat $rainfile.tmp`); # Get the current cumulative rainfall chop($infomax = `cat $year-max.txt.tmp`); # Get year to date max info ($yrmaxtemp, $yrmaxmon, $yrmaxday) = split(/ +/, $infomax); chop($infomin = `cat $year-min.txt.tmp`); # Get year to date min info ($yrmintemp, $yrminmon, $yrminday) = split(/ +/, $infomin); open(OUTPUT, ">> $outfile.tmp"); # Prepare filehandle for output #------ # Scan through @DATA and process the info for $day #------ foreach $element (@DATA) { @line = split(" ", $element); $numfields = scalar @line; if( $numfields eq 12 ) { # skip lines with bad data ($placeholder, $time, $date, $winddir, $windspd, $solar, $intemp, $outtemp, $rh, $barometer, $rainday, $rainmon, $raintot) = split(/ +/, $element); # strip off units chop $solar; chop $intemp; chop $outtemp; chop $rh; chop $barometer; substr($windspd, -3) = ""; # make integers of the temperature and wind speed data $outtemp = int($outtemp); $windspd = int($windspd); # strip off junk at end of rain data substr($rainday, -2) = ""; substr($rainmon, -2) = ""; substr($raintot, -4) = ""; # strip off year from the date substr($date, -3) = ""; #$time2 = substr($time,1); # Get rid of leading " from time #$date2 = substr($date,1); # Get rid of leading " from date $time2 = $time; $date2 = $date; # put daily rain info in variable used in original script. $rain = $rainday; ($mon, $d) = split('/', $date2); # Used to look only at the if ($mon == $monthnum && $d == $day) { # data in @DATA that matches if ($start eq "") { # $day. Then find the $start = $rain; # max/min/peak values. } &correctdata(); $last = $rain; } } # end if bad line } # End of foreach loop $dailyrain = $last - $start; # Calculate daily rainfall $monthrain = $cumrain + $dailyrain; # Calculate monthly rainfall $yearrain = $yearrain + $dailyrain; # Calculate yearly rainfall open(RAIN, "> $rainfile.tmp"); # Keep track of the monthly rainfall printf RAIN "%2.2f\n", $monthrain; # in a file for future use close RAIN; open(YEAR, "> $yearfile.tmp"); # Keep track of the yearly rainfall printf YEAR "%2.2f\n", $yearrain; # in a file for future use close YEAR; #------ # Verify if the new max/min values are greater/lesser than yearly total # to date. #------ if ($basemax > $yrmaxtemp) { $yrmaxtemp = $basemax; $yrmaxmon = $MONTHS{$monthnum}; $yrmaxday = $day; open(YRMAX, "> $year-max.txt.tmp"); print YRMAX "$yrmaxtemp $yrmaxmon $yrmaxday\n"; close YRMAX; } if ($basemin < $yrmintemp) { $yrmintemp = $basemin; $yrminmon = $MONTHS{$monthnum}; $yrminday = $day; open(YRMIN, "> $year-min.txt.tmp"); print YRMIN "$yrmintemp $yrminmon $yrminday\n"; close YRMIN; } #------ # Store the aquired information to $outfile #------ chomp $monthnum; chomp $day; chomp $basemax; chomp $maxtime; chomp $basemin; chomp $mintime; chomp $basewdir; chomp $wintime; chomp $basewind; chomp $dailyrain; printf OUTPUT "\n$monthnum/$day$basemax
($maxtime)$basemin
($mintime)$basewdir
($wintime)$basewind
($wintime)%2.2f \n\n", $dailyrain; #------ # Do this if the current day ($testday) is the first of the month #------ if ($testday == 1) { final_html(); head_html(); } #------ # Subroutine to use to determine the max and min temps and peak wind speed # for valid data (i.e. data from @DATA that matches is for $day. #------ sub correctdata { if ($outtemp > $basemax) { $basemax = $outtemp; $maxtime = $time2; } if ($outtemp < $basemin) { $basemin = $outtemp; $mintime = $time2; } if ($windspd > $basewind) { $basewind = $windspd; $basewdir = $winddir; $wintime = $time2; } } # End of subroutine #------ # Subroutine used to close out $outfile: mmyy.html #------ sub final_html { printf OUTPUT "\nTotal
Rainfall:
%2.2f\n\n\n", $monthrain; print OUTPUT "\n\n\n"; print OUTPUT "

\n"; printf OUTPUT "
\nYear-to-date rainfall: %2.2f in.\n
\n\n", $yearrain; print OUTPUT "\n"; } # End of subroutine #------ # Subroutine to prepare the new output files #------ sub head_html { chop($month = `date '+%B'`); # Determine the current month chop($prepfile = `date '+%m%y.html'`); # Setup output file chop($newrainfile = `date '+%m%y-cumrain.txt'`); # Determine rainfall file open(PREPOUT, "> $prepfile"); print PREPOUT "\n"; print PREPOUT "\n"; print PREPOUT "\n"; print PREPOUT "\n"; print PREPOUT "\n\n"; print PREPOUT "\nMonthly Summation of Surface Obs at MU\n\n\n"; print PREPOUT "
\n

Daily Summaries for
$month $year

\n\n"; print PREPOUT "\n"; print PREPOUT "\n"; print PREPOUT "\n"; print PREPOUT "\n\n\n"; close PREPOUT; `cp -p $prepfile $prepfile.tmp`; `echo 0.00 $newrainfile`; # Prepare the new monthly rainfall file `echo 0.00 $newrainfile.tmp`; # Prepare the new monthly rainfall.tmp file } # End of subroutine #------ # Final procedures done everyday #------ close OUTPUT; `cp -p $rainfile.tmp $rainfile`; `cp -p $outfile.tmp $outfile`; `cp -p $yearfile.tmp $yearfile`; `cp -p $year-max.txt.tmp $year-max.txt`; `cp -p $year-min.txt.tmp $year-min.txt`; #$date = `date +%Y%m%d%H%M`; #open OUT, ">finishedmsg"; #print OUT "Dailysum run finished on $date"; #$host = `hostname`; #print OUT "Hostname is $host\n"; #$processes = `ps -ef`; #print OUT "process snapshot is:\n"; #print OUT "$processes\n"; #close OUT; #$cmd = "/bin/mail -s 'Dailysum output' david.fitzgerald\@millersville.edu < finishedmsg"; #system($cmd);
\n"; print PREPOUT "All values are calculated midnight to midnight unless \n"; print PREPOUT "otherwise noted. The times in
( )s are the times when \n"; print PREPOUT "the value occurred.\n"; print PREPOUT "
DateMax Temp.
(oF)
Min Temp.
(oF)
Dir. of
Peak Wind
(deg.)
Peak Wind
Speed
(mph)
Daily
Rainfall
(in.)