#!/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";

    $wrfloc1 = "$topdir/patch/gfdl.$decade/data";

    $pruneloc1 = "$topdir/gfdl.$decade/data/stripped";
    $pruneloc2 = "$topdir/patch/gfdl.$decade/patched";
    $badsave   = "$topdir/gfdl.$decade/data/stripped.badsave";


    $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;
	$cinittime = $inittime;

	$mu = $m%12;
	if ($mu == 0) { $mu = 12; }
	$mu = sprintf("%2.2d",$mu);
	$dpm = $dpms[$mu];
	$etime = &changetime10($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;
	}

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

###################################################################################
## Check files.

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

	    HRS : for ($h=0;$h<=$nhrs;$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";
		$cprunefn = "gfdl.d$ddom.1hr.$cdate.nc";
		
		$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";
		$pprunefn = "gfdl.d$ddom.1hr.$pdate.nc";
		
		$ndate = &changetime10($cdate,1);
		$yyyy = substr($ndate,0,4);
		$mm   = substr($ndate,4,2);
		$dd   = substr($ndate,6,2);
		$hh   = substr($ndate,8,2);
		$nyyyymm = "$yyyy$mm";
		$ncdate = "${yyyy}-${mm}-${dd}_${hh}:00:00";
		$nwrffn = "wrfout_d${ddom}_$ncdate";
		$nprunefn = "gfdl.d$ddom.1hr.$ndate.nc";
		
		$mmdd = substr($cdate,4,4);
		if ($mmdd eq "0229") { $leap = "1"; }
		else { $leap = "0"; }
		if (! $leap) {
		    
		    $nochk = "0";
		    $nocfn = "0";
		    $nopfn = "0";
		    $nonfn = "0";
		    
		    $nocprune1 = "0";
		    $chkfile = "$pruneloc1/$cyyyymm/$cprunefn";
		    if ( -s "$chkfile" != 1921840 ) { $nocprune1 = "1"; }
		    
		    $nocprune2 = "0";
		    $chkfile = "$pruneloc2/$cyyyymm/$cprunefn";
		    if ( -s "$chkfile" < 1000 ) { $nocprune2 = "1"; }
		    
		    $nonprune1 = "0";
		    $chkfile = "$pruneloc1/$nyyyymm/$nprunefn";
		    if ( -s "$chkfile" < 1000 ) { $nonprune1 = "1"; }
		    
		    $nonprune2 = "0";
		    $chkfile = "$pruneloc2/$cyyyymm/$nprunefn";
		    if ( -s "$chkfile" < 1000 ) { $nonprune2 = "1"; }
		    
		    $nocwrf1 = "0";
		    $chkfile = "$wrfloc1/$cyyyymm/$cwrffn";
		    if ( -s "$chkfile" < 1000 ) { $nocwrf1 = "1"; }
		    
		    $nopwrf1 = "0";
		    $chkfile = "$wrfloc1/$cyyyymm/$pwrffn";
		    if ( -s "$chkfile" < 1000 ) { $nopwrf1 = "1"; }
		    
		    $nonwrf1 = "0";
		    $chkfile = "$wrfloc1/$cyyyymm/$nwrffn";
		    if ( -s "$chkfile" < 1000 ) { $nonwrf1 = "1"; }
		    
		    
		    if ($nocprune1) {
			print ("BAD $decade $cdate\n");
			print ("NOCPRUNE1 $nocprune1 $pruneloc1/$cyyyymm/$cprunefn\n");
			print ("NOCPRUNE2 $nocprune2 $pruneloc2/$cyyyymm/$cprunefn\n");
			print ("NONPRUNE1 $nonprune1 $pruneloc1/$nyyyymm/$nprunefn\n");
			print ("NONPRUNE2 $nonprune2 $pruneloc2/$nyyyymm/$nprunefn\n");
			print ("NOPWRF1   $nopwrf1 $wrfloc1/$cyyyymm/$pwrffn\n");
			print ("NOCWRF1   $nocwrf1 $wrfloc1/$cyyyymm/$cwrffn\n");
			print ("NONWRF1   $nonwrf1 $wrfloc1/$cyyyymm/$nwrffn\n");

#			if ((! $nocprune2)&&(! $nonprune1)&&(! $nonprune2)&&
			if ((! $nocprune2)&&(! $nonprune2)&&
			    (! $nopwrf1)&&($nocwrf1)&&(! $nonwrf1)) {

			    if ($mvfiles) {
				print ("Moving files. $cdate\n");
				if ( ! -d "$badsave/$cyyyymm") { system ("mkdir -p $badsave/$cyyyymm"); }
				if ( ! -d "$badsave/$nyyyymm") { system ("mkdir -p $badsave/$nyyyymm"); }
				system ("mv $pruneloc1/$cyyyymm/$cprunefn $badsave/$cyyyymm");
				system ("mv $pruneloc1/$nyyyymm/$nprunefn $badsave/$nyyyymm");
				system ("cp $pruneloc2/$cyyyymm/$cprunefn $pruneloc1/$cyyyymm/$cprunefn");
				system ("touch $pruneloc1/$cyyyymm/$cprunefn.patch.flag");
				system ("cp $pruneloc2/$nyyyymm/$nprunefn $pruneloc1/$nyyyymm/$nprunefn");
				system ("touch $pruneloc1/$nyyyymm/$nprunefn.patch.flag");
			    }
			    else {
				print ("NOT moving files. $cdate\n");
			    }
			    
			}			    

		    }

		    
		    
		} # endif leap
		
	    } # end hours loop
	} # end dom loop
    } # end month loop
} # end decades loop

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