Index: trunk/libIGCM/libIGCM_comp/libIGCM_comp.ksh
===================================================================
--- trunk/libIGCM/libIGCM_comp/libIGCM_comp.ksh	(revision 1165)
+++ trunk/libIGCM/libIGCM_comp/libIGCM_comp.ksh	(revision 1166)
@@ -950,4 +950,62 @@
 }
 
+
+
+#=======================================================================
+function IGCM_comp_modifyFile
+{
+#
+# syntax:     IGCM_comp_modifyFile  filein  key  [value]
+#
+# For example : IGCM_comp_modifyFile metrics_template.py case_id \'SE_${YEARS}\'
+#
+# This function is used to replace a pattern in a file for a specific variable.
+#
+# Arguments:
+# - filein : the file in run directory in which the variable should be set
+# - key    : the variable to modify
+# - value  : the value to set the key equal to
+#
+  IGCM_debug_PushStack "IGCM_comp_modifyFile"
+
+  typeset filein key value pattern
+
+  # Set local variables and test the arguments
+  if [ $# = 3 ] ; then
+    # Normal case with 3 arguments
+    filein=$1 ; key=$2 ; value=$3
+  else
+    IGCM_debug_Exit "IGCM_comp_modifyFile: Bad number of arguments."
+    IGCM_debug_PopStack "IGCM_comp_modifyFile"
+    return
+  fi
+  IGCM_debug_Print 1 "Entering IGCM_comp_modifyFile with arguments: ${filein} ${key} ${value}"
+
+  # Test if the file exist
+  if [ ! -f ${filein} ] ; then
+    IGCM_debug_Exit "IGCM_comp_modifyFile: ${filein} does not exist."
+    IGCM_debug_PopStack "IGCM_comp_modifyFile"
+    return
+  fi
+
+  # Read the line with key in the file without the comments
+  pattern=$( grep "^ *${key} *=" ${filein} | sed -e "s% *\!.*%%" )
+
+  # Verify the existance of the pattern
+  if [ X"${pattern}" = X ] ; then
+    # Variable key is not set in filein, stop.
+    IGCM_debug_Exit "IGCM_comp_modifyFile : Variable ${key} is not set in correct file. It should be set in ${filein}."
+    IGCM_debug_PopStack "IGCM_comp_modifyFile"
+    return
+  fi
+
+  # Now change key in filein
+  sed -e "s:${pattern}:${key}=${value}:" ${filein} > ${filein}.tmp
+  IGCM_debug_Print 1 "IGCM_comp_modifyFile: In ${filein} set ${key}=${value}"
+  \mv ${filein}.tmp ${filein}
+  
+  IGCM_debug_PopStack "IGCM_comp_modifyFile"
+}
+
 #=======================================================================
 function IGCM_comp_modifyDefFile
@@ -977,4 +1035,7 @@
   IGCM_debug_PushStack "IGCM_comp_modifyDefFile"
 
+  typeset type filein key value
+  typeset filelist nb_occ modify
+
   # Set local variables and test the arguments
   if [ $# = 4 ] ; then
@@ -989,8 +1050,8 @@
     return
   fi
-  IGCM_debug_Print 1 "Entering IGCM_comp_modifyDefFile with arguments: $type $filein $key $value"
+  IGCM_debug_Print 1 "Entering IGCM_comp_modifyDefFile with arguments: ${type} ${filein} ${key} ${value}"
 
   # Test if first argument is correct
-  if [ $type != blocker ] && [ $type != nonblocker ] && [ $type != force ] ; then
+  if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then
     IGCM_debug_Exit "IGCM_comp_modifyDefFile: Error in first argument must be blocker, nonblocker or force"
     IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
@@ -1000,5 +1061,5 @@
   # Test if the file exist
   if [ ! -f ${filein} ] ; then
-    IGCM_debug_Exit "IGCM_comp_modifyDefFile: $filein does not exist."
+    IGCM_debug_Exit "IGCM_comp_modifyDefFile: ${filein} does not exist."
     IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
     return
@@ -1009,9 +1070,9 @@
 
   # Count number of occurances for the key in all files
-  nb_occ=$( grep -w $key $filelist |grep -v "#"  |wc -l )
+  nb_occ=$( grep -w ${key} ${filelist} | grep -v "#"  | wc -l )
 
   # Test if key is set several times
-  if [ $nb_occ -gt 1 ] ; then
-    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in $filein: Variable=$key is set ${nb_occ} times"
+  if [ ${nb_occ} -gt 1 ] ; then
+    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in ${filein}: Variable=${key} is set ${nb_occ} times"
     IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
     return
@@ -1019,35 +1080,35 @@
 
   # Treatement according to different cases
-  if [ $nb_occ -eq 0 ] && [ $type = blocker ] ; then
+  if [ ${nb_occ} -eq 0 ] && [ ${type} = blocker ] ; then
     # Stop if the key is never set and the function is blocker
-    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in $filein: Variable=$key has been removed but this function is blocker. "
-    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: restore $filein for variable $key."
-  elif [ $nb_occ -eq 0 ] && [ $type = nonblocker ] ; then
+    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in ${filein}: Variable=${key} has been removed but this function is blocker. "
+    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: restore ${filein} for variable ${key}."
+  elif [ ${nb_occ} -eq 0 ] && [ ${type} = nonblocker ] ; then
     # The key is not set but it is a nonblocker call so nothing is done.
-    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key is not set in $filein. This is a nonblocker call so nothing is done."
-    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: Default value for $key from the model will be used."
+    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key} is not set in ${filein}. This is a nonblocker call so nothing is done."
+    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: Default value for ${key} from the model will be used."
     modify=no
-  elif [ $( grep $key $filein | grep -v "\#"  |wc -l ) = 0 ] ; then
+  elif [ $( grep ${key} ${filein} | grep -v "\#"  |wc -l ) = 0 ] ; then
     # Variable key is not set in filein, stop.
-    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Variable $key is not set in correct file. It should be set in $filein."
+    IGCM_debug_Exit "IGCM_comp_modifyDefFile : Variable ${key} is not set in correct file. It should be set in ${filein}."
   fi
 
   # Check if AUTO is set in the filein on the same line as the key variable
-  if [ $( grep -w $key $filein | grep -v "\#" | grep AUTO | wc -l ) = 1 ] ; then
+  if [ $( grep -w ${key} ${filein} | grep -v "\#" | grep AUTO | wc -l ) = 1 ] ; then
     # Modification will be done for all cases
     modify=yes
   else
     # The variable was not set to AUTO
-    if [ $type = blocker ] ; then
+    if [ ${type} = blocker ] ; then
       # Exit because this is a blocker call
-      IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable $key cannot be modified. It should be set to AUTO."
+      IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable ${key} cannot be modified. It should be set to AUTO."
       return
-    elif [ $type = nonblocker ] ; then
+    elif [ ${type} = nonblocker ] ; then
       # Do nothing. Suppose that the user did set the variable correct
-      IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key is set by the user. Nothing done."
+      IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key} is set by the user. Nothing done."
       modify=no
-    elif [ $type = force ] ; then
+    elif [ ${type} = force ] ; then
       # Force modification
-      IGCM_debug_Print 1 "IGCM_comp_modifyDefFile : Variabl=$key was not set to AUTO. Modification will be forced."
+      IGCM_debug_Print 1 "IGCM_comp_modifyDefFile : Variabl=${key} was not set to AUTO. Modification will be forced."
       modify=yes
     fi
@@ -1055,16 +1116,16 @@
 
   # Do the modifcation now
-  if [ $modify = yes ] ; then
+  if [ ${modify} = yes ] ; then
 
     # For option DEFAULT, read default value from file.
-    if [ X"$value" = XDEFAULT ] || [ X"$value" = X ] ; then
+    if [ X"${value}" = XDEFAULT ] || [ X"${value}" = X ] ; then
       # Case to set DEFAULT value
       # Read default value from filein
-      value=$( grep $key $filein | grep -v "\#" | awk  -F"DEFAULT *=" '{print $2}')
-
-      if [ X"$value" = X ] ; then
-        IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable $key needs a DEFAULT value in $filein."
-        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: The syntax in $filein should be:"
-        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key=_AUTO_:DEFAULT=def_value"
+      value=$( grep ${key} ${filein} | grep -v "\#" | awk  -F"DEFAULT *=" '{print $2}')
+
+      if [ X"${value}" = X ] ; then
+        IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable ${key} needs a DEFAULT value in ${filein}."
+        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: The syntax in ${filein} should be:"
+        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key}=_AUTO_:DEFAULT=def_value"
         IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
         return
@@ -1073,7 +1134,7 @@
 
     # Now change key in filein
-    sed -e "s/^${key}\ *=.*/${key}= ${value}/" $filein > $filein.tmp
-    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: In $filein set $key=$value"
-    \mv $filein.tmp $filein
+    sed -e "s/^${key}\ *=.*/${key}= ${value}/" ${filein} > ${filein}.tmp
+    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: In ${filein} set ${key}=${value}"
+    \mv ${filein}.tmp ${filein}
   fi
   IGCM_debug_PopStack "IGCM_comp_modifyDefFile"
@@ -1107,4 +1168,6 @@
   IGCM_debug_PushStack "IGCM_comp_modifyNamelist"
 
+  typeset type filein key value pattern modify
+
   # Set local variables and test the arguments
   if [ $# = 4 ] ; then
@@ -1119,8 +1182,8 @@
     return
   fi
-  IGCM_debug_Print 1 "Entering IGCM_comp_modifyNamelist with arguments: $type $filein $key $value"
+  IGCM_debug_Print 1 "Entering IGCM_comp_modifyNamelist with arguments: ${type} ${filein} ${key} ${value}"
 
   # Test if first argument is correct
-  if [ $type != blocker ] && [ $type != nonblocker ] && [ $type != force ] ; then
+  if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then
     IGCM_debug_Exit "IGCM_comp_modifyNamelist: Error in first argument must be blocker, nonblocker or force"
     IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
@@ -1130,5 +1193,5 @@
   # Test if the file exist
   if [ ! -f ${filein} ] ; then
-    IGCM_debug_Exit "IGCM_comp_modifyNamelist: $filein does not exist."
+    IGCM_debug_Exit "IGCM_comp_modifyNamelist: ${filein} does not exist."
     IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
     return
@@ -1136,10 +1199,10 @@
 
   # Read the line with key in the file without the comments
-  pattern=$( grep "^ *$key *=" $filein | sed -e "s% *\!.*%%" )
+  pattern=$( grep "^ *${key} *=" ${filein} | sed -e "s% *\!.*%%" )
 
   # Verify the existance of the pattern
   if [ X"$pattern" = X ] ; then
     # Variable key is not set in filein, stop.
-    IGCM_debug_Exit "IGCM_comp_modifyNamelist : Variable $key is not set in correct file. It should be set in $filein."
+    IGCM_debug_Exit "IGCM_comp_modifyNamelist : Variable ${key} is not set in correct file. It should be set in ${filein}."
     IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
     return
@@ -1152,16 +1215,16 @@
   else
     # The variable was not set to AUTO
-    if [ $type = blocker ] ; then
+    if [ ${type} = blocker ] ; then
       # Exit because this is a blocker call
-      IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable $key cannot be modified. It should be set to AUTO."
+      IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable ${key} cannot be modified. It should be set to AUTO."
       IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
       return
-    elif [ $type = nonblocker ] ; then
+    elif [ ${type} = nonblocker ] ; then
       # Do nothing. Suppose that the user did set the variable correct
-      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: $key is set by the user. Nothing done."
+      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: ${key} is set by the user. Nothing done."
       modify=no
-    elif [ $type = force ] ; then
+    elif [ ${type} = force ] ; then
       # Force modification
-      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist : Variabl=$key was not set to AUTO. Modification will be forced."
+      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist : Variabl=${key} was not set to AUTO. Modification will be forced."
       modify=yes
     fi
@@ -1169,16 +1232,16 @@
 
   # Do the modifcation now
-  if [ $modify = yes ] ; then
+  if [ ${modify} = yes ] ; then
 
     # For option DEFAULT, read default value from file.
-    if [ X"$value" = XDEFAULT ] || [ X"$value" = X ] ; then
+    if [ X"${value}" = XDEFAULT ] || [ X"${value}" = X ] ; then
       # Case to set DEFAULT value
       # Read default value from filein
       value=$( echo $pattern | awk  -F"DEFAULT *=" '{print $2}')
 
-      if [ X"$value" = X ] ; then
-        IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable $key needs a DEFAULT value in $filein."
-        IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: The syntax in $filein should be:"
-        IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: $key=_AUTO_:DEFAULT=def_value"
+      if [ X"${value}" = X ] ; then
+        IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable ${key} needs a DEFAULT value in ${filein}."
+        IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: The syntax in ${filein} should be:"
+        IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: ${key}=_AUTO_:DEFAULT=def_value"
         IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
         return
@@ -1187,7 +1250,7 @@
 
     # Now change key in filein
-    sed -e "s/${pattern}/       $key=${value}/" $filein > $filein.tmp
-    IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: In $filein set $key=$value"
-    \mv $filein.tmp $filein
+    sed -e "s/${pattern}/       ${key}=${value}/" ${filein} > ${filein}.tmp
+    IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: In ${filein} set ${key}=${value}"
+    \mv ${filein}.tmp ${filein}
   fi
   IGCM_debug_PopStack "IGCM_comp_modifyNamelist"
@@ -1221,4 +1284,6 @@
   IGCM_debug_PushStack "IGCM_comp_modifyXmlFile"
 
+  typeset type filein keyid keyattrib value modify
+
   # Set local variables and test the arguments
   if [ $# = 5 ] ; then
@@ -1230,8 +1295,8 @@
     return
   fi
-  IGCM_debug_Print 1 "Entering IGCM_comp_modifyXmlFile with arguments: type=$type file=$filein, id=$keyid attribute=$keyattrib, value=$value"
+  IGCM_debug_Print 1 "Entering IGCM_comp_modifyXmlFile with arguments: type=${type} file=${filein}, id=${keyid} attribute=${keyattrib}, value=${value}"
 
   # Test if first argument is correct
-  if [ $type != blocker ] && [ $type != nonblocker ] && [ $type != force ] ; then
+  if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then
     IGCM_debug_Exit "IGCM_comp_modifyXmlFile: Error in first argument must be blocker, nonblocker or force"
     IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
@@ -1241,5 +1306,5 @@
   # Test if the file exist
   if [ ! -f ${filein} ] ; then
-    IGCM_debug_Exit "IGCM_comp_modifyXmlFile: $filein does not exist."
+    IGCM_debug_Exit "IGCM_comp_modifyXmlFile: ${filein} does not exist."
     IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
     return
@@ -1247,7 +1312,7 @@
 
   # Test if keyid is set in filein, otherwise exit
-  if [ $( grep -w $keyid $filein | wc -l ) = 0 ] ; then
+  if [ $( grep -w ${keyid} ${filein} | wc -l ) = 0 ] ; then
     # Variable key is not set in filein, stop.
-    IGCM_debug_Exit "IGCM_comp_modifyXmlFile : $keyid is not set in the file. Bad syntax of $filein file."
+    IGCM_debug_Exit "IGCM_comp_modifyXmlFile : ${keyid} is not set in the file. Bad syntax of ${filein} file."
     IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
     return
@@ -1255,20 +1320,20 @@
 
   # Check if AUTO is set on the same line as keyid and keyattrib
-  if [  $( grep -w $keyid $filein | grep AUTO | wc -l ) = 1 ] ; then
+  if [  $( grep -w ${keyid} ${filein} | grep AUTO | wc -l ) = 1 ] ; then
     # Modifification will be done
     modify=yes
   else
-    if [ $type = blocker ] ; then
+    if [ ${type} = blocker ] ; then
       # Exit, the variable must be set to AUTO
-      IGCM_debug_Exit "IGCM_comp_modifyXmlFile : blocker function. The $keyattrib for $keyid must be set to AUTO in $filein."
+      IGCM_debug_Exit "IGCM_comp_modifyXmlFile : blocker function. The ${keyattrib} for ${keyid} must be set to AUTO in ${filein}."
       IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
       return
-    elif [ $type = nonblocker ] ; then
+    elif [ ${type} = nonblocker ] ; then
       # Nothing will be done
       IGCM_debug_Print 1 "Nonblocker nothing is done for ${filein}, id=${keyid} and attribute ${keyattrib}"
       modify=no
-    elif [ $type = force ] ; then
+    elif [ ${type} = force ] ; then
       # Force modification
-      IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile : Attribute=$keyattrib for id=$keyid was not set to AUTO. Modification will be forced."
+      IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile : Attribute=${keyattrib} for id=${keyid} was not set to AUTO. Modification will be forced."
       modify=yes
     fi
@@ -1276,22 +1341,22 @@
 
   # Do the modifcation now
-  if [ $modify = yes ] ; then
-    if [ $keyattrib = NONE ] ; then
+  if [ ${modify} = yes ] ; then
+    if [ ${keyattrib} = NONE ] ; then
       # Case to modify the variable itself
       IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting the variable=${value}"
-      sed -e "s/\(<[^\"]*\"${keyid}\".*>\)\([^<]*\)\(<[^>]*\)/\1${value}\3/" $filein > $filein.tmp
+      sed -e "s/\(<[^\"]*\"${keyid}\".*>\)\([^<]*\)\(<[^>]*\)/\1${value}\3/" ${filein} > ${filein}.tmp
     else
       # Check if keyattrib is set on the same line as keyid
-      if [  $( grep -w $keyid $filein | grep $keyattrib | wc -l ) = 1 ] ; then
+      if [  $( grep -w ${keyid} ${filein} | grep ${keyattrib} | wc -l ) = 1 ] ; then
         # Case to modify the attribute value
         IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting attribute to ${keyattrib}=${value}"
-        sed -e "/id=\"${keyid}\"/s/\(${keyattrib}=\"\)[^\"]*\(\"\)/\1${value}\2/" $filein > $filein.tmp
+        sed -e "/id=\"${keyid}\"/s/\(${keyattrib}=\"\)[^\"]*\(\"\)/\1${value}\2/" ${filein} > ${filein}.tmp
       else
         # Case to add the attribute and its value
         IGCM_debug_Print 1 "Now add in ${filein} for id=${keyid} the attribute ${keyattrib} to the value ${value}"
-        sed -e "/id=\"${keyid}\"/s/\/>/ ${keyattrib}=\"${value}\"\/>/" $filein > $filein.tmp
+        sed -e "/id=\"${keyid}\"/s/\/>/ ${keyattrib}=\"${value}\"\/>/" ${filein} > ${filein}.tmp
       fi
     fi
-    \mv $filein.tmp $filein
+    \mv ${filein}.tmp ${filein}
   fi
   IGCM_debug_PopStack "IGCM_comp_modifyXmlFile"
@@ -1693,5 +1758,5 @@
   # Append the sync call and the copy sequence to the IGCM_FlushRebuild function if needed
   if [ ${#rebuildedActionsList[*]} -ne 0 ] ; then
-    echo "IGCM_sys_sync         " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
+    echo "IGCM_sys_sync              " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh
     i=0
     until [ ${i} -ge ${#rebuildedActionsList[*]} ]; do
