From 0b8e5792af590afa5c3d5179ab1597e08e3ab4bf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Riette?= <sebastien.riette@meteo.fr>
Date: Tue, 26 Sep 2023 17:15:52 +0200
Subject: [PATCH] S. Riette Sept 23: add --computeRefIfNeeded and
 --onlyIfNeeded switches to check_commit_ial

--onlyIfNeeded allows to call check_commit_ial and compuation (pack creation,
               compilation, execution) are performed only if needed

--computeRefIfNeeded with this switch, if the reference simulation does not
                     exist, this reference simulation is built before
                     performing the comparison
---
 tools/check_commit_ial.sh | 472 ++++++++++++++++++++------------------
 tools/testing.sh          |   5 +-
 2 files changed, 255 insertions(+), 222 deletions(-)

diff --git a/tools/check_commit_ial.sh b/tools/check_commit_ial.sh
index 98958bdac..da9ef8911 100755
--- a/tools/check_commit_ial.sh
+++ b/tools/check_commit_ial.sh
@@ -115,7 +115,7 @@ mainPackVersion=${mainPackVersion:-${defaultMainPackVersion}}
 ################################
 
 function usage {
-  echo "Usage: $0 [-h] [-p] [-c] [-r] [-C] [-s] [-f] [--noexpand] [-t test] [--cycle CYCLE] [--repo-user] [--repo-protocol] [--remove] commit [reference]"
+  echo "Usage: $0 [-h] [-p] [-c] [-r] [-C] [-s] [-f] [--noexpand] [-t test] [--cycle CYCLE] [--scripttag TAG] [--repo-user USER] [--repo-protocol PROTOCOL] [--remove] [--onlyIfNeeded] [--computeRefIfNeeded] commit [reference]"
   echo "commit          commit hash (or a directory, or among $specialPack) to test"
   echo "reference       commit hash (or a directory, or among $specialPack) REF to use as a reference"
   echo "-s              suppress compilation pack"
@@ -129,11 +129,17 @@ function usage {
   echo "-f              full compilation (do not use pre-compiled pack)"
   echo "--cycle CYCLE   to force using CYCLE"
   echo "--scripttag TAG script tag to use in case --cycle is provided"
-  echo "--repo-user     user hosting the PHYEX repository on github,"
+  echo "--repo-user USER"
+  echo "                user hosting the PHYEX repository on github,"
   echo "                defaults to the env variable PHYEXREOuser (=$PHYEXREOuser)"
-  echo "--repo-protocol protocol (https or ssh) to reach the PHYEX repository on github,"
+  echo "--repo-protocol PROTOCOL"
+  echo "                protocol (https or ssh) to reach the PHYEX repository on github,"
   echo "                defaults to the env variable PHYEXREOprotocol (=$PHYEXREOprotocol)"
   echo "--remove        removes the pack"
+  echo "--onlyIfNeeded  performs the pack creation and/or the compilation and/or the execution"
+  echo "                only if the step has not already been done"
+  echo "--computeRefIfNeeded"
+  echo "                computes the missing references"
   echo ""
   echo "If nothing is asked (pack creation, compilation, running, check, removing) everything"
   echo "except the removing is done"
@@ -165,6 +171,8 @@ fullcompilation=0
 cycle=""
 scripttag=''
 remove=0
+onlyIfNeeded=0
+computeRefIfNeeded=0
 
 while [ -n "$1" ]; do
   case "$1" in
@@ -182,6 +190,9 @@ while [ -n "$1" ]; do
     '--repo-user') export PHYEXREPOuser=$2; shift;;
     '--repo-protocol') export PHYEXREPOprotocol=$2; shift;;
     '--remove') remove=1;;
+    '--onlyIfNeeded') onlyIfNeeded=1;;
+    '--computeRefIfNeeded') computeRefIfNeeded=1;;
+
     #--) shift; break ;;
      *) if [ -z "${commit-}" ]; then
           commit=$1
@@ -379,205 +390,207 @@ fi
 #######################
 
 if [ $packcreation -eq 1 ]; then
-  echo "### Compilation of commit $commit"
-
-  if echo $specialPack | grep -w $commit > /dev/null; then
-    echo "Special commit '$commit' cannot be compiled with this script"
-    exit 4
-  fi
-
   if [ -d $HOMEPACK/$name ]; then
-    echo "Pack already exists ($HOMEPACK/$name), suppress it to be able to compile it again (or use the -s option to automatically suppress it)"
-    exit 5
-  fi
+    if [ $onlyIfNeeded -eq 0 ]; then
+      echo "Pack already exists ($HOMEPACK/$name), suppress it to be able to compile it again (or use the -s option to automatically suppress it)"
+      exit 5
+    fi
+  else
+    echo "### Pack creation for commit $commit"
 
-  export GMKTMP=/dev/shm
+    if echo $specialPack | grep -w $commit > /dev/null; then
+      echo "Special commit '$commit' cannot be compiled with this script"
+      exit 4
+    fi
 
-  if [ $fullcompilation == 0 ]; then
-    basepack=${cycle}_main.01.${gmkpack_l}.${gmkpack_o}
-    #[ $HPC -eq 0 -a ! -d $ROOTPACK/$basepack ] &&  getpack $basepack
-    gmkpack -r ${cycle} -b phyex -v $mainPackVersion -l ${gmkpack_l} -o ${gmkpack_o} -p masterodb \
-            -f $dirpack/ \
-            -u $name
-    reftree='main'
-  else
-    if [ $(echo $cycle | cut -c 1-2) -ne 48 ]; then
-      hub='-K'
+    export GMKTMP=/dev/shm
+
+    if [ $fullcompilation == 0 ]; then
+      basepack=${cycle}_main.01.${gmkpack_l}.${gmkpack_o}
+      #[ $HPC -eq 0 -a ! -d $ROOTPACK/$basepack ] &&  getpack $basepack
+      gmkpack -r ${cycle} -b phyex -v $mainPackVersion -l ${gmkpack_l} -o ${gmkpack_o} -p masterodb \
+              -f $dirpack/ \
+              -u $name
+      reftree='main'
     else
-      hub=''
-    fi
-    #Create main pack
-    gmkpack -a $hub -r ${cycle} -b ${packBranch} -n 01 -l ${gmkpack_l} -o ${gmkpack_o} -p masterodb -h $HOMEPACK/PHYEX
-    #Populate hub
-    if [ -d $HOMEPACK/$name/hub ]; then
-      cd $HOMEPACK/$name/hub/local/src
+      if [ $(echo $cycle | cut -c 1-2) -ne 48 ]; then
+        hub='-K'
+      else
+        hub=''
+      fi
+      #Create main pack
+      gmkpack -a $hub -r ${cycle} -b ${packBranch} -n 01 -l ${gmkpack_l} -o ${gmkpack_o} -p masterodb -h $HOMEPACK/PHYEX
+      #Populate hub
+      if [ -d $HOMEPACK/$name/hub ]; then
+        cd $HOMEPACK/$name/hub/local/src
+        if [ $HPC -eq 1 ]; then
+          ssh sxphynh.cnrm.meteo.fr "wget http://anonymous:mto@webdav.cnrm.meteo.fr/public/algo/khatib/src/hub49.tgz -O -" > hub49.tgz
+        else
+          wget http://anonymous:mto@webdav.cnrm.meteo.fr/public/algo/khatib/src/hub49.tgz
+        fi
+        tar xf hub49.tgz
+        rm -f hub49.tgz
+      fi
+      #Populate
+      cd $HOMEPACK/$name/src/local/
       if [ $HPC -eq 1 ]; then
-        ssh sxphynh.cnrm.meteo.fr "wget http://anonymous:mto@webdav.cnrm.meteo.fr/public/algo/khatib/src/hub49.tgz -O -" > hub49.tgz
+        ssh sxphynh.cnrm.meteo.fr "wget http://anonymous:mto@webdav.cnrm.meteo.fr/public/algo/khatib/src/${cycle}_main.01.tgz -O -" > ${cycle}_main.01.tgz
       else
-        wget http://anonymous:mto@webdav.cnrm.meteo.fr/public/algo/khatib/src/hub49.tgz
+        wget http://anonymous:mto@webdav.cnrm.meteo.fr/public/algo/khatib/src/${cycle}_main.01.tgz
       fi
-      tar xf hub49.tgz
-      rm -f hub49.tgz
-    fi
-    #Populate
-    cd $HOMEPACK/$name/src/local/
-    if [ $HPC -eq 1 ]; then
-      ssh sxphynh.cnrm.meteo.fr "wget http://anonymous:mto@webdav.cnrm.meteo.fr/public/algo/khatib/src/${cycle}_main.01.tgz -O -" > ${cycle}_main.01.tgz
-    else
-      wget http://anonymous:mto@webdav.cnrm.meteo.fr/public/algo/khatib/src/${cycle}_main.01.tgz
-    fi
-    tar xf ${cycle}_main.01.tgz
-    rm -f ${cycle}_main.01.tgz
-    #Cleaning and moving
-    if [ "$cycle" == '48t3' -o "$cycle" == '49t0' ]; then
-      #extracting budgets from micro
-      mkdir mpa/budgets
-      for file in mpa/micro/module/moddb_intbudget.F90 mpa/micro/externals/aro_suintbudget_omp.F90 \
-                  mpa/micro/interface/aro_convbu.h mpa/micro/externals/aro_convbu.F90 \
-                  mpa/micro/interface/aro_startbu.h mpa/micro/externals/aro_startbu.F90 \
-                  mpa/micro/externals/aro_suintbudget.F90 mpa/micro/externals/aro_suintbudget_omp.F90 \
-                  mpa/micro/interface/aroini_budget.h mpa/micro/externals/aroini_budget.F90; do
-        [ -f $file ] && mv $file mpa/budgets/
-      done 
-      mkdir mpa/aux
-      for file in mpa/micro/interface/aroini_frommpa.h mpa/micro/externals/aroini_frommpa.F90 \
-                  mpa/micro/externals/modd_spp_type.F90 mpa/micro/externals/spp_mod_type.F90 \
-                  mpa/micro/interface/aroini_cstmnh.h mpa/micro/externals/aroini_cstmnh.F90; do
-        [ -f $file ] && mv $file mpa/aux/
+      tar xf ${cycle}_main.01.tgz
+      rm -f ${cycle}_main.01.tgz
+      #Cleaning and moving
+      if [ "$cycle" == '48t3' -o "$cycle" == '49t0' ]; then
+        #extracting budgets from micro
+        mkdir mpa/budgets
+        for file in mpa/micro/module/moddb_intbudget.F90 mpa/micro/externals/aro_suintbudget_omp.F90 \
+                    mpa/micro/interface/aro_convbu.h mpa/micro/externals/aro_convbu.F90 \
+                    mpa/micro/interface/aro_startbu.h mpa/micro/externals/aro_startbu.F90 \
+                    mpa/micro/externals/aro_suintbudget.F90 mpa/micro/externals/aro_suintbudget_omp.F90 \
+                    mpa/micro/interface/aroini_budget.h mpa/micro/externals/aroini_budget.F90; do
+          [ -f $file ] && mv $file mpa/budgets/
+        done 
+        mkdir mpa/aux
+        for file in mpa/micro/interface/aroini_frommpa.h mpa/micro/externals/aroini_frommpa.F90 \
+                    mpa/micro/externals/modd_spp_type.F90 mpa/micro/externals/spp_mod_type.F90 \
+                    mpa/micro/interface/aroini_cstmnh.h mpa/micro/externals/aroini_cstmnh.F90; do
+          [ -f $file ] && mv $file mpa/aux/
+        done
+        [ -f mpa/micro/externals/add_bounds.F90 ] && rm -f mpa/micro/externals/add_bounds.F90
+        [ -f mpa/micro/externals/aroini_wet_dep.F90 ] && mv mpa/micro/externals/aroini_wet_dep.F90 mpa/chem/externals/aroini_wet_dep.F90
+        [ -f mpa/micro/interface/aroini_wet_dep.h ] && mv mpa/micro/interface/aroini_wet_dep.h mpa/chem/interface/aroini_wet_dep.h
+      fi
+      if [ "$cycle" == '49t0' ]; then
+        rm -rf oopsifs
+      fi
+      #we keep everything from the official source code except internals and module subdirectories of mpa
+      #and except some files of mpa/conv/module
+      for file in modi_shallow_convection.F90 modi_shallow_convection_part1.F90 \
+                  modi_shallow_convection_part2.F90 modi_shallow_convection_part2_select.F90; do
+        if [ -f mpa/conv/module/$file ]; then
+          [ ! -d mpa/conv/module_save ] && mkdir mpa/conv/module_save
+          mv mpa/conv/module/$file mpa/conv/module_save/
+        fi
       done
-      [ -f mpa/micro/externals/add_bounds.F90 ] && rm -f mpa/micro/externals/add_bounds.F90
-      [ -f mpa/micro/externals/aroini_wet_dep.F90 ] && mv mpa/micro/externals/aroini_wet_dep.F90 mpa/chem/externals/aroini_wet_dep.F90
-      [ -f mpa/micro/interface/aroini_wet_dep.h ] && mv mpa/micro/interface/aroini_wet_dep.h mpa/chem/interface/aroini_wet_dep.h
-    fi
-    if [ "$cycle" == '49t0' ]; then
-      rm -rf oopsifs
-    fi
-    #we keep everything from the official source code except internals and module subdirectories of mpa
-    #and except some files of mpa/conv/module
-    for file in modi_shallow_convection.F90 modi_shallow_convection_part1.F90 \
-                modi_shallow_convection_part2.F90 modi_shallow_convection_part2_select.F90; do
-      if [ -f mpa/conv/module/$file ]; then
-        [ ! -d mpa/conv/module_save ] && mkdir mpa/conv/module_save
-        mv mpa/conv/module/$file mpa/conv/module_save/
+      for rep in turb micro conv; do
+        mkdir -p phyex/$rep
+        rm -rf mpa/$rep/internals mpa/$rep/module
+      done
+      [ -d mpa/conv/module_save ] && mv mpa/conv/module_save mpa/conv/module
+      if [ -f /cnrm/algo/khatib/drhook.c_for_ubuntu.tar -a $(echo $cycle | cut -c 1-2) -eq 48 ]; then
+        #If file exists it means that we are running on a CTI computer, so we are using ubuntu
+        tar xf /cnrm/algo/khatib/drhook.c_for_ubuntu.tar
       fi
-    done
-    for rep in turb micro conv; do
-      mkdir -p phyex/$rep
-      rm -rf mpa/$rep/internals mpa/$rep/module
-    done
-    [ -d mpa/conv/module_save ] && mv mpa/conv/module_save mpa/conv/module
-    if [ -f /cnrm/algo/khatib/drhook.c_for_ubuntu.tar -a $(echo $cycle | cut -c 1-2) -eq 48 ]; then
-      #If file exists it means that we are running on a CTI computer, so we are using ubuntu
-      tar xf /cnrm/algo/khatib/drhook.c_for_ubuntu.tar
-    fi
-    #Special modification of the compilation configuration file and script
-    sed -i 's/-ftree-vectorize//' $HOMEPACK/$name/.gmkfile/${gmkpack_l}.*
-    sed -i "/^MACROS_FRT/s/$/ -DREPRO48/" $HOMEPACK/$name/.gmkfile/${gmkpack_l}.*
-    #sed -i "s/PHYEX\/${cycle}_$$.01.${gmkpack_l}.${gmkpack_o}/$(echo $name | sed 's/\//\\\//')/" $HOMEPACK/$name/ics_masterodb #this line could be used if pack was renamed before compilation but it does not work on belenos
+      #Special modification of the compilation configuration file and script
+      sed -i 's/-ftree-vectorize//' $HOMEPACK/$name/.gmkfile/${gmkpack_l}.*
+      sed -i "/^MACROS_FRT/s/$/ -DREPRO48/" $HOMEPACK/$name/.gmkfile/${gmkpack_l}.*
+      #sed -i "s/PHYEX\/${cycle}_$$.01.${gmkpack_l}.${gmkpack_o}/$(echo $name | sed 's/\//\\\//')/" $HOMEPACK/$name/ics_masterodb #this line could be used if pack was renamed before compilation but it does not work on belenos
 
-    resetpack -f #Is it really useful?
-    reftree='local'
-  fi
-  cd $HOMEPACK/$name/src/local/phyex
+      resetpack -f #Is it really useful?
+      reftree='local'
+    fi
+    cd $HOMEPACK/$name/src/local/phyex
 
-  MNH_EXPAND_DIR=$PHYEXTOOLSDIR/mnh_expand
-  export PATH=$MNH_EXPAND_DIR/filepp:$MNH_EXPAND_DIR/MNH_Expand_Array:$PATH
+    MNH_EXPAND_DIR=$PHYEXTOOLSDIR/mnh_expand
+    export PATH=$MNH_EXPAND_DIR/filepp:$MNH_EXPAND_DIR/MNH_Expand_Array:$PATH
 
-  if [ $useexpand == 1 ]; then
-    expand_options="-D MNH_EXPAND -D MNH_EXPAND_LOOP"
-  else
-    expand_options=""
-  fi
-  subs="-s gmkpack_ignored_files -s turb -s micro -s aux -s ext -s conv -s externals" #externals is the old name for aux/ext
-  prep_code=$PHYEXTOOLSDIR/prep_code.sh
-  if [ "$fromdir" == '' ]; then
-    echo "Clone repository, and checkout commit $commit (using prep_code.sh)"
-    if [[ $commit == arome${separator}* ]]; then
-      $prep_code -c $commit PHYEX #This commit is ready for inclusion
+    if [ $useexpand == 1 ]; then
+      expand_options="-D MNH_EXPAND -D MNH_EXPAND_LOOP"
     else
-      $prep_code -c $commit $expand_options $subs -m arome PHYEX
-    fi
-  else
-    echo "Copy $fromdir"
-    mkdir PHYEX
-    scp -q -r $fromdir/src PHYEX/
-    $prep_code $expand_options $subs -m arome PHYEX
-  fi
-  find PHYEX -type f -exec touch {} \; #to be sure a recompilation occurs
-  for rep in turb micro conv aux; do
-    [ -d PHYEX/$rep ] && mv PHYEX/$rep .
-  done
-  #modd_nsv.F90 has been moved and gmkpack is lost in case a different version exists in main/.../micro
-  if [ -f ../../main/phyex/micro/modd_nsv.F90 -a -f aux/modd_nsv.F90 ]; then
-    mv aux/modd_nsv.F90 micro/
-    if [ -f PHYEX/gmkpack_ignored_files ]; then
-      grep -v micro/modd_nsv.F90 PHYEX/gmkpack_ignored_files > PHYEX/gmkpack_ignored_files_new
-      mv PHYEX/gmkpack_ignored_files_new PHYEX/gmkpack_ignored_files
+      expand_options=""
     fi
-  fi
-  if [ -f PHYEX/gmkpack_ignored_files ]; then
-    #gmkpack_ignored_files contains a list of file, present in the reference pack, that is not used anymore
-    #and must be excluded from compilation (in case of a full comilation) or from re-compilation (in case of a non-full
-    #compilation).
-    if [ $fullcompilation == 0 ]; then
-      #Content is added in the ics_masterodb script
-      sed -i "/^end_of_ignored_files/i $(first=1; for line in $(cat PHYEX/gmkpack_ignored_files); do echo -n $(test $first -ne 1 && echo \\n)${line}; first=0; done)" $HOMEPACK/$name/ics_masterodb
+    subs="-s gmkpack_ignored_files -s turb -s micro -s aux -s ext -s conv -s externals" #externals is the old name for aux/ext
+    prep_code=$PHYEXTOOLSDIR/prep_code.sh
+    if [ "$fromdir" == '' ]; then
+      echo "Clone repository, and checkout commit $commit (using prep_code.sh)"
+      if [[ $commit == arome${separator}* ]]; then
+        $prep_code -c $commit PHYEX #This commit is ready for inclusion
+      else
+        $prep_code -c $commit $expand_options $subs -m arome PHYEX
+      fi
     else
-      #Files must be suppressed (non phyex files)
-      for file in $(cat PHYEX/gmkpack_ignored_files); do
-        [ -f $HOMEPACK/$name/src/local/$file ] && rm -f $HOMEPACK/$name/src/local/$file
-      done
-      [ ! "$(ls -A $HOMEPACK/$name/src/local/mpa/dummy)" ] && rmdir $HOMEPACK/$name/src/local/mpa/dummy
+      echo "Copy $fromdir"
+      mkdir PHYEX
+      scp -q -r $fromdir/src PHYEX/
+      $prep_code $expand_options $subs -m arome PHYEX
     fi
-  fi
-
-  EXT=PHYEX/ext
-  [ ! -d $EXT ] && EXT=PHYEX/externals #old name for ext/aux
-  if [ -d $EXT ]; then
-    #Move manually files outside of mpa (a find on the whole repository would take too much a long time)
-    [ -f $EXT/yomparar.F90 ] && mv $EXT/yomparar.F90 ../arpifs/module/
-    [ -f $EXT/namparar.nam.h ] && mv $EXT/namparar.nam.h ../arpifs/namelist
-    [ -f $EXT/namlima.nam.h ] && mv $EXT/namlima.nam.h ../arpifs/namelist
-    [ -f $EXT/suparar.F90 ] && mv $EXT/suparar.F90 ../arpifs/phys_dmn/
-    [ -f $EXT/apl_arome.F90 ] && mv $EXT/apl_arome.F90 ../arpifs/phys_dmn/
-    [ -f $EXT/suphmpa.F90 ] && mv $EXT/suphmpa.F90 ../arpifs/phys_dmn/
-    [ -f $EXT/suphmse.F90 ] && mv $EXT/suphmse.F90 ../arpifs/phys_dmn/
-    [ -f $EXT/vdfhghtnhl.F90 ] && mv $EXT/vdfhghtnhl.F90 ../arpifs/phys_dmn/
-    [ -f $EXT/cpg_opts_type_mod.fypp ] && mv $EXT/cpg_opts_type_mod.fypp ../arpifs/module/
-    file=$EXT/cpg_pt_ulp_expl.fypp; [ -f $file ] && mv $file ../arpifs/adiab/
-    file=$EXT/field_variables_mod.fypp; [ -f $file ] && mv $file ../arpifs/module/
-    file=$EXT/cpg_type_mod.fypp; [ -f $file ] && mv $file ../arpifs/module/
-    file=$EXT/field_registry_mod.fypp; [ -f $file ] && mv $file ../arpifs/module/
-    file=$EXT/mf_phys_next_state_type_mod.fypp; [ -f $file ] && mv $file ../arpifs/module/
-    file=$EXT/yemlbc_model.F90; [ -f $file ] && mv $file ../arpifs/module/
-    [ -f $EXT/aplpar.F90 ] && mv $EXT/aplpar.F90 ../arpifs/phys_dmn/
-    [ -f $EXT/su0yomb.F90 ] && mv $EXT/su0yomb.F90 ../arpifs/setup/
-    [ -f $EXT/acvppkf.F90 ] && mv $EXT/acvppkf.F90 ../arpifs/phys_dmn/
-    #Special mpa case
-    for file in modd_spp_type.F90 spp_mod_type.F90 aroini_conf.h aroini_conf.F90; do
-      if [ -f $EXT/$file ]; then
-        [ ! -d ../mpa/aux ] && mkdir ../mpa/aux
-        mv $EXT/$file ../mpa/aux/
-      fi
+    find PHYEX -type f -exec touch {} \; #to be sure a recompilation occurs
+    for rep in turb micro conv aux; do
+      [ -d PHYEX/$rep ] && mv PHYEX/$rep .
     done
-    [ -d $EXT/dead_code ] && rm -rf $EXT/dead_code/
-    if [ $EXT == "PHYEX/externals" ]; then
-      mv $EXT .
-    else
-      #Move automatically all codes under mpa
-      for file in $EXT/*; do
-        extname=`basename $file`
-        loc=`find ../../$reftree/mpa/ -name $extname | sed "s/\/$reftree\//\/local\//g"`
-        nb=`echo $loc | wc -w`
-        if [ $nb -ne 1 ]; then
-          echo "Don't know where $file must be moved, none or several places found!"
-          exit 9
+    #modd_nsv.F90 has been moved and gmkpack is lost in case a different version exists in main/.../micro
+    if [ -f ../../main/phyex/micro/modd_nsv.F90 -a -f aux/modd_nsv.F90 ]; then
+      mv aux/modd_nsv.F90 micro/
+      if [ -f PHYEX/gmkpack_ignored_files ]; then
+        grep -v micro/modd_nsv.F90 PHYEX/gmkpack_ignored_files > PHYEX/gmkpack_ignored_files_new
+        mv PHYEX/gmkpack_ignored_files_new PHYEX/gmkpack_ignored_files
+      fi
+    fi
+    if [ -f PHYEX/gmkpack_ignored_files ]; then
+      #gmkpack_ignored_files contains a list of file, present in the reference pack, that is not used anymore
+      #and must be excluded from compilation (in case of a full comilation) or from re-compilation (in case of a non-full
+      #compilation).
+      if [ $fullcompilation == 0 ]; then
+        #Content is added in the ics_masterodb script
+        sed -i "/^end_of_ignored_files/i $(first=1; for line in $(cat PHYEX/gmkpack_ignored_files); do echo -n $(test $first -ne 1 && echo \\n)${line}; first=0; done)" $HOMEPACK/$name/ics_masterodb
+      else
+        #Files must be suppressed (non phyex files)
+        for file in $(cat PHYEX/gmkpack_ignored_files); do
+          [ -f $HOMEPACK/$name/src/local/$file ] && rm -f $HOMEPACK/$name/src/local/$file
+        done
+        [ ! "$(ls -A $HOMEPACK/$name/src/local/mpa/dummy)" ] && rmdir $HOMEPACK/$name/src/local/mpa/dummy
+      fi
+    fi
+
+    EXT=PHYEX/ext
+    [ ! -d $EXT ] && EXT=PHYEX/externals #old name for ext/aux
+    if [ -d $EXT ]; then
+      #Move manually files outside of mpa (a find on the whole repository would take too much a long time)
+      [ -f $EXT/yomparar.F90 ] && mv $EXT/yomparar.F90 ../arpifs/module/
+      [ -f $EXT/namparar.nam.h ] && mv $EXT/namparar.nam.h ../arpifs/namelist
+      [ -f $EXT/namlima.nam.h ] && mv $EXT/namlima.nam.h ../arpifs/namelist
+      [ -f $EXT/suparar.F90 ] && mv $EXT/suparar.F90 ../arpifs/phys_dmn/
+      [ -f $EXT/apl_arome.F90 ] && mv $EXT/apl_arome.F90 ../arpifs/phys_dmn/
+      [ -f $EXT/suphmpa.F90 ] && mv $EXT/suphmpa.F90 ../arpifs/phys_dmn/
+      [ -f $EXT/suphmse.F90 ] && mv $EXT/suphmse.F90 ../arpifs/phys_dmn/
+      [ -f $EXT/vdfhghtnhl.F90 ] && mv $EXT/vdfhghtnhl.F90 ../arpifs/phys_dmn/
+      [ -f $EXT/cpg_opts_type_mod.fypp ] && mv $EXT/cpg_opts_type_mod.fypp ../arpifs/module/
+      file=$EXT/cpg_pt_ulp_expl.fypp; [ -f $file ] && mv $file ../arpifs/adiab/
+      file=$EXT/field_variables_mod.fypp; [ -f $file ] && mv $file ../arpifs/module/
+      file=$EXT/cpg_type_mod.fypp; [ -f $file ] && mv $file ../arpifs/module/
+      file=$EXT/field_registry_mod.fypp; [ -f $file ] && mv $file ../arpifs/module/
+      file=$EXT/mf_phys_next_state_type_mod.fypp; [ -f $file ] && mv $file ../arpifs/module/
+      file=$EXT/yemlbc_model.F90; [ -f $file ] && mv $file ../arpifs/module/
+      [ -f $EXT/aplpar.F90 ] && mv $EXT/aplpar.F90 ../arpifs/phys_dmn/
+      [ -f $EXT/su0yomb.F90 ] && mv $EXT/su0yomb.F90 ../arpifs/setup/
+      [ -f $EXT/acvppkf.F90 ] && mv $EXT/acvppkf.F90 ../arpifs/phys_dmn/
+      #Special mpa case
+      for file in modd_spp_type.F90 spp_mod_type.F90 aroini_conf.h aroini_conf.F90; do
+        if [ -f $EXT/$file ]; then
+          [ ! -d ../mpa/aux ] && mkdir ../mpa/aux
+          mv $EXT/$file ../mpa/aux/
         fi
-        mv $file $loc
       done
+      [ -d $EXT/dead_code ] && rm -rf $EXT/dead_code/
+      if [ $EXT == "PHYEX/externals" ]; then
+        mv $EXT .
+      else
+        #Move automatically all codes under mpa
+        for file in $EXT/*; do
+          extname=`basename $file`
+          loc=`find ../../$reftree/mpa/ -name $extname | sed "s/\/$reftree\//\/local\//g"`
+          nb=`echo $loc | wc -w`
+          if [ $nb -ne 1 ]; then
+            echo "Don't know where $file must be moved, none or several places found!"
+            exit 9
+          fi
+          mv $file $loc
+        done
+      fi
     fi
+    rm -rf PHYEX
   fi
-  rm -rf PHYEX
 fi
 
 #####################
@@ -585,60 +598,70 @@ fi
 #####################
 
 if [ $compilation -eq 1 ]; then
-  echo "### Compilation of commit $commit"
-
-  cd $HOMEPACK/$name
-  sed -i 's/GMK_THREADS=1$/GMK_THREADS=10/' ics_masterodb
-  cleanpack -f
-  resetpack -f
+  if [ $onlyIfNeeded -eq 0 -o ! -f $HOMEPACK/$name/bin/MASTERODB ]; then
+    echo "### Compilation of commit $commit"
 
-  [ -f ics_packages ] && exescript Output_compilation_hub ics_packages
-  exescript Output_compilation ics_masterodb
-  if [ -f bin/MASTERODB \
-       -a $(grep Error Output_compilation | \
+    cd $HOMEPACK/$name
+    sed -i 's/GMK_THREADS=1$/GMK_THREADS=10/' ics_masterodb
+    cleanpack -f
+    resetpack -f
+  
+    [ -f ics_packages ] && exescript Output_compilation_hub ics_packages
+    exescript Output_compilation ics_masterodb
+    if [ -f bin/MASTERODB \
+         -a $(grep Error Output_compilation | \
+              grep -v TestErrorHandler | \
+              grep -v "'Error" | \
+              grep -v "'CPLNG: Error" | \
+              grep -v '"Error' | \
+              grep -v "'*** Error" | \
+              grep -v "\-\- Up-to-date:" | wc -l) -ne 0 ]; then
+      echo "MASTERODB was produced but errors occured during compilation:"
+      grep Error Output_compilation | \
             grep -v TestErrorHandler | \
             grep -v "'Error" | \
             grep -v "'CPLNG: Error" | \
             grep -v '"Error' | \
             grep -v "'*** Error" | \
-            grep -v "\-\- Up-to-date:" | wc -l) -ne 0 ]; then
-    echo "MASTERODB was produced but errors occured during compilation:"
-    grep Error Output_compilation | \
-          grep -v TestErrorHandler | \
-          grep -v "'Error" | \
-          grep -v "'CPLNG: Error" | \
-          grep -v '"Error' | \
-          grep -v "'*** Error" | \
-          grep -v "\-\- Up-to-date:"
-    echo "MASTERODB suppressed!"
-    rm -f bin/MASTERODB
-    exit 12
+            grep -v "\-\- Up-to-date:"
+      echo "MASTERODB suppressed!"
+      rm -f bin/MASTERODB
+      exit 12
+    fi
   fi
 fi
 
 if [ $run -ge 1 ]; then
-  echo "### Running of commit $commit"
-
-  if [ ! -f $HOMEPACK/$name/bin/MASTERODB ]; then
-    echo "Pack does not exist ($HOMEPACK/$name) or compilation has failed, please check"
-    exit 6
-  fi
-
   #Cleaning to suppress old results that may be confusing in case of a crash during the run
-  for t in $(echo $tests | sed 's/,/ /g'); do
-    cd $HOMEPACK/$name
-    if [ -d conf_tests/$t ]; then
-      rm -rf conf_tests/$t
-    fi
-  done
+  if [ $onlyIfNeeded -eq 0 ]; then
+    for t in $(echo $tests | sed 's/,/ /g'); do
+      cd $HOMEPACK/$name
+      if [ -d conf_tests/$t ]; then
+        rm -rf conf_tests/$t
+      fi
+    done
+  fi
 
   #Run the tests one after the other
-  for t in $(echo $tests | sed 's/,/ /g'); do
-    if echo $allowedTests | grep -w $t > /dev/null; then
+  firstrun=1
+  for t in $(echo $tests | sed 's/,/ /g'); do #loop on tests
+    if echo $allowedTests | grep -w $t > /dev/null; then #test is allowed on this plateform
       cd $HOMEPACK/$name
-      mkdir -p conf_tests/$t
-      cd conf_tests/$t
-      MYLIB=$name TESTDIR=$dirconf/$t exescript Output_run $dirconf/$t/aro${cycle}${scripttag}.sh
+      if [ ! -d conf_tests/$t ]; then #We do not enter systematically this part if onlyIfNeeded=1
+        if [ $firstrun -eq 1 ]; then
+          echo "### Running of commit $commit"
+          firstrun=0
+        fi
+
+        if [ ! -f $HOMEPACK/$name/bin/MASTERODB ]; then
+          echo "Pack does not exist ($HOMEPACK/$name) or compilation has failed, please check"
+          exit 6
+        fi
+
+        mkdir -p conf_tests/$t
+        cd conf_tests/$t
+        MYLIB=$name TESTDIR=$dirconf/$t exescript Output_run $dirconf/$t/aro${cycle}${scripttag}.sh
+      fi
     else
       echo "The test $t is not allowed"
     fi
@@ -657,6 +680,12 @@ if [ $check -eq 1 ]; then
   filestocheck=""
   for t in $(echo $tests | sed 's/,/ /g'); do
     if echo $allowedTests | grep -w $t > /dev/null; then
+      #Run the reference if needed
+      if [ $computeRefIfNeeded -eq 1 ]; then
+        $0 -p -c -r -t $t --onlyIfNeeded ${refByTest[$t]}
+      fi
+
+      #Files to compare
       if echo $t | grep 'small' > /dev/null; then
         filestocheck="$filestocheck ${t},conf_tests/$t/ICMSHFPOS+0002:00 ${t},conf_tests/$t/DHFDLFPOS+0002"
       else
@@ -666,6 +695,7 @@ if [ $check -eq 1 ]; then
       echo "The test $t is not allowed"
     fi
   done
+
   for tag_file in $filestocheck; do
       tag=$(echo $tag_file | cut -d, -f1)
       file=$(echo $tag_file | cut -d, -f2)
diff --git a/tools/testing.sh b/tools/testing.sh
index 9d9de01b9..a413671d6 100755
--- a/tools/testing.sh
+++ b/tools/testing.sh
@@ -289,16 +289,19 @@ if [ ${force} -eq 1 -o $(get_statuses "${SHA}" | grep "${context}" | wc -l) -eq
     if [ "${model}" == 'ial' ]; then
       compilation='-p -c'
       execution='-r'
+      comparison='-C --computeRefIfNeeded'
       jsonfile="src/arome/ial_version.json"
       docmp=1
     elif [ "${model}" == 'lmdz' ]; then
       compilation='-p -c --nofcm'
       execution='-r --nofcm'
+      comparison='-C'
       jsonfile="src/${model}/${model}_version.json"
       docmp=0
     else
       compilation='-c'
       execution='-r'
+      comparison='-C'
       jsonfile="src/${model}/${model}_version.json"
       docmp=1
     fi
@@ -359,7 +362,7 @@ if [ ${force} -eq 1 -o $(get_statuses "${SHA}" | grep "${context}" | wc -l) -eq
           fi
         fi
         if [ ${result} -eq 0 -a ${docmp} -eq 1 -a ${comp} -eq 1 ]; then
-          compcmd="$cmd -C ${casearg} ${refarg}"
+          compcmd="$cmd ${comparison} ${casearg} ${refarg}"
           log 1 "Comparison with ${compcmd}"
           set +e
           ${compcmd}
-- 
GitLab