source: tags/libIGCM_v1_9/libIGCM_card/libIGCM_card.ksh @ 1137

Last change on this file since 1137 was 329, checked in by mmaipsl, 14 years ago

MM : change temporary card for marshalling device with explicit mutex extension.

  • Property licence set to
    The following licence information concerns ONLY the libIGCM tools
    ==================================================================

    Copyright © Centre National de la Recherche Scientifique CNRS
    Commissariat à l'Énergie Atomique CEA

    libIGCM : Library for Portable Models Computation of IGCM Group.

    IGCM Group is the french IPSL Global Climate Model Group.

    This library is a set of shell scripts and functions whose purpose is
    the management of the initialization, the launch, the transfer of
    output files, the post-processing and the monitoring of datas produce
    by any numerical program on any plateforme.

    This software is governed by the CeCILL license under French law and
    abiding by the rules of distribution of free software. You can use,
    modify and/ or redistribute the software under the terms of the CeCILL
    license as circulated by CEA, CNRS and INRIA at the following URL
    "http://www.cecill.info".

    As a counterpart to the access to the source code and rights to copy,
    modify and redistribute granted by the license, users are provided only
    with a limited warranty and the software's author, the holder of the
    economic rights, and the successive licensors have only limited
    liability.

    In this respect, the user's attention is drawn to the risks associated
    with loading, using, modifying and/or developing or reproducing the
    software by the user in light of its specific status of free software,
    that may mean that it is complicated to manipulate, and that also
    therefore means that it is reserved for developers and experienced
    professionals having in-depth computer knowledge. Users are therefore
    encouraged to load and test the software's suitability as regards their
    requirements in conditions enabling the security of their systems and/or
    data to be ensured and, more generally, to use and operate it in the
    same conditions as regards security.

    The fact that you are presently reading this means that you have had
    knowledge of the CeCILL license and that you accept its terms.
  • Property svn:keywords set to Date Author Revision
File size: 10.7 KB
Line 
1#!/bin/ksh
2
3#**************************************************************
4# Author: Patrick Brockmann
5# Contact: Patrick.Brockmann@cea.fr
6# $Date$
7# $Author$
8# $Revision$
9# IPSL (2006)
10#  This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC
11# History:
12# Modification:
13#
14#**************************************************************
15
16#==================================================
17# The documentation of this file can be automatically generated
18# if you use the prefix #D- for comments to be extracted.
19# Extract with command: cat lib* | grep "^#D-" | cut -c "4-"
20#==================================================
21 
22#D-#==================================================================
23#D-libIGCM_card
24#D-This ksh library handles extraction of information from configuration file
25#D-called "card" file (en français fichier "carte").
26#D-All function described bellow must be prefixed by IGCM_card.
27#D-A card file is organized as follows :
28#D- ---------------------
29#D-[Messages]
30#D-Option1= "Hello Earth"
31#D-Option2= "Hello Mars"
32#D-
33#D-# My comments
34#D-[Recipes]
35#D-Cake= "file1.doc"
36#D-Starter= "file2.doc"
37#D-
38#D-[ColorValues]
39#D-Red= 120
40#D-Blue= 230
41#D-Green= 178
42#D-
43#D-[Couples]
44#D-List1=   (up, down), \
45#D-         (humid, dry), \
46#D-         (hot, cold), \
47#D-         (far, close)
48#D-List2=   (ice, fire, air, water)
49#D- ---------------------
50#D-
51
52#D-#==================================================================
53#D-function IGCM_card_PrintOption
54#D-* Purpose: Print an option from a given file.card and section
55#D-* Usage: IGCM_card_PrintOption file.card section option
56#D-
57function IGCM_card_PrintOption
58{
59    IGCM_debug_PushStack "IGCM_card_PrintOption" $@
60    if [ -r "$1" ] ; then
61        gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk "$@"
62    else
63        echo "--Error--> IGCM_card_PrintOption $@"
64        echo "           $1 is not readable"
65        IGCM_debug_Exit "IGCM_card_PrintOption"
66    fi
67    IGCM_debug_PopStack "IGCM_card_PrintOption"
68}
69
70#D-#==================================================================
71#D-function IGCM_card_PrintSection
72#D-* Purpose: Print all options from a given file.card and section
73#D-* Usage: IGCM_card_PrintSection file.card section
74#D-
75function IGCM_card_PrintSection
76{
77    IGCM_debug_PushStack "IGCM_card_PrintSection" $@
78    if [ -r "$1" ] ; then
79        gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintSection.awk -- "$@"
80    else
81        echo "--Error--> IGCM_card_PrintSection $@"
82        echo "           $1 is not readable"
83        IGCM_debug_Exit "IGCM_card_PrintSection"
84    fi
85    IGCM_debug_PopStack "IGCM_card_PrintSection"
86}
87
88#D-#==================================================================
89#D-function IGCM_card_DefineVariableFromOption
90#D-* Purpose: Define a variable from a given file.card, section and option
91#D-*          Variable name is automatically defined as file_section_option
92#D-* Usage: IGCM_card_DefineVariableFromOption file.card section option
93#D-
94function IGCM_card_DefineVariableFromOption
95{
96    IGCM_debug_PushStack "IGCM_card_DefineVariableFromOption" $@
97    if [ -r "$1" ] ; then
98        # Get basename of card file ($1)
99        typeset name1=${1##*/}
100        # Build name of variable as $1_$2_$3 (cardname_Section_Option)
101        typeset name=${name1%%.*}_${2}_${3}
102        typeset value=$( gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk -- "$@" )
103
104        if [ "${value}" = "Error: Option not found" ] ; then
105            echo "Error with readding of ${name} variable in ${1} ."
106            echo "Error: Option not found."
107            IGCM_debug_Exit
108            IGCM_debug_Verif_Exit
109        fi
110        eval ${name}=${value}
111    else
112        echo "--Error--> IGCM_card_DefineVariableFromOption $@"
113        echo "           $1 is not readable"
114        IGCM_debug_Exit "IGCM_card_DefineVariableFromOption"
115    fi
116    IGCM_debug_PopStack "IGCM_card_DefineVariableFromOption"
117}
118
119#D-#==================================================================
120#D-function IGCM_card_DefineArrayFromOption
121#D-* Purpose: Define an array variable from a given file.card, section and option
122#D-*          Array variable is automatically defined as file_section_option
123#D-* Usage: IGCM_card_DefineArrayFromOption file.card section option
124#D-
125function IGCM_card_DefineArrayFromOption
126{
127    IGCM_debug_PushStack "IGCM_card_DefineArrayFromOption" $@
128    if [ -r "$1" ] ; then
129        # Get basename of card file ($1)
130        typeset name1=${1##*/}
131        # Build name of array as $1_$2_$3 (cardname_Section_Option)
132        typeset name=${name1%%.*}_${2}_${3}
133        eval unset ${name}
134        eval ${name}[0]=${NULL_STR}
135        set +A ${name} -- $( gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk -- "$@" | gawk -- 'BEGIN {FS="[() ,]+"} {for (i=2; i <= NF-1; i++) printf("%s ",$i)}' )
136    else
137        echo "--Error--> IGCM_card_DefineArrayFromOption $@"
138        echo "           $1 is not readable"
139        IGCM_debug_Exit "IGCM_card_DefineArrayFromOption"
140    fi
141    IGCM_debug_PopStack "IGCM_card_DefineArrayFromOption"
142}
143
144#D-#==================================================================
145#D-function IGCM_card_DefineArrayFromSection
146#D-* Purpose: Define an array variable from a given file.card and section
147#D-*          Array variable is automatically defined as file_section
148#D-* Usage: IGCM_card_DefineArrayFromSection file.card section
149#D-
150function IGCM_card_DefineArrayFromSection
151{
152    IGCM_debug_PushStack "IGCM_card_DefineArrayFromSection" $@
153    if [ -r "$1" ] ; then
154        # Get basename of card file ($1)
155        typeset name1=${1##*/}
156        # Build name of array as $1_$2 (cardname_Section)
157        typeset name=${name1%%.*}_${2}
158        eval unset ${name}
159        eval ${name}[0]=${NULL_STR}
160        set +A ${name} -- $( gawk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintSection.awk -- "$@" )
161    else
162        echo "--Error--> IGCM_card_DefineArrayFromSection $@"
163        echo "           $1 is not readable"
164        IGCM_debug_Exit "IGCM_card_DefineArrayFromSection"
165    fi
166    IGCM_debug_PopStack "IGCM_card_DefineArrayFromSection"
167}
168
169#D-#==================================================================
170#D-function IGCM_card_WriteOption
171#D-* Purpose: Write an option in a given file.card and section
172#D-* Usage: IGCM_card_WriteOption file.card section newvalue
173#D-* Examples: IGCM_card_WriteOption file.card Recipes Red 150
174#D-            IGCM_card_WriteOption file.card Messages Option2 '"Hello Mercure"'
175#D-            IGCM_card_WriteOption file.card Messages ListVal1 '( 1, 2, 3 )'
176#D-            listname="(Sebastien, Martial, Patrick)"
177#D-            IGCM_card_WriteOption NewTestFile.card Messages ListVal2 "${listname}"
178#D-
179function IGCM_card_WriteOption
180{
181    IGCM_debug_PushStack "IGCM_card_WriteOption" $@
182    if [ -r "$1" ] && [ -w "$1" ] ; then
183        typeset tmpfile=tmpfile_$$
184        ( IGCM_card_PrintOption "$1" "$2" "$3" | grep "not found" ) > ${tmpfile}
185        if [ $( cat ${tmpfile} | wc -l ) -gt 0 ] ; then
186            echo "-------------------------------------------"
187            echo "!!! Problem with IGCM_card_WriteOption !!!"
188            echo "Try to write : " $@
189            echo "You have to correcte some script." 
190            echo "We won't do anything else !"
191            exit 1
192        fi
193        rm ${tmpfile}
194
195        # The tmpfile uses now the real path of the card to be modified,
196        # not just a local tmpfile with PID.
197        tmpfile=$1_mutex_$$
198
199        # Watch for possible conflics : Check for other tmpfiles.
200        unset tmpfiles
201        set +A tmpfiles -- $( ls $1_mutex_[0-9]* 2>/dev/null )
202        typeset isleep
203        ((isleep=0))
204        while [ ${#tmpfiles[@]} -gt 0 ] ; do
205            echo "Conflict between two processes working on " $1 "!!!" ${tmpfiles[@]}
206            sleep 1
207            ((isleep=isleep+1))
208            if [ isleep -gt 20 ] ; then
209                echo "Too many loops waiting for other process working on " $1 ". We continue."
210                echo "You should see if one process of your run or post-treatment may have terminated suddenly."
211                echo "Afer, you should erase this(those) file(s) : " ${tmpfiles[@]}
212# Send a mail to USER ??
213                break ;
214            fi
215            unset tmpfiles
216            set +A tmpfiles -- $( ls $1_mutex_[0-9]* 2>/dev/null )
217        done
218
219        # Do the job
220        ( gawk -f ${libIGCM}/libIGCM_card/IGCM_card_WriteOption.awk -- "$@" 2> /dev/null ) > ${tmpfile}
221
222        cp $1 $1.bak
223        mv ${tmpfile} $1
224
225    else
226        echo "--Error--> IGCM_card_WriteOption $@"
227        echo "           $1 is not readable or not writable"
228        IGCM_debug_Exit "IGCM_card_WriteOption"
229    fi
230    IGCM_debug_PopStack "IGCM_card_WriteOption"
231}
232
233#D-#==================================================================
234#D-function IGCM_card_WriteArrayOption
235#D-* Purpose: Write an array option a given file.card and section
236#D-* Usage: IGCM_card_WriteArrayOption file.card section option newarray
237#D-* Examples: set -A MyArray -- 1 2 3
238#D-            IGCM_card_WriteArrayOption file.card Recipes List MyArray
239#D-
240function IGCM_card_WriteArrayOption
241{
242    IGCM_debug_PushStack "IGCM_card_WriteArrayOption" $@
243
244    if [ -r "$1" ] && [ -w "$1" ] ; then
245        typeset tmpfile=tmpfile_$$
246        if [ X"${4}" != X"" ]; then
247            tab=$4
248            IGCM_card_WriteOption $1 $2 $3 '('$( eval echo \${${tab}[@]} | sed -e 's/ /,/g' )')'
249        else
250            IGCM_card_WriteOption $1 $2 $3 '()'
251        fi
252    else
253        echo "--Error--> IGCM_card_WriteArrayOption $@"
254        echo "           $1 is not readable or not writable"
255        IGCM_debug_Exit "IGCM_card_WriteArrayOption"
256    fi
257    IGCM_debug_PopStack "IGCM_card_WriteArrayOption"
258}
259
260#D-#==================================================================
261#D-function IGCM_card_Check
262#D-* Purpose: Check the present file by comparison with a reference file
263#D-* Usage: IGCM_card_Check
264#D-
265function IGCM_card_Check
266{
267    IGCM_debug_PushStack "IGCM_card_Check" 
268#---------------------
269    if [ ! -n "${libIGCM}" ] ; then
270        echo "Check libIGCM_card ...........................................[ FAILED ]"
271        echo "--Error--> libIGCM variable is not defined"
272        IGCM_debug_Exit "IGCM_card_Check"
273    fi
274
275#---------------------
276    whence -v gawk > /dev/null 2>&1
277    if [ ! $? -eq 0 ] ; then
278        echo "Check libIGCM_card ...........................................[ FAILED ]"
279        echo "--Error--> gawk command is not defined"
280        IGCM_debug_Exit "IGCM_card_Check"
281    fi
282
283#---------------------
284    ${libIGCM}/libIGCM_card/IGCM_card_Test.ksh > IGCM_card_Test.ref.failed 2>&1
285    sleep 2
286
287    if diff IGCM_card_Test.ref.failed ${libIGCM}/libIGCM_card/IGCM_card_Test.ref > /dev/null 2>&1 ; then
288        echo "Check libIGCM_card ...............................................[ OK ]"
289        rm -f IGCM_card_Test.ref.failed
290    else
291        echo "Check libIGCM_card ...........................................[ FAILED ]"
292        echo "--Error--> Execution of ${libIGCM}/libIGCM_card/IGCM_card_Test.ksh"
293        echo "           has produced the file IGCM_card_Test.ref.failed"
294        echo "           Please analyse differences with the reference file by typing:"
295        echo "           diff IGCM_card_Test.ref.failed ${libIGCM}/libIGCM_card/IGCM_card_Test.ref"
296        echo "           Report errors to the author: Patrick.Brockmann@cea.fr"
297        IGCM_debug_Exit "IGCM_card_Check"
298    fi
299
300#---------------------
301    IGCM_debug_PopStack "IGCM_card_Check" 
302}
Note: See TracBrowser for help on using the repository browser.