#!/bin/env perl
#
# mkdataDec1947.pl    Script to copy the first time-step of 1948 into December 1947 files.
#
#   Erik Kluzek              Jul/01/2008
#
    my $year = 1948;    # Beginning year

    my $month = "01";
    my $frsSolrfile = "clmforc.Qian.c2006.T62.Solr.$year-$month.nc";
    my $frsPrecfile = "clmforc.Qian.c2006.T62.Prec.$year-$month.nc";
    my $frsTPQWfile = "clmforc.Qian.c2006.T62.TPQW.$year-$month.nc";
    $new_month = "12";
    $new_year = $year - 1;
    #
    # Make sure files exist as expected
    #
    my $newSolrfile = "clmforc.Qian.c2006.T62.Solr.$new_year-$new_month.nc";
    my $newPrecfile = "clmforc.Qian.c2006.T62.Prec.$new_year-$new_month.nc";
    my $newTPQWfile = "clmforc.Qian.c2006.T62.TPQW.$new_year-$new_month.nc";

    my %files      = ( "TmpPrsHumWnd3Hrly"=>$newTPQWfile, "Solar6Hrly"=>$newSolrfile, "Precip6Hrly"=>$newPrecfile );
    my %frst_files = ( "TmpPrsHumWnd3Hrly"=>$frsTPQWfile, "Solar6Hrly"=>$frsSolrfile, "Precip6Hrly"=>$frsPrecfile );

    foreach my $subdir ( keys(%files) ) {
       my $newfile   = "$subdir/" . $files{$subdir};
       my $firstfile = "$subdir/" . $frst_files{$subdir};
       if ( ! -f "$firstfile" ) {
          die "ERROR: $subdir/$firstfile does NOT exist!\n";
       }
       print "Create $newfile from $firstfile\n";
       system( "/bin/rm $newfile" );
       system( "ncks -A -d time,0,0 $firstfile $newfile" );
       system( "ncap -A -s 'time=30.9999' $newfile $newfile" );
       system( "ncatted -A -a  units,time,o,c,\"days since $new_year-$new_month-01 00:00:00\" $newfile" );
    }
    print "Done creating files for $year\n";
