source: trunk/libIGCM/libIGCM_card/libIGCM_card.ksh @ 430

Last change on this file since 430 was 373, checked in by sdipsl, 14 years ago

Remove libIGCM_sys_claude.ksh and libIGCM_sys_calculo.ksh
Update/homogenize all headers usgin the new fixed-length keyword syntax regarding properties keywords
$Rev:: 13 i $: Revision of last commit
$Author:: harry $: Author of last commit
$Date:: 2006-03-15 02:33:03 -0500 (Wed, 15 Mar 2006) $: Date of last commit
Add / update original author and contact when missing

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