#!/usr/bin/perl -w
# +-======-+ 
#  Copyright (c) 2003-2007 United States Government as represented by 
#  the Admistrator of the National Aeronautics and Space Administration.  
#  All Rights Reserved.
#  
#  THIS OPEN  SOURCE  AGREEMENT  ("AGREEMENT") DEFINES  THE  RIGHTS  OF USE,
#  REPRODUCTION,  DISTRIBUTION,  MODIFICATION AND REDISTRIBUTION OF CERTAIN 
#  COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE UNITED STATES GOVERNMENT AS 
#  REPRESENTED BY THE GOVERNMENT AGENCY LISTED BELOW ("GOVERNMENT AGENCY").  
#  THE UNITED STATES GOVERNMENT, AS REPRESENTED BY GOVERNMENT AGENCY, IS AN 
#  INTENDED  THIRD-PARTY  BENEFICIARY  OF  ALL  SUBSEQUENT DISTRIBUTIONS OR 
#  REDISTRIBUTIONS  OF THE  SUBJECT  SOFTWARE.  ANYONE WHO USES, REPRODUCES, 
#  DISTRIBUTES, MODIFIES  OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED 
#  HEREIN, OR ANY PART THEREOF,  IS,  BY THAT ACTION, ACCEPTING IN FULL THE 
#  RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
#  
#  Government Agency: National Aeronautics and Space Administration
#  Government Agency Original Software Designation: GSC-15354-1
#  Government Agency Original Software Title:  GEOS-5 GCM Modeling Software
#  User Registration Requested.  Please Visit http://opensource.gsfc.nasa.gov
#  Government Agency Point of Contact for Original Software:  
#  			Dale Hithon, SRA Assistant, (301) 286-2691
#  
# +-======-+ 
#=======================================================================
# name - regrid_pl
# purpose - wrapper script for rs_hinterp_${ARCH}.x and mk_Restarts programs
#
# revision history
# 24Nov2009  Stassi    initial version of code
# 09Sep2010  Stassi    compile rs_hinterp if needed; write rst.lcv
#=======================================================================
use strict;

# global variables
#-----------------
my ($script, $noclean, $verbose, $node);
my (@UPPERAIR_REQ, @UPPERAIR_OPT, @SURFACE);
my ($ESMADIR, $ESMABIN, $rstdir, $outdir, $expid, $newid, $ymd, $hr);
my ($upair_flg, $surf_flg, @gridIN, @gridOUT);
my ($fvflg, $fvdir1, $fvdir2, $fvdir3);
my ($rs_hinterpX, $mk_restarts_dir, $mk_Restarts, @links);

# main program
#-------------
{
    init();
    convert_upper_air_restarts()    if $upair_flg;
    convert_land_surface_restarts() if $surf_flg;
    write_rst_lcv();
}

#=======================================================================
# name - init
# purpose - get runtime parameters, check environment, initialize global
#           variables
#=======================================================================
sub init {
    use Cwd;
    use File::Basename;
    use Getopt::Long;
    my ($ARCH, $dmget);
    my (%reqflg, %optflg, @optkey, @missing, $n);
    my (@grid_b, @grid_c, @grid_d1, @grid_d2, @grid_e1, @grid_e2);
    my ($grIN, $grOUT, $rsflg, $help);
    my ($type, $file, $year, $month, $rstdirX);
    my ($fvdir_old, $fvdir_new);

    $script = basename $0;
    chomp($ARCH = `uname -s`);
    chomp($node = `uname -n`);

    # restart lists
    #--------------
    @UPPERAIR_REQ = qw( fvcore_internal_rst
                        moist_internal_rst );

    @UPPERAIR_OPT = qw( agcm_internal_rst
                        gocart_internal_rst
                        pchem_internal_rst );

    @SURFACE =      qw( catch_internal_rst
                        landice_internal_rst
                        lake_internal_rst
                        saltwater_internal_rst );

    # runtime option information
    #---------------------------
    %reqflg =
        ("d"       => "restart directory location",
         "outdir"  => "output directory location",
         "expid"   => "restart expid",
         "ymd"     => "8-digit restart ymd",
         "hr"      => "2-digit restart hour",
         "in"      => "in-grid ID {a, b, c, d, e}",
         "out"     => "out-grid ID {a, b, c, d, e}");
    %optflg = 
        ("newid"   => "output expid <<n_\$expid>> where n=out-grid ID",
         "fv"      => "fv tiles; 1=old->old, 2=old->new, 3=new->new <<3>>",
         "rs"      => "restarts; 1=upperair, 2=surface, 3=both <<3>>",
         "ESMADIR" => "ESMADIR directory location <<\$ESMADIR>>",
         "dmget"   => "send dmget job <<off>>",
         "nc"      => "no clean debugging option <<off>>",
         "v"       => "verbose mode <<off>>",
         "h"       => "print usage information");
    #--------------------------------------------------------------------
    # NOTE: The @optkey values below are explicitly written out rather
    # than using the keys %optflg option, because I want them to display
    # in this particular order. This means that the following line must
    # be manually updated whenever the %optflg keys are modified.
    #--------------------------------------------------------------------
    @optkey = qw( newid fv rs ESMADIR dmget nc v h );

    # runtime options
    #----------------
    GetOptions("ESMADIR=s" => \$ESMADIR,
               "d=s"       => \$rstdir,
               "outdir=s"  => \$outdir,
               "expid=s"   => \$expid,
               "newid=s"   => \$newid,
               "ymd=i"     => \$ymd,
               "hr=i"      => \$hr,
               "in=s"      => \$grIN,
               "out=s"     => \$grOUT,
               "fv=i"      => \$fvflg,
               "rs=i"      => \$rsflg,
               "help"      => \$help,
               "h"         => \$help,
               "dmget"     => \$dmget,
               "nc"        => \$noclean,
               "v"         => \$verbose);
    usage() if $help;

    # check for mandatory runtime flags
    #----------------------------------
    @missing = ();
    push @missing, "d"       unless $rstdir;
    push @missing, "outdir"  unless $outdir;
    push @missing, "expid"   unless $expid;
    push @missing, "ymd"     unless $ymd;
    push @missing, "hr"      unless $hr;
    push @missing, "in"      unless $grIN;
    push @missing, "out"     unless $grOUT;

    if (@missing) {
        print "\n These required flags are missing:\n";
        foreach $n (@missing) { printf " -%-7s  %s\n", $n, $reqflg{$n} };

        print "\n Optional flags:\n";
        foreach $n (@optkey) { printf " -%-7s  %s\n", $n, $optflg{$n} };
        print "\n";
        exit;
    }
    $hr = "0".$hr if length($hr) == 1;

    # check restart directory (by looking for required UPPERAIR restarts)
    #--------------------------------------------------------------------
    $rstdir = $1 if $rstdir =~ /(\S+)\b/; # remove trailing '/'
    die "Cannot find restart directory: $rstdir;" unless -d $rstdir;

    foreach $type ( @UPPERAIR_REQ ) {
        $file = rstname($type);
        unless (-e "$rstdir/$file") {
            $year  = substr($ymd, 0, 4);
            $month = substr($ymd, 4, 2);

            $rstdirX = "$rstdir/$expid/rs/Y$year/M$month";
            $rstdir = $rstdirX if -e "$rstdirX/$file";

            $rstdirX = "$rstdir/rs/Y$year/M$month";
            $rstdir = $rstdirX if -e "$rstdirX/$file";
        }
        die "Cannot find $file in $rstdir;" unless -e "$rstdir/$file";
    }

    # set values for ESMADIR and ESMABIN
    #-----------------------------------
    $ESMADIR = $ENV{"ESMADIR"} unless $ESMADIR;
    die "ESMADIR not defined;" unless $ESMADIR;
    die "ESMADIR not found: $ESMADIR;"  unless -d $ESMADIR;

    $ESMABIN = "$ESMADIR/$ARCH/bin";
    die "ESMABIN not found: $ESMABIN;"  unless -d $ESMABIN;

    # check for program files
    #------------------------
    $rs_hinterpX = "$ESMADIR/src/GMAO_Shared/GEOS_Util/post/rs_hinterp_${ARCH}.x";
    my_system("$ESMADIR/src/GMAO_Shared/GEOS_Util/post/rs_hinterp", 1)
        unless -x $rs_hinterpX;
    die "program not found: $rs_hinterpX;" unless -x $rs_hinterpX;
    rs_hinterp_check();

    $mk_restarts_dir = "$ESMADIR/src/"
        .              "GEOSgcs_GridComp/"
        .              "GEOSgcm_GridComp/"
        .              "GEOSagcm_GridComp/"
        .              "GEOSphysics_GridComp/"
        .              "GEOSsurface_GridComp/"
        .              "GEOSland_GridComp/"
        .              "GEOScatch_GridComp/"
        .              "mk_restarts";
    $mk_Restarts = "$mk_restarts_dir/mk_Restarts";
    die "program not found: $mk_Restarts;" unless -x $mk_Restarts;

    # determine location of fvInput
    #------------------------------
    if ( $node =~ /^pfe/ || $node =~ /^r\d+i\d+n\d+/ ) {
        #----------#
        # pleiades #
        #----------#
        $fvdir_old = "/nobackup/dkokron/fvInput/g5gcm/bcs/g5b10p9/DC";
        $fvdir_new = "/nobackup/dkokron/fvInput/g5gcm/bcs/Fortuna-2_1";
        $node = "pfe";

    } else {
        #----------#
        # discover #
        #----------#
        $fvdir_old = "$ENV{'SHARE'}/dao_ops/fvInput_4dvar/g5gcm/bcs/g5b10p9/DC";
        $fvdir_new = "$ENV{'SHARE'}/dao_ops/fvInput/g5gcm/bcs/Fortuna-2_1";
        $node = "nccs";
    }

    # default locations
    #------------------
    $fvdir1 = $fvdir_new;
    $fvdir2 = $fvdir_new;
    $fvdir3 = $fvdir_new;

    # change if necessary
    #--------------------
    $fvflg = 3 unless $fvflg and ($fvflg == 1 || $fvflg == 2);
    $fvdir1 = $fvdir_old if $fvflg == 1 || $fvflg == 2;
    $fvdir2 = $fvdir_old if $fvflg == 1;

    # set gridIN values
    #------------------
    @grid_b  = (  "144x91", "0144x0091", "b"  );
    @grid_c  = ( "288x181", "0288x0181", "c"  );
    @grid_d1 = ( "540x361", "0540x0361", "d1" );
    @grid_d2 = ( "576x361", "0576x0361", "d2" );
    @grid_e1 = ("1080x721", "1080x0721", "e1" );
    @grid_e2 = ("1152x721", "1152x0721", "e2" );

    @gridIN = @grid_b  if $grIN eq "b";
    @gridIN = @grid_c  if $grIN eq "c";
    @gridIN = @grid_d2 if $grIN eq "d";
    @gridIN = @grid_e2 if $grIN eq "e";

    if ($fvflg == 1 or $fvflg == 2) {
        @gridIN = @grid_d1 if $grIN eq "d";
        @gridIN = @grid_e1 if $grIN eq "e";
    }

    # set gridOUT values
    #-------------------
    @gridOUT = @grid_b  if $grOUT eq "b";
    @gridOUT = @grid_c  if $grOUT eq "c";
    @gridOUT = @grid_d2 if $grOUT eq "d";
    @gridOUT = @grid_e2 if $grOUT eq "e";

    if ($fvflg == 1) {
        @gridOUT = @grid_d1 if $grOUT eq "d";
        @gridOUT = @grid_e1 if $grOUT eq "e";
    }

    die "Unrecognizable gridIN value: $grIN;"   unless @gridIN;
    die "Unrecognizable gridOUT value: $grOUT;" unless @gridOUT;
    print "WARNING: gridIN equals gridOUT (=$grIN);\n" if $grIN eq $grOUT;

    # default value for newid
    #------------------------
    $newid = "$gridOUT[2]_$expid" unless $newid;
    die "expid equals newid (=$expid);" if $expid eq $newid;

    # determine which restarts to convert
    #------------------------------------
    $rsflg = 3 unless $rsflg;

    $upair_flg = 1;
    $upair_flg = 0 if $rsflg == 2;

    $surf_flg  = 1;
    $surf_flg  = 0 if $rsflg == 1;

    # get absolute path for $outdir
    #------------------------------
    chdir $outdir or die "Error; chdir $outdir: $!";
    $outdir = cwd();

    dmget_restarts() if $dmget;
}

#=======================================================================
# name - convert_upper_air_restarts
# purpose - run rs_hinterp program to create regridded restarts
#=======================================================================
sub convert_upper_air_restarts {
    use File::Copy;
    my ($type, $target, $linkname, @other, $file);
    my ($dynrst, $moistrst);
    my ($topo_old, $topo_new, $imOUT, $jmOUT);
    my ($cmd, $rst, $newrst, $newname);

    # link required restarts to outdir
    #---------------------------------
    foreach $type ( @UPPERAIR_REQ ) {
        $target   = rstname($type, "$rstdir/$expid");
        $linkname = rstname($type, "$outdir/$expid");
        my_symlink($target, $linkname);
    }
    $dynrst = rstname("fvcore_internal_rst");
    $moistrst = rstname("moist_internal_rst");

    # check for optional restarts
    #----------------------------
    @other = ();
    foreach $type ( @UPPERAIR_OPT ) {
        $file = rstname($type);
        $target   = rstname($type, "$rstdir/$expid");
        $linkname = rstname($type, "$outdir/$expid");
        next unless -e $target;
        my_symlink($target, $linkname);
        push @other, $file;
    }

    # identify topological files
    #---------------------------
    $topo_old = "$fvdir1/$gridIN[0]/topo_DYN_ave_${gridIN[0]}_DC.data";
    $topo_new = "$fvdir2/$gridOUT[0]/topo_DYN_ave_${gridOUT[0]}_DC.data";

    # extract output IM and JM values
    #---------------------------------
    if ($gridOUT[0] =~ /\b(\d+)x(\d+)\b/) {
        $imOUT = $1;
        $jmOUT = $2;
    }

    # print program parameters to standard output
    #--------------------------------------------
    print "=============================\n";
    print "converting upper-air restarts\n";
    print "=============================\n";
    print " program: $rs_hinterpX\n";
    print "     dyn: $dynrst\n";
    print "   moist: $moistrst\n";
    print "   other: @other\n";
    print "topo_old: $topo_old\n";
    print "topo_new: $topo_new\n";
    print "      im: $imOUT\n";
    print "      jm: $jmOUT\n";
    print "  rstdir: $rstdir\n";
    print "  outdir: $outdir\n";

    # run program
    #------------
    my_chdir($outdir);
    $cmd = "$rs_hinterpX "
        .  "-dyn $dynrst "
        .  "-moist $moistrst "
        .  "-other @other "
        .  "-topo_old $topo_old "
        .  "-topo_new $topo_new "
        .  "-im $imOUT -jm $jmOUT";
    my_system($cmd, 1);

    # unlink old restarts, rename new ones
    #-------------------------------------
    foreach $type ( @UPPERAIR_REQ, @UPPERAIR_OPT ) {
        $file = rstname($type);
        next unless -e $file;
        my_unlink($file) unless $noclean;

        $newrst = "$file.$gridOUT[1]";
        $newname = rstname($type, $newid);
        my_move($newrst, $newname);
    }
}

#=======================================================================
# name - convert_land_surface_restarts
# purpose - run mk_Restarts program in mk_restarts directory to create
#           regridded restarts
#=======================================================================
sub convert_land_surface_restarts {
    use File::Copy;

    my ($mk_InData_dir, $mk_OutData_dir);
    my ($type, $file, $name, $newname);
    my ($target, $linkname);
    my ($nirdf, $visdf, $laigrn);

    @links = ();
    $mk_InData_dir  = "$mk_restarts_dir/InData";
    $mk_OutData_dir = "$mk_restarts_dir/OutData";

    # clean any lingering FV* files from OutData directory
    #-----------------------------------------------------
    foreach $file (<$mk_InData_dir/FV_*>)  { my_unlink($file) };
    foreach $file (<$mk_OutData_dir/FV_*>) { my_unlink($file) };

    # link land-surface restarts to InData directory
    #-----------------------------------------------
    foreach $type ( @SURFACE ) {
        $target   = rstname($type, "$rstdir/$expid");
        $linkname = rstname($type, "$mk_InData_dir/$expid");

        if (-e $target) { my_symlink($target, $linkname);   }
        else            { print "$target not found.\n"; }
    }

    # link tile files into the InData and OutData directories
    #--------------------------------------------------------
    $name  = "FV_${gridIN[0]}_DC_360x180_DE.til";
    $target = "$fvdir1/$gridIN[0]/$name";
    $linkname = "$mk_InData_dir/$name";
    my_symlink($target, $linkname);

    $name  = "FV_${gridOUT[0]}_DC_360x180_DE.til";
    $target = "$fvdir2/$gridOUT[0]/$name";
    $linkname = "$mk_OutData_dir/$name";
    my_symlink($target, $linkname);

    $name  = "clsm";
    $target = "$fvdir3/$gridOUT[0]/$name";
    $linkname = "$mk_OutData_dir/$name";
    my_symlink($target, $linkname);

    # data files needed with old tile format
    #---------------------------------------
    if ($fvflg == 1) {
        $nirdf = "$fvdir1/$gridIN[0]/nirdf_${gridIN[0]}_DC.dat";
        $visdf = "$fvdir1/$gridIN[0]/visdf_${gridIN[0]}_DC.dat";
        $laigrn = "$fvdir1/$gridIN[0]/lai_green_clim_${gridIN[0]}_DC.data";

        my_symlink($nirdf,  "$mk_InData_dir/nirdf.dat"   );
        my_symlink($visdf,  "$mk_InData_dir/visdf.dat"   );
        my_symlink($laigrn, "$mk_InData_dir/lai_grn_clim");
    }

    # run mk_Restarts program
    #------------------------
    my_chdir($mk_restarts_dir);
    my_system("mk_Restarts", 1);

    # cleanup links
    #--------------
    unless ($noclean) {
        my_chdir($mk_InData_dir);
        foreach (@links) { my_unlink($_) };
    }

    # rename restarts
    #----------------
    my_chdir($mk_OutData_dir);
    foreach $type ( @SURFACE ) {
        $file = rstname($type);
        $newname = rstname($type, "$outdir/$newid");
        my_move($file, $newname);
    }

    # other files created by mk_Restarts
    #-----------------------------------
    foreach $file ( "laigrn.data", "nirdf.dat", "visdf.dat" ) {
        $newname = "$outdir/${newid}.$file";
        my_move($file, $newname);
    }
}

#=======================================================================
# name - write_rst_lcv
# purpose - write the rst.lcv date/time restart file
#=======================================================================
sub write_rst_lcv {
    my ($rstlcv, $hhmmss);
    $rstlcv = rstname("rst.lcv", "${outdir}/${newid}");
    $hhmmss = $hr."0000";
    my_system("$ESMABIN/mkdrstdate.x $ymd $hhmmss $rstlcv", 1);
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#                         UTILITY subroutines
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#=======================================================================
# name - dmget_restarts
# purpose - submit job to dmget the needed restarts
#=======================================================================
sub dmget_restarts {
    my (@list, $type, $file, @restarts, $pid);

    return unless $node eq "nccs";

    # make list of restart types to dmget
    #------------------------------------
    if ($upair_flg) {
        foreach ( @UPPERAIR_REQ, @UPPERAIR_OPT) { push @list, $_ };
    }
    if ($surf_flg) {
        foreach ( @SURFACE ) { push @list, $_ };
    }

    # get actual names of restart files to dmget
    #-------------------------------------------
    foreach $type ( @list ) {
        $file = rstname($type, "$rstdir/$expid");
        push @restarts, $file if -e $file;
    }

    # fork job to dmget restarts
    #---------------------------
    defined($pid = fork) or warn "Cannot fork: $!";
    unless ($pid) {
        if ($verbose) {
            print "\ndmget the following files:\n";
            foreach (@restarts) { print "$_\n" }; 
            print "\n";
        }
        my_system("dmget @restarts", 1);
        exit;
    }
}

#=======================================================================
# name - my_chdir
# purpose - wrapper for perl chdir command
#
# input parameters
# => dir: cd to this directory
#=======================================================================
sub my_chdir {
    my $dir;
    $dir = shift @_;
    print "chdir $dir\n" if $verbose;
    chdir $dir or die "Cannot chdir to $dir: $!";
}

#=======================================================================
# name - my_move
# purpose - wrapper for perl move command
#
# input parameters
# => source: move from this name
# => dest: move to this name
#=======================================================================
sub my_move{
    use File::Copy;
    my ($source, $dest);

    $source = shift @_;
    $dest   = shift @_;
    
    print "move $source, $dest\n" if $verbose;
    move $source, $dest or die "Error moving $source, $dest: $!";
}

#=======================================================================
# name - my_symlink
# purpose - wrapper for perl symlink command
#
# input parameters
# => target: the file being linked
# => linkname: the link
#=======================================================================
sub my_symlink {
    my ($target, $linkname);

    $target   = shift @_;
    $linkname = shift @_;

    # check to be sure $target exists and $linkname does not
    #-------------------------------------------------------
    die "$target not found;"  unless -e $target;
    my_unlink($linkname) if -e $linkname or -l $linkname;

    # link target and save link name in @links
    #-----------------------------------------
    print "\nsymlink $target, $linkname\n" if $verbose;
    symlink $target, $linkname or die
        "Error, symlink failed; $target <- $linkname: $!";
    push @links, $linkname;
}

#=======================================================================
# name - my_system
# purpose - wrapper for system command
#=======================================================================
sub my_system {
    my ($cmd, $flag, $status);
    $cmd = shift @_;
    $flag = shift @_;
    print "\n$cmd\n" if $flag;
    system($cmd);
}

#=======================================================================
# name - my_unlink
# purpose - wrapper for perl unlink command
#
# input parameters
# => name: name of file being unlinked
#=======================================================================
sub my_unlink {
    my $name;
    $name = shift @_;
    print "\nunlink $name\n" if $verbose;
    unlink $name or warn "Error unlinking $name;";
}

#=======================================================================
# name - rs_hinterp_check
# purpose - check for correct runtime flags in the rs_interp program
#           (earlier versions of the program used different runtime flags)
#
# return value -
# => errors :  =0 if all runtime flags are okay
#              >0 if one or more of the runtime flags are incorrect
# note:
# 1. It is necessary to use \s+ (whitespace) rather than \b (word
#    boundary) to delimit $rtflag since each $rtflag begins with "-".
#=======================================================================
sub rs_hinterp_check {
    my (@runtime_flags, $rtflg, $wordstring, $errors);

    # here are the rs_hinterp runtime flags the script is expecting
    #--------------------------------------------------------------
    @runtime_flags = qw( -dyn -moist -other -topo_old -topo_new -im -jm );
    chomp($wordstring = `$rs_hinterpX`);

    # the runtime flags have changed; be sure that user has the correct revision
    #---------------------------------------------------------------------------
    $errors = 0;
    foreach $rtflg (@runtime_flags) {
        unless ($wordstring =~ /\s+$rtflg\s+/) {
            print "Error: Cannot find $rtflg runflag in $rs_hinterpX\n";
            $errors++;
        }
    }

    # die if errors found
    #--------------------
    if ($errors) {
        die "It appears you need another revision of rs_hinterp program;";
    }
}

#=======================================================================
# name - rstname
# purpose - return the filename for a particular restart file
#
# input parameters
# => rsttype: type of restart (e.g. "fvcore_internal_rst")
# => head: heading for filename (optional); defaults to $expid
#
# return value
# => restart file name
#=======================================================================
sub rstname {
    my ($rsttype, $head);
    $rsttype = shift @_;
    $head = shift @_;
    $head = $expid unless $head;
    return "${head}.${rsttype}.${ymd}_${hr}z.bin";
}    

#=======================================================================
# name - usage
# purpose - print usage information
#=======================================================================
sub usage {
    print <<"EOF";
NAME
    $script

PURPOSE
    Wrapper script for programs which create a set of regridded GCM restarts

SYNOPSIS
    $script [options] -d rstdir -expid expid -ymd ymd -hr hr -gridIN grid1 -gridOUT grid2

REQUIRED FLAGS
     -d       rstdir   Location of input restart files
                       (can shortcut to directory prior to expid directory)
     -outdir  dir      Directory location for output restarts
     -expid   expid    Experiment ID of input restart files
     -ymd     ymd      8-digit data year/month/day of input restart files
     -hr      hr       2-digit data hour of restart files
     -in      grid1    Grid identifier for input restarts
                       choices include the following: {b, c, d, e}
     -out     grid2    Grid identifier for output restarts
                       same choices as those for -gridIN

OTHER OPTIONS
     -newid   newid    Label to replace expid in output restart names
                       (defaults to 'n-expid' where n is grid ID)

     -fv      flag     Flag for fvInput tiles files
                       =1 old tile files for input and output
                       =2 old tile files for input; new for output
                       =3 new tile files for input and output (default)

     -rs      flag     Flag indicating which restarts to regrid
                       =1 for upper-air restarts only
                       =2 for land-surface restarts only
                       =3 for both upper-air and land-surface restarts (default)

     -ESMADIR dir      Location of ESMADIR directory;
                       (defaults to value in environment variable, ESMADIR)

     -dmget            send command to dmget initial restarts

     -nc               (no clean) Do not remove links after running programs;
                       useful for debugging

     -v                Verbose mode

     -h[elp]           Print usage message

AUTHOR
    Joe Stassi, SAIC (joe.stassi\@nasa.gov)

EOF
exit;
}
