#!/usr/bin/perl
#
        $Version = "11.1-0l";

# Now enforcing [OK] at the end of Baro, Temp, Rain, Wind, & CSV file

use File::Basename;
use Getopt::Std;
use DBI;
use Time::Local;
use Date::Calc qw(Add_Delta_Days);
use Date::Calc qw(Add_Delta_DHMS);
use Date::Calc qw(Day_of_Week);

$user = "root";
$password = "ReplaceWithMyPW";
$DBhost = "localhost";
$DBname = "Weather";

@sawTimes = ();
$LastdateSQL = "";

$true = 1;
$false = 0;

$CMD = basename($0);

our( $opt_B, $opt_C, $opt_q, $opt_R, $opt_r, $opt_s, $opt_T, $opt_t, $opt_V, $opt_v, $opt_W, $opt_w);

$OPTS = "B:C:qR:r:sT:tVvW:w:";

getopts($OPTS);

$verbose = $opt_v;
$SQLverbose = $opt_V;
$quiet =  ! $opt_q;
$Separate = $opt_s;	# a Daylight Savings Time day so do things separate
$Together = $opt_t;     # not a Daylight Savings Time day so do things together
if ($Separate && $Together) {
    print " s & t mutually exclusive\n";
    exit;
}
if (! $Separate && ! $Together) {
    print " need -s or -t\n";
    exit;
}

$useCSV = $false;

$BaroFile = $opt_B;
$RainFile = $opt_R;
$TempFile = $opt_T;
$WindFile = $opt_W;
$CSVFile = $opt_C;
if ($CSVFile ne "") { $useCSV = $true; }

$rtdataFile = $opt_r;
$wxdataFile = $opt_w;

$month{ "JAN" } = "01";
$month{ "FEB" } = "02";
$month{ "MAR" } = "03";
$month{ "APR" } = "04";
$month{ "MAY" } = "05";
$month{ "JUN" } = "06";
$month{ "JUL" } = "07";
$month{ "AUG" } = "08";
$month{ "SEP" } = "09";
$month{ "OCT" } = "10";
$month{ "NOV" } = "11";
$month{ "DEC" } = "12";

$day{ "1" } = "Monday";
$day{ "2" } = "Tuesday";
$day{ "3" } = "Wednesday";
$day{ "4" } = "Thursday";
$day{ "5" } = "Friday";
$day{ "6" } = "Saturday";
$day{ "7" } = "Sunday";

sub by_numbers {
    $a <=> $b;
}

sub by_numeric {
    ($a <=> $b) || ($a cmp $b);
}

sub by_names {
    return $SortData{$a} cmp $SortData{$b};
}

sub by_count {
    return $SortData{$a} <=> $SortData{$b};
}

sub cvtTime {
    my ($time) = @_;
    my $hour;
    my $minute;
    if ($time =~ /AM/) {
        $time =~ s/AM//;
        ($hour, $minute) = split /:/, $time;
        if ($hour == 12) { $hour = 0; }
    }
    if ($time =~ /PM/) {
        $time =~ s/PM//;
        ($hour, $minute) = split /:/, $time;
        if ($hour != 12) { $hour = $hour + 12; }
    }
    $time = sprintf "%02i:%02i", $hour, $minute;
    return $time;
}

sub parseDateTime {
    my ($time) = @_;
    if ($time =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/) {
        my $year = $1;
        my $month = $2;
        my $day = $3;
        my $hour = $4;
        my $minute = $5;
        my $second = "00";
        my $timeSQL = "$year-$month-$day $hour:$minute:$second";
        my $date = "$year$month$day";
        if ($times{ "$time" } eq "") {
            $times{ "$time" } = "$timeSQL,$year,$month,$day,$hour,$minute,$second";
            @chkDates = grep(/$time/, @times);
            if ($chkDates[0] eq "") { push @times, $time; }
            @chkDates = grep(/$date/, @dates);
            if ($chkDates[0] eq "") { push @dates, $date; }
        }
        return "$time";
    }
    return "";
}


sub calcHeatIndex {
    my ($Temperature, $RelHumidity) = @_;

    my $HeatIndex;

    if ($Temperature < 80.0) {
        return $Temperature
    }
    $HeatIndex = -42.379 + (2.04901523*$Temperature) + (10.14333127*$RelHumidity) - (0.22475541*$Temperature*$RelHumidity) - ((6.83783*10**-3)*($Temperature**2)) - ((5.481717*10**-2)*($RelHumidity**2)) + ((1.22874*10**-3)*($Temperature**2)*$RelHumidity) + ((8.5282*10**-4)*$Temperature*($RelHumidity**2)) - ((1.99*10**-6)*($Temperature**2)*($RelHumidity**2));

    if ($HeatIndex < $Temperature) {
        return $Temperature;
    } else {
        return $HeatIndex;
    }
}

sub calcWindChill {
    my ($Temperature, $WindSpeed) = @_;

    my $WindChill;

    if ($WindSpeed != 0) {
        $WindChill = 35.74 + (0.6215*$Temperature) - (35.75*($WindSpeed**0.16)) + (0.4275*$Temperature*($WindSpeed**0.16));
    } else {
        $WindChill = $Temperature;
    }
    if ($WindChill > $Temperature) {
        return $Temperature;
    } else {
        return $WindChill;
    }
}

@Baro = ();
@Wind = ();
@Rain = ();
@TempHum = ();
@Forecast = ();

if (! $useCSV && $BaroFile ne "" && -e $BaroFile) {
    @FN = split /\//, $BaroFile;
    if (! $quiet) { if ($verbose) { print STDERR "reading $FN[$#FN]... "; } else { print STDERR "."; } }
    open(INFILE, "<$BaroFile") || die("ERROR! Can't open infile: $BaroFile\n");
    @Data = <INFILE>;
    close(INFILE);
    $LineNum = 0;
    foreach $line (@Data) {
        chomp($line);
        $LineNum++;
        if ($line =~ /[OK]/) {
            $line =~ s/\s+\[OK\]//i;
            ($time, $BP, $BTI, $BT, $BT2) = split /\s+/, $line;
            if (($time = parseDateTime( $time )) ne "") {
                if ($BT2 eq "") {
                    $Baro{ "$time" } = "$BP,$BTI,$BT";
                    push @Baro, "$time,$BP,$BTI,$BT";
                } else {
                    $Baro{ "$time" } = "$BP,$BTI,$BT $BT2";
                    push @Baro, "$time,$BP,$BTI,$BT $BT2";
                }
            } else {
                print STDERR "\nERROR! $BaroFile error parsing $line\n";
            }
        } else {
            print STDERR "WARN: $BaroFile [OK] not found discarding line $LineNum\n";
        }
    }
}

if (! $useCSV && $WindFile ne "" && -e $WindFile) {
    @FN = split /\//, $WindFile;
    if (! $quiet) { if ($verbose) { print STDERR "reading $FN[$#FN]... "; } else { print STDERR "."; } }
    open(INFILE, "<$WindFile") || die("ERROR! Can't open infile: $WindFile\n");
    @Data = <INFILE>;
    close(INFILE);
    $LineNum = 0;
    foreach $line (@Data) {
        chomp($line);
        $LineNum++;
        if ($line =~ /[OK]/) {
            $line =~ s/\s+\[OK\]//i;
            ($time,$Dir,$Speed,$RoseDir) = split /\s+/, $line;
            if (($time = parseDateTime( $time )) ne "") {
                $Wind{ "$time" } = "$Dir,$Speed,$RoseDir";
                push @Wind, "$time,$Dir,$Speed,$RoseDir";
            } else {
                print STDERR "\nERROR! $WindFile error parsing $line\n";
            }
        } else {
            print STDERR "WARN: $WindFile [OK] not found discarding line $LineNum\n";
        }

    }
}

if (! $useCSV && $RainFile ne "" && -e $RainFile) {
    @FN = split /\//, $RainFile;
    if (! $quiet) { if ($verbose) { print STDERR "reading $FN[$#FN]... "; } else { print STDERR "."; } }
    open(INFILE, "<$RainFile") || die("ERROR! Can't open infile: $RainFile\n");
    @Data = <INFILE>;
    close(INFILE);
    $LineNum = 0;
    foreach $line (@Data) {
        chomp($line);
        $LineNum++;
        if ($line =~ /[OK]/) {
            $line =~ s/\s+\[OK\]//i;
            ($time, $DayRain, $RainStorm, $IncRain, $RainRate, $IsRaining) = split /\s+/, $line;
            if (($time = parseDateTime( $time )) ne "") {
                if ($time =~ /-00:00$/ && $DayRain > 0) {
                    if (! $quiet) { if ($verbose) { print STDERR "$time DayRain $DayRain\n"; } }
                    $DayRain = "0.00";
                }
                $Rain{ "$time" } = "$DayRain,$RainStorm,$IncRain,$RainRate,$IsRaining";
                push @Rain, "$time,$DayRain,$RainStorm,$IncRain,$RainRate,$IsRaining";
                @date = split /-/, $time;
                $daysRain{ "$date[0]" } = $DayRain;
            } else {
                print STDERR "\nERROR! $RainFile error parsing $line\n";
            }
        } else {
            print STDERR "WARN: $RainFile [OK] not found discarding line $LineNum\n";
        }

    }
}

if (! $useCSV && $TempFile ne "" && -e $TempFile) {
    @FN = split /\//, $TempFile;
    if (! $quiet) { if ($verbose) { print STDERR "reading $FN[$#FN]... "; } else { print STDERR "."; } }
    open(INFILE, "<$TempFile") || die("ERROR! Can't open infile: $TempFile\n");
    @Data = <INFILE>;
    close(INFILE);
    $LineNum = 0;
    foreach $line (@Data) {
        chomp($line);
        $LineNum++;
        if ($line =~ /[OK]/) {
            $line =~ s/\s+\[OK\]//i;
            ($time, $inTemp, $inHum, $outTemp, $outHum, $WC, $HI, $SR, $ET, $FIN, $FRN) = split /\s+/, $line;
            if (($time = parseDateTime( $time )) ne "") {
               if ($time =~ /-00:00$/ && $ET > 0) {
                    if (! $quiet) { if ($verbose) { print STDERR "$time ET $ET\n"; } }
                    $ET = "0.000";
                }
                $TempHum{ "$time" } = "$inTemp,$inHum,$outTemp,$outHum,$WC,$HI,$SR,$ET";
                push @TempHum, "$time,$inTemp,$inHum,$outTemp,$outHum,$WC,$HI,$SR,$ET";
                if ($FIN ne "" || $FRN ne "") {
                    $Forecast{ "$time" } = "$FIN,$FRN";
                    push @Forecast, "$time,$FIN,$FRN";
                }
                @date = split /-/, $time;
                $daysET{ "$date[0]" } = $ET;
            } else {
                print STDERR "\nERROR! $TempFile error parsing $line\n";
            }
        } else {
            print STDERR "WARN: $TempFile [OK] not found discarding line $LineNum\n";
        }

    }
}

if ($CSVFile ne "" && -e $CSVFile) {
    @FN = split /\//, $CSVFile;
    if (! $quiet) { if ($verbose) { print STDERR "reading $FN[$#FN]... "; } else { print STDERR "."; } }
    open(INFILE, "<$CSVFile") || die("ERROR! Can't open infile: $CSVFile\n");
    @Data = <INFILE>;
    close(INFILE);
    $LineNum = 0;
    foreach $line (@Data) {
        printf stderr ".";
        chomp($line);
        $LineNum++;
        if ($line =~ /[OK]/) {
            $line =~ s/\s+\[OK\]//i;
            ($time,$inTemp,$inHum,$outTemp,$outHum,$WC,$HI,$SR,$ET,$FIN,$FRN,$DayRain,$RainStorm,$IncRain,$RainRate,$IsRaining,$Dir,$Speed,$RoseDir,$BP,$BTI,$BT,$Sunrise,$Sunset,$RunTimeTT,$DST,$SolarNoon,$Status) = split /,/, $line;
            if (($time = parseDateTime( $time )) ne "") {
               if ($time =~ /-00:00$/ && $ET > 0) {
                    if (! $quiet) { if ($verbose) { print STDERR "$time ET $ET\n"; } }
                    $ET = "0.000";
                }
                $TempHum{ "$time" } = "$inTemp,$inHum,$outTemp,$outHum,$WC,$HI,$SR,$ET";
                if ($verbose) { printf stderr "\$TempHum $TempHum{ \"$time\" }\n"; }
                push @TempHum, "$time,$inTemp,$inHum,$outTemp,$outHum,$WC,$HI,$SR,$ET";
                $Rain{ "$time" } = "$DayRain,$RainStorm,$IncRain,$RainRate,$IsRaining";
                if ($verbose) { printf stderr "\$Rain $Rain{ \"$time\" }\n"; }
                push @Rain, "$time,$DayRain,$RainStorm,$IncRain,$RainRate,$IsRaining";
                @date = split /-/, $time;
                $daysRain{ "$date[0]" } = $DayRain;
                $Wind{ "$time" } = "$Dir,$Speed,$RoseDir";
                if ($verbose) { printf stderr "\$Wind $Wind{ \"$time\" }\n"; }
                push @Wind, "$time,$Dir,$Speed,$RoseDir";
                $Baro{ "$time" } = "$BP,$BTI,$BT";
                if ($verbose) { printf stderr "\$Baro $Baro{ \"$time\" }\n"; }
                push @Baro, "$time,$BP,$BTI,$BT";
                if ($Sunrise ne "") { $Sunrise{ "$time" } = $Sunrise; }
                if ($Sunset ne "") { $Sunset{ "$time" } = $Sunset; }
                if ($SolarNoon ne "") { $SolarNoon{ "$time" } = $SolarNoon; }
                if ($FIN ne "" || $FRN ne "") {
                    $Forecast{ "$time" } = "$FIN,$FRN";
                    push @Forecast, "$time,$FIN,$FRN";
                }
                @date = split /-/, $time;
                $daysET{ "$date[0]" } = $ET;
            } else {
                print STDERR "\nERROR! $CSVFile error parsing $line\n";
            }
        } else {
            print STDERR "WARN: $CSVFile [OK] not found discarding line $LineNum\n";
        }
    }
    printf stderr "\ngot data\n";
}

foreach $rtAttr (@rtdata) {
    $rtdata{ "$rtAttr" } = "";
}
undef @rtdata;
if ($rtdataFile ne "" && -e $rtdataFile) {
    $rtHaveDT = $false;
    @FN = split /\//, $rtdataFile;
    if (! $quiet) { if ($verbose) { print STDERR "reading $FN[$#FN]... "; } else { print STDERR "."; } }
    open(INFILE, "<$rtdataFile") || die("ERROR! Can't open infile: $rtdataFile\n");
    @Data = <INFILE>;
    close(INFILE);
    @CaptureTimestamp = grep(/rtCaptureTimestamp.*/, @Data);
    if ($#CaptureTimestamp < 0) {
        @CaptureTimestamp = grep(/CaptureTimestamp.*/, @Data);
        @CaptureTimestamp = grep(!/(gr|hl)CaptureTimestamp.*/, @CaptureTimestamp);
    }
    if ($CaptureTimestamp[0] ne "") {
        chomp($CaptureTimestamp[0]);
        if ($verbose) { print STDERR "\n$CaptureTimestamp[0] "; }
        ($attrName, $value) = split /=/, $CaptureTimestamp[0];
        $attrName =~ s/^ +//;
        $attrName =~ s/ +$//;
        $value =~ s/^ +//;
        $value  =~ s/ +$//;
        # old Fri Jan 16 23:59:22 CST 2009
        # old Sat Jan  1 23:59:00 CST 2005
        $dayName = $month = $day = $time = $DST = $year = $hour = $minute = $second = "";
        if ($value =~ /\w\w\w\s\w\w\w\s+\d+\s\d+:\d+:\d+\s\w\w\w\s\d+/) {
            ($dayName, $month, $day, $time, $DST, $year) = split /\s+/, $value;
            $rtTTct = "";
        }
        # new Saturday January 16 2010 23:59
        # new Friday January 01 2010 23:59
        if ($value =~ /\w\w\w\w+\s\w+\s+\d+\s\d+\s\d+:\d+/) {
            ($dayName, $month, $day, $year, $time) = split /\s+/, $value;
            if ($time !~ /\d+:\d+:\d+/) { $time = "$time:00"; }
            $rtTTct = "";
        }
        # newest way 2011-03-17 12:22:37 (1300382557)
        if ($value =~ /(\d\d\d\d)-(\d\d)-(\d\d) (\d\d:\d\d:\d\d) \((\d+)\)/) {
           $year = $1;
           $month = $2;
           $day = $3;
           $time = $4;
           $rtTTct = $5;
           $dayName = "";
        }
        ($hour, $minute, $second) = split /:/, $time;
        if ($month =~ /\D+/) {
            $month =~ /(\w\w\w)\w*?/;
            $Cmonth = $1;
            $Cmonth =~ tr/a-z/A-Z/;
            $month = $month{ "$Cmonth" };
        }
        if ($year ne "" && $month ne "" && $day ne "" &&  $hour ne "" && $minute ne "" && $second ne "") {
            $rtHaveDT = $true;
            $day = sprintf "%02i", $day;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);
            $value = "$year-$month-$day $time";
            $timeStamp = "$year$month$day-$hour:$minute";
            if ("$hour:$minute" eq "00:00") {
                ($Nyear, $Nmonth, $Nday, $Nhour, $Nminute, $Nsecond) = Add_Delta_DHMS($year, $month, $day, $hour, $minute, $second, 0, 0, -1, 0);
                $lastTime = sprintf "%4i%02i%02i-%02i:%02i", $Nyear, $Nmonth, $Nday, $Nhour, $Nminute;
                if ($rtCaptureTimestamp{ "$lastTime" } eq "") {
                    $rtCaptureTimestamp{ "$lastTime" } = $value;
                    @chkDates = grep(/$lastTime/, @CaptureDT);
                    if ($chkDates[0] eq "") { push @CaptureDT, $lastTime; }
                }
            }
            
            $rtCaptureTimestamp{ "$timeStamp" } = $value;
            @chkDates = grep(/$timeStamp/, @CaptureDT);
            if ($chkDates[0] eq "") { push @CaptureDT, $timeStamp; }
            if ($verbose) { print STDERR "timeStamp $timeStamp\n"; }
        }
    }
    if ($rtHaveDT) {
        foreach $line (@Data) {
            chomp($line);
            ($attrName, $value) = split /=/, $line;
            $attrName =~ s/^ +//;
            $attrName =~ s/ +$//;
            $value =~ s/^ +//;
            $value  =~ s/ +$//;
            if ($attrName eq "rtSunrise" && $timeStamp ne "") {
                if (($timeStamp = parseDateTime( $timeStamp )) ne "") {
                    if ($value =~ /M/) { $value = cvtTime($value); }
                    $Sunrise{ "$timeStamp" } = $value;
                    @chkDates = grep(/$timeStamp/, @SunDates);
                    if ($chkDates[0] eq "") { push @SunDates, $timeStamp; }
                }
            }
            if ($attrName eq "rtSunset" && $timeStamp ne "") {
                if (($timeStamp = parseDateTime( $timeStamp )) ne "") {
                    if ($value =~ /M/) { $value = cvtTime($value); }
                    $Sunset{ "$timeStamp" } = $value;
                    @chkDates = grep(/$timeStamp/, @SunDates);
                    if ($chkDates[0] eq "") { push @SunDates, $timeStamp; }
                }
             }
            if ($attrName eq "rtSolarNoon" && $timeStamp ne "") {
                if (($timeStamp = parseDateTime( $timeStamp )) ne "") {
                    if ($value =~ /M/) { $value = cvtTime($value); }
                    $SolarNoon{ "$timeStamp" } = $value;
                    @chkDates = grep(/$timeStamp/, @SunDates);
                    if ($chkDates[0] eq "") { push @SunDates, $timeStamp; }
                }
             }
            if ($rtdata{ "$attrName" } eq "") {
                $rtdata{ "$attrName" } = $value;
                push @rtdata, $attrName;
            }
        }
    } else {
        print "Can't find date/time for Real Time data\n";
    }
}

foreach $wxAttr (@wxdata) {
    $wxdata{ "$wxAttr" } = "";
}
undef @wxdata;
if ($wxdataFile ne "" && -e $wxdataFile) {
    $wxHaveDT = $false;
    @FN = split /\//, $wxdataFile;
    if (! $quiet) { if ($verbose) { print STDERR "reading $FN[$#FN]... "; } else { print STDERR "."; } }
    open(INFILE, "<$wxdataFile") || die("ERROR! Can't open infile: $wxdataFile\n");
    @Data = <INFILE>;
    close(INFILE);
    $forYMD = "";
    @CaptureTimestamp = grep(/grCaptureTimestamp.*/, @Data);
    if ($#CaptureTimestamp < 0) {
        @CaptureTimestamp = grep(/CaptureTimestamp.*/, @Data);
        @CaptureTimestamp = grep(!/(rt|hl)CaptureTimestamp.*/, @CaptureTimestamp);
    }
    if ($CaptureTimestamp[0] ne "") {
        chomp($CaptureTimestamp[0]);
        if ($verbose) { print STDERR "\n$CaptureTimestamp[0] "; }
        ($attrName, $value) = split /=/, $CaptureTimestamp[0];
        $attrName =~ s/^ +//;
        $attrName =~ s/ +$//;
        $value =~ s/^ +//;
        $value  =~ s/ +$//;
        # old Fri Jan 16 23:59:22 CST 2009
        # old Sat Jan  1 23:59:00 CST 2005
        $dayName = $month = $day = $time = $DST = $year = $hour = $minute = $second = "";
        if ($value =~ /\w\w\w\s\w\w\w\s+\d+\s\d+:\d+:\d+\s\w\w\w\s\d+/) {
            ($dayName, $month, $day, $time, $DST, $year) = split /\s+/, $value;
            $grTTct = "";
        }
        # new Saturday January 16 2010 23:59
        # new Friday January 01 2010 23:59
        if ($value =~ /\w\w\w\w+\s+\w+\s\d+\s\d+\s\d+:\d+/) {
            ($dayName, $month, $day, $year, $time) = split /\s+/, $value;
            if ($time !~ /\d+:\d+:\d+/) { $time = "$time:00"; }
            $grTTct = "";
        }
        # newest way 2011-03-17 12:22:37 (1300382557)
        if ($value =~ /(\d\d\d\d)-(\d\d)-(\d\d) (\d\d:\d\d:\d\d) \((\d+)\)/) {
           $year = $1;
           $month = $2;
           $day = $3;
           $time = $4;
           $grTTct = $5;
           $dayName = "";
        }
        ($hour, $minute, $second) = split /:/, $time;
        if ($month =~ /\D+/) {
            $month =~ /(\w\w\w)\w*?/;
            $Cmonth = $1;
            $Cmonth =~ tr/a-z/A-Z/;
            $month = $month{ "$Cmonth" };
        }
        if ($year ne "" && $month ne "" && $day ne "" &&  $hour ne "" && $minute ne "" && $second ne "") {
            $wxHaveDT = $true;
            $day = sprintf "%02i", $day;
            $value = "$year-$month-$day $time";
            $timeStamp = "$year$month$day-$hour:$minute";
            if ("$hour:$minute" eq "00:00") {
                ($Nyear, $Nmonth, $Nday, $Nhour, $Nminute, $Nsecond) = Add_Delta_DHMS($year, $month, $day, $hour, $minute, $second, 0, 0, -1, 0);
                $forYMD = sprintf "%4i-%02i-%02i", $Nyear, $Nmonth, $Nday;
                $lastTime = sprintf "%4i%02i%02i-%02i:%02i", $Nyear, $Nmonth, $Nday, $Nhour, $Nminute;
                if ($wxCaptureTimestamp{ "$lastTime" } eq "") {
                    $wxCaptureTimestamp{ "$lastTime" } = $value;
                    @chkDates = grep(/$lastTime/, @CaptureDT);
                    if ($chkDates[0] eq "") { push @CaptureDT, $lastTime; }
                }
            } else {
                $forYMD = "$year-$month-$day";
            }
            $wxCaptureTimestamp{ "$timeStamp" } = $value;
            @chkDates = grep(/$timeStamp/, @CaptureDT);
            if ($chkDates[0] eq "") { push @CaptureDT, $timeStamp; }
            if ($verbose) { print STDERR "timeStamp $timeStamp\n"; }
        }
    }
    if ($wxHaveDT) {
        foreach $line (@Data) {
            chomp($line);
            ($attrName, $value) = split /=/, $line;
            $attrName =~ s/^ +//;
            $attrName =~ s/ +$//;
            $value =~ s/^ +//;
            $value  =~ s/ +$//;
#
            if ($forYMD ne "") {
                if ($attrName eq "grRainStormsStartDate") { @StormsStart = split /,/, $value; }
                if ($attrName eq "grRainStormsEndDate") { @StormsEnd = split /,/, $value; }
                if ($attrName eq "grRainStorms") { @StormsAmount = split /,/, $value; }
#
	        if ($attrName eq "grDewByHours") { $DewByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grTHSWHiByHours") { $THSWByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grETByHours") { $ETByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grSolarByHourAvg") { $SolarByHourAvg{ "$forYMD" } = $value; }
                if ($attrName eq "grWindAvgByHours") { $WindAvgByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grWindHiByHours") { $WindHiByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grWindDirByHours") { $WindDirByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grBarByHours") { $BarByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grOutTempByHours") { $OutTempByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grInTempByHours") { $InTempByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grOutHumByHours") { $OutHumByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grInHumByHours") { $InHumByHours{ "$forYMD" } = $value; }
                if ($attrName =~ /^grRainByHours?$/) { $RainByHours{ "$forYMD" } = $value; }	# Note added "s"
                if ($attrName eq "grRainRateByHours") { $RainRateByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grChillLoByHours") { $ChillLoByHours{ "$forYMD" } = $value; }
                if ($attrName eq "grHeatHiByHours") { $HeatHiByHours{ "$forYMD" } = $value; }
	        if ($attrName eq "grTimeRefHours") {
                    if ($value =~ /AM/) {
                        @grTimeRefHours = split /,/, $value;
                        $value = "";
                        foreach $grTRH (@grTimeRefHours) {
                            if ($grTRH =~ /M/) { $grTRH = cvtTime($grTRH); }
                            $value = "$value$grTRH,";
                        }
                        $value =~ s/,$//;
                    }
                    $DewRefHours{ "$forYMD" } = $THSWRefHours{ "$forYMD" } = $ETRefHours{ "$forYMD" } = $SolarRefHours{ "$forYMD" } = $WindRefHours{ "$forYMD" } = $BarRefHours{ "$forYMD" } = $TempRefHours{ "$forYMD" } = $HumRefHours{ "$forYMD" } = $RainRefHours{ "$forYMD" } = $ChillLoRefHours{ "$forYMD" } = $HeatHiRefHours{ "$forYMD" } = "$value";
                    @chkDates = grep(/$forYMD/, @RefHoursDates);
                    if ($chkDates[0] eq "") { push @RefHoursDates, $forYMD; }
                }
            }
#
            if ($wxdata{ "$attrName" } eq "") {
                $wxdata{ "$attrName" } = $value;
                push @wxdata, $attrName;
            }
        }
    } else {
        print "Can't find date/time for Graph data\n";
    }
}

if (! $quiet ) { print STDERR "\n"; }

@Sorted = sort by_numeric @times;
@times = @Sorted;

my $dbh=DBI->connect("DBI:mysql:host=$DBhost;database=$DBname","$user","$password");
if (!$dbh) { die("ERROR! Connecting to $DBhost $DBname.\n"); }

sub getDST {
    my ($second, $minute, $hour, $day, $month, $year) = @_;
    # need to get DST
    my $eTime = timelocal($second, $minute, $hour, $day, $month-1, $year-1900);
    ($second, $minute, $hour, $day, $month, $year, my $DST) = (localtime($eTime))[0,1,2,3,4,5,8];
    $year = $year + 1900;
    $month = sprintf "%02i", $month + 1;
    $day = sprintf "%02i", $day;
    $hour = sprintf "%02i", $hour;
    $minute = sprintf "%02i", $minute;
    $second = sprintf "%02i", $second;
    return $eTime, $second, $minute, $hour, $day, $month, $year, $DST;
}

sub getTT {
    my ($time) = @_;

    my ($date, $time) = split /\s+/, $time;
    my ($hour, $minute, $second) = split /:/, $time;
    my ($year, $month, $day) = split /-/, $date;
    my $TT = timelocal($second, $minute, $hour, $day, $month-1, $year-1900);

    return $TT;
}
    
sub setupIndex {
    my ($time) = @_;

    my $rtCaptureTime;
    my $wxCaptureTime;
    my $Attr = "";
    my $Values = "";
    my $InsertPtr;

    if ($SQLverbose) { printf stderr "setupIndex time = \"$time\"\n"; }

    $rtCaptureTime = $rtCaptureTimestamp{ "$time" };
    $wxCaptureTime = $wxCaptureTimestamp{ "$time" };

    (my $DateTimeSQL, my $year, my $month, my $day, my $hour, my $minute, my $second) = split /,/, $times{ "$time" };
    (my $dateSQL, my $timeSQL) = split /\s+/, $DateTimeSQL;
    my $DayNum = Day_of_Week($year, $month, $day);
    if ($dateSQL ne $LastdateSQL) { @sawTimes = (); }
    $LastdateSQL = $dateSQL;

    # need to get DST
    ($TT, $second, $minute, $hour, $day, $month, $year, my $DST) = getDST($second, $minute, $hour, $day, $month, $year);
    # adjust for Fall Behind Daylight Savings Time but only if the $Separate option is used
    $DSTchange = $false;
    @found = grep(/$DateTimeSQL/,@sawTimes);
    if ($Separate && $#found >= 0 && $DST == 1) {
        $DST = 0;
        $TT += 3600;
        $DSTchange = $true;
        if ($verbose) { print "$DateTimeSQL DST change\n" }
    } elsif ($Separate) {
        push @sawTimes, "$DateTimeSQL";
    }

    $Query = "SELECT bDateTime, RecID FROM DT WHERE bDateTime = \"$DateTimeSQL\" && DST = \"$DST\"";
    $sth = $dbh->prepare($Query);
    $sth->execute();

    $ref = $sth->fetchrow_hashref();
    $retDateTimeSQL = $ref->{bDateTime};
    $retRecID = $ref->{RecID};

    if ($retRecID eq "") {
        $Query = "SELECT bDateTime, RecID FROM DT WHERE YearMonthDay = \"$year$month$day\" && HHMM = \"$hour:$minute\" && DST = \"$DST\" LIMIT 1";
        $sth = $dbh->prepare($Query);
        $sth->execute();
        $ref = $sth->fetchrow_hashref();
        $retDateTimeSQL = $ref->{bDateTime};
        $retRecID = $ref->{RecID};
    }
    if ($retRecID eq "" && $retDateTimeSQL ne "$DateTimeSQL" && $DateTimeSQL ne "") {
        if ($rtCaptureTimestamp{ "$time" } eq "" && $wxCaptureTimestamp{ "$time" } eq "") {
            $Query  = "INSERT INTO IndexDT (rtCaptureTime, wxCaptureTime) VALUES (NULL, NULL)";
        } else {
            if ($rtCaptureTimestamp{ "$time" } ne "") {
                $Attr = "rtCaptureTime, ";
                $Values = "\"$rtCaptureTimestamp{ \"$time\" }\", ";
                $rtTT = getTT( $rtCaptureTimestamp{ "$time" } );
                $Attr = "$Attr rtTT, ";
                $Values = "$Values \"$rtTT\", ";
            }
            if ($wxCaptureTimestamp{ "$time" } ne "") {
                $Attr = "$Attr wxCaptureTime, ";
                $Values = "$Values \"$wxCaptureTimestamp{ \"$time\" }\", ";
                $wxTT = getTT( $wxCaptureTimestamp{ "$time" } );
                $Attr = "$Attr wxTT";
                $Values = "$Values \"$wxTT\"";
            }
            $Attr =~ s/, $//;
            $Values =~ s/, $//;
            $Query  = "INSERT INTO IndexDT ($Attr) VALUES ($Values)";
        }
        $rows = $dbh->do($Query);
        $Query = "SELECT LAST_INSERT_ID() AS NewID";
        $sth = $dbh->prepare($Query);
        $sth->execute();
        $ref = $sth->fetchrow_hashref();
        $InsertPtr = $ref->{NewID};
        if ($InsertPtr ne "") {
            my $bDST = $DST ? "TRUE" : "FALSE";
            $Query  = "INSERT INTO DT (bDateTime, RecID, YearMonthDay, YearMonth, bDate, bTime, HHMM, bYear, bMonth, bDay, bHour, bMinute, bSecond, DST, bTT) VALUES (\"$DateTimeSQL\", \"$InsertPtr\", \"$year$month$day\", \"$year$month\", \"$dateSQL\", \"$timeSQL\", \"$hour:$minute\", \"$year\", \"$month\", \"$day\", \"$hour\", \"$minute\", \"$second\", \"$DST\", \"$TT\")";
            $rows = $dbh->do($Query);
            $Query  = "UPDATE YMD SET DayName = \"$day{ \"$DayNum\" }\" WHERE YearMonthDay = \"$year$month$day\"";
            $rows = $dbh->do($Query);
        }
    } elsif ($retRecID ne "" && $DateTimeSQL ne "") {
        $InsertPtr = $retRecID;
    } else {
        $InsertPtr = "";
    }

    ($Tyear, $Tmonth, $Tday) = split /-/, $dateSQL;
    ($TT, $Tsecond, $Tminute, $Thour, $Tday, $Tmonth, $Tyear, $TDST) = getDST("00", "59", "23", $Tday, $Tmonth, $Tyear);

    $Query = "SELECT RecID FROM DT WHERE bDateTime = \"$dateSQL 23:59:00\" && DST = \"$DST\"";
    $sth = $dbh->prepare($Query);
    $sth->execute();

    $ref = $sth->fetchrow_hashref();
    if ($ref->{RecID} ne "" && ($rtCaptureTime ne "" || $wxCaptureTime ne "")) {
        $CTRecID = $ref->{RecID};
        $Query = "SELECT rtCaptureTime, wxCaptureTime FROM IndexDT where RecID = \"$CTRecID\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        $rtCaptureTimeDB = $ref->{rtCaptureTime};
        $wxCaptureTimeDB = $ref->{wxCaptureTime};

        if ($rtCaptureTimeDB ne $rtCaptureTime && $rtCaptureTime ne "") {
            $Query = "UPDATE IndexDT SET rtCaptureTime = \"$rtCaptureTime\" where RecID = \"$CTRecID\"";
            $rows = $dbh->do($Query);
            $rtTT = getTT( $rtCaptureTime );
            $Query = "UPDATE IndexDT SET rtTT = \"$rtTT\" where RecID = \"$CTRecID\"";
            $rows = $dbh->do($Query);
        }
        if ($wxCaptureTimeDB ne $wxCaptureTime && $wxCaptureTime ne "") {
            $Query = "UPDATE IndexDT SET wxCaptureTime = \"$wxCaptureTime\" where RecID = \"$CTRecID\"";
            $rows = $dbh->do($Query);
            $wxTT = getTT( $wxCaptureTime );
            $Query = "UPDATE IndexDT SET wxTT = \"$wxTT\" where RecID = \"$CTRecID\"";
            $rows = $dbh->do($Query);
        }
    }
    return $InsertPtr;
}

if (! $quiet) { if ($verbose) { print STDERR "\n"; } else { print STDERR "+"; } }

if ($Together) {
    foreach $time (@times) {
        if (! $quiet) { if ($verbose) { print STDERR "processing $time...\n"; } else { print STDERR "."; } }

        if ($Baro{ "$time" } ne "") {
            ($BP, $BTI, $BT) = split /\,/, $Baro{ "$time" };
        } else {
            $BP = $BTI = $BT = "";
        }
        if ($Wind{ "$time" } ne"") {
            ($Dir, $Speed, $RoseDir) = split /\,/, $Wind{ "$time" };
        } else {
            $Dir = $Speed = $RoseDir = "";
        }
        if ($Rain{ "$time" } ne "") {
            ($DayRain, $RainStorm, $IncRain, $RainRate, $IsRaining) = split /\,/, $Rain{ "$time" };
        } else {
            $DayRain = $RainStorm = $IncRain = $RainRate = $IsRaining = "";
        }
        if ($TempHum{ "$time" } ne "") {
            ($inTemp, $inHum, $outTemp, $outHum, $WindChill, $HeatIndex, $SolarRad, $ET) = split /\,/, $TempHum{ "$time" };
            if ($HeatIndex ne "" && $HeatIndex < $outTemp &&  $HeatIndex == 0.0) { $HeatIndex = $outTemp; } 
        } else {
            $inTemp = $inHum = $outTemp = $outHum = $WindChill = $HeatIndex = $SolarRad = $ET = "";
        }
        if ($Forecast{ "$time" } ne "") {
            ($FIN, $FRN) = split /\,/, $Forecast{ "$time" };
        } else {
            $FIN = $FRN = "";
        }

        if ($WindChill eq "" && $outTemp ne "" && $Speed ne "") {
            $WindChill = calcWindChill( $outTemp, $Speed );
        }
        if ($HeatIndex eq "" && $outTemp ne "" && $outHum ne "") {
            $HeatIndex = calcHeatIndex( $outTemp, $outHum);
        }
        processData($time);
    }
}

if ($Separate) {
    foreach $line (@Baro) {
        ($time, $BP, $BTI, $BT) = split /\,/, $line;
        processData($time);
    }
    $time = $BP = $BTI = $BT = "";
    @sawTimes = ();
    foreach $line (@Wind) {
        ($time,$Dir,$Speed,$RoseDir) = split /\,/, $line;
        processData($time);
    }
    $time = $Dir = $Speed = "";
    @sawTimes = ();
    foreach $line (@Rain) {
        ($time, $DayRain, $RainStorm, $IncRain, $RainRate, $IsRaining) = split /\,/, $line;
        processData($time);
    }
    $time = $DayRain = $RainStorm = $IncRain = $RainRate = $IsRaining = "";
    @sawTimes = ();
    foreach $line (@TempHum) {
        ($time, $inTemp, $inHum, $outTemp, $outHum, $WindChill, $HeatIndex, $SolarRad, $ET) = split /\,/, $line;
        if ($HeatIndex eq "" && $outTemp ne "" && $outHum ne "") {
            $HeatIndex = calcHeatIndex( $outTemp, $outHum);
        }
        processData($time);
    }
    $time = $inTemp = $inHum = $outTemp = $outHum = $WindChill = $HeatIndex = $SolarRad = $ET = "";
    @sawTimes = ();
    foreach $line (@Forecast) {
        ($time, $FIN, $FRN) = split /\,/, $line;
        processData($time);
    }
    $time = $FIN = $FRN = "";
    @sawTimes = ();
}


sub processData {
    
    if ($time eq "") {
        print "\nCan't process, date/time missing\n";
        return;
    }
    my ($time) = @_;

    $InsertPtr = setupIndex($time);
    if ($SQLverbose) { printf stderr "\nRecID = $InsertPtr\n"; }

    ($DateTimeSQL, $year, $month, $day, $hour, $minute, $second) = split /,/, $times{ "$time" };
    ($dateSQL, $timeSQL) = split /\s+/, $DateTimeSQL;


    if ($InsertPtr ne "") {
        $Query = "SELECT RecID FROM Temperature WHERE RecID = \"$InsertPtr\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ("$ref->{RecID}" eq "") {
            if (! $quiet) { if ($verbose) { print STDERR "processing Temperature..."; } else { print STDERR "."; } }
            $SetList = "";
            $Values = "";
            if ($inTemp ne "" && $inTemp ne "-") {
                 $SetList = "Indoor,";
                 $Values = "\"$inTemp\",";
            }
            if ($outTemp ne "" && $outTemp ne "-") {
                 $SetList = "$SetList Outdoor,";
                 $Values = " $Values \"$outTemp\",";
            }
            if ($WindChill ne "" && $WindChill ne "-") {
                 $SetList = "$SetList WindChill,";
                 $Values = " $Values \"$WindChill\",";
            }
            if ($HeatIndex ne "" && $HeatIndex ne "-") {
                 $SetList = "$SetList HeatIndex";
                 $Values = " $Values \"$HeatIndex\"";
            }
            $SetList =~ s/,$//;
            $Values =~ s/,$//;
            if ($SQLverbose) { printf stderr "Temperature SetList ($SetList) Values ($Values)\n"; }
            if ($SetList ne "") {
                $SetList = ", $SetList";
                $Values = ", $Values";
                $Query  = "INSERT INTO Temperature (RecID$SetList) VALUES (\"$InsertPtr\"$Values)";
                $rows = $dbh->do($Query);
            }
        }

        $Query = "SELECT RecID FROM Humidity WHERE RecID = \"$InsertPtr\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ("$ref->{RecID}" eq "") {
            if (! $quiet) { if ($verbose) { print STDERR " processing Humidity..."; } else { print STDERR "."; } }
            $SetList = "";
            $Values = "";
            if ($inHum ne "" && $inHum <= 100 && $inHum ne "-") {
                 $SetList = "Indoor,";
                 $Values = "\"$inHum\",";
            }
            if ($outHum ne "" && $outHum <= 100 && $outHum ne "-") {
                 $SetList = "$SetList Outdoor";
                 $Values = " $Values \"$outHum\",";
            }
            $SetList =~ s/,$//;
            $Values =~ s/,$//;
            if ($SetList ne "") {
                $SetList = ", $SetList";
                $Values = ", $Values";
                $Query  = "INSERT INTO Humidity (RecID$SetList) VALUES (\"$InsertPtr\"$Values)";
                $rows = $dbh->do($Query);
            }
        }

        $Query = "SELECT RecID FROM Barometer WHERE RecID = \"$InsertPtr\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ("$ref->{RecID}" eq "") {
            if (! $quiet) { if ($verbose) { print STDERR " processing Barometer..."; } else { print STDERR "."; } }
            if ($BP ne "" || $BTI ne "" || $BT ne "") {
                $SetList = "";
                $Values = "";
                if ($BP ne "") {
                     $SetList = "Pressure,";
                     $Values = "\"$BP\",";
                }
                if ($BTI ne "") {
                     $SetList = "$SetList TrendImage,";
                     $Values = " $Values \"$BTI\",";
                }
                if ($BT ne "") {
                     $SetList = "$SetList Trend,";
                     $BT =~ s/-/ /;
                     $Values = " $Values \"$BT\",";
                }
                $SetList =~ s/,$//;
                $Values =~ s/,$//;
                if ($SetList ne "") {
                    $SetList = ", $SetList";
                    $Values = ", $Values";
                    $Query  = "INSERT INTO Barometer (RecID$SetList) VALUES (\"$InsertPtr\"$Values)";
                    $rows = $dbh->do($Query);
                }
            }
        }

        $Query = "SELECT RecID FROM Wind WHERE RecID = \"$InsertPtr\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ("$ref->{RecID}" eq "") {
            if (! $quiet) { if ($verbose) { print STDERR " processing Wind..."; } else { print STDERR "."; } }
            if ($Speed ne "-" && $Dir ne "-") {
                $SetList = "";
                $Values = "";
                if ($Speed ne "") {
                     $SetList = "WindSpeed,";
                     $Values = "\"$Speed\",";
                }
                if ($Dir ne "") {
                     $SetList = "$SetList WindDirection,";
                     $Values = " $Values \"$Dir\",";
                }
                $SetList =~ s/,$//;
                $Values =~ s/,$//;
                if ($SetList ne "") {
                    $SetList = ", $SetList";
                    $Values = ", $Values";
                    $Query  = "INSERT INTO Wind (RecID$SetList) VALUES (\"$InsertPtr\"$Values)";
                    $rows = $dbh->do($Query);
                }
            }
        }

        if ($SolarRad ne "") {
            $Query = "SELECT RecID FROM Solar WHERE RecID = \"$InsertPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            if ("$ref->{RecID}" eq "") {
                if (! $quiet) { if ($verbose) { print STDERR " processing Solar..."; } else { print STDERR "."; } }
                $Query  = "INSERT INTO Solar (RecID, Radiation) values (\"$InsertPtr\", \"$SolarRad\")";
                $rows = $dbh->do($Query);
            }
        }

        $Query = "SELECT RecID FROM Rain WHERE RecID = \"$InsertPtr\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ("$ref->{RecID}" eq "") {
            if (! $quiet) { if ($verbose) { print STDERR " processing Rain..."; } else { print STDERR "."; } }
            $Attr = "RecID";
            $Values = "\"$InsertPtr\"";
            if ($DayRain > 0.0) {
                $Attr = "$Attr, DayAmount";
                $Values = "$Values, \"$DayRain\"";
            }
            if ($RainStorm > 0.0) {
                $Attr = "$Attr, StormAmount";
                $Values = "$Values, \"$RainStorm\"";
            }
            if ($DayRain > 0.0 || $RainStorm > 0.0) {
                $Attr = "$Attr, Rate";
                $Values = "$Values, \"$RainRate\"";
            }
            if ($IsRaining ne "") {
                 $Attr = "$Attr, CurrentlyRaining";
                 if ($IsRaining eq "yes") {
                     $Values = "$Values, \"$true\"";
                 } else {
                     $Values = "$Values, \"$false\"";
                 }
            }
            if ($ET ne "") {
                $Attr = "$Attr, ET";
                $Values = "$Values, \"$ET\"";
            }
            if ($Attr =~ /,/) {
                $Query = "INSERT INTO Rain ($Attr) values ($Values)";
                $rows = $dbh->do($Query);
            }
        }

        if ($minute eq "00" && ($FIN ne "" || $FRN ne "")) {
            if (! $quiet) { if ($verbose) { print STDERR " processing Forecast..."; } else { print STDERR "."; } }
            $Query = "SELECT RecID FROM Hourly WHERE RecID = \"$InsertPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            if ("$ref->{RecID}" eq "") {
                $Attr = "RecID";
                $Values = "\"$InsertPtr\"";
                if ($FIN ne "") {
                    $Attr = "$Attr, ForecastIconNumber";
                    $Values = "$Values, \"$FIN\"";
                }
                if ($FRN ne "") {
                    $Attr = "$Attr, ForecastRuleNumber";
                    $Values = "$Values, \"$FRN\"";
                }
                $Query = "INSERT INTO Hourly ($Attr) values ($Values)";
                $rows = $dbh->do($Query);
            } else {
                if ($FIN ne "") {
                    $Query = "UPDATE Hourly SET ForecastIconNumber = \"$FIN\" WHERE RecID = \"$InsertPtr\"";
                    $rows = $dbh->do($Query);
                }
                if ($FRN ne "") {
                    $Query = "UPDATE Hourly SET ForecastRuleNumber = \"$FRN\" WHERE RecID = \"$InsertPtr\"";
                    $rows = $dbh->do($Query);
                }
            }
        }
    }

    if (! $quiet) { if ($verbose) { print STDERR "\n"; } else { print STDERR "+"; } }
}

if ($CaptureDT[0] ne "") { if (! $quiet) { if ($verbose) { print STDERR "processing Capture TS... "; } else { print STDERR "."; } } }
foreach $time (@CaptureDT) {
    if ($times{ "$time" } ne "") {
        ($DateTimeSQL, $year, $month, $day, $hour, $minute, $second) = split /,/, $times{ "$time" };
        ($dateSQL, $timeSQL) = split /\s+/, $DateTimeSQL;

        $Query ="SELECT t1.RecID, t1.rtCaptureTime, t1.wxCaptureTime FROM IndexDT as t1 INNER JOIN DT as t2 USING (RecID) WHERE t2.bDateTime = \"$DateTimeSQL\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ($ref->{RecID} ne "") {
            $RecID = $ref->{RecID};
            if ($ref->{rtCaptureTime} ne $rtCaptureTimestamp{ "$time" }) {
                $Query = "UPDATE IndexDT SET rtCaptureTime = \"$rtCaptureTimestamp{ \"$time\" }\" WHERE RecID = \"$RecID\"";
                $rows = $dbh->do($Query);
                $rtTT = getTT( $rtCaptureTimestamp{ "$time" } );
                $Query = "UPDATE IndexDT SET rtTT = \"$rtTT\" where RecID = \"$RecID\"";
                $rows = $dbh->do($Query);
            }
            if ($ref->{wxCaptureTime} ne $wxCaptureTimestamp{ "$time" }) {
                $Query = "UPDATE IndexDT SET wxCaptureTime = \"$wxCaptureTimestamp{ \"$time\" }\" WHERE RecID = \"$RecID\"";
                $rows = $dbh->do($Query);
                $wxTT = getTT( $wxCaptureTimestamp{ "$time" } );
                $Query = "UPDATE IndexDT SET wxTT = \"$wxTT\" where RecID = \"$RecID\"";
                $rows = $dbh->do($Query);
            }
        }
    }
}

if ($SunDates[0] ne "") { if (! $quiet) { if ($verbose) { print STDERR "processing Sunrise/Sunset... "; } else { print STDERR "."; } } }
foreach $time (@SunDates) {

    ($DateTimeSQL, $year, $month, $day, $hour, $minute, $second) = split /,/, $times{ "$time" };
    ($dateSQL, $timeSQL) = split /\s+/, $DateTimeSQL;

    if ($Sunrise{ "$time" } ne "" ) {
        $Query = "SELECT Sunrise FROM YMD WHERE YearMonthDay = \"$year$month$day\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ($ref->{Sunrise} ne $Sunrise{ "$time" }) {
            $Query = "UPDATE YMD SET Sunrise = \"$Sunrise{ \"$time\" }\" WHERE YearMonthDay = \"$year$month$day\"";
            $rows = $dbh->do($Query);
        }
    }

    if ($Sunset{ "$time" } ne "" ) {
        $Query = "SELECT Sunset FROM YMD WHERE YearMonthDay = \"$year$month$day\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ($ref->{Sunset} ne $Sunset{ "$time" }) {
            $Query = "UPDATE YMD SET Sunset = \"$Sunset{ \"$time\" }\" WHERE YearMonthDay = \"$year$month$day\"";
            $rows = $dbh->do($Query);
        }
    }

    if ($SolarNoon{ "$time" } ne "" ) {
        $Query = "SELECT SolarNoon FROM YMD WHERE YearMonthDay = \"$year$month$day\"";
        $sth = $dbh->prepare($Query);
        $sth->execute();

        $ref = $sth->fetchrow_hashref();
        if ($ref->{SolarNoon} ne $SolarNoon{ "$time" }) {
            $Query = "UPDATE YMD SET SolarNoon = \"$SolarNoon{ \"$time\" }\" WHERE YearMonthDay = \"$year$month$day\"";
            $rows = $dbh->do($Query);
        }
    }
}

#
#	Add Hourly Stuff
#
#
foreach $Date (@RefHoursDates) {

    $DateAdjusted = $Date;
    $_ = $DateAdjusted;
    /(\d\d\d\d)-(\d\d)-(\d\d)/;
    $year = $1;
    $month = $2;
    $day = $3;
    ($year, $month, $day) = Add_Delta_Days($year, $month, $day, -1);
    $month = sprintf "%02i", $month;
    $day = sprintf "%02i", $day;
    $DateAdjusted = "$year-$month-$day";

    $OutTempDate = $Date;
    $OutTempDateAdjusted = $DateAdjusted;
    if ($OutTempByHours{ "$OutTempDate" } ne "" && $TempRefHours{ "$OutTempDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Outdoor Temp... "; } else { print STDERR "."; } }
        @OutTempByHours = split /,/, $OutTempByHours{ "$OutTempDate" };
        @TempRefHours = split /,/, $TempRefHours{ "$OutTempDate" };
        $Pos = 0;
        $OutTempNextDay = $false;
        while ($OutTempByHours[$Pos] ne "") {
            $OutTemp = $OutTempByHours[$Pos];
            $OutTempTime = "$TempRefHours[$Pos]:00";
#
            if ($OutTempNextDay || $OutTempTime eq "00:00:00") {
                $_ = $OutTempDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $OutTempDateAdjusted = "$year-$month-$day";
                $OutTempNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $OutTempTime;
            ($year, $month, $day) = split /-/, $OutTempDateAdjusted; 
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$OutTempDateAdjusted $OutTempTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $OutTempIndexPtr = $ref->{RecID};
            if ($OutTempIndexPtr eq "") {
                $bOutTempDateTime = $OutTempDateAdjusted;
                $bOutTempDateTime =~ s/-//g;
                $bOutTempDateTime = "$bOutTempDateTime-$OutTempTime";
                $bOutTempDateTime =~ s/:00$//;
                if ($times{ "$bOutTempDateTime" } eq "") {
                    $bOutTempDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bOutTempDateTime" } = "$OutTempDateAdjusted $OutTempTime,$1,$2,$3,$4,$5,00";
                }
                $OutTempIndexPtr = setupIndex($bOutTempDateTime);
            }

            $Query = "SELECT RecID, OutTemp FROM Hourly where RecID = \"$OutTempIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $OutTempDB = $ref->{OutTemp};

            if ($OutTempIndexPtr ne "" && $OutTempDB ne $OutTemp) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, OutTemp) VALUES (\"$OutTempIndexPtr\", \"$OutTemp\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET OutTemp = \"$OutTemp\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

    $InTempDate = $Date;
    $InTempDateAdjusted = $DateAdjusted;
    if ($InTempByHours{ "$InTempDate" } ne "" && $TempRefHours{ "$InTempDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Indoor Temp... "; } else { print STDERR "."; } }
        @InTempByHours = split /,/, $InTempByHours{ "$InTempDate" };
        @TempRefHours = split /,/, $TempRefHours{ "$InTempDate" };
        $Pos = 0;
        $InTempNextDay = $false;
        while ($InTempByHours[$Pos] ne "") {
            $InTemp = $InTempByHours[$Pos];
            $InTempTime = "$TempRefHours[$Pos]:00";
#
            if ($InTempNextDay || $InTempTime eq "00:00:00") {
                $_ = $InTempDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $InTempDateAdjusted = "$year-$month-$day";
                $InTempNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $InTempTime;
            ($year, $month, $day) = split /-/, $InTempDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$InTempDateAdjusted $InTempTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $InTempIndexPtr = $ref->{RecID};
            if ($InTempIndexPtr eq "") {
                $bInTempDateTime = $InTempDateAdjusted;
                $bInTempDateTime =~ s/-//g;
                $bInTempDateTime = "$bInTempDateTime-$InTempTime";
                $bInTempDateTime =~ s/:00$//;
                if ($times{ "$bInTempDateTime" } eq "") {
                    $bInTempDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bInTempDateTime" } = "$InTempDateAdjusted $InTempTime,$1,$2,$3,$4,$5,00";
                }
                $InTempIndexPtr = setupIndex($bInTempDateTime);
            }

            $Query = "SELECT RecID, InTemp FROM Hourly where RecID = \"$InTempIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $InTempDB = $ref->{InTemp};

            if ($InTempIndexPtr ne "" && $InTempDB ne $InTemp) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, InTemp) VALUES (\"$InTempIndexPtr\", \"$InTemp\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET InTemp = \"$InTemp\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

#$OutHumByHours
    $OutHumDate = $Date;
    $OutHumDateAdjusted = $DateAdjusted;
    if ($OutHumByHours{ "$OutHumDate" } ne "" && $HumRefHours{ "$OutHumDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Outdoor Humidity... "; } else { print STDERR "."; } }
        @OutHumByHours = split /,/, $OutHumByHours{ "$OutHumDate" };
        @HumRefHours = split /,/, $HumRefHours{ "$OutHumDate" };
        $Pos = 0;
        $OutHumNextDay = $false;
        while ($OutHumByHours[$Pos] ne "") {
            $OutHum = $OutHumByHours[$Pos];
            $OutHumTime = "$HumRefHours[$Pos]:00";
#
            if ($OutHumNextDay || $OutHumTime eq "00:00:00") {
                $_ = $OutHumDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $OutHumDateAdjusted = "$year-$month-$day";
                $OutHumNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $OutHumTime;
            ($year, $month, $day) = split /-/, $OutHumDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$OutHumDateAdjusted $OutHumTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $OutHumIndexPtr = $ref->{RecID};
            if ($OutHumIndexPtr eq "") {
                $bOutHumDateTime = $OutHumDateAdjusted;
                $bOutHumDateTime =~ s/-//g;
                $bOutHumDateTime = "$bOutHumDateTime-$OutHumTime";
                $bOutHumDateTime =~ s/:00$//;
                if ($times{ "$bOutHumDateTime" } eq "") {
                    $bOutHumDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bOutHumDateTime" } = "$OutHumDateAdjusted $OutHumTime,$1,$2,$3,$4,$5,00";
                }
                $OutHumIndexPtr = setupIndex($bOutHumDateTime);
            }

            $Query = "SELECT RecID, OutHum FROM Hourly where RecID = \"$OutHumIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $OutHumDB = $ref->{OutHum};

            if ($OutHumIndexPtr ne "" && $OutHumDB ne $OutHum) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, OutHum) VALUES (\"$OutHumIndexPtr\", \"$OutHum\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET OutHum = \"$OutHum\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

    $InHumDate = $Date;
    $InHumDateAdjusted = $DateAdjusted;
    if ($InHumByHours{ "$InHumDate" } ne "" && $HumRefHours{ "$InHumDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Indoor Humidity... "; } else { print STDERR "."; } }
        @InHumByHours = split /,/, $InHumByHours{ "$InHumDate" };
        @HumRefHours = split /,/, $HumRefHours{ "$InHumDate" };
        $Pos = 0;
        $InHumNextDay = $false;
        while ($InHumByHours[$Pos] ne "") {
            $InHum = $InHumByHours[$Pos];
            $InHumTime = "$HumRefHours[$Pos]:00";
#
            if ($InHumNextDay || $InHumTime eq "00:00:00") {
                $_ = $InHumDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $InHumDateAdjusted = "$year-$month-$day";
                $InHumNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $InHumTime;
            ($year, $month, $day) = split /-/, $InHumDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$InHumDateAdjusted $InHumTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $InHumIndexPtr = $ref->{RecID};
            if ($InHumIndexPtr eq "") {
                $bInHumDateTime = $InHumDateAdjusted;
                $bInHumDateTime =~ s/-//g;
                $bInHumDateTime = "$bInHumDateTime-$InHumTime";
                $bInHumDateTime =~ s/:00$//;
                if ($times{ "$bInHumDateTime" } eq "") {
                    $bInHumDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bInHumDateTime" } = "$InHumDateAdjusted $InHumTime,$1,$2,$3,$4,$5,00";
                }
                $InHumIndexPtr = setupIndex($bInHumDateTime);
            }

            $Query = "SELECT RecID, InHum FROM Hourly where RecID = \"$InHumIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $InHumDB = $ref->{InHum};

            if ($InHumIndexPtr ne "" && $InHumDB ne $InHum) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, InHum) VALUES (\"$InHumIndexPtr\", \"$InHum\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET InHum = \"$InHum\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

#$RainByHour{
    $RainDate = $Date;
    $RainDateAdjusted = $DateAdjusted;
    if ($RainByHours{ "$RainDate" } ne "" && $RainRefHours{ "$RainDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Rain... "; } else { print STDERR "."; } }
        @RainByHours = split /,/, $RainByHours{ "$RainDate" };
        @RainRefHours = split /,/, $RainRefHours{ "$RainDate" };
        $Pos = 0;
        $RainNextDay = $false;
        while ($RainByHours[$Pos] ne "") {
            $Rain = $RainByHours[$Pos];
            $RainTime = "$RainRefHours[$Pos]:00";
#
            if ($RainNextDay || $RainTime eq "00:00:00") {
                $_ = $RainDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $RainDateAdjusted = "$year-$month-$day";
                $RainNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $RainTime;
            ($year, $month, $day) = split /-/, $RainDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$RainDateAdjusted $RainTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $RainIndexPtr = $ref->{RecID};
            if ($RainIndexPtr eq "") {
                $bRainDateTime = $RainDateAdjusted;
                $bRainDateTime =~ s/-//g;
                $bRainDateTime = "$bRainDateTime-$RainTime";
                $bRainDateTime =~ s/:00$//;
                if ($times{ "$bRainDateTime" } eq "") {
                    $bRainDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bRainDateTime" } = "$RainDateAdjusted $RainTime,$1,$2,$3,$4,$5,00";
                }
                $RainIndexPtr = setupIndex($bRainDateTime);
            }

            $Query = "SELECT RecID, RainAmount FROM Hourly where RecID = \"$RainIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $RainDB = $ref->{RainAmount};

            if ($RainIndexPtr ne "" && $RainDB ne $Rain) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, RainAmount) VALUES (\"$RainIndexPtr\", \"$Rain\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET RainAmount = \"$Rain\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

#$RainRateByHours{
    $RainRateDate = $Date;
    $RainRateDateAdjusted = $DateAdjusted;
    if ($RainRateByHours{ "$RainRateDate" } ne "" && $RainRefHours{ "$RainRateDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Rain Rate... "; } else { print STDERR "."; } }
        @RainRateByHours = split /,/, $RainRateByHours{ "$RainRateDate" };
        @RainRefHours = split /,/, $RainRefHours{ "$RainRateDate" };
        $Pos = 0;
        $RainRateNextDay = $false;
        while ($RainRateByHours[$Pos] ne "") {
            $RainRate = $RainRateByHours[$Pos];
            $RainRateTime = "$RainRefHours[$Pos]:00";
#
            if ($RainRateNextDay || $RainRateTime eq "00:00:00") {
                $_ = $RainRateDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $RainRateDateAdjusted = "$year-$month-$day";
                $RainRateNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $RainRateTime;
            ($year, $month, $day) = split /-/, $RainRateDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$RainRateDateAdjusted $RainRateTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $RainRateIndexPtr = $ref->{RecID};
            if ($RainRateIndexPtr eq "") {
                $bRainRateDateTime = $RainRateDateAdjusted;
                $bRainRateDateTime =~ s/-//g;
                $bRainRateDateTime = "$bRainRateDateTime-$RainRateTime";
                $bRainRateDateTime =~ s/:00$//;
                if ($times{ "$bRainRateDateTime" } eq "") {
                    $bRainRateDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bRainRateDateTime" } = "$RainRateDateAdjusted $RainRateTime,$1,$2,$3,$4,$5,00";
                }
                $RainRateIndexPtr = setupIndex($bRainRateDateTime);
            }

            $Query = "SELECT RecID, RainRate FROM Hourly where RecID = \"$RainRateIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $RainRateDB = $ref->{RainRate};

            if ($RainRateIndexPtr ne "" && $RainRateDB ne $RainRate) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, RainRate) VALUES (\"$RainRateIndexPtr\", \"$RainRate\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET RainRate = \"$RainRate\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }


    $BaroDate = $Date;
    $BaroDateAdjusted = $DateAdjusted;
    if ($BarByHours{ "$BaroDate" } ne "" && $BarRefHours{ "$BaroDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Barometer... "; } else { print STDERR "."; } }
        @BarByHours = split /,/, $BarByHours{ "$BaroDate" };
        @BarRefHours = split /,/, $BarRefHours{ "$BaroDate" };
        $Pos = 0;
        $BaroNextDay = $false;
        while ($BarByHours[$Pos] ne "") {
            $Baro = $BarByHours[$Pos];
            $BaroTime = "$BarRefHours[$Pos]:00";
#
            if ($BaroNextDay || $BaroTime eq "00:00:00") {
                $_ = $BaroDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $BaroDateAdjusted = "$year-$month-$day";
                $BaroNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $BaroTime;
            ($year, $month, $day) = split /-/, $BaroDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$BaroDateAdjusted $BaroTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $BaroIndexPtr = $ref->{RecID};
            if ($BaroIndexPtr eq "") {
                $bBaroDateTime = $BaroDateAdjusted;
                $bBaroDateTime =~ s/-//g;
                $bBaroDateTime = "$bBaroDateTime-$BaroTime";
                $bBaroDateTime =~ s/:00$//;
                if ($times{ "$bBaroDateTime" } eq "") {
                    $bBaroDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bBaroDateTime" } = "$BaroDateAdjusted $BaroTime,$1,$2,$3,$4,$5,00";
                }
                $BaroIndexPtr = setupIndex($bBaroDateTime);
            }

            $Query = "SELECT RecID, Barometer FROM Hourly where RecID = \"$BaroIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $BaroDB = $ref->{Barometer};

            if ($BaroIndexPtr ne "" && $BaroDB ne $Baro) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, Barometer) VALUES (\"$BaroIndexPtr\", \"$Baro\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET Barometer = \"$Baro\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

    $DewDate = $Date;
    $DewDateAdjusted = $DateAdjusted;
    if ($DewByHours{ "$DewDate" } ne "" && $DewRefHours{ "$DewDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Dew Point... "; } else { print STDERR "."; } }
        @DewByHours = split /,/, $DewByHours{ "$DewDate" };
        @DewRefHours = split /,/, $DewRefHours{ "$DewDate" };
        $Pos = 0;
        $DewNextDay = $false;
        while ($DewByHours[$Pos] ne "") {
            $DewPoint = $DewByHours[$Pos];
            $DewPointTime = "$DewRefHours[$Pos]:00";
#
            if ($DewNextDay || $DewPointTime eq "00:00:00") {
                $_ = $DewDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $DewDateAdjusted = "$year-$month-$day";
                $DewNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $DewPointTime;
            ($year, $month, $day) = split /-/, $DewDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$DewDateAdjusted $DewPointTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $DewIndexPtr = $ref->{RecID};
            if ($DewIndexPtr eq "") {
                $bDewDateTime = $DewDateAdjusted;
                $bDewDateTime =~ s/-//g;
                $bDewDateTime = "$bDewDateTime-$DewPointTime";
                $bDewDateTime =~ s/:00$//;
                if ($times{ "$bDewDateTime" } eq "") {
                    $bDewDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bDewDateTime" } = "$DewDateAdjusted $DewPointTime,$1,$2,$3,$4,$5,00";
                }
                $DewIndexPtr = setupIndex($bDewDateTime);
            }

            $Query = "SELECT RecID, DewPoint FROM Hourly where RecID = \"$DewIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $DewPointDB = $ref->{DewPoint};

            if ($DewIndexPtr ne "" && $DewPointDB ne $DewPoint) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, DewPoint) VALUES (\"$DewIndexPtr\", \"$DewPoint\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET DewPoint = \"$DewPoint\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

#$ChillLoByHours{
    $WindChillDate = $Date;
    $WindChillDateAdjusted = $DateAdjusted;
    if ($ChillLoByHours{ "$WindChillDate" } ne "" && $ChillLoRefHours{ "$WindChillDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Wind Chill... "; } else { print STDERR "."; } }
        @ChillLoByHours = split /,/, $ChillLoByHours{ "$WindChillDate" };
        @ChillLoRefHours = split /,/, $ChillLoRefHours{ "$WindChillDate" };
        $Pos = 0;
        $WindChillNextDay = $false;
        while ($ChillLoByHours[$Pos] ne "") {
            $WindChill = $ChillLoByHours[$Pos];
            $WindChillTime = "$ChillLoRefHours[$Pos]:00";
#
            if ($WindChillNextDay || $WindChillTime eq "00:00:00") {
                $_ = $WindChillDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $WindChillDateAdjusted = "$year-$month-$day";
                $WindChillNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $WindChillTime;
            ($year, $month, $day) = split /-/, $WindChillDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$WindChillDateAdjusted $WindChillTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $WindChillIndexPtr = $ref->{RecID};
            if ($WindChillIndexPtr eq "") {
                $bWindChillDateTime = $WindChillDateAdjusted;
                $bWindChillDateTime =~ s/-//g;
                $bWindChillDateTime = "$bWindChillDateTime-$WindChillTime";
                $bWindChillDateTime =~ s/:00$//;
                if ($times{ "$bWindChillDateTime" } eq "") {
                    $bWindChillDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bWindChillDateTime" } = "$WindChillDateAdjusted $WindChillTime,$1,$2,$3,$4,$5,00";
                }
                $WindChillIndexPtr = setupIndex($bWindChillDateTime);
            }

            $Query = "SELECT RecID, WindChill FROM Hourly where RecID = \"$WindChillIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $WindChillDB = $ref->{WindChill};

            if ($WindChillIndexPtr ne "" && $WindChillDB ne $WindChill) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, WindChill) VALUES (\"$WindChillIndexPtr\", \"$WindChill\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET WindChill = \"$WindChill\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

#$HeatHiByHours{
    $HeatIndexDate = $Date;
    $HeatIndexDateAdjusted = $DateAdjusted;
    if ($HeatHiByHours{ "$HeatIndexDate" } ne "" && $HeatHiRefHours{ "$HeatIndexDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Heat Index... "; } else { print STDERR "."; } }
        @HeatHiByHours = split /,/, $HeatHiByHours{ "$HeatIndexDate" };
        @HeatHiRefHours = split /,/, $HeatHiRefHours{ "$HeatIndexDate" };
        $Pos = 0;
        $HeatIndexNextDay = $false;
        while ($HeatHiByHours[$Pos] ne "") {
            $HeatIndex = $HeatHiByHours[$Pos];
            $HeatIndexTime = "$HeatHiRefHours[$Pos]:00";
#
            if ($HeatIndexNextDay || $HeatIndexTime eq "00:00:00") {
                $_ = $HeatIndexDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $HeatIndexDateAdjusted = "$year-$month-$day";
                $HeatIndexNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $HeatIndexTime;
            ($year, $month, $day) = split /-/, $HeatIndexDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$HeatIndexDateAdjusted $HeatIndexTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $HeatIndexIndexPtr = $ref->{RecID};
            if ($HeatIndexIndexPtr eq "") {
                $bHeatIndexDateTime = $HeatIndexDateAdjusted;
                $bHeatIndexDateTime =~ s/-//g;
                $bHeatIndexDateTime = "$bHeatIndexDateTime-$HeatIndexTime";
                $bHeatIndexDateTime =~ s/:00$//;
                if ($times{ "$bHeatIndexDateTime" } eq "") {
                    $bHeatIndexDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bHeatIndexDateTime" } = "$HeatIndexDateAdjusted $HeatIndexTime,$1,$2,$3,$4,$5,00";
                }
                $HeatIndexIndexPtr = setupIndex($bHeatIndexDateTime);
            }

            $Query = "SELECT RecID, HeatIndex FROM Hourly where RecID = \"$HeatIndexIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $HeatIndexDB = $ref->{HeatIndex};

            if ($HeatIndexIndexPtr ne "" && $HeatIndexDB ne $HeatIndex) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, HeatIndex) VALUES (\"$HeatIndexIndexPtr\", \"$HeatIndex\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET HeatIndex = \"$HeatIndex\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }


    $THSWDate = $Date;
    $THSWDateAdjusted = $DateAdjusted;
    if ($THSWByHours{ "$THSWDate" } ne "" && $THSWRefHours{ "$THSWDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly THSW Index... "; } else { print STDERR "."; } }
        @THSWByHours = split /,/, $THSWByHours{ "$THSWDate" };
        @THSWRefHours = split /,/, $THSWRefHours{ "$THSWDate" };
        $Pos = 0;
        $THSWNextDay = $false;
        while ($THSWByHours[$Pos] ne "") {
            $THSW = $THSWByHours[$Pos];
            $THSWTime = "$THSWRefHours[$Pos]:00";
#
            if ($THSWNextDay || $THSWTime eq "00:00:00") {
                $_ = $THSWDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $THSWDateAdjusted = "$year-$month-$day";
                $THSWNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $THSWTime;
            ($year, $month, $day) = split /-/, $THSWDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$THSWDateAdjusted $THSWTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $THSWIndexPtr = $ref->{RecID};
            if ($THSWIndexPtr eq "") {
                $bTHSWDateTime = $THSWDateAdjusted;
                $bTHSWDateTime =~ s/-//g;
                $bTHSWDateTime = "$bTHSWDateTime-$THSWTime";
                $bTHSWDateTime =~ s/:00$//;
                if ($times{ "$bTHSWDateTime" } eq "") {
                    $bTHSWDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bTHSWDateTime" } = "$THSWDateAdjusted $THSWTime,$1,$2,$3,$4,$5,00";
                }
                $THSWIndexPtr = setupIndex($bTHSWDateTime);
            }

            $Query = "SELECT RecID, THSWIndex FROM Hourly where RecID = \"$THSWIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $THSWIndexDB = $ref->{THSWIndex};

            if ($THSWIndexPtr ne "" && $THSWIndexDB ne $THSW) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, THSWIndex) VALUES (\"$THSWIndexPtr\", \"$THSW\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET THSWIndex = \"$THSW\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

    $ETDate = $Date;
    $ETDateAdjusted = $DateAdjusted;
    if ($ETByHours{ "$ETDate" } ne "" && $ETRefHours{ "$ETDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly ET... "; } else { print STDERR "."; } }
        @ETByHours = split /,/, $ETByHours{ "$ETDate" };
        @ETRefHours = split /,/, $ETRefHours{ "$ETDate" };
        $Pos = 0;
        $ETNextDay = $false;
        while ($ETByHours[$Pos] ne "") {
            $ET = $ETByHours[$Pos];
            $ETTime = "$ETRefHours[$Pos]:00";
#
            if ($ETNextDay || $ETTime eq "00:00:00") {
                $_ = $ETDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $ETDateAdjusted = "$year-$month-$day";
                $ETNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $ETTime;
            ($year, $month, $day) = split /-/, $ETDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$ETDateAdjusted $ETTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $ETIndexPtr = $ref->{RecID};
            if ($ETIndexPtr eq "") {
                $bETDateTime = $ETDateAdjusted;
                $bETDateTime =~ s/-//g;
                $bETDateTime = "$bETDateTime-$ETTime";
                $bETDateTime =~ s/:00$//;
                if ($times{ "$bETDateTime" } eq "") {
                    $bETDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bETDateTime" } = "$ETDateAdjusted $ETTime,$1,$2,$3,$4,$5,00";
                }
                $ETIndexPtr = setupIndex($bETDateTime);
            }

            $Query = "SELECT RecID, ET FROM Hourly where RecID = \"$ETIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $ETDB = $ref->{ET};

            if ($ETIndexPtr ne "" && $ETDB ne $ET) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, ET) VALUES (\"$ETIndexPtr\", \"$ET\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET ET = \"$ET\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

    $SolarDate = $Date;
    $SolarDateAdjusted = $DateAdjusted;
    if ($SolarByHourAvg{ "$SolarDate" } ne "" && $SolarRefHours{ "$SolarDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Solar... "; } else { print STDERR "."; } }
        @SolarByHours = split /,/, $SolarByHourAvg{ "$SolarDate" };
        @SolarRefHours = split /,/, $SolarRefHours{ "$SolarDate" };
        $Pos = 0;
        $SolarNextDay = $false;
        while ($SolarByHours[$Pos] ne "") {
            $Solar = $SolarByHours[$Pos];
            $SolarTime = "$SolarRefHours[$Pos]:00";
#
            if ($SolarNextDay || $SolarTime eq "00:00:00") {
                $_ = $SolarDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $SolarDateAdjusted = "$year-$month-$day";
                $SolarNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $SolarTime;
            ($year, $month, $day) = split /-/, $SolarDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$SolarDateAdjusted $SolarTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $SolarIndexPtr = $ref->{RecID};
            if ($SolarIndexPtr eq "") {
                $bSolarDateTime = $SolarDateAdjusted;
                $bSolarDateTime =~ s/-//g;
                $bSolarDateTime = "$bSolarDateTime-$SolarTime";
                $bSolarDateTime =~ s/:00$//;
                if ($times{ "$bSolarDateTime" } eq "") {
                    $bSolarDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bSolarDateTime" } = "$SolarDateAdjusted $SolarTime,$1,$2,$3,$4,$5,00";
                }
                $SolarIndexPtr = setupIndex($bSolarDateTime);
            }

            $Query = "SELECT RecID, SolarRadAvg FROM Hourly where RecID = \"$SolarIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $SolarDB = $ref->{SolarRadAvg};

            if ($SolarIndexPtr ne "" && $SolarDB ne $Solar) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, SolarRadAvg) VALUES (\"$SolarIndexPtr\", \"$Solar\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET SolarRadAvg = \"$Solar\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

    $WindSpeedDate = $Date;
    $WindSpeedDateAdjusted = $DateAdjusted;
    if ($WindAvgByHours{ "$WindSpeedDate" } ne "" && $WindRefHours{ "$WindSpeedDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Wind... "; } else { print STDERR "."; } }
        @WindByHours = split /,/, $WindAvgByHours{ "$WindSpeedDate" };
        @WindRefHours = split /,/, $WindRefHours{ "$WindSpeedDate" };
        $Pos = 0;
        $WindSpeedNextDay = $false;
        while ($WindByHours[$Pos] ne "") {
            $WindSpeed = $WindByHours[$Pos];
            $WindSpeedTime = "$WindRefHours[$Pos]:00";
#
            if ($WindSpeedNextDay || $WindSpeedTime eq "00:00:00") {
                $_ = $WindSpeedDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $WindSpeedDateAdjusted = "$year-$month-$day";
                $WindSpeedNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $WindSpeedTime;
            ($year, $month, $day) = split /-/, $WindSpeedDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$WindSpeedDateAdjusted $WindSpeedTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $WindSpeedIndexPtr = $ref->{RecID};
            if ($WindSpeedIndexPtr eq "") {
                $bWindSpeedDateTime = $WindSpeedDateAdjusted;
                $bWindSpeedDateTime =~ s/-//g;
                $bWindSpeedDateTime = "$bWindSpeedDateTime-$WindSpeedTime";
                $bWindSpeedDateTime =~ s/:00$//;
                if ($times{ "$bWindSpeedDateTime" } eq "") {
                    $bWindSpeedDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bWindSpeedDateTime" } = "$WindSpeedDateAdjusted $WindSpeedTime,$1,$2,$3,$4,$5,00";
                }
                $WindSpeedIndexPtr = setupIndex($bWindSpeedDateTime);
            }

            $Query = "SELECT RecID, WindSpeedAvg FROM Hourly where RecID = \"$WindSpeedIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $WindSpeedDB = $ref->{WindSpeedAvg};

            if ($WindSpeedIndexPtr ne "" && $WindSpeedDB ne $WindSpeed) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, WindSpeedAvg) VALUES (\"$WindSpeedIndexPtr\", \"$WindSpeed\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET WindSpeedAvg = \"$WindSpeed\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

#$WindHiByHours{
    $WindGustDate = $Date;
    $WindGustDateAdjusted = $DateAdjusted;
    if ($WindHiByHours{ "$WindGustDate" } ne "" && $WindRefHours{ "$WindGustDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Wind Gust... "; } else { print STDERR "."; } }
        @WindHiByHours = split /,/, $WindHiByHours{ "$WindGustDate" };
        @WindRefHours = split /,/, $WindRefHours{ "$WindGustDate" };
        $Pos = 0;
        $WindGustNextDay = $false;
        while ($WindHiByHours[$Pos] ne "") {
            $WindGust = $WindHiByHours[$Pos];
            $WindGustTime = "$WindRefHours[$Pos]:00";
#
            if ($WindGustNextDay || $WindGustTime eq "00:00:00") {
                $_ = $WindGustDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $WindGustDateAdjusted = "$year-$month-$day";
                $WindGustNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $WindGustTime;
            ($year, $month, $day) = split /-/, $WindGustDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$WindGustDateAdjusted $WindGustTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $WindGustIndexPtr = $ref->{RecID};
            if ($WindGustIndexPtr eq "") {
                $bWindGustDateTime = $WindGustDateAdjusted;
                $bWindGustDateTime =~ s/-//g;
                $bWindGustDateTime = "$bWindGustDateTime-$WindGustTime";
                $bWindGustDateTime =~ s/:00$//;
                if ($times{ "$bWindGustDateTime" } eq "") {
                    $bWindGustDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bWindGustDateTime" } = "$WindGustDateAdjusted $WindGustTime,$1,$2,$3,$4,$5,00";
                }
                $WindGustIndexPtr = setupIndex($bWindGustDateTime);
            }

            $Query = "SELECT RecID, WindSpeedHigh FROM Hourly where RecID = \"$WindGustIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $WindGustDB = $ref->{WindSpeedHigh};

            if ($WindGustIndexPtr ne "" && $WindGustDB ne $WindGust) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, WindSpeedHigh) VALUES (\"$WindGustIndexPtr\", \"$WindGust\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET WindSpeedHigh = \"$WindGust\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }


    $WindDirDate = $Date;
    $WindDirDateAdjusted = $DateAdjusted;
    if ($WindDirByHours{ "$WindDirDate" } ne "" && $WindRefHours{ "$WindDirDate" } ne "") {
        if (! $quiet) { if ($verbose) { print STDERR "processing hourly Wind Dir... "; } else { print STDERR "."; } }
        @WindDirByHours = split /,/, $WindDirByHours{ "$WindDirDate" };
        @WindRefHours = split /,/, $WindRefHours{ "$WindDirDate" };
        $Pos = 0;
        $WindDirNextDay = $false;
        while ($WindByHours[$Pos] ne "") {
            $WindDir = $WindDirByHours[$Pos];
            $WindDirTime = "$WindRefHours[$Pos]:00";
#
            if ($WindDirNextDay || $WindDirTime eq "00:00:00") {
                $_ = $WindDirDateAdjusted;
                /(\d\d\d\d)-(\d\d)-(\d\d)/;
                $year = $1;
                $month = $2;
                $day = $3;
                ($year, $month, $day) = Add_Delta_Days($year, $month, $day, +1);
                $month = sprintf "%02i", $month;
                $day = sprintf "%02i", $day;
                $WindDirDateAdjusted = "$year-$month-$day";
                $WindDirNextDay = $false;
            }
            ($hour, $minute, $second) = split /:/, $WindDirTime;
            ($year, $month, $day) = split /-/, $WindDirDateAdjusted;
            ($TT, $second, $minute, $hour, $day, $month, $year, $DST) = getDST($second, $minute, $hour, $day, $month, $year);

            $Query = "SELECT RecID FROM DT where bDateTime = \"$WindDirDateAdjusted $WindDirTime\" && DST = \"$DST\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $WindDirIndexPtr = $ref->{RecID};
            if ($WindDirIndexPtr eq "") {
                $bWindDirDateTime = $WindDirDateAdjusted;
                $bWindDirDateTime =~ s/-//g;
                $bWindDirDateTime = "$bWindDirDateTime-$WindDirTime";
                $bWindDirDateTime =~ s/:00$//;
                if ($times{ "$bWindDirDateTime" } eq "") {
                    $bWindDirDateTime =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d):(\d\d)/;
                    $times{ "$bWindDirDateTime" } = "$WindDirDateAdjusted $WindDirTime,$1,$2,$3,$4,$5,00";
                }
                $WindDirIndexPtr = setupIndex($bWindDirDateTime);
            }

            $Query = "SELECT RecID, WindDirAvg FROM Hourly where RecID = \"$WindDirIndexPtr\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();

            $ref = $sth->fetchrow_hashref();
            $HourlyRecID = $ref->{RecID};
            $WindDirDB = $ref->{WindDirAvg};

           if ($WindDirIndexPtr ne "" && $WindDirDB ne $WindDir) {
                if ($HourlyRecID eq "") {
                    $Query  = "INSERT INTO Hourly (RecID, WindDirAvg) VALUES (\"$WindDirIndexPtr\", \"$WindSpeed\")";
                    $rows = $dbh->do($Query);
                } else {
                    $Query  = "UPDATE Hourly SET WindDirAvg = \"$WindDir\" WHERE RecID = \"$HourlyRecID\"";
                    $rows = $dbh->do($Query);
                }
            }
            $Pos++;
        }
    }

}

if ($StormsStart[0] ne "") {
    if (! $quiet) { if ($verbose) { print STDERR "processing Rain Storms... "; } else { print STDERR "."; } }
    $StormNum = 0;
    while ($StormsStart[$StormNum] ne "") {
        if ($StormsStart[$StormNum] ne "n/a") {
            $_ = $StormsStart[$StormNum];
            /(\d\d)-(\w\w\w)-(\d\d\d\d)/;
            $StormStartedDate = "$3-$month{ \"$2\" }-$1";
            $_ = $StormsEnd[$StormNum];
            /(\d\d)-(\w\w\w)-(\d\d\d\d)/;
            if ($1 ne "" && $2 ne "" && $3 ne "") {
                $StormEndedDate = "$3-$month{ \"$2\" }-$1";
            } else {
                $StormEndedDate = "";
            }
#
            $Query = "SELECT t1.RecID, t2.StormStartedDate, t2.StormAmount FROM DT AS t1 INNER JOIN Rain AS t2 USING (RecID) where t1.bDate = \"$StormStartedDate\" ORDER BY t1.bTime";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $StormPtr = "";
            while ($StormPtr eq "" && ($ref=$sth->fetchrow_hashref())) {
                if ($ref->{StormAmount} > 0) {
                    $StormPtr = $ref->{RecID};
                    $StormStartedDateDB = $ref->{StormStartedDate};
                }
            }

            $Query = "SELECT StormStartedDate, StormEndedDate, StormAmount, HighestRate, HighestRateDT FROM Storms WHERE StormStartedDate = \"$StormStartedDate\"";
            $sth = $dbh->prepare($Query);
            $sth->execute();
            $ref = $sth->fetchrow_hashref();
            $StormStartedDateDB = $ref->{StormStartedDate};
            $StormEndedDateDB = $ref->{StormEndedDate};
            $StormAmountDB = $ref->{StormAmount};
            $HighestRateDB = $ref->{HighestRate};
            $HighestRateDTDB = $ref->{HighestRateDT};

            $StormRate = "";
            $StormRateDT = "";
            if ($StormStartedDate ne "" && $StormEndedDate ne "") {
                $Query = "SELECT t1.bDateTime, t2.Rate FROM DT AS t1 INNER JOIN Rain AS t2 USING (RecID) where t1.bDateTime >= \"$StormStartedDate 00:00:00\" && t1.bDateTime <= \"$StormEndedDate 23:59:59\" && t2.StormAmount > 0 ORDER BY t1.bDateTime";
                $sth = $dbh->prepare($Query);
                $sth->execute();
                while ($ref=$sth->fetchrow_hashref()) {
                    if ($ref->{Rate} > $StormRate) {
                        $StormRate = $ref->{Rate};
                        $StormRateDT = $ref->{bDateTime};
                    }
                }
                if ($StormRate eq "" || $StormRateDT eq "") {
                    $Query = "SELECT t1.bDateTime, t2.RainRate FROM DT AS t1 INNER JOIN Hourly AS t2 USING (RecID) where t1.bDateTime >= \"$StormStartedDate 00:00:00\" && t1.bDateTime <= \"$StormEndedDate 23:59:59\" && t2.RainAmount > 0 ORDER BY t1.bDateTime";
                    $sth = $dbh->prepare($Query);
                    $sth->execute();
                    while ($ref=$sth->fetchrow_hashref()) {
                        if ($ref->{RainRate} > $StormRate) {
                            $StormRate = $ref->{RainRate};
                            $StormRateDT = $ref->{bDateTime};
                        }
                    }
                }
            }

#
            if ($StormStartedDateDB ne "$StormStartedDate") {
                $Query  = "UPDATE Rain SET StormStartedDate = \"$StormStartedDate\" WHERE RecID = \"$StormPtr\"";
                $rows = $dbh->do($Query);
            }
#
            if ("$StormStartedDateDB" ne "$StormStartedDate") {
                $Attr = "StormStartedDate, StormEndedDate, StormAmount";
                $Values = "\"$StormStartedDate\", \"$StormEndedDate\", \"$StormsAmount[$StormNum]\"";
                if ($StormRate ne "") {
                    $Attr = "$Attr, HighestRate";
                    $Values = "$Values, \"$StormRate\"";
                }
                if ($StormRateDT ne "") {
                    $Attr = "$Attr, HighestRateDT";
                    $Values = "$Values, \"$StormRateDT\"";
                }
                $Query  = "INSERT INTO Storms ($Attr) values ($Values)";
                $rows = $dbh->do($Query);
            } else {
                if ($StormEndedDateDB ne $StormEndedDate && $StormEndedDate ne "") {
                    $Query  = "UPDATE Storms SET StormEndedDate = \"$StormEndedDate\" WHERE StormStartedDate = \"$StormStartedDate\"";
                    $rows = $dbh->do($Query);
                }
                if ($StormAmountDB ne $StormsAmount[$StormNum] && $StormsAmount[$StormNum] ne "") {
                    $Query  = "UPDATE Storms SET StormAmount = \"$StormsAmount[$StormNum]\" WHERE StormStartedDate = \"$StormStartedDate\"";
                    $rows = $dbh->do($Query);
                }
                if ($HighestRateDB ne $StormRate && $StormRate ne "") {
                    $Query  = "UPDATE Storms SET HighestRate = \"$StormRate\" WHERE StormStartedDate = \"$StormStartedDate\"";
                    $rows = $dbh->do($Query);
                }
                if ($HighestRateDTDB ne $StormRateDT && $StormRateDT ne "") {
                    $Query  = "UPDATE Storms SET HighestRateDT = \"$StormRateDT\" WHERE StormStartedDate = \"$StormStartedDate\"";
                    $rows = $dbh->do($Query);
                }
            }
        }
        $StormNum++;
    }
}

if (! $quiet) { print STDERR "\n"; }

if ($sth) {
    $sth->finish();
} else {
    print "\nDoesn't look like anything was processed\n\n";
}
if ($dbh) { $dbh->disconnect(); }

#
