#!/usr/bin/perl

###################################################################################
## Set variables.
$mnamelc    = "gfdl";
$mnameuc    = uc $mnamelc;
$version    = "v20160404";
$mvfiles    = "0";

DECS : for ($dec=1970;$dec<=2090;$dec=($dec+10)) {

    $decade = "$dec"."s";
    if ($dec eq "1970") {
	$nmonths = 120;
	$firsttime  = "1970010100";
    }
    else {
	$nmonths = 136;
	$fy = $dec - 2;
	$firsttime  = "$fy"."090100";
    }

    if ($dec ne "2090") { next DECS; }
    print ("$decade\n");
    
    $topdir  = "/home/rocinante/steed/data/gfdl";

    if ($dec < 2025) {
	$wrfloc  = "/home/rocinante/steed/data/archive/cmip5/rcp8.5/gfdl/gfdl.$decade/data";
    }
    else {
	$wrfloc  = "/home/disk/mass/steed/data/archive/cmip5/rcp8.5/gfdl/gfdl.$decade/data";
    }
    $dstloc  = "$topdir/gfdl.$decade/fixnewyear";
    $url     = "https://s3-us-west-2.amazonaws.com/gfdl/gfdl/$version/gfdl.$decade/data";

    $inc     = 1;
    $ndom    = 2;
    $driver  = "$dir/driver";
    $smm     = substr($firsttime,4,2);
    
    @dpms    = ();
    @dpms    = (0,31,28,31,30,31,30,31,31,30,31,30,31);

    require "$topdir/utils.pl";

################################################################################
## Loop over months.
    $etime = $firsttime;
    MONTHS : for ($m=$smm;$m<=($smm+($nmonths-1));$m++) {

	$pinittime = $inittime;
	$inittime = $etime;

	$mu = $m%12;
	if ($mu == 0) { $mu = 12; }
	$mu = sprintf("%2.2d",$mu);
	$dpm = $dpms[$mu];
	$etime = &ct10($inittime,($dpm*24));

	$ninittime = $etime;

	($ddiff,$sdiff)  = &td("${inittime}0000","yy","${etime}0000","yy");
	$hhhh = ($ddiff*24)+($sdiff/3600);
	$ntimes = ($hhhh / $inc) + 1;
	$nhrs = $hhhh;

	if ($inittime eq $firsttime) {
	    $inittime = &changetime10($inittime,24);
	    $nhrs = $nhrs - 24;
	}
	$cinittime = $inittime;

	$pyyyymm = substr($pinittime,0,6);
	$cyyyymm = substr($cinittime,0,6);
	$nyyyymm = substr($ninittime,0,6);

	$cmm = substr($cinittime,4,2);
	if ($cmm ne "01") { next MONTHS; }
#	if ($m ne "73") { next MONTHS; }
	print ("$m $cinittime $pinittime\n");

#    }
#}
#exit 0;
#{
#    {
	
###################################################################################
## Check files.

	for ($d=2;$d<=$ndom;$d++) {
	    $ddom = sprintf("%2.2d",$d);

	    HRS : for ($h=0;$h<=0;$h++) {

		if (($m == $smm)&&($h == 0)) { next HRS; }
		if (($m == ($smm+($nmonths-1)))&&($h == $nhrs)) { next HRS;  }
	    
		$cdate = &changetime10($inittime,$h);
		$yyyy = substr($cdate,0,4);
		$mm   = substr($cdate,4,2);
		$dd   = substr($cdate,6,2);
		$hh   = substr($cdate,8,2);
		$cyyyymm = "$yyyy$mm";
		$ccdate = "${yyyy}-${mm}-${dd}_${hh}:00:00";
		$cwrffn = "wrfout_d${ddom}_$ccdate";
		
		$pdate = &changetime10($cdate,-1);
		$yyyy = substr($pdate,0,4);
		$mm   = substr($pdate,4,2);
		$dd   = substr($pdate,6,2);
		$hh   = substr($pdate,8,2);
		$pyyyymm = "$yyyy$mm";
		$pcdate = "${yyyy}-${mm}-${dd}_${hh}:00:00";  
		$pwrffn = "wrfout_d${ddom}_$pcdate";
		
		$mmdd = substr($cdate,4,4);
		if ($mmdd eq "0229") { $leap = "1"; }
		else { $leap = "0"; }
		if ($leap) { next HRS; }

		$dstdir = "$dstloc/$cinittime";
		if ( ! -d "$dstdir" ) { system ("mkdir -p $dstdir"); }

## Pfile.		
		$dstfile = "$dstdir/$pwrffn";

		if ( -s "$dstfile" < 1000 ) {
		    system ("rm -f $dstfile");
		    $chkfile = "$wrfloc/$pinittime/$pwrffn";
		    if ( -s "$chkfile" > 1000 ) {
			system ("cp $chkfile $dstfile");
		    }
		}

		if ( -s "$dstfile" < 1000 ) {
		    system ("rm -f $dstfile");
		    $chkfile = "$wrfloc/$pinittime/$pwrffn.gz";
		    if ( -s "$chkfile" > 1000 ) {
			system ("cp $chkfile $dstfile.gz");
			system ("gunzip $dstfile.gz");
		    }
		}

		if ( -s "$dstfile" < 1000 ) {
		    system ("rm -f $dstfile");
		    chdir ("$dstdir");
		    system ("wget $url/$pinittime/$pwrffn");
		}
		
## Cfile.			    
		$dstfile = "$dstdir/$cwrffn";

		if ( -s "$dstfile" < 1000 ) {
		    system ("rm -f $dstfile");
		    $chkfile = "$wrfloc/$pinittime/$cwrffn";
		    if ( -s "$chkfile" > 1000 ) {
			system ("cp $chkfile $dstfile");
		    }
		}

		if ( -s "$dstfile" < 1000 ) {
		    system ("rm -f $dstfile");
		    $chkfile = "$wrfloc/$pinittime/$cwrffn.gz";
		    if ( -s "$chkfile" > 1000 ) {
			system ("cp $chkfile $dstfile.gz");
			system ("gunzip $dstfile.gz");
		    }
		}

		if ( -s "$dstfile" < 1000 ) {
		    system ("rm -f $dstfile");
		    chdir ("$dstdir");
		    system ("wget $url/$pinittime/$cwrffn");
		}

		system ("$topdir/strip.gfdl.1-hourly.newyear.pl $decade $cdate $cinittime");
		
	    } # end hours loop
	} # end dom loop
    } # end month loop
} # end decades loop

###################################################################################
## Done.
exit 0;
