##!/bin/bash
# ------------------------------------------------------------------
# Author: andreasalazar@g.harvard.edu
# Title: test_create_new_build.sh
# Description: creates a CESM2 case, configures it, and builds it
# ------------------------------------------------------------------
USAGE="Usage: $0 CASE"
OPTIONS="res [default:f09_f09_mg17], compset [default:F2000climo] in that order"
NOTES=""
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
    echo $USAGE
    echo $OPTIONS
    echo $NOTES
    exit 1;
fi
CASE=$1
# Accept arguments with defaults:
# RES=${2:-"f09_g17"} # https://csegweb.cgd.ucar.edu/exp2-public/cgi-bin/expListPublic.cgi
# COMPSET=${3:-"BCO2x4cmip6"} # https://csegweb.cgd.ucar.edu/exp2-public/cgi-bin/expListPublic.cgi
# RESTART_FILE=${4:-"b.e21.BCO2x4cmip6.f09_g17.CMIP6-abrupt4xCO2.001"}
# echo RES:$RES
# echo COMPSET:$COMPSET

RES=${2:-"f19_g17"} #
COMPSET=${3:-"B1850"} 
echo RES:$RES
echo COMPSET:$COMPSET

export COMPILER="intel"

# Set directories
# illustration of directories: http://www.cesm.ucar.edu/events/tutorials/2016/practical1-bertini.pdf p.48
SCRIPTDIR=$PWD
CESMROOT=/glade/work/asalazar/derecho_cesm2
cd $CESMROOT/cime/scripts/
CASEROOT="${HOME}/EPS231/INTRO/${CASE}"
#SOURCEMODS="${HOME}/cesm2/cloud_locking_raw"
OUTPUT="/glade/derecho/scratch/asalazar/${CASE}" # best to use scratch
RUNDIR="${OUTPUT}/run"
echo $CASEROOT

# Account (e.g. uhar0009)
export PROJECT=UHAR0028

# Create case
./create_newcase --case ${CASEROOT} --res ${RES} --compset ${COMPSET} --project ${PROJECT} --compiler intel --run-unsupported|| exit -1

cd $CASEROOT
./case.setup --clean

# Move custom modifications (user_nl_*, source mods) to CASEROOT
cp -a $SCRIPTDIR/$0 $CASEROOT # make a copy in the caseroot
cp -a $SCRIPTDIR/user_nl_* $CASEROOT
#cp -a $SCRIPTDIR/user_nl_clm $CASEROOT
# cp -a $SCRIPTDIR/user_nl_cpl $CASEROOT
#./preview_namelist # to check consistency of user_nl_* files
# Copy source modifications


# Modify tasks/cores: env_mach_pes.xml


./xmlchange JOB_WALLCLOCK_TIME=12:00:00
# Set up case run scripts
./case.setup
# ln -sT $OUTPUT scratch # not sure what this does...link?
# ./preview_run # more info on how case will be run
cp -a $SCRIPTDIR/SourceMods/* $CASEROOT/SourceMods/src.cam
# Modify build: env_build.xml
./xmlchange EXEROOT=$OUTPUT/bld
#./xmlchange DEBUG=TRUE

# Set number of CAM levels
#   this is not recommended
#     these are set as compset variables and should not gen be modified for supported/verified compsets
#   also need to provide new initial  condition files at the new vertical resolution
#./xmlchange -file env_build.xml -id CAM_CONFIG_OPTS -val "-nlev 56"

# Build executables
#qcmd -- ./case.build --clean
qcmd -- ./case.build 
echo 

# Modify run: env_run.xml

# storage:
./xmlchange --id RUN_STARTDATE --val 0001-01-01

./xmlchange RUNDIR=$RUNDIR
./xmlchange DOUT_S=TRUE # Reorganizes the files into nice folders
./xmlchange DOUT_S_ROOT=$OUTPUT

# ./xmlchange --id POSTRUN_SCRIPT_BATCH --val "TRUE" # Cloud locking
#./xmlchange --id POSTRUN_SCRIPT --val "adjustCloudData" # Cloud locking
# duration of run:
./xmlchange STOP_OPTION=nyears
./xmlchange STOP_N=5
./xmlchange RESUBMIT=3 # total times submitted = RESUBMIT+1

# continuation or new run?
./xmlchange CONTINUE_RUN=FALSE

# debug?
# ./xmlchange INFO_DBUG=3

# Set CO2 concentration
#   alternative ways to change CO2:
#     set co2vmr variable or forcings in user_nl_cam
#     pick a different compset
#./xmlchange -file env_run.xml -id CCSM_CO2_PPMV -val 560

# Change internal timestep
# if ATM_NCPL is set and run fails with 'ERROR: Bad namelist settings for FV subcycling':
#   then also modify NSPLIT, NSPLTRAC, NSPLTVRM in user_nl_cam
#./xmlchange -file env_run.xml -id ATM_NCPL -val 96 # coupling times per day; ATM_NCPL/24 = number of timesteps per hour

# List any changes made to namelist files or source code
grep -Ev '^\s*$|^\s*!' user_nl_*
\ls -l SourceMods/*/*

# if resubmit, add a couple lines to the PBS script on top
# sed -i '/RESUBMIT > 0/i cd $CASEROOT' case.run

# Instructions to submit the job upon build
printf "To submit job, issue:\n"
printf "cd ${CASEROOT}\n"
printf "./case.submit\n"

# printf "To extend the run after completion, issue:\n"
# printf "./xmlchange CONTINUE_RUN=TRUE\n"
# printf "similarly, may want to modify STOP_N, RESUBMIT"
# printf "then re-submit with ./case.submit as before"

