#!/bin/csh

# Here we use the Regional Arctic System Model (RASM) for all our simulations (https://nps.edu/web/rasm)
# We have only active land and atmosphere components. Sea ice, ocean, and land ice models are using specified (data model) configurations
# It is using the CESM2-LE MOAR simulation #2 simulation for 1 October, 1991 as our initial and boundary conditions
# See https://www.cesm.ucar.edu/community-projects/lens2 for more details

# RASM uses CTSM as the land model - https://escomp.github.io/ctsm-docs/versions/master/html/users_guide/overview/introduction.html
# RASM uses WRFV3.7.1 for the atmosphere - https://www2.mmm.ucar.edu/wrf/users/docs/user_guide_V3/user_guide_V3.7/ARWUsersGuideV3.7.pdf
# sea ice and sea surface temperatures are specified from the CESM2-LE simulation
# General RASM references
# https://nps.edu/web/rasm
# https://nps.edu/web/rasm/publications
# Example papers describing RASM - https://journals.ametsoc.org/view/journals/clim/30/15/jcli-d-15-0775.1.xml, https://journals.ametsoc.org/view/journals/clim/29/18/jcli-d-15-0415.1.xml

# All model codes are in the public domain 
# CESM/CTSM https://www.cesm.ucar.edu/models/cesm2/copyright
# WRF https://github.com/wrf-model/WRF/blob/master/LICENSE.txt
# components not used in this example
# RVIC river routing model, GNU GPL v3.0, https://rvic.readthedocs.io/en/latest/
# POP ocean model, public domain - https://www.cesm.ucar.edu/models/cesm2/copyright, https://ncar.github.io/POP/doc/build/html/index.html, https://www.cesm.ucar.edu/models/pop 
# CICE sea ice model, https://github.com/CICE-Consortium/CICE-svn-trunk/blob/main/LICENSE.pdf, 
# CPL7, coupler for RASM, https://www2.cesm.ucar.edu/models/cesm1.0/cpl7/cpl7_doc/c19.html

# this example Readme assumes the user is building the code on the NCAR Derecho system
# at the time of the runs, the environment was set as:
# module load ncarenv/23.06
# module load craype
# module load intel/2023.0.0
# module load ncarcompilers
# module load cray-mpich/8.1.25
# module load netcdf-mpi/4.9.2
# module load parallel-netcdf/1.12.3
# module load cray-libsci/23.02.1.1

# These specific modules may not be available at the current time.  Please use versions of all libraries that are compatiable with each other


#need to change this to appropriate path where you are running the sample simulation
set workdir = /glade/work/tcraig/NNA
set inputdir = ${WORKDIR}

cd ${workdir}
tar -xvf rasm_nna.derecho_sample.tar
cd ${inputdir}
tar -xvf inputdata.NNA.4km.sample.tar

set sandbox = ${workdir}/rasm_nna.derecho_sample
set inputdata = ${inputdir}/inputdata.NNA.4km.sample

cd ${sandbox}/models/lnd/ctsm/bld_rasm/namelist_files
cp lnd_in.nna04a lnd_in.nna04a.orig
sed -i "s|/glade/p/ral/hap/common_data/rasm/inputdata|${inputdata}|g" lnd_in.nna04a
cp ctsm.input_data_list.nna04a ctsm.input_data_list.nna04a.orig
sed -i "s|/glade/p/ral/hap/common_data/rasm/inputdata|${inputdata}|g" ctsm.input_data_list.nna04a

cd ${sandbox}/scripts

set casename = NNA.4km.test
./create_newcase -case ${casename} -res n04a_n04a -compset NC2LE2h -mach derecho_intel

set rundir = "/glade/derecho/scratch/${user}/${casename}/run"

cd ${casename}
./xmlchange RUN_TYPE=branch
./xmlchange RUN_REFCASE=NNA.4km.hC2LE2.1989.006
./xmlchange RUN_REFDATE=1991-10-01
./xmlchange RUN_STARTDATE=1991-10-01
./xmlchange STOP_OPTION=ndays
./xmlchange STOP_N=2
./xmlchange DIN_LOC_ROOT=${inputdata}
./cesm_setup

echo "stream_fldfilename = '${inputdata}/ocn/cesm2le/CESM2_LE_SST_CI.daily.1991.1031.002.230420.nc'" >> user_nl_cice

cp CaseDocs/docn.streams.txt.cesm2le2h user_docn.streams.txt.cesm2le2h
chmod 644 user_*
# edit user_docn.streams.txt.cesm2le2h, delete all lines in the "fileNames" section except the "1991" file.

./*.build

cp -p ${inputdata}/NNA.4km.hC2LE2.1989.006/rest/1991-10-01-00000/*  ${rundir}/

# review PBS settings in ${casename}.run, change account that provides your compute allocation for instance
./*.submit

