#!/usr/bin/perl -w
# $HeadURL: file:///afs/sipb.mit.edu/contrib/weather/SVNRep/tools/update.pl $
# $Id: update.pl 14 2007-09-18 21:03:34Z foley $
# Program for grabbing climate data from the NOAA
# By Joe Foley <foley@mit.edu>

use strict;

# lets see where we're at
my $pwd = `pwd`;  chomp($pwd);
my $basedir = "$pwd";
if($pwd =~ m|^/afs/sipb(.mit.edu)?/contrib/weather|) {
    $basedir = "/afs/sipb.mit.edu/contrib/weather";
}
if($pwd =~ m|(.*/weather).*|) {
    $basedir = "$1";
}

my $compdir = "$basedir/compressed";
my $uncompdir = "$basedir/uncompressed";

my $baseurl = "http://www5.ncdc.noaa.gov/ulcd";
my $startyear = 1996; 
my $months = 12;  # remember that we are in counting numbers

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
my $wget = "/usr/bin/wget";
my $tar = "/afs/athena.mit.edu/project/gnu/gbin/tar";

my $maxyear = $year;
my $maxmonth = $mon;

my $MODE = shift;

if(!$MODE or $MODE eq "wget") {
    print "Updating entries in $compdir from $startyear to $maxyear.\n";
    for(my $y = $startyear; $y <= $maxyear; $y++) {
	# does the directory exist yet?  If not, make it
	if (! -d "$compdir/$y") {
	    mkdir "$compdir/$y";
	}
	for(my $m = 1; $m <= $months; $m++) {       	
	    # if it's the last year, not done collecting data
	    if($y == $maxyear && $m == $maxmonth) {
		last;
	    }
	    my $strm = sprintf("%02d", $m);
	    my $file = "$y$strm.tar.gz";
	    my $path = "$compdir/$y/$file";
	    print "Checking $path...";
	    if (-e $path) {
		if (-s $path > 0) {
		    print "exists, skipping\n";
		    next;
		}
		else {
		    print "looks like an empty file, continuing...";
		}
	    }
	    my $cmd = "$wget $baseurl/$file -O $path";
	    print "downloading...";
#	    print "Executing $cmd\n";
#	    print "\n";
	    system($cmd);
	    print "done.\n";
	
	}
    }
}

if(!$MODE or $MODE eq "tar") {
    print "Decompressing data into $uncompdir\n";
    for(my $y = $startyear; $y <= $maxyear; $y++) {
	# does the directory exist yet?  If not, make it
	if (! -d "$uncompdir/$y") {
	    mkdir "$uncompdir/$y";
	}
	for(my $m = 1; $m <= $months; $m++) {       	
	    # if it's the last year, not done collecting data
	    if($y == $maxyear && $m == $maxmonth) {
		last;
	    }
	    my $strm = sprintf("%02d", $m);
	    my $file = "$y$strm.tar.gz";
	    my $tarfile = "$compdir/$y/$file";
	    ! -r $tarfile and next;
	    -s $tarfile > 0 or next;
	    my $basepath = "$uncompdir/$y";
	    my @datafiles = qw(dailyavg.txt daily.txt hourly.txt hpd.txt);
	    @datafiles = map {$_ = "$basepath/$y$strm$_"} @datafiles;
	    my $already_unpacked = 1;
	    foreach(@datafiles) {
		my $path = $_;
		print "Checking $path...";
		if (-e $path) {
		    if (-s $path > 0) {
			print "exists, skipping\n";
			next;
		    }
		    else {
			print "empty file, ";
			$already_unpacked = 0;
			last;
		    }			
		}
		else {
		    print "missing, ";
		    $already_unpacked = 0;
		    last;
		}
	    }
	    if(!$already_unpacked) {
		my $cmd = "cd $basepath; $tar -xvpzf $tarfile";
		print "extracting...";
		system($cmd);
		print "\"$cmd\"...";
		print "done.\n";
		sleep 1; 
		## this is to give us a chance of ctl-c'ing
		## while debugging
	    }
	    else {
		print "Nothing missing.\n";
	    }		
	}
    }
}

