#!/bin/bash

change_rpointer () {

    local RUN_DIR=$1
    local YEAR=$2
    local MONTH=$3
    local DAY=$4

    sed -i "1s/r\.....-..-../r\.${YEAR}-${MONTH}-${DAY}/g" ${RUN_DIR}/rpointer.atm
    sed -i "1s/r\.....-..-../r\.${YEAR}-${MONTH}-${DAY}/g" ${RUN_DIR}/rpointer.drv
    sed -i "1s/r\.....-..-../r\.${YEAR}-${MONTH}-${DAY}/g" ${RUN_DIR}/rpointer.lnd
    sed -i "1s/r\.....-..-../r\.${YEAR}-${MONTH}-${DAY}/g" ${RUN_DIR}/rpointer.rof
    sed -i "1s/r\.....-..-../r\.${YEAR}-${MONTH}-${DAY}/g" ${RUN_DIR}/rpointer.ice
   }
## ====================================================================
change_rpointer_ocn () {

    local RUN_DIR=$1
    local YEAR=$2
    local MONTH=$3
    local DAY=$4

    sed -i "1s/ro\.....-..-../ro\.${YEAR}-${MONTH}-${DAY}/g" ${RUN_DIR}/rpointer.ocn.ovf
    sed -i "1s/r\.....-..-../r\.${YEAR}-${MONTH}-${DAY}/g" ${RUN_DIR}/rpointer.ocn.restart
   }
## ====================================================================

test_case_created () {
   local atm_init_file=$1
    if [ -f ${RUNDIR_INI}/${atm_init_file} ] ; then
     echo ${CASE} has been created
     continue
    fi
 
  } 

#############################################################

test_case_ready () {
   local atm_init_file=$1
   local analysis_inc=$2
   if [ -f ${RUNDIR_INI}/${atm_init_file} ] && [ -f ${DIR_ASSIM}/${analysis_inc} ] ; then
    echo ${CASE} can be created NOW
   else
    echo ${RUNDIR_INI}/${atm_init_file} or ${DIR_ASSIM}/${analysis_inc} not exist
    echo ${CASE} cannot be created NOW
    continue
  fi

  }   

#   case 
## ====================================================================

export CCSMTAG='cesm1_2_0'
export CASE_INI='prest'
export RUNDIR_INI=/THL7/home/timli02/jiangls/CESM1_2_0/INITIAL_CONDITION/prest/1971-01-01
export DIR_ASSIM=/THL7/home/timli02/jiangls/CESM1_2_0/sst_flow/sst/yearobs/day

export YEAR_INI='1971'
export MONTH_INI='01'
export DAY_INI='01'

export YEAR_INI_OCN='1972'
export MONTH_INI_OCN='01'
export DAY_INI_OCN='01'

#for YEAR_INI in 1851   ; do
#for MONTH_INI in 01 ; do
#for DAY_INI  in 01 ; do

export CASE='tna_obs_1'
## ====================================================================
#   define directories
## ====================================================================
export MACH='tianhe'
export CCSMROOT=/THL7/home/timli02/jiangls/${CCSMTAG} 
export CASEROOT=/THL7/home/timli02/jiangls/CESM1_2_0/script/${CASE}
export EXEROOT=/THL7/home/timli02/jiangls/CESM1_2_0/run/${CASE}
export RUNDIR=${EXEROOT}/run/ 
export DATADIR=/THL7/home/timli02/jiangls/cesm1_0/data

test_case_created ${CASE_INI}.cam.r.${YEAR_INI}-${MONTH_INI}-01-00000.nc
test_case_ready ${CASE_INI}.cam.r.${YEAR_INI}-${MONTH_INI}-01-00000.nc sst.1980.001.12.nc
## ====================================================================
#   create new case, configure, compile and run
## ====================================================================

rm -rf $CASEROOT 

#------------------
## create new case
#------------------

cd  $CCSMROOT/scripts

./create_newcase  -case $CASEROOT -mach $MACH -res f09_g16 -compset B_2000  -compiler intel  #1850_CAM5_CLM40%SP_CICE_POP2_RTM_SGLC_SWAV

#------------------
## set environment
#------------------

cd $CASEROOT

./xmlchange  -file env_run.xml -id  RUN_TYPE  -val 'hybrid'
./xmlchange  -file env_run.xml -id  RUN_REFCASE -val 'prest'
./xmlchange  -file env_run.xml -id  RUN_REFDATE -val ${YEAR_INI}-${MONTH_INI}-01
#./xmlchange  -file env_run.xml -id  RUN_STARTDATE -val ${YEAR_INI}-${MONTH_INI}-01
./xmlchange  -file env_run.xml -id  RUN_STARTDATE -val 1977-01-01
# ./xmlchange  -file env_run.xml -id  RUN_STARTDATE -val 2010-01-01
./xmlchange  -file env_run.xml -id  DIN_LOC_ROOT  -val '/THL7/home/timli02/jiangls/cesm1_0/data' 
./xmlchange  -file env_run.xml -id  RESUBMIT -val '0'
#./xmlchange  -file env_run.xml -id  STOP_N  -val '348'
./xmlchange  -file env_run.xml -id  STOP_N  -val '480'
./xmlchange  -file env_run.xml -id  STOP_OPTION  -val 'nmonths'
./xmlchange  -file env_run.xml -id  REST_N  -val '12'
./xmlchange  -file env_run.xml -id  REST_OPTION  -val 'nmonths'
./xmlchange  -file env_run.xml -id  CONTINUE_RUN -val 'FALSE'
./xmlchange  -file env_run.xml -id  DOUT_L_MS  -val 'FALSE'
./xmlchange  -file env_run.xml -id  GET_REFCASE -val 'FALSE'


 ./xmlchange -file env_mach_pes.xml -id NTASKS_ATM -val '64'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_ATM -val '1'
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_ATM -val '0'

 ./xmlchange -file env_mach_pes.xml -id NTASKS_LND -val '64'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_LND -val '1'
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_LND -val '0'

 ./xmlchange -file env_mach_pes.xml -id NTASKS_ICE -val '64'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_ICE -val '1'
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_ICE -val '0'

 ./xmlchange -file env_mach_pes.xml -id NTASKS_OCN -val '40'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_OCN -val '1'
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_OCN -val '64'

 ./xmlchange -file env_mach_pes.xml -id NTASKS_GLC -val '64'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_GLC -val '1'
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_GLC -val '0'

 ./xmlchange -file env_mach_pes.xml -id NTASKS_CPL -val '64'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_CPL -val '1'
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_CPL -val '0'

 ./xmlchange -file env_mach_pes.xml -id NTASKS_ROF -val '64'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_ROF -val '1'
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_ROF -val '0'

 ./xmlchange -file env_mach_pes.xml -id NTASKS_WAV -val '64'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_WAV -val '1'
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_WAV -val '0'

#------------------
## configure
#------------------
cd $CASEROOT
sed -i '252 s/qw/(qw/g' cesm_setup
sed -i '252 s/WAV)/WAV))/g' cesm_setup    
./cesm_setup
./xmlchange -file env_build.xml -id EXEROOT   -val $EXEROOT
./xmlchange -file env_run.xml   -id RUNDIR    -val $RUNDIR 

#------------------
## build
#------------------

#cp -rf ${CASEROOT_INI}/SourceMods ${CASEROOT}
cp -rf /THL7/home/timli02/jiangls/CESM1_2_0/sst_nudging/* ${CASEROOT}/SourceMods/src.pop2

sed -i "62s/^.*$/set nrevsn_rtm = \"nrevsn_rtm=\'\$\{RUN_REFCASE\}.rtm.r.\$\{RUN_REFDATE\}-\$\{RUN_REFTOD\}.nc\'\"/" ${CASEROOT}/Buildconf/rtm.buildnml.csh  #it is a bug of cesm

cd $CASEROOT
csh ./${CASE}.build

cp ${RUNDIR_INI}/${CASE_INI}*${YEAR_INI}-${MONTH_INI}-01* ${RUNDIR}
cp ${RUNDIR_INI}/${CASE_INI}*${YEAR_INI_OCN}-${MONTH_INI_OCN}-01* ${RUNDIR}
cp ${RUNDIR_INI}/rpointer* ${RUNDIR}
change_rpointer ${RUNDIR} ${YEAR_INI} ${MONTH_INI} 01
change_rpointer_ocn ${RUNDIR} ${YEAR_INI_OCN} ${MONTH_INI_OCN} 01

sed -i "/chmod.*\+x.*yhbatch_use.csh/,+2000d" ./${CASE}.run

csh ./${CASE}.run

cat << EOF >> ${RUNDIR}/pop2_in
&res_nml
forcing_infile_sst='/THL7/home/timli02/jiangls/CESM1_2_0/sst_flow/sst/yearobs/day/sst'
forcing_infile_spc='/THL7/home/timli02/jiangls/CESM1_2_0/sst_flow/spc/TNA/day/spc'
/
EOF

#done
#done
#done

exit
