source: tags/libIGCM_v1/libIGCM_card/libIGCM_card.ksh @ 484

Last change on this file since 484 was 2, checked in by mmaipsl, 16 years ago

MM: import first trunk version of libIGCM.

File size: 8.5 KB
Line 
1#!/bin/ksh
2
3#**************************************************************
4# Author: Patrick Brockmann
5# Contact: Patrick.Brockmann@cea.fr
6# $Date: 2007/03/15 10:40:19 $
7# $Name: libIGCM_v1 $
8# $Revision: 1.7 $
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        awk -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        awk -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=$( awk -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        set -A ${name} -- ${NULL_STR}
134        set +A ${name} -- $( awk -f ${libIGCM}/libIGCM_card/IGCM_card_PrintOption.awk -- "$@" | awk -- 'BEGIN {FS="[() ,]+"} {for (i=2; i <= NF-1; i++) printf("%s ",$i)}' )
135    else
136        echo "--Error--> IGCM_card_DefineArrayFromOption $@"
137        echo "           $1 is not readable"
138        IGCM_debug_Exit "IGCM_card_DefineArrayFromOption"
139    fi
140    IGCM_debug_PopStack "IGCM_card_DefineArrayFromOption"
141}
142
143#D-#==================================================================
144#D-function IGCM_card_DefineArrayFromSection
145#D-* Purpose: Define an array variable from a given file.card and section
146#D-*          Array variable is automatically defined as file_section
147#D-* Usage: IGCM_card_DefineArrayFromSection file.card section
148#D-
149function IGCM_card_DefineArrayFromSection
150{
151    IGCM_debug_PushStack "IGCM_card_DefineArrayFromSection" $@
152    if [ -r "$1" ] ; then
153        # Get basename of card file ($1)
154        typeset name1=${1##*/}
155        # Build name of array as $1_$2 (cardname_Section)
156        typeset name=${name1%%.*}_${2}
157        set -A ${name} -- ${NULL_STR}
158        set +A ${name} -- $( awk -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 from 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        ( awk -f ${libIGCM}/libIGCM_card/IGCM_card_WriteOption.awk -- "$@" 2> /dev/null ) > ${tmpfile}
183        mv ${tmpfile} $1
184    else
185        echo "--Error--> IGCM_card_WriteOption $@"
186        echo "           $1 is not readable or not writable"
187        IGCM_debug_Exit "IGCM_card_WriteOption"
188    fi
189    IGCM_debug_PopStack "IGCM_card_WriteOption"
190}
191
192#D-#==================================================================
193#D-function IGCM_card_Check
194#D-* Purpose: Check the present file by comparison with a reference file
195#D-* Usage: IGCM_card_Check
196#D-
197function IGCM_card_Check
198{
199    IGCM_debug_PushStack "IGCM_card_Check" 
200#---------------------
201    if [ ! -n "${libIGCM}" ] ; then
202        echo "Check libIGCM_card ...........................................[ FAILED ]"
203        echo "--Error--> libIGCM variable is not defined"
204        IGCM_debug_Exit "IGCM_card_Check"
205    fi
206
207#---------------------
208    whence -v awk > /dev/null 2>&1
209    if [ ! $? -eq 0 ] ; then
210        echo "Check libIGCM_card ...........................................[ FAILED ]"
211        echo "--Error--> awk command is not defined"
212        IGCM_debug_Exit "IGCM_card_Check"
213    fi
214
215#---------------------
216    ${libIGCM}/libIGCM_card/IGCM_card_Test.ksh > IGCM_card_Test.ref.failed 2>&1
217    sleep 2
218
219    if diff IGCM_card_Test.ref.failed ${libIGCM}/libIGCM_card/IGCM_card_Test.ref > /dev/null 2>&1 ; then
220        echo "Check libIGCM_card ...............................................[ OK ]"
221        rm -f IGCM_card_Test.ref.failed
222    else
223        echo "Check libIGCM_card ...........................................[ FAILED ]"
224        echo "--Error--> Execution of ${libIGCM}/libIGCM_card/IGCM_card_Test.ksh"
225        echo "           has produced the file IGCM_card_Test.ref.failed"
226        echo "           Please analyse differences with the reference file by typing:"
227        echo "           diff IGCM_card_Test.ref.failed ${libIGCM}/libIGCM_card/IGCM_card_Test.ref"
228        echo "           Report errors to the author: Patrick.Brockmann@cea.fr"
229        IGCM_debug_Exit "IGCM_card_Check"
230    fi
231
232#---------------------
233    IGCM_debug_PopStack "IGCM_card_Check" 
234}
Note: See TracBrowser for help on using the repository browser.