Index: /trunk/libIGCM/libIGCM_ensemble/libIGCM_ensemble.ksh
===================================================================
--- /trunk/libIGCM/libIGCM_ensemble/libIGCM_ensemble.ksh	(revision 678)
+++ /trunk/libIGCM/libIGCM_ensemble/libIGCM_ensemble.ksh	(revision 679)
@@ -4,7 +4,7 @@
 # Author: Sonia Labetoulle
 # Contact: Sonia.Labetoulle__at__locean-ipsl.upmc.fr
-# $Revision$ Revision of last commit
-# $Author$ Author of last commit
-# $Date$ Date of last commit
+# $Revision::                                          $ Revision of last commit
+# $Author::                                            $ Author of last commit
+# $Date::                                              $ Date of last commit
 # IPSL (2012)
 #  This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC
@@ -12,7 +12,396 @@
 #**************************************************************
 
-function IGCM_ensemble_FilesUpdate
+function IGCM_ensemble_CastInit
 {
-  IGCM_debug_PushStack "IGCM_ensemble_FilesUpdate"
+  IGCM_debug_PushStack "IGCM_ensemble_CastInit"
+
+  set -A Alpha      A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
+  set -A AlphaMonth a b c d e f g h i j k l
+
+  IGCM_sys_Mkdir ${RUN_DIR}
+
+  IGCM_sys_Cp ${SUBMIT_DIR}/config.card   ${RUN_DIR}
+  IGCM_sys_Cp ${SUBMIT_DIR}/ensemble.card ${RUN_DIR}
+  IGCM_sys_Cp ${SUBMIT_DIR}/Job_*         ${RUN_DIR}
+  IGCM_sys_Cp ${SUBMIT_DIR}/run.card.init ${RUN_DIR}
+
+  if [ -f  ${SUBMIT_DIR}/CreatedDir.txt ] ; then
+    IGCM_sys_Cp ${SUBMIT_DIR}/CreatedDir.txt ${RUN_DIR}
+  fi
+  if [ -f  ${SUBMIT_DIR}/Qsub.txt ] ; then
+    IGCM_sys_Cp ${SUBMIT_DIR}/Qsub.txt ${RUN_DIR}
+  fi
+
+  IGCM_card_DefineVariableFromOption ${FileName} Ens_PERTURB NAME
+  IGCM_card_DefineVariableFromOption ${FileName} Ens_PERTURB DEBUT_INIT
+  IGCM_card_DefineVariableFromOption ${FileName} Ens_PERTURB FIN_INIT
+  IGCM_card_DefineVariableFromOption ${FileName} Ens_PERTURB PERIODICITE
+  IGCM_card_DefineArrayFromOption    ${FileName} Ens_PERTURB NONPERIODIC
+  IGCM_card_DefineVariableFromOption ${FileName} Ens_PERTURB DUREE
+  IGCM_card_DefineArrayFromOption    ${FileName} Ens_PERTURB DUREE_NONPERIODIC
+  IGCM_card_DefineVariableFromOption ${FileName} Ens_PERTURB MEMBER
+  IGCM_card_DefineArrayFromOption    ${FileName} Ens_PERTURB PERTURB_BIN
+  IGCM_card_DefineVariableFromOption ${FileName} Ens_PERTURB INITFROM
+  IGCM_card_DefineVariableFromOption ${FileName} Ens_PERTURB INITPATH
+  IGCM_card_DefineVariableFromOption config.card UserChoices TagName
+  IGCM_card_DefineVariableFromOption config.card UserChoices CalendarType
+  IGCM_card_DefineArrayFromSection   config.card ListOfComponents
+
+  IGCM_debug_Print 1 "NAME              = ${ensemble_Ens_PERTURB_NAME}"
+  IGCM_debug_Print 1 "DEBUT_INIT        = ${ensemble_Ens_PERTURB_DEBUT_INIT}"
+  IGCM_debug_Print 1 "FIN_INIT          = ${ensemble_Ens_PERTURB_FIN_INIT}"
+  IGCM_debug_Print 1 "PERIODICITE       = ${ensemble_Ens_PERTURB_PERIODICITE}"
+  IGCM_debug_Print 1 "NONPERIODIC       = ${ensemble_Ens_PERTURB_NONPERIODIC[*]}"
+  IGCM_debug_Print 1 "DUREE             = ${ensemble_Ens_PERTURB_DUREE}"
+  IGCM_debug_Print 1 "DUREE_NONPERIODIC = ${ensemble_Ens_PERTURB_DUREE_NONPERIODIC[*]}"
+  IGCM_debug_Print 1 "MEMBER            = ${ensemble_Ens_PERTURB_MEMBER}"
+  IGCM_debug_Print 1 "PERTURB_BIN       = ${ensemble_Ens_PERTURB_PERTURB_BIN[*]}"
+  IGCM_debug_Print 1 "INITFROM          = ${ensemble_Ens_PERTURB_INITFROM}"
+  IGCM_debug_Print 1 "INITFROM          = ${ensemble_Ens_PERTURB_INITPATH}"
+  IGCM_debug_Print 1 "TagName           = ${config_UserChoices_TagName}"
+  IGCM_debug_Print 1 "CalendarType      = ${config_UserChoices_CalendarType}"
+  IGCM_debug_Print 1 "ListOfComponents  = ${config_ListOfComponents[*]}"
+
+  PerturbExe=${ensemble_Ens_PERTURB_PERTURB_BIN[0]}
+  PerturbComp=${ensemble_Ens_PERTURB_PERTURB_BIN[1]}
+  PerturbFile=${ensemble_Ens_PERTURB_PERTURB_BIN[2]}
+  PerturbVar=${ensemble_Ens_PERTURB_PERTURB_BIN[3]}
+  PerturbAmp=${ensemble_Ens_PERTURB_PERTURB_BIN[4]}
+
+  IGCM_debug_Print 1 "PerturbExe  = ${PerturbExe}"
+  IGCM_debug_Print 1 "PerturbFile = ${PerturbFile}"
+  IGCM_debug_Print 1 "PerturbComp = ${PerturbComp}"
+  IGCM_debug_Print 1 "PerturbVar  = ${PerturbVar}"
+  IGCM_debug_Print 1 "PerturbAmp  = ${PerturbAmp}"
+
+  IGCM_debug_PopStack "IGCM_ensemble_CastInit"
+}
+
+function IGCM_ensemble_CastPeriodicStarts
+{
+  IGCM_debug_PushStack "IGCM_ensemble_CastPeriodicStarts"
+
+  echo "Manage periodic starts"
+
+#.. Manage periodic starts ..
+#   ======================
+
+# ... Determine number of year(s) in PERIODICITE ...
+  case ${ensemble_Ens_PERTURB_PERIODICITE} in
+  0)
+    IGCM_debug_Print 1 "No periodic start"
+    ;;
+  *Y|*y)
+    PeriodLengthInYears=$( echo ${ensemble_Ens_PERTURB_PERIODICITE} | sed -e 's/[yY]//' );;
+  *M|*m)
+    PeriodLengthInMonths=$( echo ${ensemble_Ens_PERTURB_PERIODICITE} | sed -e 's/[mM]//' );;
+  *)
+    IGCM_debug_Exit "IGCM_config_Analyse_PeriodLength " \
+      ${ensemble_Ens_PERTURB_PERIODICITE} \
+      " invalid PERIODICITE : choose in *Y."
+    IGCM_debug_Verif_Exit ;;
+  esac
+# ... Determine number of year(s) in DUREE ...
+  case ${ensemble_Ens_PERTURB_DUREE} in
+  *Y|*y)
+    DureeLengthInYears=$( echo ${ensemble_Ens_PERTURB_DUREE} | sed -e 's/[yY]//' ) ;;
+  *M|*m)
+    DureeLengthInMonths=$( echo ${ensemble_Ens_PERTURB_DUREE} | sed -e 's/[mM]//' ) ;;
+  *)
+    IGCM_debug_Exit "IGCM_config_Analyse_PeriodLength " \
+      ${ensemble_Ens_PERTURB_DUREE} \
+      " invalid DUREE : choose in *Y."
+    IGCM_debug_Verif_Exit ;;
+  esac
+
+# ... Loop over DateBegin ...
+  eval DateBegin=\${ensemble_Ens_PERTURB_DEBUT_INIT}
+
+  while [ ${DateBegin} -le ${ensemble_Ens_PERTURB_FIN_INIT}  ] ; do
+    IGCM_date_GetYearMonth ${DateBegin} year month
+
+  # - Determine number of day(s) in PERIODICITE
+    PeriodLengthInDays=0
+    case ${ensemble_Ens_PERTURB_PERIODICITE} in
+    *Y|*y)
+      i=0
+      until [ $i -ge ${PeriodLengthInYears} ] ; do
+        (( PeriodLengthInDays = PeriodLengthInDays + $( IGCM_date_DaysInYear $(( year + i )) ) ))
+        (( i = i + 1 ))
+      done
+      ;;
+    *M|*m)
+      i=0
+      year_wrk=$year
+      month_wrk=$month
+      until [ $i -ge ${PeriodLengthInMonths} ] ; do
+        (( PeriodLengthInDays = PeriodLengthInDays + $( IGCM_date_DaysInMonth $year_wrk $month_wrk ) ))
+        if [ $month_wrk -lt 12 ] ; then
+          (( month_wrk = month_wrk + 1 ))
+        else
+          (( year_wrk  = year_wrk + 1 ))
+          (( month_wrk = 1 ))
+        fi
+        (( i = i + 1 ))
+      done
+      ;;
+    esac
+  # - Determine number of day(s) in DUREE
+    DureeLengthInDays=0
+    case ${ensemble_Ens_PERTURB_DUREE} in
+    *Y|*y)
+      i=0
+      until [ $i -ge ${DureeLengthInYears} ] ; do
+        (( DureeLengthInDays = DureeLengthInDays + $( IGCM_date_DaysInYear $(( year + i )) ) ))
+        (( i = i + 1 ))
+      done
+      ;;
+    *M|*m)
+      i=0
+      year_wrk=$year
+      month_wrk=$month
+      until [ $i -ge ${DureeLengthInMonths} ] ; do
+        (( DureeLengthInDays = DureeLengthInDays + $( IGCM_date_DaysInMonth $year_wrk $month_wrk ) ))
+        if [ $month_wrk -lt 12 ] ; then
+          (( month_wrk = month_wrk + 1 ))
+        else
+          (( year_wrk  = year_wrk + 1 ))
+          (( month_wrk = 1 ))
+        fi
+        (( i = i + 1 ))
+      done
+      ;;
+    esac
+
+    (( DureeLengthInDays = DureeLengthInDays - 1 ))
+
+  # - Determine DateEnd
+    (( DateEnd = $( IGCM_date_AddDaysToGregorianDate ${DateBegin} ${DureeLengthInDays} ) ))
+
+  # - Build directory name
+    IGCM_ensemble_CastDirectoryName ${ensemble_Ens_PERTURB_NAME} ${ensemble_Ens_PERTURB_PERIODICITE} $year $month $StartDir
+
+  # - Determine RestartDate
+    (( Offset = -1 ))
+    (( RestartDate = $( IGCM_date_AddDaysToGregorianDate ${DateBegin} ${Offset} ) ))
+
+    IGCM_debug_Print 2 "${DateBegin} => ${DateEnd} : ${StartDir}"
+    echo "${DateBegin} ${DateEnd} ${StartDir}" >> CreatedDir.txt
+
+  # - Create directory for current DateBegin
+    if [ ! -d  ${StartDir} ] ; then
+      IGCM_sys_Mkdir ${SUBMIT_DIR}/${StartDir}
+      IGCM_sys_Cd ${SUBMIT_DIR}/${StartDir}
+      ln -s ../../.resol .
+      ln -s ../../.libmpi .
+      IGCM_sys_Cd ${RUN_DIR}
+    fi
+
+  # - Create directory in which to store new restart files
+    RestartDir=$ARCHIVE/IGCM_IN/${config_UserChoices_TagName}/${StartDir}
+    if [ ! -d  ${RestartDir} ] ; then
+      IGCM_sys_MkdirArchive ${RestartDir}
+    fi
+
+  # - Loop over members
+    i=0
+    while [ $i -lt ${ensemble_Ens_PERTURB_MEMBER} ] ; do
+      MemberDir="${StartDir}${Alpha[$i]}"
+      IGCM_debug_Print 3 "${MemberDir}"
+
+      JobName="Job_${MemberDir}"
+
+      pwd
+      ls
+
+    # * Create directory if it doesn't exist and copy/link files
+      if [ ! -d  ${SUBMIT_DIR}/${StartDir}/${MemberDir} ] ; then
+        IGCM_sys_Mkdir ${SUBMIT_DIR}/${StartDir}/${MemberDir}
+#      IGCM_sys_Cp -r COMP/ PARAM/ ${StartDir}/${MemberDir}
+        IGCM_sys_Cd ${SUBMIT_DIR}/${StartDir}/${MemberDir}
+        ln -s ../../COMP
+        ln -s ../../PARAM
+        ln -s ../../POST
+        IGCM_sys_Cd ${RUN_DIR}
+        IGCM_sys_Cp config.card run.card.init ${SUBMIT_DIR}/${StartDir}/${MemberDir}
+        IGCM_sys_Cp Job_${ensemble_Ens_PERTURB_NAME} ${SUBMIT_DIR}/${StartDir}/${MemberDir}/${JobName}
+
+        echo "cd ${StartDir}/${MemberDir}/ ; ccc_msub ${JobName} ;" >> Qsub.txt
+
+        echo "sed -e 's/PeriodNb=.*/PeriodNb=48/' ${JobName} > ${JobName}.tmp ; " >> Qsub.txt
+        echo "mv ${JobName}.tmp ${JobName} ; cd - ;" >> Qsub.txt
+      fi
+
+    # * Update files : config.card, Job_, COMP/comp.card
+      IGCM_ensemble_CastFilesUpdate ${DateBegin} ${DateEnd} ${RestartDate}
+
+    # * Apply noise on restart file
+      IGCM_ensemble_CastPerturbFile
+
+      (( i = i + 1 ))
+    done
+
+  # - Next DateBegin
+    echo "$DateBegin  $PeriodLengthInDays"
+    case ${ensemble_Ens_PERTURB_PERIODICITE} in
+    *Y|*y|*M|*m)
+      (( DateBegin = $( IGCM_date_AddDaysToGregorianDate ${DateBegin} ${PeriodLengthInDays} ) ))
+      ;;
+    esac
+    echo "New DateBegin = $DateBegin"
+    echo "========================================================================"
+  done
+  IGCM_debug_PopStack "IGCM_ensemble_CastPeriodicStarts"
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+function IGCM_ensemble_CastNonPeriodicStarts
+{
+  IGCM_debug_PushStack "IGCM_ensemble_CastNonPeriodicStarts"
+
+  #.. Manage non periodic starts => Loop over DateBegin ..
+  #   ==========================
+
+  echo "Manage non periodic starts"
+
+  DateNum=0
+# ... Loop over ensemble_Ens_PERTURB_NONPERIODIC ...
+  echo ">${DateNum}<"
+  echo ">${#ensemble_Ens_PERTURB_NONPERIODIC[*]}<"
+  while [ $DateNum -lt ${#ensemble_Ens_PERTURB_NONPERIODIC[*]} ] ; do
+    DateBegin=${ensemble_Ens_PERTURB_NONPERIODIC[$DateNum]}
+    Duree=${ensemble_Ens_PERTURB_DUREE_NONPERIODIC[$DateNum]}
+    echo ">${DateBegin}<"
+    echo ">${Duree}<"
+  # - Determine number of year(s) in DUREE_NONPERIODIC
+    case ${Duree} in
+    0)
+      IGCM_debug_Print 1 "No non-periodic start"
+      exit
+      ;;
+    *Y|*y)
+      DureeLengthInYears=$( echo ${Duree} | sed -e 's/[yY]//' )
+      ;;
+    *M|*m)
+      DureeLengthInMonths=$( echo ${Duree} | sed -e 's/[mM]//' )
+      ;;
+    *)
+      IGCM_debug_Exit "IGCM_config_Analyse_PeriodLength " ${Duree} " invalid DUREE : choose in *Y."
+      IGCM_debug_Verif_Exit ;;
+    esac
+  # - Determine number of day(s) in DUREE_NONPERIODIC
+    IGCM_date_GetYearMonth ${DateBegin} year month
+    DureeLengthInDays=0
+    case ${Duree} in
+    *Y|*y)
+      i=0
+      until [ $i -ge ${DureeLengthInYears} ] ; do
+        (( DureeLengthInDays = DureeLengthInDays + $( IGCM_date_DaysInYear $(( year + i )) ) ))
+        (( i = i + 1 ))
+      done
+      ;;
+    *M|*m)
+      i=0
+      until [ $i -ge ${DureeLengthInMonths} ] ; do
+        (( DureeLengthInDays = DureeLengthInDays + $( IGCM_date_DaysInMonth $year $(( month + i )) ) ))
+        (( i = i + 1 ))
+      done
+      ;;
+    esac
+    (( DureeLengthInDays = DureeLengthInDays - 1 ))
+
+  # - Build directory name
+    echo "========================================================================"
+    echo "ensemble_Ens_PERTURB_NAME = ${ensemble_Ens_PERTURB_NAME}"
+    IGCM_ensemble_CastDirectoryName ${ensemble_Ens_PERTURB_NAME} ${Duree} $year $month $StartDir
+
+  # - Determine DateEnd
+    (( DateEnd = $( IGCM_date_AddDaysToGregorianDate ${DateBegin} ${DureeLengthInDays} ) ))
+    echo ">${DateEnd}<"
+
+    echo "tout va bien 1"
+
+  # - Determine RestartDate
+    (( Offset = -1 ))
+    (( RestartDate = $( IGCM_date_AddDaysToGregorianDate ${DateBegin} ${Offset} ) ))
+
+    IGCM_debug_Print 2 "${DateBegin} => ${DateEnd} : ${StartDir}"
+
+  # -  Does $StartDir already exist ?
+    #echo "tout va bien 2" ${StartDir}
+    if [ ! -d ${SUBMIT_DIR}/${StartDir} ] ; then
+      echo "create dir"
+      IGCM_sys_Mkdir ${SUBMIT_DIR}/${StartDir}
+      IGCM_sys_Cd ${SUBMIT_DIR}/${StartDir}
+      ln -s ../../.resol .
+      ln -s ../../.libmpi .
+      IGCM_sys_Cd ${RUN_DIR}
+      echo "${DateBegin} ${DateEnd} ${StartDir}" >> CreatedDir.txt
+    fi
+    PeriodDateEnd=$( grep -m1 ${StartDir} CreatedDir.txt | cut -f2 -d\  )
+
+  # - Create directory in which to store new restart files if it does'nt already exist
+    RestartDir=$DMFDIR/IGCM_IN/${config_UserChoices_TagName}/${StartDir}
+    if [ ! -d  ${RestartDir} ] ; then
+      IGCM_sys_MkdirArchive ${RestartDir}
+    fi
+
+  # - Loop over members
+    i=0
+    while [ $i -lt ${ensemble_Ens_PERTURB_MEMBER} ] ; do
+      MemberDir="${StartDir}${Alpha[$i]}"
+      IGCM_debug_Print 3 "${MemberDir}"
+
+      JobName="Job_${MemberDir}"
+
+    # * Create directory if it doesn't exist and copy files
+      if [ ! -d  ${SUBMIT_DIR}/${StartDir}/${MemberDir} ] ; then
+        IGCM_sys_Mkdir ${SUBMIT_DIR}/${StartDir}/${MemberDir}
+        #IGCM_sys_Cp -r COMP/ PARAM/ ${StartDir}/${MemberDir}
+        IGCM_sys_Cd ${SUBMIT_DIR}/${StartDir}/${MemberDir}
+        ln -s ../../COMP
+        ln -s ../../PARAM
+        ln -s ../../POST
+        IGCM_sys_Cd ${RUN_DIR}
+        IGCM_sys_Cp config.card run.card.init ${SUBMIT_DIR}/${StartDir}/${MemberDir}
+        IGCM_sys_Cp Job_${ensemble_Ens_PERTURB_NAME} ${SUBMIT_DIR}/${StartDir}/${MemberDir}/Job_${MemberDir}
+
+        echo "cd ${StartDir}/${MemberDir}/ ; ccc_msub ${JobName} ;" >> Qsub.txt
+
+        echo "sed -e 's/PeriodNb=.*/PeriodNb=48/' ${JobName} > ${JobName}.tmp ; " >> Qsub.txt
+        echo "mv ${JobName}.tmp ${JobName} ; cd - ;" >> Qsub.txt
+      fi
+
+    # * Update files : config.card, Job_, COMP/comp.card
+      echo "${PeriodDateEnd} ? ${DateEnd}"
+      if [ ${PeriodDateEnd} -gt ${DateEnd} ] ; then
+        DateEnd=${PeriodDateEnd}
+      fi
+      IGCM_ensemble_CastFilesUpdate ${DateBegin} ${DateEnd} ${RestartDate}
+
+    # * Apply noise on restart file
+      IGCM_ensemble_CastPerturbFile
+      (( i = i + 1 ))
+    done
+
+    (( DateNum = DateNum + 1 ))
+  done
+  IGCM_debug_PopStack "IGCM_ensemble_CastNonPeriodicStarts"
+}
+
+function IGCM_ensemble_CastFilesUpdate
+{
+  IGCM_debug_PushStack "IGCM_ensemble_CastFilesUpdate"
 
 #  set -vx
@@ -20,5 +409,5 @@
   # Debug Print :
   echo
-  IGCM_debug_Print 1 "IGCM_ensemble_FilesUpdate :"
+  IGCM_debug_Print 1 "IGCM_ensemble_CastFilesUpdate :"
   echo
 
@@ -50,14 +439,14 @@
   IGCM_sys_Mv Job_${MemberDir}.tmp ${SUBMIT_DIR}/${StartDir}/${MemberDir}/Job_${MemberDir}
 
-  IGCM_debug_PopStack "IGCM_ensemble_FilesUpdate"
+  IGCM_debug_PopStack "IGCM_ensemble_CastFilesUpdate"
 }
 
-function IGCM_ensemble_DirectoryName
+function IGCM_ensemble_CastDirectoryName
 {
-  IGCM_debug_PushStack "IGCM_ensemble_DirectoryName"
+  IGCM_debug_PushStack "IGCM_ensemble_CastDirectoryName"
 
   #.. Debug Print ..
   echo
-  IGCM_debug_Print 1 "IGCM_ensemble_DirectoryName :"
+  IGCM_debug_Print 1 "IGCM_ensemble_CastDirectoryName :"
   echo
 
@@ -74,5 +463,5 @@
   case ${Duree} in
     *Y|*y)
-      siecle="$( echo $year | cut -c1-2 )" 
+      siecle="$( echo $year | cut -c1-2 )"
       siecle=$( (( $siecle - 18 )) )
       StartYear="${siecle}$( echo $year | cut -c3-4 )"
@@ -81,5 +470,5 @@
     *M|*m)
       echo $month
-      siecle="$( echo $year | cut -c1-2 )" 
+      siecle="$( echo $year | cut -c1-2 )"
       siecle=$( (( $siecle - 18 )) )
       StartYear="${siecle}$( echo $year | cut -c3-4 )"
@@ -89,14 +478,14 @@
   esac
 
-  IGCM_debug_PopStack "IGCM_ensemble_DirectoryName"
+  IGCM_debug_PopStack "IGCM_ensemble_CastDirectoryName"
 }
 
-function IGCM_ensemble_PerturbFile
+function IGCM_ensemble_CastPerturbFile
 {
-  IGCM_debug_PushStack "IGCM_ensemble_PerturbFile"
+  IGCM_debug_PushStack "IGCM_ensemble_CastPerturbFile"
 
   #.. Debug Print ..
   echo
-  IGCM_debug_Print 1 "IGCM_ensemble_PerturbFile :"
+  IGCM_debug_Print 1 "IGCM_ensemble_CastPerturbFile :"
   echo
 
@@ -149,4 +538,4 @@
   IGCM_card_WriteOption ${SUBMIT_DIR}/${StartDir}/${MemberDir}/config.card ${PerturbComp} RestartPath    ${RestartDir}/
 
-  IGCM_debug_PopStack "IGCM_ensemble_PerturbFile"
+  IGCM_debug_PopStack "IGCM_ensemble_CastPerturbFile"
 }
