#! /bin/csh -f 

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 the user changes any input datasets - be sure to give it a    #
 # unique filename. Do not duplicate any existing input files       #
 #******************************************************************#

# started as a hybrid run

set runtype = hybrid
if ($CONTINUE_RUN == 'TRUE') set runtype = 'continue'

set exedir = $EXEROOT/lnd
cd $exedir

if ($CLM_BGC == CN) then
   set fndepdat = 'regrid_ndep_clm_128x064.nc'          
   $UTILROOT/Tools/ccsm_getinput lnd/clm2/ndepdata/1890/$fndepdat || exit7
endif
 
#set pftdat     = 'pft-physiology-cn16.c040719'
set pftdat     = 'cn17_pftphys_20060104'
#set fsurdat    = 'surfdata_64x128_060401.nc'
set fsurdat    = 'T42.clamp.surface-data.060412.nc'
#set fatmgrid   = 'griddata_64x128_060401.nc'
set fatmgrid   = ''
#set fatmlndfrc = 'fracdata_64x128_gx1v3_060401.nc'
set fatmlndfrc = ''
set finidat    = 'i01.01cn.clm2.i.0501-01-01-00000.nc'
set nrevsn     = ''

$UTILROOT/Tools/ccsm_getinput lnd/clm2/pftdata/$pftdat || exit 1
$UTILROOT/Tools/ccsm_getinput lnd/clm2/rtmdata/rdirc.05        || exit 1
$UTILROOT/Tools/ccsm_getinput lnd/clm2/surfdata/$fsurdat
$UTILROOT/Tools/ccsm_getinput lnd/clm2/srfdata/csm/$fsurdat   || exit 1
if ($fatmgrid != "" && $runtype != continue) then
  $UTILROOT/Tools/ccsm_getinput lnd/clm2/griddata/$fatmgrid  || exit 1
endif
if ($fatmlndfrc != "" && $runtype != continue) then
  $UTILROOT/Tools/ccsm_getinput lnd/clm2/griddata/$fatmlndfrc || exit 1
endif
if ($finidat != "" && $runtype != continue) then
  #$UTILROOT/Tools/ccsm_getfile i01.01cn/lnd/init/$finidat || exit 1 
  $UTILROOT/Tools/ccsm_getfile lnd/init/$finidat || exit 1 
endif 

if ($runtype == 'startup' ) set nsrest = 0
if ($runtype == 'hybrid'  ) set nsrest = 0
if ($runtype == 'branch'  ) set nsrest = 3
if ($runtype == 'continue') set nsrest = 1

if ($CLM_BGC == 'CN' && $ATM_GRID == 'T31') then
   set dtimex = 1200
   set rtm_nstepsx = 36
else if ($CLM_BGC == 'CASA' && $ATM_GRID == 'T31') then
   set dtimex = 1200
   set rtm_nstepsx = 36
else
   set dtimex = 1200
   set rtm_nstepsx = 36
endif
endif

cat >! lnd.stdin << EOF
 &clmexp
 caseid           = '$CASE'
 ctitle           = '$CASE $CASESTR'  
 brnch_retain_casename = .true.
 nsrest           =  $nsrest
 start_ymd        =  05010101
 start_tod        =  0
 nelapse          = -9999    
 rtm_nsteps       =  $rtm_nstepsx
 dtime            =  $dtimex
 irad             = -1
 csm_doflxave     = .true.
 hist_nhtfrq      =  0
 hist_crtinic     = 'YEARLY'
 nrevsn           = '$nrevsn'
 finidat          = '$finidat'
 fsurdat          = '$fsurdat'
 fatmgrid         = '$fatmgrid'
 fatmlndfrc       = '$fatmlndfrc'
 fpftcon          = '$pftdat'
 frivinp_rtm      = 'rdirc.05'
 mss_irt          =  0
 rpntpath         = 'rpointer.lnd'
 co2_type         = '$CLM_CO2_TYPE'
EOF
 
if ($CLM_BGC == CN) then
cat >> lnd.stdin << EOF
 fndepdat         = '$fndepdat'
EOF
endif

if ($CLM_BGC == CASA) then
cat >> lnd.stdin << EOF
 spunup           = 0
 q10              = 2.0
EOF
endif

cat >> lnd.stdin << EOF
 /
EOF

if ( $OS == ESOS ) then
  cat lnd.stdin | sed 's#^ */ *$# clump_pproc      = 1#' >! lnd.stdin.tmp
  echo ' /' >>! lnd.stdin.tmp
  /bin/mv -f lnd.stdin.tmp lnd.stdin
endif

