## 04/04/2024
# @Najda
# documente l'utilisation des scripts *emulator_predictions* et exemple map

if [ $# -eq 1 ] ; then
  if [ "$1" == "map" ] ; then
    mode=map
  else
    echo "wrong option $1. Should be map or nothing"
    exit
  fi
else
  mode=ref
fi

python=python
if [ "$( which python )" = "" ] ; then echo You need python ; exit 1 ; fi
## This is common to all modes
exe=exemple.sh
bash $exe clean

# now specific actions
case $mode in 
  map) echo "Exemple in map mode"
  ###############################################
  ##  Émulation carte de flux avec $ns sondes  ##
  ###############################################
  ns=100
  npix=1638400 # same as in reference map
  npix=40000
  
  dir=map_${ns}sondes_${npix}predicts
  
  if [ -d $dir ] ; then 
    echo "$dir" exists
    echo "moving it to ${dir}_$$"
    mv $dir ${dir}_$$
  fi
  
  mkdir $dir
  
  echo "logs redirectd to $dir/log ..."
  
  sed_i "s/nsondes=.*$/nsondes=$ns/g" $exe
  sed_i "s/nsondes=.*$/nsondes=$ns/g" map_make_files.sh
  sed_i "s/nsondes=.*$/nsondes=$ns/g" plot_map_emulated.py 
  
  bash $exe map > $dir/log 2>&1
  
  Rscript htune_emulator_predictions.R -sample_size $npix -seed 123 >> $dir/log 2>&1
  
  cp plot_map_emulated.py map_make_files.sh $dir
  cp Metrics.csv Par1D_Wave1.asc Predictions_Wave1.asc $dir
  cp -r WAVE1/ $dir
  
  cd $dir
  bash map_make_files.sh
  
  echo "Plotting..."
  python plot_map_emulated.py
  
  echo Done. Check results in $dir.

  ;;

  ref) echo "Exemple in map mode"
  ###############################################
  ##  Utilisation de l'exemple de base (awk)   ##
  ###############################################

  ns=10
  sed_i "s/nsondes=.*$/nsondes=$ns/g" $exe

  # run 4 waves of exemple, introduce a second metric at 3rd wave
  for i in 1 2 3 4
  do
    bash $exe $i 3
  done
  
  # generate Predictions_Wave4.asc containing expectation & variance of emulators
  # for all metrics and waves up to 4th
  Rscript htune_emulator_predictions.R -wave 4 -seed 234
  
  # plot results, only waves 1,4 only as a function of parameter a
  # -y == ylim of the plots
  python plot_emulator_predictions.py -w 1,4 -p a 

  ;;

esac
