# $Id: job_ORCA2_LIM.ksh 1301 2009-02-09 17:15:38Z ctlod $
set -xv

CONF=ORCA2_LIM_PISCES
# 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
MYO_EXP=xxx
TSYMB=xxx

# Variables changed by ins_valid.ksh script
MAINDIR=/workdir/rech/eee/reee534/AUTRE
WORK=/workdir/rech/eee/reee534/AUTRE/modipsl/config/
REF_OD=/u/rech/eee/reee831/NEMO_VALIDATION

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

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

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

# Working directory
cd ${TMPDIR}
#-T- osxxlf mkdir -p ${WORK}/NEMO_VALID/W${CONF}/${RUN}/${DIROUT}/${MYO_EXP}/RUN
#-T- osxxlf cd ${WORK}/NEMO_VALID/W${CONF}/${RUN}/${DIROUT}/${MYO_EXP}/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
##############################################################
##############################################################
##############################################################
# Local function to copy namelists files
get_namelist () {
    if [ -a $1/$2_$3 ] ; then
        cp $1/$2_$3 $4
    else
        echo '                ###############################################################'
        echo '                ######################## W A R N I N G ########################'
        echo '                ######################## W A R N I N G ########################'
        echo '                ######################## W A R N I N G ########################'
        echo '                ###############################################################'
        echo "              $2_$3 file doesn't exist, we use the standard $2"
        echo '                ###############################################################'
        echo '                ######################## W A R N I N G ########################'
        echo '                ######################## W A R N I N G ########################'
        echo '                ######################## W A R N I N G ########################'
        echo '                ###############################################################'
        cp $1/$2 $4
    fi
}

# Local function to find namelists parameters
supergrep () {
    grep "^ *$1 *=" $2 | sed -e "s% *\!.*%%"
}

# Ocean namelist
LOC_NAM=${MAINDIR}/modipsl/config/${CONF}/EXP00
get_namelist ${LOC_NAM} namelist ${MYO_EXP} namelist

# Sea-Ice namelist
get_namelist ${LOC_NAM} namelist_ice_lim2 ${MYO_EXP} namelist_ice

# Passive transport namelist
get_namelist ${LOC_NAM} namelist_top ${MYO_EXP} namelist_top

# Pisces namelist
get_namelist ${LOC_NAM} namelist_pisces ${MYO_EXP} namelist_pisces
get_namelist ${LOC_NAM} kRGB61.txt      ${MYO_EXP} kRGB61.txt

# Local function to find namelists parameters
supergrep () {
    grep "^ *$1 *=" $2 | sed -e "s% *\!.*%%"
}

# namelist modifications
PAT_NIT000=$( supergrep nit000             namelist )
PAT_NITEND=$( supergrep nitend             namelist )
PAT_NSTOCK=$( supergrep nstock             namelist )
PAT_NWRITE=$( supergrep nwrite             namelist )
PAT_NRSTDT=$( supergrep nrstdt             namelist )
PAT_NBITCM=$( supergrep nbit_cmp           namelist )
PAT_TRACEN=$( supergrep ln_traadv_cen2     namelist )
PAT_TRATVD=$( supergrep ln_traadv_tvd      namelist )
PAT_N_CLAA=$( supergrep n_cla              namelist )
PAT_NSOLVV=$( supergrep nsolv              namelist )
PAT_RESTAR=$( supergrep ln_rstart          namelist )
PAT_RESMLD=$( supergrep 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_TRACEN%   ln_traadv_cen2   =  .false.%"     \
    -e "s%$PAT_TRATVD%   ln_traadv_tvd    =  .true.%"      \
    -e "s%$PAT_N_CLAA%   n_cla   = 0%"                     \
    -e "s%$PAT_NSOLVV%   nsolv     =      2%"              \
    -e "s%$PAT_RESMLD%   ln_trdmld_restart =  .false.%" namelist > nam.tmp

mv nam.tmp namelist

# namelist top modifications
PAT_NWRTRC=$( supergrep nwritetrc         namelist_top )
PAT_LRSTTR=$( supergrep ln_rsttr            namelist_top )
PAT_NRSTTR=$( supergrep nrsttr            namelist_top )
PAT_TRCRAD=$( supergrep ln_trcrad         namelist_top )

sed -e "s%$PAT_NWRTRC%   nwritetrc   = ${ITERATIONS}%" \
    -e "s%$PAT_LRSTTR%   ln_rsttr      =  .false.%"      \
    -e "s%$PAT_NRSTTR%   nrsttr      =     0   %"       \
    -e "s%$PAT_TRCRAD%   ln_trcrad   =  .false.%" namelist_top > nam.tmp     

mv nam.tmp namelist_top

# namelist pisces modifications
PAT_DUSTF=$( supergrep ln_dustfer        namelist_pisces )
PAT_RIVER=$( supergrep ln_river          namelist_pisces )
PAT_NDEPO=$( supergrep ln_ndepo          namelist_pisces )
PAT_SEDIN=$( supergrep ln_sedinput       namelist_pisces )

sed -e "s%$PAT_DUSTF%   ln_dustfer    =  .false.%"  \
    -e "s%$PAT_RIVER%   ln_river      =  .false.%"  \
    -e "s%$PAT_NDEPO%   ln_ndepo      =  .false.%"  \
    -e "s%$PAT_SEDIN%   ln_sedinput   =  .false.%" namelist_pisces > nam.tmp     

mv nam.tmp namelist_pisces

# Get forcing files
${D_GET} ${D_INPUT}/ORCA2_LIM_nemo_v3.1.tar ORCA2_LIM_nemo_v3.1.tar
tar xvf ORCA2_LIM_nemo_v3.1.tar
rm -f ORCA2_LIM_nemo_v3.1.tar

# Get forcing pisces files
${D_GET} ${D_INPUT}/INPUTS_PISCES_v3.tar INPUTS_PISCES_v3.tar
tar xvf INPUTS_PISCES_v3.tar
rm -f INPUTS_PISCES_v3.tar

# 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/${MYO_EXP}/*_restart.nc restart.nc
        cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/${MYO_EXP}/*restart_ice.nc restart_ice_in.nc
        cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/${MYO_EXP}/*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/${MYO_EXP}/*_restart_${i}.nc restart_${i}.nc
           cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/${MYO_EXP}/*restart_ice_${i}.nc restart_ice_in_${i}.nc
           cp ${WORK}/NEMO_VALID/W${CONF}/${RUN}/1_SHORT/${MYO_EXP}/*restart_trc_${i}.nc restart_trc_${i}.nc
           let i=$i+1
        done
    fi

    # Ocean namelist modifications
    PAT_NIT000=$( supergrep nit000             namelist )
    PAT_NITEND=$( supergrep nitend             namelist )
    PAT_NRSTDT=$( supergrep nrstdt             namelist )
    PAT_RESTAR=$( supergrep ln_rstart          namelist )
    PAT_RESMLD=$( supergrep 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_LRSTTR=$( supergrep ln_rsttr        namelist_top )
    PAT_NRSTTR=$( supergrep nrsttr        namelist_top )

    sed -e "s%$PAT_LRSTTR%   ln_rsttr     = .true. %"  \
        -e "s%$PAT_NRSTTR%   nrsttr     = 2%" namelist_top > nam.tmp

    mv nam.tmp namelist_top

   PAT_PISDMP=$( supergrep ln_pisdmp     namelist_pisces )
   sed -e "s%$PAT_PISDMP%  ln_pisdmp     = .false.%" namelist_pisces > nam.tmp
   mv nam.tmp namelist_pisces
fi

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

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

#
# 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}_${MYO_EXP}) )"    >> memory_size.txt 
#-T- sx8brodie echo "Static memory: $( (size opa_${CONF}_memo_${MYO_EXP}) )"   >> memory_size.txt  ;;
#-T- sx8mercure export F_PROGINF=detail
#-T- sx8mercure echo "Stack memory: $( (size opa_${CONF}_${RUN}_${MYO_EXP}) )"    >> memory_size.txt
#-T- sx8mercure echo "Static memory: $( (size opa_${CONF}_memo_${MYO_EXP}) )"   >> memory_size.txt  ;;
#-T- aix6 echo "Stack memory: $( (size opa_${CONF}_memo_${MYO_EXP}) )"    >> memory_size.txt 
#-T- aix6 echo "Static memory: $( (size opa_${CONF}_${RUN}_${MYO_EXP}) )"   >> memory_size.txt ;;
#-T- osxxlf echo "Stack memory: $( (size opa_${CONF}_memo_${MYO_EXP}) )"    >> memory_size.txt 
#-T- osxxlf echo "Static memory: $( (size opa_${CONF}_${RUN}_${MYO_EXP}) )"   >> memory_size.txt ;; 
        'mpi' ) 
#-T- sx8brodie export MPIPROGINF=detail ;;
#-T- sx8mercure export MPIPROGINF=detail ;;
#-T- aix6     ;;
#-T- osxxlf   ;;
        'omp' ) 
#-T- sx8brodie export F_PROGINF=detail
#-T- sx8brodie export OMP_NUM_THREADS=${NB_PROCS} ;;
#-T- sx8mercure export F_PROGINF=detail
#-T- sx8mercure export OMP_NUM_THREADS=${NB_PROCS} ;;
#-T- aix6     ;;
#-T- osxxlf   ;;
esac
#
# >>>>>> EXECUTION < < < <
##########################

case ${RUN} in
        'mon' | 'omp' ) 
              ${CPERF} ./opa_${CONF}_${RUN}_${MYO_EXP} ;;
        'mpi' ) 
#-T- sx8brodie         ${CPERF} ${LEXE} ./opa_${CONF}_${RUN}_${MYO_EXP} ;;
#-T- sx8mercure         ${CPERF} ${LEXE} ./opa_${CONF}_${RUN}_${MYO_EXP} ;;
#-T- aix6              ${CPERF} ${LEXE} ./opa_${CONF}_${RUN}_${MYO_EXP} ;;
#-T- osxxlf   mpd&
#-T- osxxlf   sleep 5
#-T- osxxlf   ${CPERF} ${LEXE} ./opa_${CONF}_${RUN}_${MYO_EXP}
#-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* ORCA2* restart* mono*' ;;
	'mpi' )
               liste_files='*abort* namelist* solver.stat perfs* ocean.output* layout* ORCA2* 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}/${MYO_EXP}/${kc}
       ind=0
   fi
   cp $k ${WORK}/NEMO_VALID/W${CONF}/${RUN}/${DIROUT}/${MYO_EXP}/$k
done

##############################################################
##############################################################
##############################################################
###             Retrieve some reference run files
##############################################################
##############################################################
##############################################################
if [ ${REF_TAG} = 'nemo_v2' -o ${REF_TAG} = 'nemo_v2_3' -o ${REF_TAG} = 'nemo_v3' ]
then
   STORE_DATA=${REF_OD}/${CONF}/${REF_TAG}/${RUN}/${DIROUT}
   LFILES="solver.stat memory_size.txt ocean.output ${RUN}${CONF}_err"
   SAV_DIR=${WORK}/NEMO_VALID/W${CONF}/${RUN}/${DIROUT}/${REF_TAG}
   if [ ! -d ${SAV_DIR} ] ;  then
      mkdir ${SAV_DIR}
   fi
   for ref_file in ${LFILES}
   do
       ${D_GET} ${STORE_DATA}/${TSYMB}${ref_file} ${SAV_DIR}/${ref_file}
   done
fi

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