
dirpng=${DODSDIR}/${login}/lmdz/VLR

if [ ${hostname:0:5} = cicla ] ; then
########################################################################################################
# Environnement
module purge
module load netcdf4/4.3.3.1-ifort cdo/1.6.8 nco/4.5.2  ncl/6.3.0
ulimit -s unlimited
export PYTHONPATH=:/opt/canopy-1.3.0/Canopy_64bit/User/bin/python:/home/igcmg/prodiguer-client:/home/fabric/users/denvil/climaf:/home/fabric/users/denvil/climaf
export PATH=$PATH:/home/fabric/users/denvil/climaf/bin
STORAGE=$DODSDIR/$login/lmdz/STORE
\rm -rf $DODSDIR/$login/climafCache$$
mkdir -p $DODSDIR/$login/climafCache$$
export CLIMAF_CACHE=$DODSDIR/$login/climafCache$$
echo CLIMAF_CACHE $DODSDIR/$login/climafCache$$
MULTIDIR=$DODSDIR/$login/lmdz/MultiSimu
DIRATLAS=$DODSDIR/$login/lmdz/atlas
########################################################################################################
ATLAS_DIR=$DODSDIR/$login/lmdz/atlas/Atlas$$
if [ ! -d $DODSDIR/$login/lmdz/atlas/Atlas$$ ] ; then mkdir -p $DODSDIR/$login/lmdz/atlas/Atlas$$ ; fi
fi

if [ $# = 0 ] ; then
 echo ARG=0
   comp=$COMP
   dataref=$DATAREF
fi

echo comp=$comp
echo dataref=$dataref

Dhtml=/thredds/ipsl/${login}/lmdz/MultiSimu/${comp}/ATLAS
mkdir -p ${Dhtml}

local=`pwd -P`
runstxt=$local/runs.txt

login=${login}
DODSDIR=/thredds/ipsl

echo DODSDIR : $DODSDIR
MAIN_SE=$DODSDIR/$login/lmdz/SE
TS_DA=$DODSDIR/$login/lmdz/TS_DA
TS_MO=$DODSDIR/$login/lmdz/TS_MO
MULTIDIR=$DODSDIR/$login/lmdz/MultiSimu

COMP_D=$MULTIDIR/$comp

###
DEF_FILE=$COMP_D/def.txt ; if [ ! -f $DEF_FILE ] ; then echo renseigner $DEF_FILE ; exit ; fi
RUNS="" 
RUNS=`awk ' {print $1"_"$2 } ' $DEF_FILE`
echo $RUNS

### definir la reference par rapport au flag 
if [ $dataref = 1 ]; then
ref=OBS
fi
echo ref is $ref

ns=0
listsimss="" ;
for ss in `awk ' {print $1"_"$2 } ' $DEF_FILE`    ; do
if [ $dataref = 0 ] ; then
   if [ ${ns} = 0 ] ; then
      ref=$ss
      echo ref is $ref
   fi
fi
let "ns = ${ns} + 1"
listsimss="$listsimss $ss" ;
done

for sim in $RUNS; do
echo sim=$sim
echo sim $sim
srun=`echo $sim | sed -e 's/_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]//'`
echo srun $srun
simdir=`grep -w "^$srun " $runstxt | awk ' { print $2 } '`
echo $sim $srun $simdir
run=`basename $simdir `
years=`echo $sim | sed -e 's/'$srun'//' | sed -e 's/_//'`
sim=${run}_${years}
echo ================================================================
echo la commande suivante 
echo ls -l $MAIN_SE/ORIG/${run}_SE_${years}_1M_histmth.nc
echo peut prendre beaucoup de temps lorsque
echo l acces aux espaces du CCRT et/ou de l IDRIS thredds est lent ou si
echo les espaces /thredds/tgcc et/ou /thredds/idris ne sont pas montes sur ciclad.
echo On ne l utilise plus !!!
echo ================================================================
###ls -l ${MAIN_SE}/ORIG/${run}_SE_${years}_1M_histmth.nc

cd $ATLAS_DIR

qsub=qsub
submit=${qsub}

natlas=`wc -l $COMP_D/atlas-def | awk ' { print $1 } '`
echo $natlas
count_atlas=0

atlas=1
   il=1
   if [ ! -f $COMP_D/atlas-def ] ; then
      echo Definir les atlas a construire dans $COMP_D/atlas-def
      echo Example :
      cat $COMP_D/../CMIP5to6/atlas-def
   fi
   while [ $il -le $natlas ] ; do
      line=`sed -n -e ${il}p $COMP_D/atlas-def`
      seas=`echo $line | awk ' { print $1 } '`
      proj=`echo $line | awk ' { print $2 } '`
      vars=`echo $line | awk ' { print $3 } '`
      atlas=${sim}+${seas}+${proj}+$vars
      varg=`echo $vars | sed -e 's/,/ /g'`
         if [ $qsub = 0 ] ; then
            ./atlas.sh RUN=$sim,SEASON=$seas,PROJ=$proj,OUTDIR=$COMP_D/ATLAS,REF=$ref,VARS="$varg",RUNSTXT=$runstxt
         else
          for var in $varg; do
           mkdir -p ${dirpng}/${seas}/${ref}/MAP
           if [ "${sim}" != "OBS" ]; then
            mkdir -p ${dirpng}/${seas}/${sim}/MAP
            mkdir -p ${dirpng}/${seas}/${sim}/DMAP_${ref}
            mapok=${dirpng}/${seas}/${sim}/MAP/${var}.pdf
            dmapok=${dirpng}/${seas}/${sim}/DMAP_${ref}/${var}.pdf
           if [ ! -f ${mapok} ] || [ ! -f ${dmapok} ]; then
            \rm -rf $DODSDIR/$login/climafCache$$
            if [ ! -d $DODSDIR/$login/climafCache$$ ] ; then 
             mkdir -p $DODSDIR/$login/climafCache$$ ; 
            fi
            export CLIMAF_CACHE=$DODSDIR/$login/climafCache$$
            datevar=`date`
            echo before python var=$var $datevar
            echo python /data/${login}/Multi_atlas/atlas/atlas_none.py -v ${var} -s ${sim} -t ${ref} -p ${seas} --dirpng ${dirpng}
#IM : test si pres existe pour l interpolation des vars. 3D s/ des niveaux de pression
            if [ $var == ua ] || [ $var == va ] || [ $var == ta ] || [ $var == hur ] || [ $var == hus ]; then
             run=`basename $simdir `
             echo sim=$sim
             echo sim $sim
             srun=`echo $sim | sed -e 's/_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]//'`
             echo srun $srun
             simdir=`grep -w "^$srun " $runstxt | awk ' { print $2 } '`
             echo $sim $srun $simdir
             run=`basename $simdir `
             years=`echo $sim | sed -e 's/'$srun'//' | sed -e 's/_//'`
             sim=${run}_${years}

             if [ -f ${MAIN_SE}/ORIG/${run}_SE_${years}_1M_histmth.nc ]; then
              CheckPres=`ncdump -c ${MAIN_SE}/ORIG/${run}_SE_${years}_1M_histmth.nc|grep -i 'float pres('`
              echo MAIN_SE/ORIG
              ls -l ${MAIN_SE}/ORIG/${run}_SE_${years}_1M_histmth.nc

             elif [ -f ${simdir}/ATM/Analyse/SE/${run}_SE_${years}_1M_histmth.nc]; then
              CheckPres=`ncdump -c ${simdir}/ATM/Analyse/SE/${run}_SE_${years}_1M_histmth.nc|grep -i 'float pres('`
              echo ATM/Analyse/SE
              ls -l ${MAIN_SE}/ORIG/${run}_SE_${years}_1M_histmth.nc

             else
              \rm years.txt
              echo $years > years.txt
              sed -i -e 's/_/ /' years.txt
              yrbegin=`more years | awk '{print $1}'`
              yrend=`more years | awk '{print $2}'`
              CheckPres=`ncdump -c ${simdir}/ATM/Output/MO/${run}_${yrbegin}*_${yrend}*_1M_histmth.nc|grep -i 'float pres('`
              echo ls -l ATM/Output/MO
              ls -l ${simdir}/ATM/Output/MO/${run}_${yrbegin}*_${yrend}*_1M_histmth.nc
             fi

             if [ $CheckPres == ""]; then
               echo pres is not available in the input file, stop here!
               echo Modify atlas-def and launch multi-comparaison $comp again!
               exit
             fi 
            fi 
              python /data/${login}/Multi_atlas/atlas/atlas_none.py -v ${var} -s ${sim} -t ${ref} -p ${seas} --dirpng ${dirpng}
            datevar=`date`
            echo after python var=$var $datevar
            mv index_GLOB_${seas}_${sim}.html $Dhtml/.

            din=${dirpng}/${seas}/${sim}/MAP
            cd ${din}
            convert ${var}.pdf ${var}.png
            convert -size 1500x1000 xc:white ${var}.png -geometry 1440x890+30+30 -composite -trim -gravity North ${var}.png

            din=${dirpng}/${seas}/${sim}/DMAP_${ref}
            cd ${din}
            convert ${var}.pdf ${var}.png
            convert -size 1500x1000 xc:white ${var}.png -geometry 1440x890+30+30 -composite -trim -gravity North ${var}.png

            convert dmapzon${var}.pdf dmapzon${var}.png
            convert -size 1500x1000 xc:white dmapzon${var}.png -geometry 1440x890+30+30 -composite -trim -gravity North dmapzon${var}.png

            cd ${local}
           fi
           fi
          done
         fi
         (( count_atlas = $count_atlas + 1 ))
      (( il = $il + 1 ))
   done
done

exit
