Changeset 1085


Ignore:
Timestamp:
10/10/14 15:26:31 (10 years ago)
Author:
jgipsl
Message:

Added function IGCM_comp_modifyNamelist
See #222

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libIGCM/libIGCM_comp/libIGCM_comp.ksh

    r1081 r1085  
    10761076} 
    10771077 
     1078 
     1079#======================================================================= 
     1080function IGCM_comp_modifyNamelist 
     1081{ 
     1082# 
     1083# syntax:     IGCM_comp_modifyNamelist  type  filein  key  [value] 
     1084# 
     1085# For example : IGCM_comp_modifyNamelist blocker run.def day_step 1200 
     1086# 
     1087# This function is used to modify a parameter file for a specific variable. 
     1088# The file must be a ".def" file, i.e. with IOIPSL parameter file syntax. 
     1089# This function can be used in the comp.driver files for the components. 
     1090# 
     1091# Arguments: 
     1092# - type   : first argument must be blocker, nonblocker or force 
     1093#            For "blocker" case, the variable must be attributed the keyworld AUTO 
     1094#            otherwise this function will exit. 
     1095#            For "nonblocker" case, the user can remove or modify the variable. For 
     1096#            this case, as long as AUTO is not set, no modification will be done. 
     1097#            For "force" case, the variable will be modified even if it is not set to AUTO 
     1098# - filein : the file in run directory of .def type in which the variable should be set 
     1099# - key    : the variable to modify 
     1100# - value  : the value to set the key equal to, optional. If value is not set or if 
     1101#            value=DEFAULT, then a default value must be given in filein using syntax : 
     1102#            key= AUTO : DEFAULT=def_value 
     1103# 
     1104  IGCM_debug_PushStack "IGCM_comp_modifyNamelist" 
     1105 
     1106  # Set local variables and test the arguments 
     1107  if [ $# = 4 ] ; then 
     1108    # Normal case with 4 arguments 
     1109    type=$1 ; filein=$2 ; key=$3 ; value=$4 
     1110  elif [ $# = 3 ] ; then 
     1111    # Normal case with 3 arguments 
     1112    type=$1 ; filein=$2 ;       key=$3; value="DEFAULT" 
     1113  else 
     1114    IGCM_debug_Exit "IGCM_comp_modifyNamelist: Bad number of arguments." 
     1115    IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 
     1116    return 
     1117  fi 
     1118  IGCM_debug_Print 1 "Entering IGCM_comp_modifyNamelist with arguments: $type $filein $key $value" 
     1119 
     1120 
     1121  # Test if first argument is correct 
     1122  if [ $type != blocker ] && [ $type != nonblocker ] && [ $type != force ] ; then 
     1123    IGCM_debug_Exit "IGCM_comp_modifyNamelist: Error in first argument must be blocker, nonblocker or force" 
     1124    IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 
     1125    return 
     1126  fi 
     1127 
     1128  # Test if the file exist 
     1129  if [ ! -f ${filein} ] ; then 
     1130    IGCM_debug_Exit "IGCM_comp_modifyNamelist: $filein does not exist." 
     1131    IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 
     1132    return 
     1133  fi 
     1134 
     1135  # Read the line with key in the file without the comments 
     1136  pattern=$( grep "^ *$key *=" $filein | sed -e "s% *\!.*%%" ) 
     1137   
     1138  # Verify the existance of the pattern 
     1139  if [ X"$pattern" = X ] ; then 
     1140      # Variable key is not set in filein, stop. 
     1141      IGCM_debug_Exit "IGCM_comp_modifyNamelist : Variable $key is not set in correct file. It should be set in $filein." 
     1142      IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 
     1143      return 
     1144  fi 
     1145   
     1146  # Check if the variable is set to AUTO in the filein 
     1147  if [ $( echo $pattern | grep AUTO | wc -l ) = 1 ] ; then 
     1148      # Modification will be done for all cases 
     1149      modify=yes 
     1150  else 
     1151      # The variable was not set to AUTO 
     1152      if [ $type = blocker ] ; then 
     1153          # Exit because this is a blocker call 
     1154          IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable $key cannot be modified. It should be set to AUTO." 
     1155          IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 
     1156          return 
     1157       elif [ $type = nonblocker ] ; then 
     1158          # Do nothing. Suppose that the user did set the variable correct 
     1159          IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: $key is set by the user. Nothing done." 
     1160          modify=no 
     1161       elif [ $type = force ] ; then 
     1162          # Force modification 
     1163          IGCM_debug_Print 1 "IGCM_comp_modifyNamelist : Variabl=$key was not set to AUTO. Modification will be forced." 
     1164          modify=yes 
     1165      fi 
     1166  fi 
     1167 
     1168  # Do the modifcation now 
     1169  if [ $modify = yes ] ; then 
     1170 
     1171      # For option DEFAULT, read default value from file. 
     1172      if [ X"$value" = XDEFAULT ] || [ X"$value" = X ] ; then 
     1173        # Case to set DEFAULT value 
     1174        # Read default value from filein 
     1175        value=$( echo $pattern | awk  -F"DEFAULT *=" '{print $2}') 
     1176 
     1177        if [ X"$value" = X ] ; then 
     1178          IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable $key needs a DEFAULT value in $filein." 
     1179          IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: The syntax in $filein should be:" 
     1180          IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: $key=_AUTO_:DEFAULT=def_value" 
     1181          IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 
     1182          return 
     1183        fi 
     1184      fi 
     1185 
     1186      # Now change key in filein 
     1187      sed -e "s/${pattern}/       $key=${value}/" $filein > $filein.tmp 
     1188      IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: In $filein set $key=$value" 
     1189      \mv $filein.tmp $filein 
     1190  fi 
     1191  IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 
     1192} 
     1193 
    10781194#======================================================================= 
    10791195function IGCM_comp_modifyXmlFile 
Note: See TracChangeset for help on using the changeset viewer.