set -xv

CONF=GYRE_LOBSTER
# Variables changed by lance_batch.ksh script
RUN=xxx
NB_PROCS=xxx
LAUNCH=xxx
LEXE=xxx
CPERF=xxx
D_INPUT=xxx
D_GET=xxx
REF_TAG=xxx
TSYMB=xxx

# Variables changed by ins_valid.ksh script
MAINDIR=/Users/ctlod/WORK/AZE
WORK=/Users/ctlod/WORK/AZE/modipsl/config
REF_OD=/Volumes/FENUA/ORCA2_LIM_nemo_v2_IO

# Arguments
# type = 'long' or '1_short' or '2_short'
type=$1

case ${type} in
        'long' ) 
             DIROUT='LONG'
             ITERATIONS=360 ;;
              
        '1_short' ) 
             DIROUT='1_SHORT'
             ITERATIONS=180 ;;
              
        '2_short' ) 
             DIROUT='2_SHORT'
             ITERATIONS=180 ;;

        'gtime' ) 
             DIROUT='GTIME'
             ITERATIONS=4320 ;;
esac

# Working directory
cd ${TMPDIR}
#-T- osxxlf cd ${WORK}/NEMO_VALID/W${CONF}/${RUN}/${DIROUT}/RUN
#-T- osxxlf zback=$?
#-T- osxxlf if [ ${zback} = 0 ] ; then rm -rf namelist* *.nc memory* solver.stat ocean.output* mpp.output* mono.output* ; fi

##############################################################
##############################################################
##############################################################
###             Input files
##############################################################
##############################################################
##############################################################
# Ocean namelist
cp ${MAINDIR}/modipsl/config/${CONF}/EXP00/namelist .

# namelist modifications
PAT_NIT000=$( grep 'nit000      =' namelist )
PAT_NITEND=$( grep 'nitend      =' namelist )
PAT_NSTOCK=$( grep 'nstock      =' namelist )
PAT_NWRITE=$( grep 'nwrite      =' namelist )
PAT_NRSTDT=$( grep 'nrstdt      =' namelist )
PAT_NBITCM=$( grep 'nbit_cmp   ='   namelist )
PAT_NSOLVV=$( grep 'nsolv       =' namelist )
PAT_RESTAR=$( grep 'ln_rstart   =' namelist )
PAT_RESMLD=$( grep 'ln_trdmld_restart =' namelist )

sed    -e "s%$PAT_RESTAR%   ln_rstart   =  .false.%" \
-e "s%$PAT_NIT000%   nit000     =     1%" \
-e "s%$PAT_NITEND%   nitend     =     ${ITERATIONS}%" \
-e "s%$PAT_NSTOCK%   nstock     =     ${ITERATIONS}%" \
-e "s%$PAT_NWRITE%   nwrite     =     75%" \
-e "s%$PAT_NRSTDT%   nrstdt     =     0%" \
-e "s%$PAT_NBITCM%   nbit_cmp =     1%" \
-e "s%$PAT_NSOLVV%   nsolv     =      2%" \
-e "s%$PAT_RESMLD%   ln_trdmld_restart =  .false.%" \
namelist > nam.tmp

mv nam.tmp namelist

# Biogeochemistry namelists
cp ${MAINDIR}/modipsl/config/${CONF}/EXP00/namelist_top namelist_top_f90
cp ${MAINDIR}/modipsl/config/${CONF}/EXP00/namelist_lobster namelist_lobster_f90

# namelist modifications
PAT_TRC=$( grep 'nwritetrc   =' namelist_top_f90 )
PAT_TRD=$( grep 'nwritetrd  ='  namelist_top_f90 )

sed    -e "s/$PAT_TRC/   nwritetrc   = ${ITERATIONS},/" \
-e "s/$PAT_TRD/   nwritetrd  = ${ITERATIONS},/" \
namelist_top_f90 > nam.tmp

mv nam.tmp namelist_top

PAT_ADD=$( grep 'nwritedia    =' namelist_lobster_f90 )
sed    -e "s/$PAT_ADD/   nwritedia    = ${ITERATIONS},/" namelist_lobster_f90 > nam.tmp
mv nam.tmp namelist_lobster

# Analytical forcing

# Get restarts files if needed
if [ $type = '2_short' ]
then
    if [ ${RUN} = 'mon' -o ${RUN} = 'omp' ] ; then 
        cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/*_restart.nc restart.nc
        cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/*_restart_mld.nc restart_mld.nc
        cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/*_restart_trc.nc restart.trc.nc
    fi

    if [ ${RUN} = 'mpi' ] ; then
        typeset -Z4 i=0
        while [ $i -lt $NB_PROCS ]
        do
           cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/*_restart_${i}.nc restart_${i}.nc
           cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/*_restart_mld_${i}.nc restart_mld_${i}.nc
           cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/*_restart_trc_${i}.nc restart.trc_${i}.nc
           let i=$i+1
        done
    fi

    # Ocean namelist modifications
    PAT_NIT000=$( grep 'nit000     =' namelist )
    PAT_NITEND=$( grep 'nitend     =' namelist )
    PAT_NRSTDT=$( grep 'nrstdt     =' namelist )
    PAT_RESTAR=$( grep 'ln_rstart   =' namelist )
    PAT_RESMLD=$( grep 'ln_trdmld_restart =' namelist )
    ORCA_NIT000=$(( $ITERATIONS + 1 ))
    ORCA_NITEND=$(( $ITERATIONS*2 ))
    
    sed    -e "s/$PAT_RESTAR/   ln_rstart   =  .true./" \
    -e "s/$PAT_NIT000/   nit000     =     $ORCA_NIT000/" \
    -e "s/$PAT_NITEND/   nitend     =     $ORCA_NITEND/" \
    -e "s/$PAT_NRSTDT/   nrstdt     =     2/" \
    -e "s/$PAT_RESMLD/   ln_trdmld_restart =  .true./" \
    namelist > nam.tmp
    
    mv nam.tmp namelist

    # Biogeochemistry namelist modifications
    PAT_RESTAR=$( grep '   lrsttr      = ' namelist_top )
    
    sed    -e "s/$PAT_RESTAR/   lrsttr      = .true.,/" namelist_top > nam.tmp
    
    mv nam.tmp namelist_top

fi

##############################################################
##############################################################
##############################################################
###             Launch run
##############################################################
##############################################################
##############################################################

cp ${WORK}/NEMO_VALID/W${CONF}/opa_${CONF}_${RUN} opa_${CONF}_${RUN}
cp ${WORK}/NEMO_VALID/W${CONF}/opa_${CONF}_memo opa_${CONF}_memo
chmod 777 opa_${CONF}_${RUN}
chmod 777 opa_${CONF}_memo

#
# Profiling & Executable memory size
####################################
touch memory_size.txt

case ${RUN} in
        'mon' ) 
#-T- sx8brodie export F_PROGINF=detail
#-T- sx8brodie echo "Stack memory: $( (size opa_${CONF}_${RUN}) )"    >> memory_size.txt 
#-T- sx8brodie echo "Static memory: $( (size opa_${CONF}_memo) )"   >> memory_size.txt  ;;
#-T- aix     ;;
#-T- ax_mono echo "Stack memory: $( (size opa_${CONF}_memo) )"    >> memory_size.txt 
#-T- ax_mono echo "Static memory: $( (size opa_${CONF}_${RUN}) )"   >> memory_size.txt ;;
#-T- osxxlf echo "Stack memory: $( (size opa_${CONF}_memo) )"    >> memory_size.txt 
#-T- osxxlf echo "Static memory: $( (size opa_${CONF}_${RUN}) )"   >> memory_size.txt ;; 
        'mpi' ) 
#-T- sx8brodie export MPIPROGINF=detail ;;
#-T- aix     ;;
#-T- ax_mono ;;
#-T- osxxlf  ;;
        'omp' ) 
#-T- sx8brodie export F_PROGINF=detail
#-T- sx8brodie export OMP_NUM_THREADS=${NB_PROCS} ;;
#-T- aix     ;;
#-T- ax_mono ;;
#-T- osxxlf  ;;
esac
#
# >>>>>> EXECUTION < < < <
##########################

case ${RUN} in
        'mon' | 'omp' ) 
              ${CPERF} ./opa_${CONF}_${RUN} ;;
        'mpi' ) 
#-T- sx8brodie              ${CPERF} ${LEXE} ./opa_${CONF}_${RUN} ;;
#-T- aix              ${CPERF} ${LEXE} ./opa_${CONF}_${RUN} ;;
#-T- osxxlf   mpd&
#-T- osxxlf   sleep 5
#-T- osxxlf   ${CPERF} ${LEXE} ./opa_${CONF}_${RUN}
#-T- osxxlf   mpdallexit ;;
esac

ls -l
#

##############################################################
##############################################################
##############################################################
###             Save output files
##############################################################
##############################################################
##############################################################
case ${RUN} in
	'mon' | 'omp' )
               liste_files=' *abort* memory* namelist* perfs* solver.stat ocean.output layout* GYRE* restart* mono*' ;;
	'mpi' )
               liste_files=' *abort* namelist* solver.stat perfs* ocean.output* layout* GYRE* restart* mpp*' ;;
esac

# Ensure that all files saved are coming from the current run
ind=1
for k in $liste_files
do
   echo $k > temp
   var=$( ( awk '{printf("%-.4s", varin ) } ' varin=$k temp ) )
   if [ ${ind} = 1 -a ${var} = 'perf' ] ; then
       kc='perf*'
       rm -f ${WORK}/NEMO_VALID/W${CONF}/${RUN}/${DIROUT}/${kc}
       ind=0
   fi
   cp $k ${WORK}/NEMO_VALID/W${CONF}/${RUN}/${DIROUT}/$k
done

##############################################################
##############################################################
##############################################################
###             Retrieve some reference run files
##############################################################
##############################################################
##############################################################
if [ ${REF_TAG} ]
then
   STORE_DATA=${REF_OD}/${CONF}/${REF_TAG}/${RUN}/${DIROUT}
   LFILES="solver.stat memory_size.txt ocean.output ${RUN}${CONF}_err"
   for ref_file in ${LFILES}
   do
       ${D_GET} ${STORE_DATA}/${TSYMB}${ref_file} ${WORK}/NEMO_VALID/W${CONF}/${RUN}/${DIROUT}/REF/${ref_file}
   done
fi

##############################################################
##############################################################
##############################################################
###             Launch restart job
##############################################################
##############################################################
##############################################################
if [ $type = '1_short' ] ; then
    cd ${WORK}/NEMO_VALID/W${CONF}/${RUN}/2_SHORT
    ${LAUNCH}job_${RUN}_2_short.ksh
fi
