#!/bin/bash
######################
## CESIUM   CEA ##
######################
#MSUB -r allwei         # Nom du job                
#MSUB -n 1              # Reservation du processus
#MSUB -N 1              # Reservation du noeud
#MSUB -T 86400          # Limite de temps elapsed du job
#MSUB -e allwei.out     # Sortie standard
#MSUB -o allwei.out     # Sortie standard
#MSUB -q xlarge 
#MSUB -Q normal
#MSUB -p gen2211

module list
  
set -vx

SUBMIT_DIR=${SUBMIT_DIR:-${BRIDGE_MSUB_PWD:-${PWD}}}
JOB_ID=${BRIDGE_MSUB_JOBID:-${$}}

RESOL=$(basename ${SUBMIT_DIR})
RESOL_DIR=${SUBMIT_DIR}
#RESOL=ORCA1xLMD9695
#RESOL_DIR=${DMFDIR}/CPL/FILES/${RESOL}

TMPDIR=${SCRATCHDIR}/${RESOL}.${JOB_ID}
mkdir -p ${TMPDIR} || exit 1
cd ${TMPDIR} || exit 2

echo RESOL : ${RESOL}
RESOL_OCE=$(echo ${RESOL} | sed 's/x.*//' | sed 's/ORCA//' )
RESOL_ATM=$(echo ${RESOL} | sed 's/.*x//' | sed 's/LMD//'  )
echo RESOL OCE : ${RESOL_OCE}
echo RESOL ATM : ${RESOL_ATM}

# ==============================================
pwd

cp ${HOME}/CPL/MOZAIC/util/*.sh .
cp ${HOME}/CPL/MOZAIC/bin/*.exe .

cp ${RESOL_DIR}/run.def .
cp ${RESOL_DIR}/orca${RESOL_OCE}.*.nc .
# cp ${RESOL_DIR}/grids.nc .
# cp ${RESOL_DIR}/masks.nc .
# cp ${RESOL_DIR}/areas.nc .
# cp ${RESOL_DIR}/o2a.diag.nc .
# cp ${RESOL_DIR}/mozaic.wo2a.* .
# cp ${RESOL_DIR}/mozaic.wa2o.* .

ls -al

time ./allgrid.sh -c ${RESOL}
time ./mosaic.exe     > mosaic.out      2>&1
time ./allgrid.sh -c -r ${RESOL}
#time ./cotes.exe      > cotes.out       2>&1 
time ./cotes_etal.exe > cotes_etal.out  2>&1 
time ./icestream.exe  > icestream.out   2>&1
time ./tst.exe        > tst.out         2>&1
time ./tst.exe -r     > tst_r.out       2>&1

date

ls -al

chmod a+r *

cp  *.out \
    LMDZ4.0_*_grid.nc \
    a2o.* o2a.* itarget.nc \
    areas.nc masks.nc grids.nc \
    dist_cote.nc flux_iceberg \
    lmd.*.r8 lmd.*.i4 lmd.*.i8 lmdz.*.nc \
    mozaic.*.nc mozaic.*r8 \
    poly.* \
    ${RESOL_DIR}

# ==============================================

