#!/usr/bin/perl

##############################################################################
## Set main variables.

$decade     = "1980s";
$ctime      = "1988110122";

$decade     = $ARGV[0];
$ctime      = $ARGV[1];
$cinittime  = $ARGV[2];

$yyyymm     = substr($ctime,0,6);
$d          = 2;
$mnamelc    = "gfdl";
$mnameuc    = uc $mnamelc;
$version    = "v20160404";
$ddom       = sprintf("%2.2d",$d);
$dir        = "/home/rocinante/steed/data/gfdl/$mnamelc.$decade";
$tmpdir     = "$dir/data/tmpstrip.1hrly";

$topout     = "/home/rocinante/steed/data/gfdl/$mnamelc.$decade/data/stripped";
$baddata    = "$topout.badsave";

#$topout     = "/home/rocinante/steed/data/gfdl/$mnamelc.$decade/fixnewyear/stripped";
#$baddata    = "$topout.badsave";

$rawdata    = "/home/rocinante/steed/data/gfdl/$mnamelc.$decade/fixnewyear";
$driverdir  = "$dir/driver";
#$ENV{LD_LIBRARY_PATH} = "/usr/lib";

require ("$dir/../utils.pl");
$inc        = 1;
$pcpdifh    = 1;

$indir  = "$rawdata/$cinittime";

$baddir = "$baddata/$yyyymm";
$outdir = "$topout/$yyyymm";


if ( ! -d "$outdir" ) { system ("mkdir -p $outdir"); }
if ( ! -d "$baddir" ) { system ("mkdir -p $baddir"); }
if (! -d "$tmpdir") { system ("mkdir -p $tmpdir"); }

@dpms       = ();
@dpms       = (0,31,28,31,30,31,30,31,31,30,31,30,31);

@saves=();
@saves=(
    "PSFC",
    "Q2",
    "T2",
    "U10",
    "V10",
    "SEAICE",
    "SST",
    "TSK",
    "SFROFF",
    "UDROFF",
    "CANWAT",
    "POTEVP",
    "SNOW",
    "SNOWH",
    "PBLH",
    "SWDOWN",
    "GLW",
    "GRDFLX",
    "OLR",
    "HFX",
    "QFX",
    "LH",
    "HGT",
    "P_TOP",
    "P00",
    "T00",
    "Times",
    );

##############################################################################
## Strip the data we want.

$date = $ctime;
$yyyy = substr($date,0,4);
$mm   = substr($date,4,2);
$dd   = substr($date,6,2);
$hh   = substr($date,8,2);
$cdate = "$yyyy"."-"."$mm"."-"."$dd"."_"."$hh".":00:00";
$cfn = "wrfout_d"."$ddom"."_"."$cdate";
$cinfile = "$tmpdir/$cfn";

$date = &changetime10($ctime,(-1*$pcpdifh));
$ptime = $date;
$yyyy = substr($date,0,4);
$mm   = substr($date,4,2);
$dd   = substr($date,6,2);
$hh   = substr($date,8,2);
$cdate = "$yyyy"."-"."$mm"."-"."$dd"."_"."$hh".":00:00";
$pfn = "wrfout_d"."$ddom"."_"."$cdate";
$pinfile = "$tmpdir/$pfn";

if ( ! -e "$pinfile" ) {
    system ("cp $indir/$pfn $pinfile");
}

if ( -e "$indir/$cfn" ) {
    system ("cp $indir/$cfn $cinfile");
    print ("Copying $indir/$cfn\n");
}
else {
    system ("ncea -h $pinfile $ninfile $cinfile");
}

##############################################################################
## Make sure we have bothall three files.
if ((-s "$cinfile" < 1000)||(-s "$pinfile" < 1000)) {
    print ("Missing data.\n");
    exit 0;
}

##############################################################################
## Strip out rainc and rainnc from current and previous times, subtract and add.
chdir ("$tmpdir");
$tmpfile1 = "$tmpdir/rainnc.nc";
system ("rm -f $tmpfile1");
system ("ncbo -O --history --operation=subtract -v RAINNC $cinfile $pinfile $tmpfile1");
system ("ncrename -O -h -v RAINNC,PREC $tmpfile1");

$tmpfile2 = "$tmpdir/rainc.nc";
system ("rm -f $tmpfile2");
system ("ncbo -O --history --operation=subtract -v RAINC  $cinfile $pinfile $tmpfile2");
system ("ncrename -O -h -v RAINC,PREC $tmpfile2");

$tmpfile = "$tmpdir/$mnamelc.d$ddom.1hr.$ctime.nc";
system ("rm -f $tmpfile");
system ("ncbo -O -h --operation=add -v PREC $tmpfile1 $tmpfile2 $tmpfile");
system ("ncatted -O -h -a description,PREC,o,c,'ACCUM TOTAL PRECIP PAST $pcpdifh HOURS' $tmpfile");
system ("rm -f $tmpfile1");
system ("rm -f $tmpfile2");

##############################################################################
## Strip out fields from current wrfout file.
for ($s=0;$s<=$#saves;$s++) {
    $save = $saves[$s];
    system ("ncks -A -h -v $save $cinfile $tmpfile");
}
	    
##############################################################################
## Move output file.
$outfile = "$outdir/$mnamelc.d$ddom.1hr.$ctime.nc";
if ( -e "$outfile" ) {
    system ("mv $outfile $baddir");
    system ("touch $outfile.newyear.flag");
}
system ("mv $tmpfile $outfile");

##############################################################################
## Clean up.
system ("rm -f $pinfile");
system ("rm -f $cinfile");
system ("rm -Rf $tmpdir");

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