#!/bin/bash

#####################################################################
# This script manages the call to setup.sh
#
# Authors :
# F. Hourdin, frederic.hourdin@lmd.ipsl.fr
# Modified A. Sima, adriana.sima@lmd.ipsl.fr
#
# Some options can only be changed directly in lmdz_env.sh or setup.sh
# --> In setup.sh :
# * Orchidee version, to be defined through "veget" option
#   default : OR-CMIP6
# * Aerosol forcing, to be defined through "aerosols" option :
#   n (no, default) / clim (an2000)/ spla (interactive dust and sea salt)
#   !!! STRONG recommendation : experiments with DIFFERENT Orchidee or aerosol options
#   !!!   should be performed in DIFFERENT TEST_PROD folders 
#   !!!  (especially as they may need different initial files) 
#  * Use or not of the RRTM radiation code : rrtm=true/false
#  * Compiling options : debug, use or not of the fcm makefile
#
# --> In lmdz_env.sh :
# * Model & configuration setup
#    If you install the present tutorial_prod package in $STORE/your_folder instead of $STORE
#       modify STORED variable accordingly
#    The model will be installed in $LMDZD directory defined in lmdz_env.sh
#      Default : LMDZD=$WORK ; can be changed in LMDZD=$WORK/Your_directory
#    To use a model already installed (and compiled), by you or someone else (check the acces!):
#       set LMDZD=path_to_model_to_use
#       You'll also need to (re)define "LMDZname" accordingly, in setup.sh 
#####################################################################

#===========================================================
# 1. Model setup
#===========================================================

# Version of the tar file on https://lmdz.lmd.jussieu.fr/pub/src
# Last "testing" version, thoroughly checked by the LMDZ team : contains LMDZ rev 4729 (2023-10-22)
version="20240308.trunk" #!! DON'T CHANGE IT WITHOUT CHECKING WITH LMDZ TEAM !!
svn="" 			 #!! DON'T CHANGE IT WITHOUT CHECKING WITH LMDZ TEAM !!	
	# CONTACT LMDZ Team : 
	# 	email: poihl@listes.lmd.ipsl.fr
	# 	Mattermost: https://mattermost.lmd.ipsl.fr/lmdz/channels/installation-et-tutoriels

#-----------------------------------------------------------
# Grid number of points IMxJMxLM
#-----------------------------------------------------------
resol=144x142x79

#-----------------------------------------------------------
# To install the model :  -install 
#-----------------------------------------------------------
install=""
install=-install

#-----------------------------------------------------------
# Using XIOS for IOs
#-----------------------------------------------------------
xios="-xios"
xios=""

#-----------------------------------------------------------
# Using or not the Cosp simulator  
#-----------------------------------------------------------
#cosp="-cosp"
cosp=""
echo $xios $cosp

#-----------------------------------------------------------
# Choice of physics (default : "lmd" for "phylmd" )
#   Examples : lmd_phys="new" to compile with phynew instead of phylmd,
#              lmd_phys="lmdiso" to run with Isotopes 
#-----------------------------------------------------------
lmd_phys="lmd"

#===========================================================
# 2. Simulation setup
#===========================================================

#-----------------------------------------------------------
# Initial state and Boundary conditions
#-----------------------------------------------------------

# init=   1: to create a new start in INT
#         0: to read start files in INIT
#       SIM: to read start files from previous simulation SIM0
# limit.nc and aerosols forcing are put in ./LIMIT
init=1
#init=SIM0 # SIM0 must be in the same folder as the new SIM

#-----------------------------------------------------------
# climato=1 : Climatological SSTs with 360-day calendar 
#        =0 : interannual SSTs with true (ie gregorian) calendar 
#-----------------------------------------------------------
climato=1

#-----------------------------------------------------------
# Nudging: Can only be activated with climato=0 and freq=mo
#-----------------------------------------------------------
nudging="-nudging"
nudging=""
#If using nudging, then check DEF/guide.def :
#  - ok_guide will be automatically set to "y" by setup.sh
#  - You may want to check/customize the nudging parameters

#-----------------------------------------------------------
# Length of elementary simulations yr (year) or mo (month)
#-----------------------------------------------------------
freq=yr

#-----------------------------------------------------------
# Initial/final month for simulation
#-----------------------------------------------------------
# If $init=1, the INIT file will be called start.200001.nc,
# but the data correspond in fact to another day.
# NB : the run stops in the BEGINNING of mthend (test "next=stopsim")
mthini=200001
mthend=200501

#-----------------------------------------------------------
# Output files, frequencies, levels 
#   If you use IOIPSL (option xios=""), you may want to choose and customize DEF/config.def. 
#     A few versions are available as DEF/config.def_* ; config.def is a copy of config.def_default 
#     See phys_out_filekeys, phys_out_filelevels, phys_out_filetimesteps, and supplementary variables
#   If you use XIOS (option xios="-xios"), check/modify DEF/XMLfiles*/file*xml 
#   In both cases, the default output is "histday".
#-----------------------------------------------------------
# Grid characteristics (regular, zoomed)
#   You may want to choose and customize DEF/gcm.def 
#   A few versions are available as DEF/gcm.def_* ; 
#      The default, for regular grid 144x142x79, corresponds to _iperiod7 
#      (Expert : TEMPORARILY : setup.sh forces use of gcm.def_zNAfrica_BiJe for aerosols=spla)

#===========================================================
# 3. Example of calling setup.sh in loop
#===========================================================

# By default, the series is done on one or more of the various versions of physiq.def 
# to be chosen among DEF/PHYS/physiq.def_* , and listed in "physics" :
# NOTE : automatically choosing DEF/PHYS/physiq.def_NPiso for isotopes
physics="NPv6.3"
if [ $lmd_phys = "lmdiso" ] ; then physics="NPiso" ; fi

for phys in $physics ; do
  deffile=DEF/PHYS/physiq.def_${phys}
  if [ -f $deffile ] ; then 
     cp -p $deffile DEF/physiq.def

    # name of simulation : can be changed to user's convenience
    # (Default : name=$phys to loop on different physics) 
    name=${phys}      

    # launching setup.sh with the options defined in this main.sh. 
    # Remember : some other options are only available in lmdz_env.sh and setup.sh.
    ./setup.sh -v "$version" $svn -d $resol $install $cosp $xios -init $init -climato $climato $nudging -f $freq -mthini $mthini -mthend $mthend -p $lmd_phys -name $name

    nphys=`echo "$physics" |wc -w` ; if [ $nphys -gt 1 -a $init = 1 ] ; then echo "!=!=! TO LOOP OVER MULTIPLE "physics", YOU NEED init /= 1 AND start* + limit* files ALREADY AVAILABLE IN THIS FOLDER !=!=!" ; exit ; fi

else  # wrong "{phys}" name in "physics" list 
 echo File $deffile inexistent
 exit
fi

done

