Index: /trunk/libIGCM/libIGCM_ensemble/libIGCM_ensemble.ksh
===================================================================
--- /trunk/libIGCM/libIGCM_ensemble/libIGCM_ensemble.ksh	(revision 737)
+++ /trunk/libIGCM/libIGCM_ensemble/libIGCM_ensemble.ksh	(revision 738)
@@ -202,4 +202,8 @@
         IGCM_sys_Cp Job_${ensemble_Ens_PERTURB_NAME} ${SUBMIT_DIR}/${StartDir}/${MemberDir}/${JobName}
 
+        ##############################
+        # TO BE A SYS FUNCTION BEGIN #
+        ##############################
+
         # Dump command to be lauched in Qsub.txt
         echo "cd ${StartDir}/${MemberDir}/ ;"  >> Qsub.txt
@@ -207,4 +211,8 @@
         echo "mv ${JobName}.tmp ${JobName} ; " >> Qsub.txt
         echo "ccc_msub ${JobName} ; cd -"      >> Qsub.txt
+
+        ############################
+        # TO BE A SYS FUNCTION END #
+        ############################
 
       fi
@@ -311,4 +319,8 @@
         IGCM_sys_Cp Job_${ensemble_Ens_PERTURB_NAME} ${SUBMIT_DIR}/${StartDir}/${MemberDir}/Job_${MemberDir}
 
+        ##############################
+        # TO BE A SYS FUNCTION BEGIN #
+        ##############################
+
         # Dump command to be lauched in Qsub.txt
         echo "cd ${StartDir}/${MemberDir}/ ;"  >> Qsub.txt
@@ -316,4 +328,9 @@
         echo "mv ${JobName}.tmp ${JobName} ; " >> Qsub.txt
         echo "ccc_msub ${JobName} ; cd -"      >> Qsub.txt
+
+        ############################
+        # TO BE A SYS FUNCTION END #
+        ############################
+
       fi
 
@@ -338,6 +355,4 @@
 {
   IGCM_debug_PushStack "IGCM_ensemble_CastFilesUpdate"
-
-#  set -vx
 
   # Debug Print :
@@ -365,5 +380,8 @@
   done
   # ==> Job
-  # SD : to be encapsulated in a sys function
+  ##############################
+  # TO BE A SYS FUNCTION BEGIN #
+  ##############################
+
   sed -e "s/\(#MSUB -r \)[A-Za-z0-9]*\( *#.*\)/\1 ${MemberDir} \2/"           \
       -e "s/\(#MSUB -o Script_Output_\)[A-Za-z0-9]*\(\.*\)/\1${MemberDir}\2/" \
@@ -372,4 +390,8 @@
   IGCM_sys_Mv Job_${MemberDir}.tmp ${SUBMIT_DIR}/${StartDir}/${MemberDir}/Job_${MemberDir}
 
+  ############################
+  # TO BE A SYS FUNCTION END #
+  ############################
+
   IGCM_debug_PopStack "IGCM_ensemble_CastFilesUpdate"
 }
@@ -383,6 +405,4 @@
   IGCM_debug_Print 1 "IGCM_ensemble_CastDirectoryName :"
   echo
-
-#  set -vx
 
   Name=$1
@@ -422,11 +442,7 @@
   #.. FileIn ? => RestartDate ..
   DirIn="${ensemble_Ens_PERTURB_INITPATH}/${ensemble_Ens_PERTURB_INITFROM}/${PerturbComp}/Restart"
-  FileIn="${PerturbComp}_${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_${PerturbFile}.nc"
+  DirInTar="${ensemble_Ens_PERTURB_INITPATH}/${ensemble_Ens_PERTURB_INITFROM}/RESTART"
+  FileIn="${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_${PerturbFile}"
   DirOut="${RestartDir}/${MemberDir}/${PerturbComp}/Restart"
-#  FileOut="${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_${PerturbFile}_${MemberDir}.nc"
-#  FileOut=${FileIn}
-  FileOut="${MemberDir}_${RestartDate}_${PerturbFile}.nc"
-  IGCM_debug_Print 1 "FileIn  = ${DirIn}/${FileIn}"
-  IGCM_debug_Print 1 "FileOut = ${DirOut}/${FileOut}"
 
   # * Create member restart directory
@@ -437,20 +453,89 @@
   fi
 
-  IGCM_sys_TestFileArchive ${DirOut}/${FileOut}
+  FileOut="${MemberDir}_${RestartDate}_${PerturbFile}"
+  IGCM_debug_Print 1 "FileOut = ${DirOut}/${FileOut}.nc"
+
+  #IGCM_debug_Print 1 "FileIn  = ${DirIn}/${FileIn}"
+
+  IGCM_sys_TestFileArchive ${DirOut}/${FileOut}.nc
   RET=$?
   if [ $RET -gt 0 ] ; then
 
-#    ${PerturbExe} ${DirIn}/${FileIn} ${DirOut}/${FileOut} \
-#                  ${PerturbVar} ${PerturbAmp}
-#    IGCM_sys_Cp ${DirIn}/${FileIn} ${DirOut}/${FileOut}
-#    IGCM_sys_Chmod u+w ${DirOut}/${FileOut}
-#    ${PerturbExe} ${DirOut}/${FileOut} ${PerturbVar} ${PerturbAmp}
-
-    IGCM_sys_Get ${DirIn}/${FileIn} ${RUN_DIR}/${FileOut}
-    IGCM_sys_Chmod 744 ${RUN_DIR}/${FileOut}
-
-    IGCM_debug_Print 1 "${PerturbExe} ${RUN_DIR}/${FileOut} ${PerturbVar} ${PerturbAmp}"
+    # * Look for the restart file we apply the noise to
+
+    ##########################
+    # TO BE A FUNCTION BEGIN #
+    ##########################
+
+    if [ $( IGCM_sys_TestFileBuffer ${DirIn}/${FileIn}*.nc ; echo $? ) = 0 ] ; then
+      IGCM_debug_Print 3 "Buffered restart"
+      Buffered=true
+      Archived=false
+      Tared=false
+      nb_restart_file=$(IGCM_sys_CountFileBuffer ${DirIn}/${FileIn}_????.nc)
+    elif [ $( IGCM_sys_TestFileArchive ${DirIn}/${FileIn}*.nc ; echo $? ) = 0 ] ; then
+      IGCM_debug_Print 3 "Archived restart"
+      Buffered=false
+      Archived=true
+      Tared=false
+      nb_restart_file=$(IGCM_sys_CountFileArchive ${DirIn}/${FileIn}_????.nc)
+    else
+      IGCM_debug_Print 3 "Tared restart"
+      Buffered=false
+      Archived=false
+      Tared=true
+      # Look for the tar file we want if we did not found it already
+      #if [ X${IsMatching} = X ] ; then
+        for PotentialTarFile in $( find ${ensemble_Ens_PERTURB_INITPATH}/${ensemble_Ens_PERTURB_INITFROM}/RESTART -name "${ensemble_Ens_PERTURB_INITFROM}_*_restart.tar" -print ) ; do
+          IsMatching=$( echo ${PotentialTarFile##*/} | sed "s:^${ensemble_Ens_PERTURB_INITFROM}_::" | sed "s:\.restart\.tar$::" | gawk -F_ -v restartdate=${RestartDate} '{if (($1 < restartdate) && ($2 >= restartdate)) {print $1"_"$2}}' )
+          if [ ! X${IsMatching} = X ] ; then
+            TarFileFound=${PotentialTarFile}
+            break
+          fi
+        done
+      #fi
+      # already specialized for CPL sstoc and flxat!
+      IGCM_debug_Print 1 "tar xvf ${TarFileFound} ${PerturbComp}_${FileIn}*.nc ${PerturbComp}_${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_flxat.nc"
+      tar xvf ${TarFileFound} ${PerturbComp}_${FileIn}*.nc ${PerturbComp}_${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_flxat.nc
+      nb_restart_file=$( IGCM_sys_CountFileBuffer ${PerturbComp}_${FileIn}_????.nc )
+    fi
+
+    if [ ${nb_restart_file} -gt 1 ] ; then
+      j=0
+      until [ $j -ge $nb_restart_file ]; do
+        j4=${j}
+        if [ X${Buffered} = Xtrue ] ; then
+          IGCM_sys_GetBuffer ${DirIn}/${FileIn}_${j4}.nc ${RUN_DIR}/${FileOut}_${j4}.nc
+        elif [ X${Archived} = Xtrue ] ; then
+          IGCM_sys_Get ${DirIn}/${FileIn}_${j4}.nc ${RUN_DIR}/${FileOut}_${j4}.nc
+        elif [ X${Tared} = Xtrue ] ; then
+          IGCM_sys_Mv ${PerturbComp}_${FileIn}_${j4}.nc ${RUN_DIR}/${FileOut}_${j4}.nc
+        fi
+        (( j=j+1 ))
+      done
+    else
+      if [ X${Buffered} = Xtrue ] ; then
+        IGCM_sys_GetBuffer ${DirIn}/${FileIn}.nc ${RUN_DIR}/${FileOut}.nc
+        # already specialized for CPL sstoc and flxat!
+        IGCM_sys_RshArchive "cd ${DirOut} ; ln -s ${DirIn}/${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_flxat.nc ${MemberDir}_${RestartDate}_flxat.nc"
+      elif [ X${Archived} = Xtrue ] ; then
+        IGCM_sys_Get ${DirIn}/${FileIn}.nc ${RUN_DIR}/${FileOut}.nc
+        # already specialized for CPL sstoc and flxat!
+        IGCM_sys_RshArchive "cd ${DirOut} ; ln -s ${DirIn}/${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_flxat.nc ${MemberDir}_${RestartDate}_flxat.nc"
+      elif [ X${Tared} = Xtrue ] ; then
+        IGCM_sys_Mv ${PerturbComp}_${FileIn}.nc ${RUN_DIR}/${FileOut}.nc
+        # already specialized for CPL sstoc and flxat!
+        IGCM_sys_Mv ${PerturbComp}_${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_flxat.nc ${DirOut}/${MemberDir}_${RestartDate}_flxat.nc
+      fi
+    fi
+
+    ########################
+    # TO BE A FUNCTION END #
+    ########################
+
+    IGCM_sys_Chmod 644 ${RUN_DIR}/${FileOut}.nc
+    IGCM_debug_Print 1 "${PerturbExe} ${RUN_DIR}/${FileOut}.nc ${PerturbVar} ${PerturbAmp}"
     echo
-    ${PerturbExe} ${RUN_DIR}/${FileOut} ${PerturbVar} ${PerturbAmp} > /dev/null 2>&1
+    ${PerturbExe} ${RUN_DIR}/${FileOut}.nc ${PerturbVar} ${PerturbAmp} > /dev/null 2>&1
 
     if [ $? -ne 0 ] ; then
@@ -458,11 +543,5 @@
       return
     fi
-
-    IGCM_sys_Put_Out ${RUN_DIR}/${FileOut} ${DirOut}/ 744
-
-#    IGCM_sys_Cd ${DirOut}
-#    ln -s ${DirIn}/${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_flxat.nc ${MemberDir}_${RestartDate}_flxat.nc
-#    IGCM_sys_Cd -
-    IGCM_sys_RshArchive "cd ${DirOut} ; ln -s ${DirIn}/${ensemble_Ens_PERTURB_INITFROM}_${RestartDate}_flxat.nc ${MemberDir}_${RestartDate}_flxat.nc"
+    IGCM_sys_Put_Out ${RUN_DIR}/${FileOut}.nc ${DirOut}/ 644
   fi
 
