#!/bin/bash

set -eu

#################################################################
# Script de lancement d'une serie de simulations pour HIGH TUNE 
# Author : N. Villefranque, 2018
# Modifications: 
#        2022 - M. Coulon and N. Villefranque - run on SCM profiles
#  17/07/2025 - N. Villefranque - source env_ECRAD.profile
#################################################################

####### To run several occurences in parrallel ######
# You can put nproc = 1 to run it in sequential mode

nproc=$(( $(lscpu  -e=core | sort | uniq | wc -l) - 1 ))
nproc=6
echo nproc = $nproc
wait_max=300
if [ $nproc = 1 ]   ; then wait_max=1 ; fi

###### To choose LES1D_ecRad file #####
## default option is empty
dir_les=L12.8km_hourly_instant

### Reading arguments 

if [[ $# -lt 3 ]]; then
	echo "Use : ./$0 RAD/CASE/SUBCASE,RAD/CASE2/SUBCASE... WAVEN MODEL"
	exit 1
fi

if [ $# -gt 3 ] ; then
  listecas=${*:1:$#-2} # all but last two arguments
else
  listecas=$1
fi

WAVEN=${*: -2:1}       # antelast arg
MODEL=${*: -1}       # last arg
echo "$listecas"

HERE=$(pwd) # WORK/BENCHMODEL

if [[ "$MODEL" == LMDZ ]] ; then
  echo "you are running ECRAD on $MODEL SCM"
  echo "WARNING : SCM clear sky is replaced by LES clear sky !"
elif [[ "$MODEL" != ECRAD ]] ; then
  echo "you ask to run ECRAD on $MODEL SCM"
  echo "some hypothesis are made for the treatment of the SCM for ECRAD to be able to run on it, on dephy2ecrad.sh and replaceclr_SCMtoLES.sh ONLY AVAILABLE for LMDZ SCM"
  echo "ask Maelle Coulon or Najda Villefranque for more informations"
  exit 1
fi

### NOM DE LA NAMELIST A UTILISER POUR FAIRE TOURNER ECRAD 
namelist=config_ArtIIImean_spartacus
########

# source ECRAD environnement
local=$(pwd)
echo $local
install_dir=$local/../../../install_shared
source $install_dir/base_env.profile
source $install_dir/env_ECRAD.profile

# Here the model is ecrad
# This is filled automatically by setup_ECRAD.sh
ECRAD=$ECRADdir
HTN=$ECRAD/work/tuning_ArtIIImean-spartacusmean3D/

if [[ ! -d $ECRAD ]]
then
  echo "$ECRAD cannot be found. Modify the ECRAD variable in $0 and try again."
  exit 1
fi

echo "Creating $HTN directory. Files from runECRAD will be copied in it."
if [[ ! -d $HTN ]]
then
  mkdir -p $HTN
fi

ref_sza=ref.nc

cp -f runECRAD/*sh "$HTN"
cp -f runECRAD/$ref_sza "$HTN/ref.nc"
cp -f runECRAD/$namelist.nam "$HTN"

EXPE=$(pwd)/WAVE$WAVEN
bin=$ECRAD/bin/ecrad
dup=$HTN/duplicate_profiles.sh
scl=$HTN/scale_input.sh
nam2nam=$HTN/nam2nam.sh
path_data=../../data # path depuis le repertoire ou ecrad tourne

# if ECRAD standalone
L1D="$ECRAD/../LES1D_ecRad/$dir_les"

# create config files from $EXPE/Par1D_Wave1.asc
sed -e 's/"//g' $EXPE/Par1D_Wave$WAVEN.asc > $HTN/param.asc

#Some path changes on files on ${ECRAD}/dephy2ecard
sed -e "s#HERE=.*.#HERE=${ECRAD}/dephy2ecRad#" ${ECRAD}/dephy2ecRad/dephy2ecrad.sh > ${ECRAD}/dephy2ecRad/tmp_dephy2ecrad.sh
mv ${ECRAD}/dephy2ecRad/tmp_dephy2ecrad.sh ${ECRAD}/dephy2ecRad/dephy2ecrad.sh
chmod +x ${ECRAD}/dephy2ecRad/dephy2ecrad.sh

sed -e "s#HERE=.*.#HERE=${ECRAD}/dephy2ecRad#" ${ECRAD}/dephy2ecRad/replaceclr_SCMtoLES.sh > ${ECRAD}/dephy2ecRad/tmp_replaceclr_SCMtoLES.sh
mv ${ECRAD}/dephy2ecRad/tmp_replaceclr_SCMtoLES.sh ${ECRAD}/dephy2ecRad/replaceclr_SCMtoLES.sh
chmod +x ${ECRAD}/dephy2ecRad/replaceclr_SCMtoLES.sh 


cd $HTN

mkdir -p ${EXPE}/NAMECRAD

set +u
params=()
for i in `head -1 param.asc` ; do
   params=( ${params[*]} $i )
done
nl=`wc -l param.asc | awk ' { print $1 } '` # number of lines in the param.asc file
il=2
# start loop on parameters vectors for which we run simulations

######## Creating ECRAD namelist : namelist-ecrad_SCM-${WAVEN}-${sim} ######
echo "CREATING ECRAD NAMELIST"
#loop on simulations
while [ $il -le $nl ] ; do 
  params=()
  for i in `head -1 param.asc` ; do
     params=( ${params[*]} $i )
  done
  # extracting parameter values
  vals=()
  for i in `sed -n -e ${il}p param.asc`  ; do
    vals=( ${vals[*]} $i )
  done

  sim=${vals[0]: -3} # vals[0] = SCM-XXX => sim = XXX
  names=`head -1 param.asc | sed -e 's/\"//g' -e 's/ /,/g'` 
  vals_nam2nam=`sed -n -e ${il}p param.asc  | sed -e 's/\"//g' -e 's/ /,/g'`
  outnam=$EXPE/NAMECRAD/namelist_ecrad_SCM-${WAVEN}-${sim}
  $nam2nam -names $names -vals $vals_nam2nam -input $HTN/$namelist.nam -output $outnam 
  sed_i -e "s#directory_name.*.#directory_name=\"$path_data\"#" $outnam
  (( il = $il + 1 ))
done

set -u

SCMnamelist=namelist_ecrad_SCM-${WAVEN}-


######### Runing ECRAD ###########
echo RUNING ECRAD
cp $EXPE/NAMECRAD/${SCMnamelist}* $HTN
if [ -f $EXPE/NAMECRAD/sclvals.txt ]  ; then cp $EXPE/NAMECRAD/sclvals.txt $HTN ; fi

iii=0
# start loop on CASE/SUBCASE
for cas in $listecas 
do
  #echo $cas      # RAD/CASE/SUBCASEXXX
  tmp=${cas:4}   # CASE/SUBCASEXXX
  CASE="$(sed 's/\/.*//' <<< "$tmp")"     # CASE
  SUBC="$(sed 's/^[^;]*\///' <<< "$tmp")" # SUBCASEXXX
  TIME=${SUBC: -3}                        # XXX
  SUBC=`echo $SUBC | sed -e "s/$TIME//g"` # SUBCASE
  echo START LOOP ON $CASE ${SUBC}${TIME}
  #echo "cas  =" $cas
  #echo "tmp  =" $tmp
  #echo "case =" $CASE
  #echo "subc =" $SUBC
  #echo "time =" $TIME
  
  mkdir -p ${EXPE}/${CASE}/${SUBC}

  inp_in=$HTN/${CASE}-${SUBC}-${TIME}
  les1D=$L1D/${SUBC}.1.${CASE}.${TIME}_1D #LES pour le rayonnement

  function run_ECRAD {
    thisnam=$1
    inp=$2
    outecrad=$3
    inp=${inp}-${WAVEN}-${sim}
    suf=${CASE}-${SUBC}${TIME}-${WAVEN}-${sim}
    if [ "$MODEL" == ECRAD ]
    then
      cp "$les1D.nc" "$inp.nc"
    else
      tmp=${EXPE}/${CASE}/${SUBC}/SCM-${WAVEN}-${sim}.nc
      extract_time=`echo "$TIME-1" | bc -l`
      ncks -d time,$extract_time $tmp -O ${inp}-tmp-ncks.nc
      bash ${ECRAD}/dephy2ecRad/dephy2ecrad.sh ${inp}-tmp-ncks.nc ${inp}-dephy.nc > log_dephy2ecrad_$suf.out
      bash ${ECRAD}/dephy2ecRad/replaceclr_SCMtoLES.sh ${inp}-dephy.nc ${les1D}.nc ${inp}-replace.nc > log_replace-$suf.out
      cp ${inp}-replace.nc ${inp}.nc
      ##################### WATCH OUT ##################
      # THIS IS ONLY HERE BECAUSE THE MC REFS WERE RUN #
      #             WITH RE = 10 e-6 !!                #
      ncap2 -s "re_liquid=re_liquid*0+1e-5" ${inp}.nc -O ${inp}.nc
      ##################################################
    fi
    scout=${inp}
    # duplicate input profile
    $dup ${inp}.nc ${inp}_sza.nc > log_duplicate-$suf.out 2>&1

    # first: scale input profiles ${inp}_sza.nc
    if [ -f sclvals.txt ]
    then
      (( ip = $il + 1 ))
      sclvals=`sed -n "$ip p " sclvals.txt`
      echo $sclvals
      $scl ${inp}_sza.nc ${scout}_sza.nc ${sclvals}
      inp=${scout}
    fi
    cmd="$bin $thisnam ${inp}_sza.nc $outecrad"
    outlog=log_ECRAD_$suf.out

    eval "$cmd" &> "$outlog"
    cp $outecrad ${EXPE}/${CASE}/${SUBC}/RAD${TIME}-${WAVEN}-${sim}.nc
    if [ ! $? -eq 0 ]
    then
      echo "WARNING !!! Simulation $outecrad crashes"
      exit 1
    fi

    # get rid of nc files, keep only namelists and logs
    mkdir -p poub/
    mv ${inp}-replace.nc ${inp}-dephy.nc ${inp}-tmp-ncks.nc ${inp}.nc ${inp}_sza.nc poub/
  }

  il=0
  iproc=0
  nsims=`ls ${SCMnamelist}* | wc -l `
  echo nsims = $nsims
  for sim in ` ( cd ${EXPE}/NAMECRAD ; ls ${SCMnamelist}* | sed -e "s/${SCMnamelist}//" ) ` ; do
    (( il = $il + 1 ))
    thisnam=$HTN/${SCMnamelist}${sim}
    outecrad=${HTN}/OUT-$CASE-$SUBC-RAD${TIME}-${WAVEN}-${sim}.nc
    suf=${CASE}-${SUBC}${TIME}-${WAVEN}-${sim}
    out=log_ECRAD_${suf}.out
    echo "!!!!!!!!!!  RUNING $CASE ${SUBC}${TIME} $sim (log $(pwd)/$out) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    # Parallel mode : ecrad runs as background process
    run(){ 
      time run_ECRAD $thisnam $inp_in $outecrad > $out 2>&1 
      if [ $? -ne 0 ] ; then echo run_ECRAD crash in $0 ; exit 1 ; fi
      grep crashes $out > /dev/null 2>&1
      if [ $? -ne 0 ] ; then
        rm -f $out.gz ; gzip $out ; rm
      fi
    }
    run & # as a background process
    sleep 0.1
    (( iproc = $iproc + 1 ))
    if [ $iproc -eq $nproc ] ; then
      wait    # until all background processes are finished
      iproc=0 # reset number of busy procs
    fi
  done # fin boucle sur les simus
  wait # until all background processes are finished
  
  #############################################################################
  # To be sure that all simulations are finished
  #############################################################################
  isim=2
  for sim in ` ( cd ${EXPE}/NAMECRAD ; ls ${SCMnamelist}* | sed -e "s/${SCMnamelist}//" ) ` ; do
    echo Final check for $sim
    outecrad=${HTN}/OUT-$CASE-$SUBC-RAD${TIME}-${WAVEN}-${sim}.nc
    iwait=0
    while [ ! -f $outecrad ] ; do
      echo Waiting for passenger $outecrad
      sleep 3
      iwait=$(($iwait+1))
      if [ $iwait -ge 10 ] ; then echo stop.............. ; exit 1 ; fi
    done
    mv $outecrad poub/
    (( isim = $isim + 1 ))
  done

  cd $HTN
done # fin boucle sur les cas

#on vide le repertoire de travail de ecrad pour pas être géné à la prochaine vague
#rm $HTN/*.nc
if [ -f $HTN/sclvals.txt ] ; then rm $HTN/sclvals.txt ; fi 
exit






