#!/bin/env perl
#
# mkdata.pl    Script to split datm7 CLMNCEP data by type of data and temporal frequency.
#              Solar and precipitation data are really 6 hour data that are repeated on the
#              original dataset. Hence we split out only the 6 hour data for this part.
#
#   Erik Kluzek              Jun/27/2008
#
my $year_beg = 1948;    # Beginning year
my $year_end = 2004;    # Final year

my $dir = "/cgd/tss/NCEPDATA.datm7.Qian.T62.c060410";

#
# The variables to split out for each set of files
#
my $TPQWvars   = "EDGEW,EDGEE,EDGES,EDGEN,LONGXY,LATIXY,TBOT,WIND,QBOT,PSRF,time";
my $SolarVars  = "EDGEW,EDGEE,EDGES,EDGEN,LONGXY,LATIXY,FSDS,time";
my $PrecipVars = "EDGEW,EDGEE,EDGES,EDGEN,LONGXY,LATIXY,PRECTmms,time";

#
# Loop over all years and 12 months of the year
#
for( my $year = $year_beg; $year <= $year_end; $year++ ) {
  foreach my $month ( "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" ) {
    my $file    = "$dir/clmncep.T62.$year-$month.nc";
    my $newtmpfile = "clmforc.Qian.c2006.T62.$year-$month.nc";
    my $newSolrfile = "clmforc.Qian.c2006.T62.Solr.$year-$month.nc";
    my $newPrecfile = "clmforc.Qian.c2006.T62.Prec.$year-$month.nc";
    my $newTPQWfile = "clmforc.Qian.c2006.T62.TPQW.$year-$month.nc";
    if ( ! -f $file ) {
      die "ERROR: $file does NOT exist!\n";
    } else {
      #
      # Remove old file if it exists
      #
      my %files = ( "TmpPrsHumWnd3Hrly"=>$newTPQWfile, "Solar6Hrly"=>$newSolrfile, "Precip6Hrly"=>$newPrecfile );
      foreach my $subdir ( keys(%files) ) {
         system( "/bin/rm -f $subdir/$newtmpfile $subdir/$files{$subdir}" );
      }
      #
      # Copy the relevent variables to the new file
      #
      print "Convert $file\n";
      system( "ncks -A -v $TPQWvars                $file TmpPrsHumWnd3Hrly/$newTPQWfile" );
      system( "ncap -O -s 'time=time-0.0625'             TmpPrsHumWnd3Hrly/$newTPQWfile TmpPrsHumWnd3Hrly/$newTPQWfile" );
      system( "ncks -A -v $SolarVars  -d time,0,,2 $file Solar6Hrly/$newSolrfile" );
      system( "ncap -O -s 'time=time-0.125'              Solar6Hrly/$newSolrfile Solar6Hrly/$newSolrfile" );
      system( "ncks -A -v $PrecipVars -d time,0,,2 $file Precip6Hrly/$newPrecfile" );

      #--- add global attributes ---
      foreach my $subdir ( keys(%files) ) {
         my $newfile = "$subdir/" . $files{$subdir};
         system( "ncatted -h -O -a       title,global,o,c,'CLM-forcing datm input data from Qian et. al 2006'   $newfile" );
         system( "ncatted -h -O -a conventions,global,o,c,'CF-1.0'                                              $newfile" );
         system( "ncatted -h -O -a     history,global,a,c,'\nOriginal data from NCAR mss:/TQIAN/LSMF/NCEPDATA ' $newfile" );
         system( "ncatted -h -O -a     long_name,time,o,c,'observation time' $newfile" );
         system( "ncatted -h -O -a     units,time,o,c,'days since $year-$month-01 00:00:00' $newfile" );
         system( "ncatted -h -O -a     calendar,time,o,c,'noleap' $newfile" );
         print "    Created:          $newfile\n";


      }
    }
  }
}
