#!/bin/sh
YEAR=1985
casename="test_mg4"
casename_root="c81_mg0"

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

SCRATCH=/work1/lkkbox945
mach_name=peta
CASERUN=$SCRATCH/scratch/cesm_work/$casename
CASEROOT=$SCRATCH'/scratch/cesm_work/case/'$casename
CLONEROOT="/work1/lkkbox945/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/lkkbox945/scratch/cesm_work/archive/${casename_root}/rest/${YEAR}-01-01-00000/* $CASERUN'/run/'
cp /work1/lkkbox945/models/cesm/source/nudging_sst_201124/restoring_201216/baroclinic.F90    $CASEROOT/SourceMods/src.pop2/
cp /work1/lkkbox945/models/cesm/source/nudging_sst_201124/restoring_201216/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\/c85_mg4.sst/g" $CASEROOT/SourceMods/src.pop2/restoring_mod.F90
sed -i "s/unknown_infile_spc/restoring_spc\/c85_mg4.spc/g" $CASEROOT/SourceMods/src.pop2/restoring_mod.F90

#mkdir -p $CASERUN'/run/restoring_sst/'
#mkdir -p $CASERUN'/run/restoring_spc/'
rm $CASERUN/run/restoring_sst/*.nc
rm $CASERUN/run/restoring_spc/*.nc
for ddd in {001..100}; do
ln -s /work1/lkkbox945/data/cesm1_archive/restoring_input/c85_mg4/c85_mg4.sst.1985.040.12.nc $CASERUN/run/restoring_sst/c85_mg4.sst.1985.${ddd}.12.nc
ln -s /work1/lkkbox945/data/cesm1_archive/restoring_input/c85_mg4/c85_mg4.spc.1985.040.12.nc $CASERUN/run/restoring_spc/c85_mg4.spc.1985.${ddd}.12.nc
done
#ln -s /work1/lkkbox945/data/cesm1_archive/restoring_input/c85_mg4/c85_mg4.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=40
#./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 ndays
./xmlchange -file env_run.xml -id STOP_N        -val 2 
#./xmlchange DOUT_S_ROOT="/work1/lkkbox945/scratch/cesm_work/archive/$casename"
#./xmlchange RUN_STARTDATE="${YEAR}-01-01"
#./xmlchange RUN_REFDATE="${YEAR}-01-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
chmod -R u+wr $CASERUN/run/

./*.submit

watch 'qstat | grep "dc20200053"'



