#!/bin/bash -fe

set -e 

#..................................................................................
# setup
#..................................................................................
readonly MACHINE="pm-gpu"
readonly COMPILER="gnugpu"
readonly PROJECT="e3sm"
#readonly QUEUE="debug"
readonly QUEUE="regular"
#readonly QUEUE="compute"
readonly DEBUG_COMPILE=FALSE

#..................................................................................
# code
#..................................................................................
# /pscratch/sd/k/kaizhang/model/e3sm_20250625_ad9d54f/E3SM/work
readonly CODE_ROOT="/pscratch/sd/k/kaizhang/model/E3SM" 
readonly SCREAMDOCS_ROOT="${CODE_ROOT}/docs"
readonly CHECKOUT=""
readonly BRANCH=""
readonly CHERRY=( )

#..................................................................................
# compset, relution, pelayout, time 
#..................................................................................
readonly COMPSET="F2010-EAMxx-MAM4xx"
readonly RESOLUTION="ne30pg2_ne30pg2"
##readonly PELAYOUT="640x1"
readonly WALLTIME="05:59:00"

#..................................................................................
# case
#..................................................................................
readonly CASE_NAME=${COMPSET}_${RESOLUTION}_mamxx_20250718
readonly CASE_ROOT="${CODE_ROOT}/work/${CASE_NAME}"
readonly CASE_GROUP=""
readonly MODS_ATM=""

#..................................................................................
# coupler output frequecy
#..................................................................................
readonly HIST_OPTION="nmonths"
readonly HIST_N="1"

#..................................................................................
# run options
#..................................................................................
readonly MODEL_START_TYPE="initial"  # "initial", "continue", "branch", "hybrid"
readonly START_DATE="0001-01-01"     # "" for default, or explicit "0001-01-01"
readonly STOP_OPTION="nmonths"
readonly STOP_N="12"
readonly REST_OPTION="nmonths"
readonly REST_N="12"
readonly RESUBMIT="1"
readonly DO_SHORT_TERM_ARCHIVING=false
readonly OLD_EXECUTABLE=""

#..................................................................................
# branch/hybrid run setup
#..................................................................................
readonly GET_REFCASE=false
readonly RUN_REFDIR=""
readonly RUN_REFCASE=""
readonly RUN_REFDATE=""   # same as MODEL_START_DATE for 'branch', can be different for 'hybrid'

#..................................................................................
# sub-directories
#..................................................................................
readonly CASE_BUILD_DIR=${CASE_ROOT}/build
readonly CASE_ARCHIVE_DIR=${CASE_ROOT}/archive
readonly CASE_SCRIPTS_DIR=${CASE_ROOT}/case_scripts
readonly CASE_RUN_DIR=${CASE_ROOT}/run

umask 022

#..................................................................................
# create case
#..................................................................................
##	--pecount ${PELAYOUT} \
${CODE_ROOT}/cime/scripts/create_newcase \
	--case ${CASE_NAME} \
	--output-root ${CASE_ROOT} \
	--script-root ${CASE_SCRIPTS_DIR} \
	--handle-preexisting-dirs u \
	--res ${RESOLUTION} \
	--compset ${COMPSET} \
	--machine ${MACHINE} \
	--compiler ${COMPILER} \
	--project ${PROJECT} \
	--queue ${QUEUE} \
	--user-mods-dir ${MODS_ATM} \
	--walltime ${WALLTIME}

pushd ${CASE_SCRIPTS_DIR}

./xmlchange EXEROOT=${CASE_BUILD_DIR}
./xmlchange RUNDIR=${CASE_RUN_DIR}
./xmlchange DOUT_S=${DO_SHORT_TERM_ARCHIVING}
./xmlchange DOUT_S_ROOT=${CASE_ARCHIVE_DIR}

#..................................................................................
# PELAYOUT
#..................................................................................
# ne30pg2 
    nt=16
    np=32
# ne4pg2 
#    nt=2
#    np=4
    ./xmlchange --file env_mach_pes.xml NTASKS_ATM="$np"
    ./xmlchange --file env_mach_pes.xml NTASKS_LND="$np"
    ./xmlchange --file env_mach_pes.xml NTASKS_ICE="$np"
    ./xmlchange --file env_mach_pes.xml NTASKS_OCN="$np"
    ./xmlchange --file env_mach_pes.xml NTASKS_CPL="$np"
    ./xmlchange --file env_mach_pes.xml NTASKS_ROF="$np"
    ./xmlchange --file env_mach_pes.xml NTASKS_GLC="1"
    ./xmlchange --file env_mach_pes.xml NTASKS_WAV="1"
    ./xmlchange --file env_mach_pes.xml NTASKS_IAC="$np"
    ./xmlchange --file env_mach_pes.xml NTASKS_ESP="$np"
    ./xmlchange --file env_mach_pes.xml NTHRDS_LND="$nt"
    ./xmlchange --file env_mach_pes.xml NTHRDS_ICE="$nt"

#..................................................................................
# setup case 
#..................................................................................
./case.setup --reset
popd

pushd ${CASE_SCRIPTS_DIR}

#..................................................................................
# specific test configurations 
#..................................................................................
./xmlchange DEBUG=${DEBUG_COMPILE}

#..................................................................................
# build model
#..................................................................................
pushd ${CASE_SCRIPTS_DIR}
./case.build --sharedlib-only
./case.build --model-only

#..................................................................................
# Some user_nl settings won't be updated to *_in files under the run directory
# Call preview_namelists to make sure *_in and user_nl files are consistent.
#..................................................................................
pushd ${CASE_SCRIPTS_DIR}
./preview_namelists

echo " " 
echo " " 
echo " " 

popd


#..................................................................................
# configure run
#..................................................................................
pushd ${CASE_SCRIPTS_DIR}

./xmlchange RUN_STARTDATE=${START_DATE}

./atmchange output_yaml_files="/pscratch/sd/k/kaizhang/model/yaml/scream_default_monthly_mamall_micro.yaml"
#./atmchange output_yaml_files+="${CODE_ROOT}/work/yaml/scream_default_daily_mamall.yaml"

./atmchange mam4_aero_microphys::extra_mam4_aero_microphys_diags=true -b

# Segment length
./xmlchange STOP_OPTION=${STOP_OPTION,,},STOP_N=${STOP_N}

# Restart frequency
./xmlchange REST_OPTION=${REST_OPTION,,},REST_N=${REST_N}

# Coupler history
./xmlchange HIST_OPTION=${HIST_OPTION,,},HIST_N=${HIST_N}

# Coupler budgets (always on)
./xmlchange BUDGETS=TRUE

# Set resubmissions
./xmlchange RESUBMIT=${RESUBMIT}

#..................................................................................
# run type
#..................................................................................
# Start from default of user-specified initial conditions
if [ "${MODEL_START_TYPE,,}" == "initial" ]; then
    ./xmlchange RUN_TYPE="startup"
    ./xmlchange CONTINUE_RUN="FALSE"

# Continue existing run
elif [ "${MODEL_START_TYPE,,}" == "continue" ]; then
    ./xmlchange CONTINUE_RUN="TRUE"

elif [ "${MODEL_START_TYPE,,}" == "branch" ] || [ "${MODEL_START_TYPE,,}" == "hybrid" ]; then
    ./xmlchange RUN_TYPE=${MODEL_START_TYPE,,}
    ./xmlchange GET_REFCASE=${GET_REFCASE}
    ./xmlchange RUN_REFDIR=${RUN_REFDIR}
    ./xmlchange RUN_REFCASE=${RUN_REFCASE}
    ./xmlchange RUN_REFDATE=${RUN_REFDATE}
    echo 'Warning: $MODEL_START_TYPE = '${MODEL_START_TYPE}
    echo '$RUN_REFDIR = '${RUN_REFDIR}
    echo '$RUN_REFCASE = '${RUN_REFCASE}
    echo '$RUN_REFDATE = '${START_DATE}

else
    echo 'ERROR: $MODEL_START_TYPE = '${MODEL_START_TYPE}' is unrecognized. Exiting.'
    exit 380
fi

echo " " 
echo " " 
echo " " 

popd

#..................................................................................
# submit job
#..................................................................................
pushd ${CASE_SCRIPTS_DIR}
./case.submit
popd




