Index: branches/libIGCM_pack/AA_pack_output
===================================================================
--- branches/libIGCM_pack/AA_pack_output	(revision 1587)
+++ branches/libIGCM_pack/AA_pack_output	(revision 1588)
@@ -155,4 +155,8 @@
 DEBUG_debug=${DEBUG_debug:=false}
 
+#D- Surpack_mode : to pack existing packs output files 
+#D- Default : n
+surpack_mode=n
+
 ########################################################################
 
@@ -231,13 +235,4 @@
 IGCM_debug_Verif_Exit
 
-IGCM_debug_Print 1 "Check coherence between PackFrequency and PeriodLength"
-IGCM_post_CheckModuloFrequency PeriodPack config_UserChoices_PeriodLength NbPeriodPerFrequency
-# ------------------------------------------------------------------
-# Test if all was right before proceeding further
-# ------------------------------------------------------------------
-IGCM_debug_Verif_Exit
-
-IGCM_debug_Print 1 "We must process ${NbPeriodPerFrequency} files for each pack"
-
 # Init loop
 date_begin_pack=${DateBegin}
@@ -248,12 +243,17 @@
 IGCM_debug_PrintVariables 3 date_end_simulation
 
-while [ ${date_begin_pack} -le ${date_end_simulation} ] ; do
-
-  IGCM_debug_PrintVariables 3 number_pack
-  DaysTemp=$( IGCM_date_DaysInCurrentPeriod ${date_begin_pack} ${PeriodPack} )
-  date_end_pack=$( IGCM_date_AddDaysToGregorianDate ${date_begin_pack} $(( ${DaysTemp} - 1 )) )
-
-  for comp in ${config_ListOfComponents[*]} ; do
+# Loop over components
+for comp in ${config_ListOfComponents[*]} ; do
+
+    IGCM_card_DefineArrayFromOption ${SUBMIT_DIR}/config.card ListOfComponents ${comp}
+    eval compname=\${config_ListOfComponents_${comp}[0]} > /dev/null 2>&1
+    card=${SUBMIT_DIR}/COMP/${compname}.card
+    #	  
+    if [ X${surpack_mode} = Xy ] ; then
+	dirList=$( find ${R_SAVE}/${comp}/Output -maxdepth 1 -mindepth 1 -type d )  
+    else
     dirList=$( find ${R_BUFR}/${comp}/Output -maxdepth 1 -mindepth 1 -type d )
+    fi
+    # Loop over directories containing output files
     for dir in ${dirList} ; do
       # dirID is like ATM.Output.MO
@@ -266,10 +266,53 @@
       # Loop over the file type and pack them when in between date_begin_pack and date_end_pack
       for myType in ${fileType} ; do
+		# Read comp.card and find specific pack period for each type of file
+      		IGCM_card_DefineArrayFromOption ${card} OutputFiles List
+      		ListFilesName=${compname}_OutputFiles_List
+       		eval FileName0=\${${ListFilesName}[0]} > /dev/null 2>&1
+
+       		if [ X${FileName0} != X${NULL_STR} ] ; then
+       		    eval NbFiles=\${#${ListFilesName}[@]} > /dev/null 2>&1
+		    
+		    (( i=0 ))
+		    until [ $i -ge $NbFiles ]; do
+			(( i_ = i+1 ))
+			eval file_out_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
+			eval file_out=${file_out_}
+			(( i_ = i+3 ))
+			eval Testvar_=\${${ListFilesName}[$i_]} > /dev/null 2>&1
+			eval Testvar=${Testvar_}
+			if [[ ${Testvar} =~ [0-9][DMY]$ ]] ; then
+			    eval PeriodPack_FileType=${Testvar}
+			    ((i = i+4))
+			else
+			    PeriodPack_FileType=${PeriodPack}
+			    ((i = i+3))
+			fi
+			if [[ ${file_out} =~ ${myType} ]] ; then
+			    break
+			fi
+		    done
+		fi
+		IGCM_debug_Print 1 "Check coherence between PackFrequency and PeriodLength"
+		IGCM_post_CheckModuloFrequency PeriodPack_FileType config_UserChoices_PeriodLength NbPeriodPerFrequency
+		# ------------------------------------------------------------------
+                # Test if all was right before proceeding further
+                # ------------------------------------------------------------------
+                IGCM_debug_Verif_Exit
+
+		# Loop over period pack
+		while [ ${date_begin_pack} -le ${date_end_simulation} ] ; do
+		    DaysTemp=$( IGCM_date_DaysInCurrentPeriod ${date_begin_pack} ${PeriodPack_FileType} )
+		    date_end_pack=$( IGCM_date_AddDaysToGregorianDate ${date_begin_pack} $(( ${DaysTemp} - 1 )) )		    
+		    #
         grep ${myType} liste_files.${dirID}.txt > liste_files.${dirID}.${myType}.txt
         nbfile=0
+		    # If no surpack, no need to demigrate.
+		    if [ X${surpack_mode} = Xn ] ; then
         for file in $( gawk '{print $11}' liste_files.${dirID}.${myType}.txt ); do
           extract_date_file=$( echo ${file}  | sed -e "s/.*${JobName}_[0-9]*_//" )
           date_file=$( echo ${extract_date_file} | sed 's/\([0-9]\{8\}\)_.*$/\1/g' )
           # echo pack nÂ°${number_pack}  ${date_file} ${date_begin_pack} ${date_end_pack}
+			# Ajouter un test sur la date
           if [ ${date_file} -le ${date_end_pack} ] && [ ${date_file} -ge ${date_begin_pack} ] ; then
             echo ${file} >> liste_pack_${myType}_${date_begin_pack}_${date_end_pack}.txt
@@ -278,4 +321,28 @@
           fi
         done
+		    else
+			for file in $( gawk '{print $11}' liste_files.${dirID}.${myType}.txt ); do
+			    extract_date_file=$( echo ${file}  | sed -e "s/.*${JobName}_[0-9]*_//" )
+			    date_file=$( echo ${extract_date_file} | sed 's/\([0-9]\{8\}\)_.*$/\1/g' )
+			# echo pack nÂ°${number_pack}  ${date_file} ${date_begin_pack} ${date_end_pack}
+			# Ajouter un test sur la date
+			    if [ ${date_file} -le ${date_end_pack} ] && [ ${date_file} -ge ${date_begin_pack} ] ; then
+				echo ${file} >> liste_pack_${myType}_${date_begin_pack}_${date_end_pack}.txt
+				(( nbfile = nbfile + 1 ))
+			    fi
+			done
+			# If surpack_mode, archive demigration is needed on TGCC
+#-Q- irene-amd			ccc_hsm get $( cat liste_pack_${myType}_${date_begin_pack}_${date_end_pack}.txt )
+#-Q- irene			ccc_hsm get $( cat liste_pack_${myType}_${date_begin_pack}_${date_end_pack}.txt )
+			for file in $( gawk '{print $11}' liste_files.${dirID}.${myType}.txt ); do
+			    extract_date_file=$( echo ${file}  | sed -e "s/.*${JobName}_[0-9]*_//" )
+			    date_file=$( echo ${extract_date_file} | sed 's/\([0-9]\{8\}\)_.*$/\1/g' )
+			# echo pack nÂ°${number_pack}  ${date_file} ${date_begin_pack} ${date_end_pack}
+			# Ajouter un test sur la date
+			    if [ ${date_file} -le ${date_end_pack} ] && [ ${date_file} -ge ${date_begin_pack} ] ; then
+				ncdump -h ${file} | grep -E 'float|double' | cut -f 1 -d '(' | cut -f 2 -d ' ' >> liste_variables_${myType}_${date_begin_pack}_${date_end_pack}.txt
+			    fi
+			done
+		    fi
 
         if [ ${nbfile} = 0 ] ; then
@@ -283,7 +350,12 @@
           IGCM_debug_Print 1 "We should have found ${NbPeriodPerFrequency} files"
           IGCM_debug_Print 1 "As some files can be produced only for some selected period we consider we can move to the next file type"
+			date_begin_pack=$( IGCM_date_AddDaysToGregorianDate ${date_end_pack} 1 )
+			continue
+		    fi
+		    if [ X${surpack_mode} = Xy ] &&  [ ${nbfile} = 1 ] ; then
+			IGCM_debug_Print 1 "Only 1 file to pack in surpack mode, nothing to be done"
+			date_begin_pack=$( IGCM_date_AddDaysToGregorianDate ${date_end_pack} 1 )
           continue
         fi
-
         # Select list of variables to work with
         list_var=$( cat liste_variables_${myType}_${date_begin_pack}_${date_end_pack}.txt | sort | uniq -c | awk -v nbfile=$nbfile '{if ($1 != nbfile) {print $2}}' | paste -s -d ',' )
@@ -291,9 +363,12 @@
         # Create packed files
         IGCM_debug_Print 1 "Ncrcat ongoing for ${dir} and ${myType}"
+		    if [ X${surpack_mode} = Xn ] ; then
         if [ ! ${nbfile} = ${NbPeriodPerFrequency} ] ; then
+#			    if [ ${date_end_pack} -le ${date_end_simulation} ] ; then
           IGCM_debug_Print 1 "Number of files to process is not equal to what it should be"
           IGCM_debug_Print 1 "We found ${nbfile} files and it should have been ${NbPeriodPerFrequency} files"
           IGCM_debug_Exit "ERROR in number of files to process. STOP HERE INCLUDING THE COMPUTING JOB"
           IGCM_debug_Verif_Exit
+			fi
         fi
         output=${JobName}_${date_begin_pack}_${date_end_pack}_${myType}
@@ -309,5 +384,9 @@
         IGCM_debug_Verif_Exit
         # Save it
+		    if [ X${surpack_mode} = Xy ] ; then
+			IGCM_sys_Put_Out ${output} ${R_SAVE}/$( echo $dir | sed "s:${R_SAVE}/::" )/${output}
+		    else
         IGCM_sys_Put_Out ${output} ${R_SAVE}/$( echo $dir | sed "s:${R_BUFR}/::" )/${output}
+		    fi
         # Clean file produced by ncrcat
         IGCM_sys_Rm ${output}
@@ -322,13 +401,12 @@
         IGCM_debug_Print 1 "Ncrcat and cleaning done for ${dir} and ${myType}"
         echo
+		    # Add 1 day to date_end_pack to have the new date_begin_pack
+		    date_begin_pack=$( IGCM_date_AddDaysToGregorianDate ${date_end_pack} 1 )
       done
     done
   done
-  (( number_pack = number_pack + 1 ))
-  # Add 1 day to date_end_pack to have the new date_begin_pack
-  date_begin_pack=$( IGCM_date_AddDaysToGregorianDate ${date_end_pack} 1 )
 done
-
 # Flush post-processing submission
+if [ X${surpack_mode} = Xn ] && [ ${StandAlone} = false ] ; then
 if [ -f ${R_BUFR}/FlushPost_${DateEnd}.ksh ] ; then
   . ${R_BUFR}/FlushPost_${DateEnd}.ksh
@@ -336,5 +414,5 @@
   #IGCM_sys_Rm -f ${R_BUFR}/FlushPost_${DateEnd}.ksh
 fi
-
+fi
 # Clean RUN_DIR_PATH (necessary for cesium and titane only)
 IGCM_sys_RmRunDir -Rf ${RUN_DIR_PATH}
