NUM_EXP=$1
# Le nb de niveaux se determine a la compilation: compile.x
NLEV=100
# Le pas de temps se regle dans gcm.def (day_step)
TSTEP=60
case $NUM_EXP in
6)  PHYSIQ=AR4    ;EXPER=AR4_L"$NLEV"_"$TSTEP"s;;
7)  PHYSIQ=NPv3.1 ;EXPER=NPv3.1_L"$NLEV"_"$TSTEP"s;;
esac
# 5=iperiod from gcm.def et 2=nsplit_phys from gcm.def
daystep=`echo $TSTEP | awk ' { print 86400. *5. / 2. / $TSTEP } ' | sed -e 's/,/\./'`
#daystep=`echo $TSTEP | awk ' { print 86400. / $TSTEP } ' | sed -e 's/,/\./'`
sed -e 's/day_step=.*.$/day_step='$daystep'/' def$PHYSIQ/gcm.def > def$PHYSIQ/gcm2.def
mv def$PHYSIQ/gcm2.def def$PHYSIQ/gcm.def
rm -f def$PHYSIQ/gcm2.def
echo "Pas de temps="$TSTEP,$daystep
grep day_step def$PHYSIQ/*def
echo $EXPER
dir_results=./$EXPER
echo "dir_results="$dir_results
echo "----------------------------------------------------"
#====================================
  if [ -d $dir_results ] ; then
     echo " Directory is OK :"$dir_results
   else
     echo " !! No Directory :"$dir_results ", on le cree" 
     mkdir -v $dir_results
  fi
#====================================
   ecrit_mth=`echo $TSTEP | awk ' { print $TSTEP / 86400. } ' | sed -e 's/,/\./'`
   echo $TSTEP $ecrit_mth
  if [ -s def$PHYSIQ/config.def ] ; then
   cp -f ./def$PHYSIQ/*.def $dir_results/.
   echo def$PHYSIQ/config.def "existe"
   sed -e 's/phys_out_filetimesteps.*.$/phys_out_filetimesteps='$ecrit_mth'/' ./def$PHYSIQ/config.def >| $dir_results/config.def
   diff  ./def$PHYSIQ/config.def  $dir_results/config.def
  else
   cp -f ./def$PHYSIQ/*.def $dir_results/.
   echo def$PHYSIQ/physiq.def "n'existe pas"
   sed -e 's/phys_out_filetimesteps.*.$/phys_out_filetimesteps='$ecrit_mth'/' ./def$PHYSIQ/physiq.def >| $dir_results/physiq.def
   diff  ./def$PHYSIQ/physiq.def  $dir_results/physiq.def
  fi
#
  if [ -d $dir_results ] ; then
     echo " Directory is OK :"$dir_results
   else
     echo " !! No Directory :"$dir_results ; mkdir $dir_results
  fi
   cp -f ./def$PHYSIQ/*data $dir_results/.
   cp ./composite_ref_init.nc $dir_results/.
   cp ./ifa_sanduref.txt $dir_results/.
   cp ./prof.inp.001 $dir_results/.  
   proc=$(basename $0) # present procedure name.
   cp $proc $dir_results/mr
#====================================
  cd $dir_results
  ../lmdz1d.e > OUT.$EXPER 2>&1
#- - - - - - - - - - - - - - - - -
# Traitement du fichier netcdf :
#- - - - - - - - - - - - - - - - -
if [ $TSTEP  -lt 100 ] ; then
        TAVE="t_ave_000000"$TSTEP
else
        TAVE="t_ave_00000"$TSTEP
fi
echo "TAVE="$TAVE
  ncrename -O -h -v $TAVE,time_counter histhf.nc
# Pour mettre la bonne date et la bonne heure en heures LT
  ncatted -O -h -a units,time_counter,m,c,"seconds since 2006-07-15 10:00:00" histhf.nc
# ncatted -O -a positive,presnivs,o,c,"down" histhf.nc
# Pour retirer 0 comme etant la valeur manquante
# ncatted -O -a missing_value,,d,, histhf.nc
# ncatted -O -a positive,presnivs,o,c,down -a _FillValue,,m,f,1.e35 histhf.nc

#  On met 1.e35 comme valeur indefinie ; selon les version de nco on passe par un chgmnt de nom de _FillValue ou non.
#  histhf.nc
  ncrename -a _FillValue,bidonundef histhf.nc  2>/dev/null
#
  if [ "$?" = "1" ]
  then
   ncatted -O -a positive,presnivs,o,c,down -a _FillValue,,m,f,1.e35 histhf.nc
  else
   ncatted -O -a positive,presnivs,o,c,down -a bidonundef,,m,f,1.e35 histhf.nc
   ncrename -a bidonundef,_FillValue histhf.nc
  fi
#  profil_avg.nc
  ncatted -O -a missing_value,,d,, Lefebvre_profiles_avg.nc
# ncrename -a _FillValue,bidonundef Lefebvre_profiles_avg.nc  2>/dev/null
#
# if [ "$?" = "1" ]
# then
#  ncatted -O -a positive,presnivs,o,c,down -a _FillValue,,m,f,1.e35 Lefebvre_profiles_avg.nc
# else
#  ncatted -O -a positive,presnivs,o,c,down -a bidonundef,,m,f,1.e35 Lefebvre_profiles_avg.nc
#  ncrename -a bidonundef,_FillValue Lefebvre_profiles_avg.nc
# fi
#  scalars.nc
  ncatted -O -a missing_value,,d,, Lefebvre_scalars.nc
#
  grep Everything OUT*
# On detruit fichiers non utilises
  rm -fv histday.nc
  rm -fv histdayNMC.nc
  rm -fv histhfNMC.nc
  rm -fv histmthNMC.nc
  rm -fv histrac.nc
