#! /bin/csh -fxv
### If the above line does not work, use dos2unix fileName 
### to remvoe the invisible characters added by Window editors
### 
### Comment out #SBATCH --mem=MaxMemPerNode  line in Case.run file if
### it has a memory problem after submit. 
##
#
#---------------------------------------------
##   Scripts to create and build a new CESM1 new case 
##   scancel job-id      to cancel a job
##
#---------------------------------------------
##   To continue a transient CO2 run with fixed CO2 of the last model year, reset the
##   RAMP_CO2_annual_rate to 0 in env_run.xml and set co2vmr to the CO2 level (e.g., 569.4E-6) 
##   at the end of the last model year in user_nl_cam (default co2vmr is set to 284.7ppmv in 
##   env_run.xml (no need to change that value). 
## 
#--------------------------------------------- 
###
#### Set compset, see http://www.cesm.ucar.edu/models/cesm1.2/cesm/doc/modelnl/compsets.html           
###
#setenv COMPSET  F_2000_CAM5           # for AMIP run with CAM5
setenv COMPSET  B_1850_CN             # for fixed CO2 run (e.g., piControl, 2XCO2, 4XCO2) with CAM4 and starting from 1850 condition
#setenv COMPSET  B_1850_RAMPCO2_CN     # for increasing CO2 run with CAM4 and starting from 1850 condition (also used for
                                      # Bo Dong's piControl run (with co2 rate set to 0, which is the same as B_1850_CN).  
                                      # CO2vmr=248.7 ppmv for piControl, and 2x and 4x that for 2XCO2 and 4xCO2 cases.
#setenv COMPSET E_1850_CN              # for corresponding slab ocean run (for 2XCO or 4xCO2 case)

###
#### set model resol., see http://www.cesm.ucar.edu/models/cesm1.2/cesm/doc/modelnl/grid.html
###
setenv RES  f19_g16     # for 1.9x2.5_gx1v6 grid,  ~2deg grid for CAM
#setenv RES  f45_g37     # for 4x5_gx3v7 grid, ~4x5deg grid fro CAM, for quick testing

###
setenv CCSMTAG  cesm1_2_1      # This ver uses CAM4 (not CAM5)!   
#setenv CASE      piControl     # pre-industrial (1850) control run
#setenv CASE      piCtl20     # pre-industrial (1850) control run done in 2020
setenv CASE      piCtl24     # pre-industrial (1850) control run done in 2024
#setenv CASE     1pctCO2    # your case name
#setenv CASE      som_2XCO2  # for 2XCO2 run with slab ocean
setenv Version  cesm1.2.1

#set node = 1    # for using 1 node on Snow
set node = 4    # for using 4 nodes on Snow: need to change "nodes" to 4, "time" to 11:59:00, and "partition" to "snow" in $CASE.run file 

#---------------------------------------------
## define resubmssion to 0 for the first job: use xmlchange CONTINUE_RUN=TRUE  xmlchange RESUBMIT=N
## reset them after sumitting the first job, where N=no of years to be continue after the 1st yr. 
#---------------------------------------------
setenv CONTINUE_RUN      FALSE
setenv RESUBMIT          0

#---------------------------------------------
## set CO2 increase rate: =0 --> fixed; =1 --> 1%/yr, =2 --> 2%/yr increase
#---------------------------------------------
set CO2 = 0    # set CO2.le.0 for fixed CO2 runs, =1 for increasing CO2 runs
               # set CO2=0 for piControl, CO2=-1 for 2XCO2 and CO2=-2 for 4XCO2 of pi level
set ramp_co2_annual_rate = 0    # =0 for fixed CO2, =1 for 1%/yr increase, =2 for 2%/yr increase in CO2 

#---------------------------------------------
##   define directories
#---------------------------------------------                                                                                                                                         
setenv MACH      snow        
setenv PTMP      /network/rit/lab/dailab_rit/adai/cesm
setenv CCSMROOT  /network/rit/home/br546577/roselab_rit/cesm/cesm1_2_1
setenv CASEROOT  $PTMP/$CASE        # make sure this dir does not exist
setenv EXEROOT   $CASEROOT/run      # for executable files
setenv RUNDIR    $CASEROOT/run      # for runtime output, including log files
setenv DIN_LOC_ROOT  /adai/bdong/cesm_inputdata   # for initial files 
#setenv DOUT_S_ROOT   /adai/adai/cesm1_2_1/$CASE   # for final output files
setenv DOUT_S_ROOT   /network/rit/lab/dailab_rit/adai/cesm1_2_1/$CASE
setenv BUILD_DIR     /network/rit/lab/dailab_rit/adai/cesm/BuildCase

## dir containing case specific codes: 
setenv FOOLROOT  /network/rit/lab/dailab_rit/adai/cesm/fixedIce/src_dai    
set idir = src.cice_fixedAiceFresh        # dir containing modified codes for ice model

#----------------------------------------------------
##  set if ice modifications are needed: 
#----------------------------------------------------
set newIce = 0   # =0 --> no change to ice mode, =1 --> use codes in $idir

#----------------------------------------------------------------------------------------------------
# for using your own ice_in (newIce=1 case), need to make the following change before submit the job:
#Near the end of Buildconf/cice.buildnml.csh :

# Copy resolved namelist to $CASEROOT/CaseDocs and $RUNDIR : change to use ice_in in casedir:
#if (-d ${RUNDIR}) then
##  cp $CASEBUILD/ciceconf/ice_in ${RUNDIR}/$ice_in_filename || exit -2                 
### change to this line: 
#   cp $CASEROOT/ice_in ${RUNDIR}/$ice_in_filename || exit -2
#endif 
#----------------------------------------------------------------------------------------------------


########################################################################
#----------------------------------------------------------------------
### No changes are needed below this line 
#----------------------------------------------------------------------
########################################################################

setenv CO2RATE 'scenario_ghg=&apos;RAMP_CO2_ONLY&apos;ramp_co2_annual_rate='${ramp_co2_annual_rate}

#------------------
## create new case
#------------------
if ( -d $CASEROOT ) then
 echo Remove $CASEROOT and submit this script again!
 exit
endif
#rm -rf $CASEROOT   # be careful with this removal of $CASEROOT

cd  $PTMP
$CCSMROOT/scripts/create_newcase  -case $CASE -mach $MACH -res $RES -compset $COMPSET -compiler intel

cd $FOOLROOT
cp -f -p src.cam/*.* $CASEROOT/SourceMods/src.cam/              # this is to fix a bug with mixing ratio

if ( $newIce == 1 ) then 
 cp -f -p $idir/*.F90 $CASEROOT/SourceMods/src.cice/
 ls -f $CASEROOT/SourceMods/src.cice/
endif


cd $CASEROOT

#-----------------------------
## changing  runtime options
#-----------------------------
 ./xmlchange -file env_run.xml   -id RUNDIR        -val $RUNDIR
 ./xmlchange -file env_run.xml   -id EXEROOT       -val $EXEROOT
 ./xmlchange  -file env_run.xml -id  DIN_LOC_ROOT  -val $DIN_LOC_ROOT
 ./xmlchange  -file env_run.xml -id  DOUT_S_ROOT   -val $DOUT_S_ROOT
 ./xmlchange  -file env_run.xml -id  RUN_TYPE      -val 'hybrid'
# ./xmlchange  -file env_run.xml -id  CONTINUE_RUN  -val 'FALSE'
 ./xmlchange  -file env_run.xml -id  CONTINUE_RUN  -val $CONTINUE_RUN
 ./xmlchange  -file env_run.xml -id  RESUBMIT -val $RESUBMIT

 ./xmlchange -file env_run.xml   -id RUN_STARTDATE -val '0001-01-01'
 ./xmlchange -file env_run.xml   -id RUN_REFDATE   -val '0501-01-01'

 ./xmlchange  -file env_run.xml -id  STOP_N        -val '60'
 ./xmlchange  -file env_run.xml -id  STOP_OPTION   -val 'nmonths'

 ./xmlchange  -file env_run.xml -id  REST_OPTION   -val 'nmonths'
 ./xmlchange  -file env_run.xml -id  REST_N        -val '60'

## skip this line if it's a fixed CO2 run: don't skip it!
#  if ( $CO2 == 1 ) then 
  ./xmlchange  -file env_run.xml -id CAM_NAMELIST_OPTS  -val $CO2RATE
#  endif

#-----------------------
# changing the PE layout: this is for 4 nodes on Snow:
#-----------------------             
if ( $node == 1) goto setup   # skip this and keep the default settings for 1 node testing

 ./xmlchange -file env_mach_pes.xml -id NTASKS_ATM -val '128'
 ./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 '128'
 ./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 '128'
 ./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 '128'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_OCN -val '1' 
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_OCN -val '0' 

 ./xmlchange -file env_mach_pes.xml -id NTASKS_GLC -val '128'
 ./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 '128'
 ./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 '128'
 ./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 '128'
 ./xmlchange -file env_mach_pes.xml -id NTHRDS_WAV -val '1' 
 ./xmlchange -file env_mach_pes.xml -id ROOTPE_WAV -val '0' 

 ./xmlchange -file env_build.xml -id EXEROOT   -val $EXEROOT



#------------------
## building
#------------------
setup:
cd $CASEROOT
 ./cesm_setup

#cd $FOOLROOT
cd $BUILD_DIR
if ($CO2 == -1 ) then 
 cp -f user_nl_cam_2XCO2 $CASEROOT/user_nl_cam
else if ($CO2 == -2 ) then 
 cp -f user_nl_cam_4XCO2 $CASEROOT/user_nl_cam
else
 cp -f user_nl_cam $CASEROOT/
endif

#------------------
## building
#------------------
build:
cd $CASEROOT
 ./$CASE.build

#------------------
## copy new ice_in: still need to make the change in Buildconf/cice.buildnml.csh
## only for newIce=1 case: 
#------------------
runice:
if ( $newIce == 1) then 
 set icein = $FOOLROOT/$idir/ice_in
else   # for control run: this is not done before 10/25/2020 and the default list was used
 set icein = $BUILD_DIR/ice_in.piControl_fixIce_outputList
endif

 rm -f $CASEROOT/CaseDocs/ice_in
 cp -f $icein $CASEROOT/CaseDocs/
 cp -f $icein $RUNDIR/
 cp -f $icein $CASEROOT
#endif

echo "Change co2_rate in user_nl_cam to avoid confusion (it does not matter)."
echo "Need to make the following change near the end of Buildconf/cice.buildnml.csh" 
echo "only for newIce=1 cases"    
echo #"if (-d ${RUNDIR}) then "
echo #"  # cp $CASEBUILD/ciceconf/ice_in ${RUNDIR}/$ice_in_filename || exit -2   # skip this line "
echo #"    cp $CASEROOT/ice_in ${RUNDIR}/$ice_in_filename || exit -2    # use this  "
echo ##endif

exit
