#!/bin/csh -fv

### =============
### Job settings
### =============

### project numbers available
setenv PROJECT       XXXX
setenv timewall      0:20
setenv queue         regular
setenv PBS_ACCOUNT   XXXX

### Run with ./create_scam6_iop <IOP> where IOP names are listed below
### Alternatively with the 'parallel' script: 
### parallel -j12 ./create_scam6_iop {} ::: arm95 arm97 atex bomex cgilsS11 cgilsS12 cgilsS6 dycomsRF01 dycomsRF02 gateIII mpace rico sparticus togaII twp06

### (-j12 builds & runs 12 create_scam6 cases in parallel and each case should call make with 4 processors so that uses all 48 processors on a node.) 



setenv this_dir  `pwd`

### ===========================================
### Location of cesm code and case directories
### ===========================================

### Model
set MODEL = SCAM5
set CESMDIR = XXXX
set INPUTDIR = XXXX
set CASEDIR = XXXX

### Standard Run Settings
set RES  = T42_T42
set NL   = 30
set DT   = 1800
set LT   = 100
set COMPSET = FSCAM
set COMPILER = intel

### Machine
set MACH = cheyenne
 
### Name of the 'IOP'
set IOP = rcemip
set SST = 300

### Case Name
set CASENAME = ${MODEL}.${IOP}_sst${SST}.L${NL}.${DT}s.${LT}d
cd  $CASEDIR

### Location of IOP data in CESM Tag
set MODSDIR = $CESMDIR/components/cam/cime_config/usermods_dirs



### ================
### Create the case
### ================

### Create a new case
$CESMDIR/cime/scripts/create_newcase --compset $COMPSET --res $RES --compiler $COMPILER --case $CASEDIR/$CASENAME --user-mods-dir ${MODSDIR}/scam5_${IOP} --run-unsupported --machine $MACH

cd  $CASEDIR/$CASENAME

### Build/Run directory (here under case directory)
set BLDDIR = ${CASEDIR}/${CASENAME}/bld
set RUNDIR = ${CASEDIR}/${CASENAME}/run

### Set build and run directories to be under case directory.
./xmlchange EXEROOT=$BLDDIR
./xmlchange RUNDIR=$RUNDIR
./xmlchange DOUT_S="FALSE"



### =========================
### Additional modifications
### =========================

### DEBUG
#./xmlchange DEBUG='TRUE'

### CAM CONFIGURATION OPTIONS HERE
./xmlchange CAM_CONFIG_OPTS="-phys cam5 -chem none"

### Set levels (NL)
./xmlchange --append CAM_CONFIG_OPTS="-nlev ${NL}"

### Set timestep (DT):
### For CESM timestep is atm_cpl_dt. Set as atm_cpl_dt = NCPL_BASE_PERIOD / ATM_NCPL
### Default NCPL_BASE_PERIOD = 1 day = 86400 sec., default ATM_NCPL = 48 => default atm_cpl_dt = 1800 sec.
@ atmncpl = 86400 / $DT
./xmlchange ATM_NCPL=$atmncpl

### Set running period
@ nstep = $LT * 86400 / $DT
./xmlchange STOP_OPTION=nsteps
./xmlchange STOP_N=$nstep

### Set sea surface temperature (SST) and sea ice data file
./xmlchange SSTICE_DATA_FILENAME="${INPUTDIR}/SST${SST}K_ICEnone_180x360.nc"

### Change CPL_ALBAV to set "flux_albav = .true." in drv_in (for the fixed surface albedo)
./xmlchange CPL_ALBAV="TRUE"

### Add all user specific cam namelist changes here
### Output can also be specified here (e.g. fincl1)
cat >> user_nl_cam << EOF
ncdata  = '${INPUTDIR}/RCEMIPinit_sst${SST}_lv${NL}.nc'
iopfile = '${INPUTDIR}/RCEMIPiop_sst${SST}_lv${NL}_1200d.nc'
history_chemistry      = .false.
history_clubb          = .false.
EOF
### Alternative ICs
#cat >> user_nl_cam << EOF
#ncdata  = '${INPUTDIR}/RCEALTinit_sst${SST}_rh70_u5_lv${NL}.nc'
#iopfile = '${INPUTDIR}/RCEALTiop_sst${SST}_rh70_u5_lv${NL}_1200d.nc'
#EOF



### ==================================
### Setup ,build, and submit the case
### ==================================

### Setup Case
#./case.setup -clean
./*.setup #-d -v  #-d -v for verbose and debug file

### Copy source mods into case directory
/bin/cp  ${MODSDIR}/scam5_${IOP}/*.F90 SourceMods/src.cam/
/bin/mv  SourceMods/src.cam/seq_flux_mct.F90 SourceMods/src.drv/seq_flux_mct.F90

### Build
qcmd -- ./*.build #-d -v   #-d -v for verbose and debug file
### Re-build
#./case.build #-d -v   #-d -v for verbose and debug file

### Make timing dir kludge
mkdir -p $RUNDIR/timing/checkpoints

### Submit to Queue
./*.submit





###  After building the case you can also run interactively instead of going through the queue

#  cd /scratch/cluster/$USER/$CASENAME/run  
#  ../bld/cesm.exe 

