#!/usr/bin/ksh
#
	version="11.1.0"

# This script will populate the mySQL Weather DB from History files
# using the "LoadWeatherData" Perl script. The history files it 
# looks for are:
# 
# BaroPresFile.ArcData      - see the end of this script for format
# RainByMinutesData.ArcData - see the end of this script for format
# TempHumidFile.ArcData     - see the end of this script for format
# LastWindDirFile.ArcData   - see the end of this script for format
# rtdata = Standard output from vproweater's -x option
# wxdata = Standard output from vproweater's -g option
#
# NOTE: if you have the realtime and graph output in one file you
# can change the *FN names below to the one, data won't load twice.
#
# Located in the $ArchDir archirvce location with the format
# /Weather/Archives/<year>/<month>/<day>
# where:
#        <year>  is the 4 digit year
#        <month> is the 2 digit month with leading 0 (zero)
#        <day>   is the 2 digit day with leading 0 (zero)
#
# $years is the list of years to process, space delimitated
# $month is the months to process in wildcard format
# $day   is the days to process in wildcard format
#
# $DST is a list of dates when Daylight Savings Time occurs
#      and only those dates when a hour is lost should be
#      included in the list
#

ArchDir="/data00/Weather/Archives"

years="2004 2005 2006 2007 2008 2009 2010 2011"
month=\?\?
day=\?\?

years="2011"
month="03"
day=13

BaroArcFN="BaroPresFile.ArcData"
RainArcFN="RainByMinutesData.ArcData"
TempArcFN="TempHumidFile.ArcData"
WindArcFN="LastWindDirFile.ArcData"
rtdataFN="rtdata"
wxdataFN="wxdata"

DST=" 20101107 20091101 20081102 20071104 20061029 20051030"

for year in $years
do
	echo
	echo "Starting $year"

	for dir in `ls -ald $ArchDir/$year/$month/$day | grep ^d | awk '{print $NF}'`
	do
		echo
		echo -n "LOADING: `date` $dir"
		Bfile=""
		Rfile=""
		Tfile=""
		Wfile=""
		rFile=""
		wFile=""
		dirYMD=`echo $dir | sed -e 's/\/$//' | awk -F/ '{print $(NF-2)$(NF-1)$NF}'`
		TS="-t"
		if [[ -n `echo $DST | grep $dirYMD` ]]
		then
			TS="-s"
			echo -n " \"DST\""
		fi

		if [[ -e $dir/$BaroArcFN ]]
		then
			Bfile="-B $dir/$BaroArcFN"
			echo -n " Baro"
		fi
		if [[ -e $dir/$RainArcFN ]]
		then
			Rfile="-R $dir/$RainArcFN"
			echo -n " Rain"
		fi
		if [[ -e $dir/$TempArcFN ]]
		then
			Tfile="-T $dir/$TempArcFN"
			echo -n " Temp"
		fi
		if [[ -e $dir/$WindArcFN ]]
		then
			Wfile="-W $dir/$WindArcFN"
			echo -n " Wind"
		fi
		if [[ -e $dir/$rtdataFN ]]
		then
			rFile="-r $dir/$rtdataFN"
			echo -n " rtdata"
		fi
		if [[ -e $dir/$wxdataFN ]]
		then
			wFile="-w $dir/$wxdataFN"
			echo -n " wxdata"
		fi
		echo
                if [[ -n $Bfile ]] || [[ -n $Rfile ]] || [[ -n $Tfile ]] || [[ -n $Wfile ]] || [[ -n $rFile ]] || [[ -n $wFile ]]
		then
			./LoadWeatherData $TS $Bfile $Rfile $Tfile $Wfile $rFile $wFile
        		echo "LOADING: `date` COMPLETE"
		else
			echo "LOADING: `date` ABORTED"
		fi
	done
done

#

#
# Data File Formats
# NOTE: All lines start with the Date/Time with no white space at the beginning
#
# File: "rtdata"
#
# Normal output from the Real Time data -x option. However a timestamp is
# needed in the file of the format:
#
# CaptureTimestamp = Fri Jan 16 23:59:22 CST 2009
# Or:
# CaptureTimestamp = Saturday January 16 2010 23:59
# Or:
# rtCaptureTimestamp = 2011-03-17 12:22:37 (1300382557)
#
# File: "wxdata"
#
# Normal output from the Graph data -g option. However a timestamp is
# needed in the file of the format:
#
# CaptureTimestamp = Saturday January 16 2010 23:59
# Or:
# CaptureTimestamp = Saturday January 16 2010 23:59
# Or:
# grCaptureTimestamp = 2011-03-17 12:22:37 (1300382557)
#
# Rain Storms, and other Hourly data is loaded from the wxdata file containing
# the vProWeather Graph Data in its native format.
#
# File: "BaroPresFile.ArcData"
#
# 20090519-00:45  30.089  baro_s  Steady  [OK]
# 20090519-01:00  30.091  baro_s  Steady  [OK]
# 20090519-01:15  30.086  baro_fs Falling-Slowly  [OK]
# 20090519-01:30  30.082  baro_fs Falling-Slowly  [OK]
#
# Column 1 = Date/Time stamp
# Column 2 = Barometric Pressure
# Column 3 = Barometric Trend Abbreviation
# Column 4 = Barometric
# Column 5 = [OK] (Indicates this row of data is valid will be ignored otherwise)
#
# File: "RainByMinutesData.ArcData"
#
# 20090711-05:18    0.09    0.09    0.01    0.32    yes     [OK]
# 20090711-05:19    0.11    0.11    0.02    1.76    yes     [OK]
# 20090711-05:20    0.12    0.12    0.01    0.64    yes     [OK]
# 20090711-05:21    0.13    0.13    0.01    0.60    yes     [OK]
#
# Column 1 = Date/Time stamp
# Column 2 = Daily Rain Amount
# Column 3 = Rain Storm Total
# Column 4 = Current Rain Amount
# Column 5 = Rain Rate
# Column 6 = Is Raining (yes/no) 
# Column 7 = [OK] (Indicates this row of data is valid will be ignored otherwise)
#
# File: "LastWindDirFile.ArcData" 
#
# 20090519-00:06  182     1       S       [OK]
# 20090519-00:07  205     0       SSW     [OK]
# 20090519-00:08  202     0       SSW     [OK]
# 20090519-00:09  193     2       SSW     [OK]
# 
# Column 1 = Date/Time stamp
# Column 2 = Wind Direction in degrees
# Column 3 = Wind Speed
# Column 4 = Wind Compass Direction
# Column 5 = [OK] (Indicates this row of data is valid will be ignored otherwise)

# File: "TempHumidFile.ArcData"
#
# 20090519-00:04  72.8    26      55.9    48      55.9    55.9    0       0.001   6       44      [OK]
# 20090519-00:05  72.8    26      55.9    48      55.9    55.9    0       0.001   6       44      [OK]
# 20090519-00:06  72.8    26      55.7    48      55.7    55.7    0       0.001   6       44      [OK]
# 20090519-00:07  72.8    26      55.7    48      55.7    55.7    0       0.001   6       44      [OK]
#
# Column  1 = Date/Time stamp
# Column  2 = Indoor Temperature
# Column  3 = Indoor Humidity
# Column  4 = Outdoor Temperature
# Column  5 = Outdoor Humidity
# Column  6 = Windchill Temperature
# Column  7 = Heat Index
# Column  8 = Solar Radiation
# Column  9 = Evapotranspiration
# Column 10 = Forecast Icon Number
# Column 11 = Forecast Rule Number
# Column 12 = [OK] (Indicates this row of data is valid will be ignored otherwise)
#
#
 
