#! /bin/tcsh -f
#
set NTKS = 6
setenv OMP_NUM_THREADS 1

setenv OMP_STACKSIZE 64M

set USER_FC = mpif90
set FC_TYPE = pgi
setenv USER_CC gcc

set NETCDF = /usr/local/netcdf-4.5.0

setenv INC_NETCDF ${NETCDF}/include
setenv LIB_NETCDF ${NETCDF}/lib
setenv MOD_NETCDF $INC_NETCDF

setenv MPI_TYPE_MAX 100000

## Do our best to get sufficient stack memory
limit stacksize unlimited

## ROOT OF CAM DISTRIBUTION - probably needs to be customized.
## Contains the source code for the CAM distribution.
## (the root directory contains the subdirectory "models")

set curdir = `pwd`
set camroot = $curdir/..

## ROOT OF CAM DATA DISTRIBUTION - needs to be customized unless running at NCAR.
## Contains the initial and boundary data for the CAM distribution.
## (the root directory contains the subdirectories "atm" and "lnd")
setenv CSMDATA     /terminator-data1/fvitt/inputdata

## Default namelist settings:
## $case is the case identifier for this run. It will be placed in the namelist.
## $runtype is the run type: initial, restart, or branch.
## $nelapse is the number of timesteps to integrate, or number of days if negative.
set case         = waccm_xnox_restart
set runtype      = continue
set stop_n       = 2

## $wrkdir is a working directory where the model will be built and run.
## $blddir is the directory where model will be compiled.
## $rundir is the directory where the model will be run.
## $cfgdir is the directory containing the CAM configuration scripts.
set wrkdir       = /terminator-data1/$LOGNAME/testruns
set blddir       = $wrkdir/$case/bld
set rundir       = $wrkdir/$case/rest
set cfgdir       = $camroot/components/cam/bld

#rm -fr $wrkdir/$case
#rm -fr $blddir
#rm -fr $blddir/cam

## Ensureq that run and build directories exist
mkdir -p $rundir                || echo "cannot create $rundir" && exit 1
mkdir -p $blddir                || echo "cannot create $blddir" && exit 1

## If an executable doesn't exist, build one.
if ( ! -x $blddir/cam ) then
    cd $blddir                  || echo "cd $blddir failed" && exit 1
    $cfgdir/configure -ldflags "-llapack -lblas -Mnobounds" \
      -fc $USER_FC -fopt " -O1 -Kieee" \
      -fc_type $FC_TYPE \
      -aquaplanet \
      -phys cam6 -chem waccm_tsmlt_mam4 -usr_mech_infile $camroot/MZ251_TSMLT1_XNOX_20190109.in \
      -spmd \
      -smp \
      -dyn fv \
      -hgrid 10x15 \
   || echo "configure failed" && exit 1

    echo "building CAM in $blddir ..."
    rm -f Depends
    /usr/bin/gmake -j4 >&! MAKE.out || echo "CAM build failed: see $blddir/MAKE.out" && exit 1
#    rm -f *.o *.mod
endif

echo " "
echo "build complete"
echo " "
#exit 0

## Create the namelist
cd $blddir || echo "cd $blddir failed" && exit 1
$cfgdir/build-namelist -case $case -runtype $runtype -d $rundir -ignore_ic_date -use_case waccm_tsmlt_hist_cam6 -infile $curdir/restart_nl -test \
 -namelist "&camexp  stop_n=$stop_n, stop_option='nsteps'\
 phys_loadbalance = 2\
 restart_option='none'\
 npr_yz = 3,2,2,3\
 / "  || echo "build-namelist failed" && exit 1

#exit 0

## Run CAM
cd $rundir                      || echo "cd $rundir failed" && exit 1
echo "running CAM in $rundir..."

mpirun -l -np $NTKS $blddir/cam >&! rest.log || echo "CAM run failed" && exit 1

exit 0
