#!/bin/csh -f
setenv CCSMROOT /glade/p/cesm/cseg/collections/cesm1_1_1
setenv GSCRATCH /glade/scratch/qiangsun

setenv MYRUNS $GSCRATCH
setenv CASE1 $MYRUNS/g40_EBMpaper_revision_upwind3_vsf_ebm_HU10HL10

#set case specifications

set EBM_TYPE=2
#EBM_TYPE = 1 vsf     (ROFF_VSF only)
#EBM_TYPE = 2 vsf_ebm (ROFF_VSF + EXCH_VSF)

set HIS_OUTPUT=2
#HIS_OUTPUT = 1 daily history output
#HIS_OUTPUT = 2 monthly history output

set TADVECT_CTYPE = 0
#TADVECT_CTYPE = 0 upwind3 (all tracers+T+S)
#TADVECT_CTYPE = 1 lm_win  (all tracers+T+S)
#TADVECT_CTYPE = 2 only salinity lim_win

set REF_S=1
#REF_S = 1 local salinity reference
#REF_S = 2 global salinity reference

set EBM_TP=1
#EBM_TP = 0 EBM tidal pumping off
#EBM_TP = 1 EBM tidal pumping on

cd $CCSMROOT/scripts
./create_newcase -compset GIAF -res T62_gx1v6 -mach yellowstone -case $CASE1
cd $CASE1
source $CASE1/Tools/ccsm_getenv

./xmlchange -file env_run.xml -id STOP_OPTION -val nyears
./xmlchange -file env_run.xml -id STOP_N -val 1
#./xmlchange -file env_run.xml -id CONTINUE_RUN -val TRUE
#./xmlchange -file env_run.xml -id RESUBMIT -val 5

./xmlchange -file env_run.xml -id RUN_TYPE -val hybrid
./xmlchange -file env_run.xml -id RUN_REFCASE -val g40.000
./xmlchange -file env_run.xml -id RUN_REFDATE -val 0301-01-01

./xmlchange -file env_mach_pes.xml -id NTASKS_ATM -val 96
./xmlchange -file env_mach_pes.xml -id NTASKS_LND -val 96
./xmlchange -file env_mach_pes.xml -id NTASKS_ICE -val 96
./xmlchange -file env_mach_pes.xml -id NTASKS_OCN -val 96
./xmlchange -file env_mach_pes.xml -id NTASKS_CPL -val 96
./xmlchange -file env_mach_pes.xml -id NTASKS_GLC -val 96
./xmlchange -file env_mach_pes.xml -id NTASKS_ROF -val 96
#./xmlchange -file env_mach_pes.xml -id NTASKS_WAV -val 96

./xmlchange -file env_mach_pes.xml -id NTHRDS_ATM -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_LND -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_ICE -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_OCN -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_CPL -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_GLC -val 1
./xmlchange -file env_mach_pes.xml -id NTHRDS_ROF -val 1
#./xmlchange -file env_mach_pes.xml -id NTHRDS_WAV -val 1

./xmlchange -file env_mach_pes.xml -id ROOTPE_OCN -val 0

./cesm_setup

###sed -i '$s/$/\n rof2ocnFmapname = '"'"'\/glade\/p\/cgd\/oce\/people\/ytseng\/ccsm\/mapping\/makemaps\/runoff_maps\/map_rx1_to_gx1v6_nearestdtos_rs_ac_v3_nloc_150801.nc'"'"'/' user_nl_cpl
sed -i '$s/$/\n rof2ocnFmapname = '"'"'\/glade\/p\/cgd\/oce\/people\/ytseng\/ccsm\/mapping\/makemaps\/runoff_maps\/map_rx1_to_gx1v6_ac_nn_160304.nc'"'"'/' user_nl_cpl

cp ~/QS_CESM/CHANGES/v1_1_1_EBM_VSF_EBMpaper/src.pop2/* $CASE1/SourceMods/src.pop2/

sed -i '$s/$/\n estuary_option = '"'"on"'"' /' user_nl_pop2

#EBM_VSF setting
if ( $EBM_TYPE == 1 ) then
 sed -i '$s/$/\n estuary_type = '"'"vsf"'"'/' user_nl_pop2
else if ( $EBM_TYPE == 2) then
 sed -i '$s/$/\n estuary_type = '"'"vsf_ebm"'"'/' user_nl_pop2
endif

sed -i '$s/$/\n ebm_param_option = '"'"file"'"' /' user_nl_pop2
if ( $EBM_TP == 0 ) then
 sed -i '$s/$/\n ebm_param_filename = '"'"'\/glade\/u\/home\/qiangsun\/QS_CESM\/CHANGES\/v1_1_1_EBM_VSF_EBMpaper\/EBM_para_infile\/EBMparameter_TPoff_global_HU20HL20.nc'"'"' /' user_nl_pop2
else if ( $EBM_TP == 1 ) then
### sed -i '$s/$/\n ebm_param_filename = '"'"'\/glade\/u\/home\/qiangsun\/QS_CESM\/CHANGES\/v1_1_1_EBM_VSF_EBMpaper\/EBM_para_infile\/EBMparameter_global_HU20HL20.nc'"'"' /' user_nl_pop2
 sed -i '$s/$/\n ebm_param_filename = '"'"'\/glade\/u\/home\/qiangsun\/QS_CESM\/EBM_para_infile\/EBMparameter_global_HU20HL20_allROFF_CESM1G_HU10HL10.nc'"'"' /' user_nl_pop2
endif
sed -i '$s/$/\n ebm_param_file_fmt = '"'"nc"'"' /' user_nl_pop2

#history output setting
if ( $HIS_OUTPUT == 1 ) then
 sed -i '$s/$/\n tavg_file_freq_opt = '"'"nday"' ""'"nmonth"' ""'"once"' "'/' user_nl_pop2
 sed -i '$s/$/\n tavg_freq_opt = '"'"nday"' ""'"nday"' ""'"once"' "'/' user_nl_pop2
endif

#TRACER ADVECDTION SCHEME SETTING
if ( $TADVECT_CTYPE == 0) then
 rm $CASE1/SourceMods/src.pop2/advection.F90
else if ( $TADVECT_CTYPE == 1) then
 sed -i '$s/$/\n tadvect_ctype='"'"'lw_lim'"'"'/' user_nl_pop2
else if ( $TADVECT_CTYPE == 2) then
 sed -i "s/-DCCSMCOUPLED/-DCCSMCOUPLED -DTADVECT_ITYPE2/" $CASE1/Buildconf/pop2.buildexe.csh
endif

#REFERENCE SALINITY SELECTION
if ( $REF_S == 1) then
 rm $CASE1/SourceMods/src.pop2/estuary_vsf_mod.F90_GLOBAL_refS
else if ( $REF_S == 2) then
 cp $CASE1/SourceMods/src.pop2/estuary_vsf_mod.F90_GLOBAL_refS $CASE1/SourceMods/src.pop2/estuary_vsf_mod.F90
 rm $CASE1/SourceMods/src.pop2/estuary_vsf_mod.F90_GLOBAL_refS
endif

#$CASE1/*.build 
#cp ~/QS_CESM/CHANGES/v1_1_1_EBM_VSF_EBMpaper/run/* $CASE1/run

#CHECK RUN
sed -i "s/ 4:00/ 12:00/g" $CASE1/*.run
#sed -i "s/ regular/ capability/g" $CASE1/*.run
sed -i '/scratch_ok/d' $CASE1/*.run
sed -i "s/ P00000000/ P93300012/g" $CASE1/*.run
sed -i "s/ ptile=15/ ptile=16/g" $CASE1/*.run
#bsub < $CASE1/*.run
