Changeset 1069


Ignore:
Timestamp:
09/23/14 17:06:35 (10 years ago)
Author:
jgipsl
Message:

Added new function that can be used in the comp.driver to modify .def files.
See ticket #222

File:
1 edited

Legend:

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

    r1043 r1069  
    945945 
    946946#======================================================================= 
     947function IGCM_comp_modifyDefFile 
     948{ 
     949# 
     950# syntax:     IGCM_comp_modifyDefFile  type  filein  key  [value] 
     951# 
     952# For example : IGCM_comp_modifyDefFile blocker run.def day_step 1200 
     953# 
     954# This function is used to modify a parameter file for a specific variable. 
     955# The file must be a ".def" file, i.e. with IOIPSL parameter file syntax.  
     956# This function can be used in the comp.driver files for the components.  
     957# 
     958# Arguments:  
     959# - type   : first argument must be blocker or nonblocker.  
     960#            For "blocker" case, the variable must be attributed the keyworld AUTO  
     961#            otherwise this function will exit. 
     962#            For "nonblocker" case, the user can remove or modify the variable. For  
     963#            this case, as long as AUTO is not set, no modification will be done.  
     964# - filein : the file in run directory of .def type in which the variable should be set 
     965# - key    : the variable to modify 
     966# - value  : the value to set the key equal to, optional. If value is not set or if  
     967#            value=DEFAULT, then a default value must be given in filein using syntax : 
     968#            key= AUTO : DEFAULT=def_value 
     969# 
     970    IGCM_debug_PushStack "IGCM_comp_modifyDefFile" 
     971     
     972    # Set local variables and test the arguments 
     973    if [ $# = 4 ] ; then 
     974        # Normal case with 4 arguments 
     975        type=$1 ; filein=$2 ; key=$3 ; value=$4 
     976    elif [ $# = 3 ] ; then 
     977        # Normal case with 3 arguments 
     978        type=$1 ; filein=$2 ;   key=$3; value="DEFAULT" 
     979    else 
     980        IGCM_debug_Exit "IGCM_comp_modifyDefFile: Bad number of arguments." 
     981        return 
     982    fi 
     983    IGCM_debug_Print 1 "Entering IGCM_comp_modifyDefFile with arguments: $type $filein $key $value" 
     984 
     985    # Test if first argument is correct 
     986    if [ $type != blocker ] && [ $type != nonblocker ] ; then 
     987        IGCM_debug_Exit "IGCM_comp_modifyDefFile: Error in first argument must be blocker or nonblocker" 
     988        return 
     989    fi 
     990 
     991    # Test if the file exist 
     992    if [ ! -f ${filein} ] ; then 
     993        IGCM_debug_Exit "IGCM_comp_modifyDefFile: $filein does not exist." 
     994        return 
     995    fi 
     996 
     997    # Define list of files to test using all files with suffix .def 
     998    filelist=$( ls *def ) 
     999 
     1000    # Count number of occurances for the key in all files 
     1001    nb_occ=$( grep -w $key $filelist |grep -v "#"  |wc -l ) 
     1002 
     1003    # Test if key is set several times 
     1004    if [ $nb_occ -gt 1 ] ; then 
     1005        IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in $filein: Variable=$key is set ${nb_occ} times" 
     1006        return 
     1007    fi 
     1008 
     1009    # Treatement according to different cases 
     1010    if [ $nb_occ -eq 0 ] && [ $type = blocker ] ; then 
     1011        # Stop if the key is never set and the function is blocker 
     1012        IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in $filein: Variable=$key has been removed but this function is blocker. " 
     1013        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: restore $filein for variable $key." 
     1014    elif [ $nb_occ -eq 0 ] && [ $type = nonblocker ] ; then 
     1015        # The key is not set but it is a nonblocker call so nothing is done. 
     1016        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key is not set in $filein. This is a nonblocker call so nothing is done." 
     1017        IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: Default value for $key from the model will be used." 
     1018    elif [ $( grep $key $filein | grep -v "\#"  |wc -l ) = 0 ] ; then 
     1019        # Variable key is not set in filein, stop.  
     1020        IGCM_debug_Exit "IGCM_comp_modifyDefFile : Variable $key is not set in correct file. It should be set in $filein." 
     1021    else 
     1022        # Read the value of key from filein 
     1023        if [ X"$( grep $key $filein | grep -v "\#" | grep AUTO )" = "X" ] ; then 
     1024            # The key variable is not set to AUTO  
     1025            if [ $type = blocker ] ; then 
     1026                # Stop because this is a blocker call 
     1027                IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable $key cannot be modified. It should be set to AUTO." 
     1028            else 
     1029                # Do nothing. Suppose that the user did set the variable correct 
     1030                IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key is set by the user. Nothing done." 
     1031            fi 
     1032        else 
     1033            # The variable key is set to AUTO. 
     1034 
     1035            # For option DEFAULT, read default value from file. 
     1036            if [ X"$value" = XDEFAULT ] || [ X"$value" = X ] ; then 
     1037                # Case to set DEFAULT value 
     1038                # Read default value from filein 
     1039                value=$( grep $key $filein | grep -v "\#" | awk  -F"DEFAULT *=" '{print $2}') 
     1040 
     1041                if [ X"$value" = X ] ; then 
     1042                    IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable $key needs a DEFAULT value in $filein." 
     1043                    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: The syntax in $filein should be:" 
     1044                    IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $key=_AUTO_:DEFAULT=def_value" 
     1045                    return 
     1046                fi 
     1047            fi 
     1048 
     1049            # Now change key in filein 
     1050            sed -e "s/^${key}\ *=.*/${key}= ${value}/" $filein > $filein.tmp 
     1051            IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: In $filein set $key=$value" 
     1052            \mv $filein.tmp $filein 
     1053        fi 
     1054    fi   
     1055} 
     1056 
     1057#======================================================================= 
    9471058function IGCM_comp_Update 
    9481059{ 
Note: See TracChangeset for help on using the changeset viewer.