#!/usr/bin/env perl
#
# Script to copy restarts from rs location to recycle
#
# !REVISION HISTORY:
#
#  28Feb2005  Todling   Initial code (very crude).
#  05May2005  Todling   Updated list of G5GCM rsts
#-----------------------------------------------------------------------------

use Env;                 # make env vars readily available
use File::Basename;      # for basename(), dirname()
use File::Copy "cp";     # for cp()
use Getopt::Long;        # command line options
 
# Command line options
# --------------------
  GetOptions( "h", "mst=s", "scr=s" );

  usage() if $opt_h;

# FVROOT is where the binaries have been installed
# ------------------------------------------------
  $fvroot  = dirname($FindBin::Bin);
  $fvroot  =~ s|/u/.realmounts/share|/share|;   # for portability accros
                                                # NAS machines
  $user = getlogin();

  init();

# Define GCM's restart names
# --------------------------
  if ( $g5gcm ) {
     @grs_list = ( d catch_internal fvcore_import fvcore_internal irrad_internal lake_internal landice_internal moist_internal ocean_internal solar_internal superdyn_import superdyn_internal surf_import turb_import vegdyn_internal )
  } else {
     @grs_list = ( d p lsm c );
  }

# Copy files from permanent place to recycle dir
# ----------------------------------------------
  if ( $g5gcm ) {
       foreach $fn (@grs_list) {
           if ( "$fn" == "d" ) {
                cp("$mst/$expid.rst.lcv.$itime.bin","$scr/$expid/recycle/$expid.d_rst") ;
           } else {
                cp("$mst/$expid.${fn}_rst.$itime.bin","$scr/$expid/recycle/$expid.${fn}_rst") ;
           }
       }
  } else {
    cp($mst/$expid.rst.lcv.$itime.bin  ,$scr/$expid/recycle/$expid.d_rst);
    cp($mst/$expid.rst.chem.$itime.bin ,$scr/$expid/recycle/$expid.c_rst);
    cp($mst/$expid.rst.phys.$itime.bin ,$scr/$expid/recycle/$expid.p_rst);
    cp($mst/$expid.rst.lsm.$itime.bin  ,$scr/$expid/recycle/$expid.lsm_rst);
  }

#------------------------------------------------------------------------------------

sub init {


  if ( $#ARGV < 3 ) {
       print STDERR "missing nymd, nhms and/or expid; see usage";
       usage();
  } else {              # required command lile args
       $nymd  = $ARGV[0];
       $nhms  = sprintf("%6.6d",$ARGV[1]);
       $expid = $ARGV[2];
       $yyyy = substr($nymd,0,4);
       $mm   = substr($nymd,4,2);
       $dd   = substr($nymd,6,2);
       $hh   = substr($nhms,0,2);
       $itime = "${nymd}_${hh}z";

  }

  if( $opt_mst ) {
      $mst = $opt_mst;
  } else {
      $mst = "/output/$user/$expid/rs/Y$yyyy/M$mm";
  }

  if( $opt_scr ) {
      $scr = $opt_scr;
  } else {
      $scr = "/scratch1/$user/$expid/recycle";
  }

  $g5gcm   = 1  if ( -e "$fvroot/bin/GEOSgcm.x" );
  $fvgcm   = 1  if ( -e "$fvroot/bin/fvpsas.x"  );

}

#......................................................................
#------------------------------------------------------------------------------------

sub usage {

   print <<"EOF";

NAME
     rst2rcyc - Copy GEOS-4/5 restarts from permanent storage to recycle
          
SYNOPSIS

     rst2rcyc [...options...]  nymd  nhms  expid  
          
DESCRIPTION

     The following parameter are required 

     nymd     Year-month-day, e.g., 19990901  for 01 Sept 1999 
     nhms     Hour-minutes-seconds, e.g., 120000
     expid    Experiment name

OPTIONS
 
 -h            prints this usage notice

 -mst          directory path name where original restarts are stored,
               e.g., /output/todling/f5b2_d1/rs/Y2004/M07; 
                  default: /output/user/expid/rs/Yyyyy/Mmm

 -scr          directory path where restarts are to be copied to with
               naming convention for initializing DAS/GCM, e.g.,
               /scratch1/todling/f5b2_d1/recycle;
                  default: /scratch1/user/expid/recycle

ENVIRONMENT

AUTHOR
      R. Todling (todling\@gmao.gsfc.nasa.gov), NASA/GSFC/GMAO

EOF

  exit(1)

	   }
