Index: BOL/LMDZ_Setup_amaury/main.sh
===================================================================
--- BOL/LMDZ_Setup_amaury/main.sh	(revision 4939)
+++ BOL/LMDZ_Setup_amaury/main.sh	(revision 4940)
@@ -41,6 +41,6 @@
 # TODO move to expert-level settings
 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 : 
+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
@@ -54,4 +54,5 @@
 # Using XIOS for IOs: "-xios" for enabled, else ""
 xios=""
+# TODO Amaury: je pensais que l'idée de main.sh c'était de mettre des paramètres qu'on peut changer en recompilant uniquement lmdz. Or ce n'est pas le cas d'XIOS...
 
 # Using or not the Cosp simulator: "-cosp" for enabled, else ""
Index: BOL/LMDZ_Setup_amaury/setup.sh
===================================================================
--- BOL/LMDZ_Setup_amaury/setup.sh	(revision 4939)
+++ BOL/LMDZ_Setup_amaury/setup.sh	(revision 4940)
@@ -74,12 +74,11 @@
 function load_install_lib {
   # Fetch and source install_lmdz.sh
-  if [[ ! -f "lib_install_lmdz.sh" ]]; then
+  if [[ ! -f "install_lmdz.sh" ]]; then
     wget "http://svn.lmd.jussieu.fr/LMDZ/BOL/script_install_amaury/install_lmdz.sh" # TODO change to normal branch once ready
-    mv install_lmdz.sh lib_install_lmdz.sh  # rename to avoid misleading users as to which script to run
-    sed -i'' "2i# This is a library used in the installation, DO NOT RUN IT YOURSELF unless you're an expert !" lib_install_lmdz.sh
   fi
-  source lib_install_lmdz.sh
+  # shellcheck disable=SC1090
+  source <(sed 's/function \(.*\) {/function installlmdz_\1 {/g' install_lmdz.sh)  # source with a namespace for functions
+  function myget { installlmdz_myget "$@"; }
 }
-load_install_lib  # Do not move later, as it would overwrite some functions
 
 function set_default_params {
@@ -149,7 +148,4 @@
   done
 
-  # Isotopes : Compile and run with isotopes if lmdz_phys="lmdiso" in main.sh
-  if [[ $phylmd = "lmdiso" ]]; then isotopes="y"; else isotopes="n"; fi
-
   # Initialisation
   if [[ $init = 1 || $init = 0 ]]; then
@@ -162,6 +158,4 @@
   if [[ $freq = yr ]]; then stopsim=$(echo "$mthend" | cut -c-4); else stopsim=$mthend; fi
 
-  if [[ $svn = "" ]]; then svnopt=""; else svnopt="-r $svn"; fi
-
   if [[ -d $SIM ]]; then
      echo "La simulation $SIM existe deja. Il est conseillé d'arrêter et de vérifier."
@@ -170,13 +164,8 @@
      if [[ $ans != y ]]; then exit 1; fi
   fi
-
-  if [[ $cosp = y ]]; then ins_cosp="-cosp v1"; else ins_cosp=""; fi
-
-  if [[ $xios = y ]]; then ins_xios="-xios"; else ins_xios=""; fi
 }
 
 function ensure_correct_option_combinations {
   # AVOID COMBINATIONS OF OPTIONS THAT DON'T WORK in user choices
-
   if [[ $ok_guide = y && $climato = 1 ]]; then
      echo "Running nudged simulations with climatological SSTs is not planned. Change <climato> to <0> or modify the setup (experts)"; exit 1
@@ -187,157 +176,65 @@
      echo "Change <climato> to <1> or <freq> to <mo> or modify setup.sh (experts)"; exit 1
   fi
-
-  # STOP if trying to use both ORCHIDEE and Isotopes :
-  if [[ $isotopes = "y" && $veget != "NONE" ]]; then
-    echo "STOP: You cannot run LMDZ with ORCHIDEE and ISOtopes at the same time; either change <veget> option to NONE, or isotopes to <n> in setup.sh"; exit 1
-  fi
-
-  # STOP if trying to use both SPLA and Isotopes :
-  if [[ $isotopes = "y" && $aerosols = "spla" ]]; then
-    echo "STOP: You cannot run LMDZ with Isotopes and aerosols=spla at the same time; change <aerosols> option to <n> or <clim> in setup.sh"; exit 1
-  fi
-
-  # (Temporary) STOP if trying to use Isotopes with XIOS :
-  # TODO Amaury: check if still relevant
-  if [[ $isotopes = "y" && $xios = "y" ]]; then
-    echo "STOP: Isotopes cannont yet be run with XIOS; change <xios> option in main.sh"; exit 1
-  fi
-
-  # (Temporary) Constraints for aerosols=spla :
-  # --> resolution 128x88x79 and rad=rrtm
-  if [[ $aerosols = spla && $resol != 128x88x79 ]]; then
-    echo 'For now, <aerosols=spla> requires <resol=128x88x79>, and uses the zoomed grid from gcm.def_zNAfrica_BiJe, for which forcing & initial files are available'
-    echo "Right now aerosols=<$aerosols>, resol=<$resol>"
-    exit 1
-  fi
-
-  if [[ $aerosols = "spla" && $rad != "rrtm" ]]; then
-    echo "STOP: For the time being, <aerosols=spla> requires <rad=rrtm>"; exit 1
+}
+
+function install_model {
+  mkdir -p "$LMDZD"
+
+  version_name=LMDZ$(echo "$version" | sed -e 's/-v//g' -e 's/-unstable//' -e 's/-r/r/' -e 's/ //g')
+  LMDZname="${version_name}${svn}OR$veget${ins_xios}"
+  MODEL="$LMDZD/$LMDZname/modipsl/modeles/LMDZ"
+
+  if [[ $install = 1 ]]; then  # run install_lmdz to pull the required files and compile once
+    if [[ -d $MODEL ]]; then
+      echo "MODEL=$MODEL"
+      echo "STOP: Vous essayez d'installer une version déja présente (option <-install>)"; exit 1
+    else
+      echo "Installing model"
+      cd "$LMDZD"
+      cp "$local/install_lmdz.sh" .
+      chmod +x install_lmdz.sh
+      # TODO handle arch/env here (used to be -env-file)
+      echo "./install_lmdz.sh $fcm $optim -v $version $svnopt -d $resol -rad $rad -bench 0 -parallel mpi_omp $ins_cosp $ins_xios -name $LMDZname -veget $veget -netcdf 0 -arch $ARCH" >> install_lmdz_options.$$.sh
+      chmod +x install_lmdz_options.$$.sh
+      echo "Running install_lmdz_options.$$.sh"
+      ./install_lmdz_options.$$.sh
+      mv install_lmdz.sh install_lmdz.$$.sh
+      cd -
+     fi
+  else
+     if [[ ! -d $MODEL ]]; then
+        echo "Le model $MODEL n'est pas installé. Vous pouvez relancez avec <-install>."; exit 1
+     fi
   fi
 }
 
-# TODO Amaury: commented-out for now
-## Set up the appropriate environment
-##------------------------------------
-#sed -i -e "s/hostname=.*.$/hostname=$(hostname)/" lmdz_env.sh
-#. lmdz_env.sh
-#module list
+# Set up the appropriate environment
+source lmdz_env.sh
 
 # TODO Amaury: commented-out for now
 ## Pour les post-traitements
-#sed -i'' -e "s/groupe@cpu/$groupe@cpu/" seasonal.sh  # TODO Amaury: $groupe jamais défini, à virer ?
+#sed -i'' -e "s/groupe@cpu/$groupe@cpu/" seasonal.sh
 local=$(pwd)
 
+load_install_lib
 define_expert_options
 set_default_params
 read_cmdline_args "$@"
 ensure_correct_option_combinations
+install_model
 
 exit 1
 
 
-
-
-
-
-
-########################################################################
-# Recuperation ou compilation du GCM
-########################################################################
-
-if [ ! -d $LMDZD ]; then mkdir -p $LMDZD; fi
-version_name=LMDZ`echo $version | sed -e 's/-v//g' -e 's/-unstable//' -e 's/-r/r/' -e 's/ //g'`
-LMDZname=${version_name}${svn}${optim}OR${veget}${ins_xios}
-MODEL=$LMDZD/$LMDZname/modipsl/modeles/LMDZ
-
-if [ $install = 1 ]; then
-   if [ -d $MODEL ]; then
-	   echo $MODEL
-      echo "Vous essayez d installer une version deja presente"
-      exit
-   else
-      echo On installe le modele
-      cd $LMDZD
-      echo wget https://lmdz.lmd.jussieu.fr/pub/install_lmdz.sh
-      \rm -f install_lmdz.sh
-      wget --no-cache https://lmdz.lmd.jussieu.fr/pub/install_lmdz.sh
-      # Si la ligne ci-dessus "wget https://lmdz.lmd..." ne marche pas pour "certificat expired", utiliser :  
-      #wget https://web.lmd.jussieu.fr/~lmdz/pub/install_lmdz.sh
-      chmod +x install_lmdz.sh
-      echo "./install_lmdz.sh $fcm $optim -v $version $svnopt -d $resol -rad $rad -bench 0 -parallel mpi_omp $ins_cosp $ins_xios -name $LMDZname -env_file $local/lmdz_env.sh -veget $veget" >> $local/install_lmdz_options.$$
-      ./install_lmdz.sh $fcm $optim -v $version $svnopt -d $resol -rad $rad -bench 0 -parallel mpi_omp $ins_cosp $ins_xios -name $LMDZname -env_file $local/lmdz_env.sh -veget $veget
-      mv install_lmdz.sh install_lmdz.$$
-      if grep "Not Found" $LMDZname/get.log >/dev/null; then echo "version=$version is NOT A TESTING VERSION, Ask LMDZ Team"; exit; fi
-      cd $local
-   fi
-else
-   if [ ! -d $MODEL ]; then
-      echo Le model $MODEL n est pas installe
-      echo relancer avec install=1 si vous voulez l installer automatiquement
-      exit
-   fi
-fi
-
-# Determine svn revision number, which will determine compilation options, executable names etc
-# If svn not available, will use the svn writen in $MODEL/Readm*md
-# For old version it assumes that it is before 4185 (the version
-# for which the test was introduced
-mysvn=`svnversion $MODEL | egrep -o "[0-9]+" 2>/dev/null`  
-if [ "$mysvn" = "" ]; then mysvn=`grep 'Revision: [0-9]' $LMDZD/$LMDZname/Read*.md | awk ' { print $2 } ' 2>/dev/null`; fi
-if [ "$mysvn" = "" ]; then mysvn=4190; fi
-echo mysvn $mysvn
-
-# For mysvn >= 4532, "module load netcdf95" must be added in lmdz_env.sh for Jean-Zay if it's not there;
-# lmdz_env.sh must be sourced again, and copied again as $MODEL/arch/arch-X64_JEANZAY.env
-# (first copy is done by install_lmdz.sh launched with option "-env lmdz_env.sh" )
-if [ ${hostname:0:5} = "jean-" -a $mysvn -ge 4532 -a ! "`grep netcdf95 lmdz_env.sh`" ]; then
-   cp -p lmdz_env.sh lmdz_env.orig
-   sed -i'' -e 's/module load nco/module load netcdf95\n                module load nco/' lmdz_env.sh
-   cp -p lmdz_env.sh $MODEL/arch/arch-X64_JEANZAY.env
-   . lmdz_env.sh
-fi
+# TODO check w/ Adriana the exact reasons why we recompile the model (or even compile it using install_lmdz in the first place)
 
 # COMPILATION OPTIONS depending on the user choices and LMDZ revision number $mysvn
 #-------------------------------------------------------
-if [ "$ARCH" = "" ]; then arch=; else arch="-arch $ARCH"; fi
-
-if [ $cosp = y ]; then opt_cosp="-cosp true"; else opt_cosp=""; fi
-   # makelmdz* uses "opt_cosp", and for v1 (default) needs "-cosp true/false"  
-   # When cosp v2 will be validated, we'll have opt_cosp=ins_cosp="-cosp v2" 
-   # (option cosp2 in makelmdz* to be ignored; it is a transitory version from v1 to v2)
-
-if [ $xios = y ]; then opt_xios="-io xios"; else opt_xios=""; fi
-
-case $rad in
-    oldrad) iflag_rrtm=0; NSW=2; opt_rad="";;
-    rrtm)   iflag_rrtm=1; NSW=6; if [ $mysvn -le 4185 ]; then opt_rad="-rrtm true"; else opt_rad="-rad rrtm"; fi;;
-    ecrad)  iflag_rrtm=2; NSW=6; opt_rad="-rad ecrad";;
-    *) echo Only oldrad rrtm ecrad for rad option; exit 
-esac
+arch="-arch $ARCH"
 if [ "$rad" = "ecrad" -a ! -d ecrad_data ]; then
     cd $local; wget https://lmdz.lmd.jussieu.fr/pub/3DInputData/ecrad/ecrad_data.tar; tar xvf ecrad_data.tar; cd -
 fi
-if [ "$rad" = "ecrad" -a "$aerosols" != "n" -a "$mysvn" -lt 4489 ] ; then echo Les aerosols tropospheriques ne sont pas pris en charge par ecrad avant LMDZ rev 4489, ici rev est $mysvn; exit; fi
-
-
-# Aerosols : also define the associated suffix in gcm name
-if [ $aerosols = spla ]; then
-  opt_aer=' -dust true ';  sufaer='_spla'
-else
-  opt_aer=""            ;  sufaer=''
-fi
-
-if [ $inlandsis = y ]; then
-   opt_inlandsis="-inlandsis true"
-else
-   opt_inlandsis=""
-fi
-
-# Isotopes : also define the associated suffix in gcm name
-if [ $isotopes = y ]; then
-  opt_isotopes="-isotopes true"; sufiso="_iso"
-else
-  opt_isotopes=""              ; sufiso=""
-fi
+
+if [ "$rad" = "ecrad" -a "$aerosols" != "n" -a "$mysvn" -lt 4489 ] ; then echo "Les aerosols tropospheriques ne sont pas pris en charge par ecrad avant LMDZ rev 4489, ici rev est $mysvn"; exit 1; fi
 
 # Source the surface_env file created by install_lmdz.sh, to get the "veget"-related variables
@@ -380,6 +277,4 @@
 compile_opt="$optim -p $phylmd $opt_cosp $opt_xios $opt_rad $arch -d $resol $opt_veget $opt_aer $opt_inlandsis -mem -parallel mpi_omp"
 
-if [ $mysvn -le 4185 ]; then suffix=_${resol}_phy${phylmd}_para_mem${suforch}${sufaer};
-                         else suffix=_${resol}_phy${phylmd}_${rad}_para_mem${suforch}${sufaer}; fi
 
 # use an intermediate variable to use either suffix or suffix_iso
@@ -395,12 +290,6 @@
    sleep 10
 
-   if [ "$fcm" = "-nofcm" ]; then
-       compile="./makelmdz $compile_opt \$1; mkdir -p bin; \mv -f \${prog}.e $gcm"
-       compile_iso="./makelmdz $compile_opt_iso \$1; mkdir -p bin; \mv -f \${prog}.e $gcm"
-   else
-      # makelmdz_fcm directly produces executable named $gcm, depending on the compilation options
-       compile="./makelmdz_fcm $compile_opt -j 2 \$1"
-       compile_iso="./makelmdz_fcm $compile_opt_iso -j 2 \$1"
-   fi
+   compile="./makelmdz_fcm $compile_opt -j 2 \$1"
+   compile_iso="./makelmdz_fcm $compile_opt_iso -j 2 \$1"
 
    #NB on est dans $local, qui est $STORE/$MAINDIR
@@ -436,4 +325,9 @@
    cd $local
 fi
+
+# TODO ^ qu'est-ce qui nous empêche de mettre tout ça directement dans install_lmdz ????
+# TODO Ce qu'il faudrait faire:
+# TODO 1) bien expliquer ce que fait l'option install (lancer install_lmdz une unique fois pour lancer tout sauf la compil LMDZ => maintenant ça ne fonctionne plus car on installe tjrs lmdz, mais en fait c'est pas grave car derrière on vérifie si le gcm.e existe ou non)
+# TODO 2) importer les fonctions de install_lmdz nécessaires pour "recompiler" lmdz "à la carte" (ou bien juste modifier compile.sh ??)
 
 
