#!/bin/sh
YEAR=1997
casename="s97_mg1"
casename_root="e97_mg0"
MONTH="04"

CCSMROOT=/project/MST107135/yhtseng00/models/cesm1/cesm1_2_2_1

SCRATCH=/work1/yhtseng00
mach_name=peta
CASERUN=$SCRATCH/scratch/cesm_work/$casename
CASEROOT=$SCRATCH'/scratch/cesm_work/case/'$casename
CLONEROOT="/work1/yhtseng00/scratch/cesm_work/case/${casename_root}"


### ---- create case ----
rm -rf $CASEROOT $CASERUN
cd $CCSMROOT/scripts
./create_clone -case $CASEROOT -clone $CLONEROOT 

# ---- copy initial and restoring files ----
mkdir -p $CASERUN'/run'
cp /work1/yhtseng00/scratch/cesm_work/archive/${casename_root}/rest/${YEAR}-${MONTH}-01-00000/* $CASERUN'/run/'
cp /project/MST107135/yhtseng00/models/cesm1/modified/pacemaker_experiments/restoring_210106/baroclinic.F90    $CASEROOT/SourceMods/src.pop2/
cp /project/MST107135/yhtseng00/models/cesm1/modified/pacemaker_experiments/restoring_210106/restoring_mod.F90 $CASEROOT/SourceMods/src.pop2/

chmod u+w $CASEROOT/SourceMods/src.pop2/restoring_mod.F90
sed -i "s/unknown_infile_sst/restoring_sst\/s97_mg1.sst/g" $CASEROOT/SourceMods/src.pop2/restoring_mod.F90
sed -i "s/unknown_infile_spc/restoring_spc\/s97_mg1.spc/g" $CASEROOT/SourceMods/src.pop2/restoring_mod.F90

rm $CASERUN/run/restoring_sst/*.nc
rm $CASERUN/run/restoring_spc/*.nc
mkdir -p $CASERUN'/run/restoring_sst/'
mkdir -p $CASERUN'/run/restoring_spc/'
ln -s /project/MST107135/yhtseng00/models/cesm1/modified/pacemaker_experiments/restoring_input/s97_mg1/*.sst.????.???.12.nc $CASERUN/run/restoring_sst/
ln -s /project/MST107135/yhtseng00/models/cesm1/modified/pacemaker_experiments/restoring_input/s97_mg1/*.spc.????.???.12.nc $CASERUN/run/restoring_spc/

## -----------------
cd $CASEROOT
#./cesm_setup -clean
#./xmlchange NTASKS_ATM=200
#./xmlchange NTASKS_LND=200
#./xmlchange ROOTPE_LND=0
#./xmlchange NTASKS_ICE=200
#./xmlchange ROOTPE_ICE=0
#./xmlchange NTASKS_OCN=200
#./xmlchange ROOTPE_OCN=0
#./xmlchange NTASKS_CPL=200
#./xmlchange ROOTPE_CPL=0
#./xmlchange NTASKS_ROF=200
#./xmlchange ROOTPE_ROF=0
#./xmlchange NTASKS_GLC=1
#./xmlchange NTASKS_WAV=1
./xmlchange DIN_LOC_ROOT=/project/MST107135/data/inputdata

./xmlchange -file env_run.xml -id CONTINUE_RUN  -val FALSE
./xmlchange -file env_run.xml -id STOP_OPTION   -val nmonths
./xmlchange -file env_run.xml -id STOP_N        -val 33
./xmlchange RUN_TYPE="branch"
./xmlchange DOUT_S_ROOT="/work1/lkkbox945/scratch/cesm_work/archive/$casename"
./xmlchange RUN_STARTDATE="${YEAR}-${MONTH}-01"
./xmlchange RUN_REFDATE="${YEAR}-${MONTH}-01"
./xmlchange RUN_REFCASE="${casename_root}"
./cesm_setup



./preview_namelists
$CASEROOT/*.build


#sed -i "s/MST107192/ACD107096/g" *.run
#sed -i "s/ct800/dc20200053/g" *.run
sed -i "s/MST107192/GOV110049/g" *.run
sed -i "s/ct800/dc20210018/g" *.run
chmod -R u+wr $CASERUN/run/

./*.submit

watch 'qstat | grep "dc20200053"'



