#! /bin/csh -f 

#--- acquire env vars associated with this case ---
source $CASEROOT/env_conf         || exit -1
source $CASEROOT/env_run          || exit -1
source $CASEROOT/env_mach.$MACH  || exit -1
source $CASEROOT/env_pes.$MACH  || exit -1

if !($NTASKS_ATM == 1) then
  echo "NTASKS_ATM must be 1, please correct in env_pes and reconfigure"
  exit -1
endif
if !($NTHRDS_ATM == 1) then
  echo "NTHRDS_ATM must be 1, please correct in env_pes and reconfigure"
  exit -1
endif

cd $EXEROOT/atm

#------------------------------------------------------------------------------
# specify input data files
#------------------------------------------------------------------------------
# If the user changes any input datasets - be sure they have unique filenames.  
# Do not duplicate existing input file names.
#------------------------------------------------------------------------------

if ($DATM_MODE == "TN460" ) then
   #--- set namelist values ---
   set DATAMODE = TN460
   set STREAM1  = "tn460nyf.giss.T62.stream.060209.txt  1 1 1 "
   set STREAM2  = "tn460nyf.gxgxs.T62.stream.060209.txt 1 1 1 "
   set STREAM3  = "tn460nyf.ncep.T62.stream.060209.txt  1 1 1 "
   set TN460FFN = "tn460nyf.correction_factors.T62.050923.nc  "
   set VECTOR1  = "u:v"
   #--- pre-fetch data ---
   $UTILROOT/Tools/ccsm_getinput atm/datm7/TN460/tn460nyf.giss.T62.stream.060209.txt       || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/TN460/tn460nyf.gxgxs.T62.stream.060209.txt      || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/TN460/tn460nyf.ncep.T62.stream.060209.txt       || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/TN460/tn460nyf.giss.T62.051007.nc               || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/TN460/tn460nyf.gxgxs.T62.051007.nc              || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/TN460/tn460nyf.ncep.T62.050923.nc               || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/TN460/tn460nyf.correction_factors.T62.050923.nc || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/domain.T62.050609.nc                            || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/domain.T42.050516.nc || exit 99
else if ($DATM_MODE == "CAMHIST" ) then
   #--- set namelist values ---
   set DATAMODE = CAMHIST
   set STREAM1  = "eul64x128_datm6.01.cam2.stream.060209.txt 1 5 9"
   set STREAM2  = "null "
   set STREAM3  = "null "
   set TN460FFN = "unused "
   set VECTOR1  = "u:v"
   #--- pre-fetch data ---
   $UTILROOT/Tools/ccsm_getinput atm/datm7/CAMHIST/eul64x128_datm6.01.cam2.stream.060209.txt      || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/CAMHIST/eul64x128_datm6.01.cam2.h1.0005-01-01-00000.nc || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/CAMHIST/eul64x128_datm6.01.cam2.h1.0006-01-01-00000.nc || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/CAMHIST/eul64x128_datm6.01.cam2.h1.0007-01-01-00000.nc || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/CAMHIST/eul64x128_datm6.01.cam2.h1.0008-01-01-00000.nc || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/CAMHIST/eul64x128_datm6.01.cam2.h1.0009-01-01-00000.nc || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/domain.T42.050516.nc                                   || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/domain.T42.050516.nc || exit 99
else if ($DATM_MODE == "CLMNCEP" ) then
   #--- set namelist values ---
   set DATAMODE = CLMNCEP
   set STREAM1  = "clmncep.T62.stream.060215.txt 1 1948 1972"
   set STREAM2  = "null "
   set STREAM3  = "null "
   set TN460FFN = "unused "
   set VECTOR1  = "null "
   #--- pre-fetch data ---
   # data does not exist in the standard inputdata directory (it's too large)
   # data only exists at NCAR in mss:/CSM/inputdata/atm/datm7/CLMNCEP
   $UTILROOT/Tools/ccsm_getinput atm/datm7/CLMNCEP/clmncep.T62.stream.060215.txt      || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/domain.T62.050609.nc                       || exit 99
   $UTILROOT/Tools/ccsm_getinput atm/datm7/domain.T42.050516.nc || exit 99
else 
  echo "ERROR: unrecognized DATM_MODE = $DATM_MODE 
  exit -1
endif


#------------------------------------------------------------------------------
# Create resolved namelist
#------------------------------------------------------------------------------

set RUNTYPE0 = hybrid   # RUNTYPE at configuration

if ($RUNTYPE0 == 'startup' ) set RUNTYPE = initial
if ($RUNTYPE0 == 'hybrid'  ) set RUNTYPE = initial
if ($RUNTYPE0 == 'branch'  ) set RUNTYPE = branch
if ($RUNTYPE0 == 'continue') set RUNTYPE = continue
if ($CONTINUE_RUN == 'TRUE') set RUNTYPE = continue

#--- create namelist files ---

cat >! datm.nml << EOF1
  &datm_nml
    tn460_factorFn = "$TN460FFN "
  /
EOF1

cat >! dshr.nml << EOF1
 &dshr_nml
   restType       = '$RUNTYPE'
   restPfile      = 'rpointer.atm '
   restBfile      = 'null '
   restSfile      = 'null '
   restFreq       = 'cpl'
   caseName       = '$CASE'
   caseDesc       = '$CASESTR'
   dataMode       = '$DATAMODE'
   domainFile     = 'domain.T42.050516.nc'
   streams        = '$STREAM1' ,
                    '$STREAM2' ,
                    '$STREAM3' 
   vectors        = '$VECTOR1'
   infoDbug       = 1
   ncpl           = 24
  /
EOF1

