1 | #! /bin/sh |
---|
2 | # |
---|
3 | # usage for NEMO doc to create an update of the Namelist directory : |
---|
4 | # 1- delete the existing directory (You can also choose to save it somewhere) |
---|
5 | # rm -rf Namelist |
---|
6 | # 2- create the updated Namelist directory from the SHARED/namelist_ref : |
---|
7 | # ./namelist_split.sh -i ../../NEMOGCM/CONFIG/SHARED/namelist_ref -o Namelist |
---|
8 | # |
---|
9 | # .. _namelist_split.sh: |
---|
10 | # |
---|
11 | # ================= |
---|
12 | # namelist_split.sh |
---|
13 | # ================= |
---|
14 | # |
---|
15 | # ---------------- |
---|
16 | # split a namelist |
---|
17 | # ---------------- |
---|
18 | # |
---|
19 | # SYNOPSIS |
---|
20 | # ======== |
---|
21 | # |
---|
22 | # .. code-block:: bash |
---|
23 | # |
---|
24 | # namelist_split.sh -i namelist -o dirout [-n] |
---|
25 | # |
---|
26 | # DESCRIPTION |
---|
27 | # =========== |
---|
28 | # |
---|
29 | # Split a namelist file (NEMO convention syntax) given in parameter |
---|
30 | # into files in a given output directory. |
---|
31 | # |
---|
32 | # .. option:: -i <input file (namelist)> |
---|
33 | # .. option:: -o <output directory> |
---|
34 | # .. option:: -n <numbered output files> |
---|
35 | # |
---|
36 | # Each file of the output directory is named after the *block* it contains. |
---|
37 | # |
---|
38 | # We assume here that the input file is written with this pattern of block: |
---|
39 | # |
---|
40 | # .. parsed-literal:: |
---|
41 | # |
---|
42 | # !--------------- |
---|
43 | # &nameblock |
---|
44 | # !--------------- |
---|
45 | # var1 = val1 |
---|
46 | # ... |
---|
47 | # varn = valn |
---|
48 | # / |
---|
49 | # |
---|
50 | # If ``-n`` option is used, files in the directory output are named after |
---|
51 | # the number of the block it contains and the name of this block. This might |
---|
52 | # be useful to recombine blocks in the same order. |
---|
53 | # |
---|
54 | # EXAMPLES |
---|
55 | # ======== |
---|
56 | # |
---|
57 | # To split the namelist NEMO ORCA2_LIM experiment 00 if you are in NEMO |
---|
58 | # working space : |
---|
59 | # |
---|
60 | # .. code-block:: bash |
---|
61 | # |
---|
62 | # namelist_split.sh -i CONFIG/ORCA2_LIM/EXP00/namelist -o /tmp/EXP00_namelist_split/ |
---|
63 | # |
---|
64 | # To split the namelist under data directory with numbered files: |
---|
65 | # |
---|
66 | # .. code-block:: bash |
---|
67 | # |
---|
68 | # namelist_split.sh -i ./data/namelist -o /tmp/EXP00_namelist_split/ -n |
---|
69 | # |
---|
70 | # Check can be made by concatenation of files in output directory if ``-n`` |
---|
71 | # option have been used and comparison with original file : |
---|
72 | # |
---|
73 | # .. code-block:: bash |
---|
74 | # |
---|
75 | # cat /tmp/EXP00_namelist_split/b???_* > /tmp/EXP00_namelist_rebuild |
---|
76 | # sdiff -w80 ./data/namelist /tmp/EXP00_namelist_rebuild |
---|
77 | # |
---|
78 | # Output contains lines which are before the first block, after the last |
---|
79 | # block and lines between blocks. |
---|
80 | # |
---|
81 | # EVOLUTIONS |
---|
82 | # ========== |
---|
83 | # |
---|
84 | # $Id$ |
---|
85 | # |
---|
86 | # - fplod 2008-08-11T08:56:44Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
87 | # |
---|
88 | # * commentaires dans ce fichier en reStructuredText |
---|
89 | # cf. reStructuredText_ and Docutils_ |
---|
90 | # |
---|
91 | # .. _reStructuredText: http://docutils.sourceforge.net/rst.html |
---|
92 | # .. _Docutils: http://docutils.sourceforge.net/ |
---|
93 | # |
---|
94 | # - fplod 2008-07-22T12:41:04Z aedon.locean-ipsl.upmc.fr (Darwin) |
---|
95 | # |
---|
96 | # * creation with test on |
---|
97 | # http://forge.ipsl.jussieu.fr/nemo/browser/trunk/CONFIG/ORCA2_LIM/EXP00/namelist revision 1151 |
---|
98 | #- |
---|
99 | # |
---|
100 | system=$(uname) |
---|
101 | case "${system}" in |
---|
102 | AIX|IRIX64) |
---|
103 | echo " www : no specific posix checking" |
---|
104 | ;; |
---|
105 | *) |
---|
106 | set -o posix |
---|
107 | ;; |
---|
108 | esac |
---|
109 | unset system |
---|
110 | # |
---|
111 | command=$(basename ${0}) |
---|
112 | log_date=$(date -u +"%Y%m%dT%H%M%SZ") |
---|
113 | # |
---|
114 | usage=" Usage : ${command} -i namelist -o dirout [-n]" |
---|
115 | # |
---|
116 | # default |
---|
117 | blocknum=0 |
---|
118 | # |
---|
119 | minargcount=4 |
---|
120 | if [ ${#} -lt ${minargcount} ] |
---|
121 | then |
---|
122 | echo "eee : not enough arguments" |
---|
123 | echo "${usage}" |
---|
124 | exit 1 |
---|
125 | fi |
---|
126 | # |
---|
127 | while [ ${#} -gt 0 ] |
---|
128 | do |
---|
129 | case ${1} in |
---|
130 | -i) |
---|
131 | filein=${2} |
---|
132 | shift |
---|
133 | ;; |
---|
134 | -o) |
---|
135 | dirout=${2} |
---|
136 | shift |
---|
137 | ;; |
---|
138 | -h) |
---|
139 | echo "${usage}" |
---|
140 | exit 0 |
---|
141 | ;; |
---|
142 | -n) |
---|
143 | blocknum=1 |
---|
144 | ;; |
---|
145 | *) |
---|
146 | # anything else |
---|
147 | echo "eee : unknown option ${1}" |
---|
148 | echo "eee : ${usage}" |
---|
149 | exit 1 |
---|
150 | ;; |
---|
151 | esac |
---|
152 | # next flag |
---|
153 | shift |
---|
154 | done |
---|
155 | unset usage |
---|
156 | # |
---|
157 | set -u |
---|
158 | # |
---|
159 | # check for filein |
---|
160 | if [ ! -r ${filein} ] |
---|
161 | then |
---|
162 | echo "eee : ${filein} not accessible" |
---|
163 | exit 1 |
---|
164 | fi |
---|
165 | # |
---|
166 | # check for dirout |
---|
167 | # the idea is to prevent unwanted overwrite |
---|
168 | if [ -d ${dirout} ] |
---|
169 | then |
---|
170 | echo "eee : ${dirout} already exist" |
---|
171 | exit 1 |
---|
172 | else |
---|
173 | mkdir -p ${dirout} |
---|
174 | if [ ${?} -ne 0 ] |
---|
175 | then |
---|
176 | echo "eee : cannot create ${dirout}" |
---|
177 | exit 1 |
---|
178 | fi |
---|
179 | fi |
---|
180 | # |
---|
181 | # loop on each line of ${filein} ie the namelist to be split |
---|
182 | fileout=/dev/null |
---|
183 | begin=0 |
---|
184 | if [ ${blocknum} -eq 1 ] |
---|
185 | then |
---|
186 | iblock=0 |
---|
187 | fi |
---|
188 | nbline=$(wc -l ${filein} | awk '{print $1}') |
---|
189 | il=1 |
---|
190 | while [ ${il} -le ${nbline} ] |
---|
191 | do |
---|
192 | line=$(sed -ne "${il},${il}p" ${filein}) |
---|
193 | # if current line start with "&", we can establish the name of output file |
---|
194 | echo ${line} | grep -q "^&" |
---|
195 | if [ ${?} -eq 0 ] |
---|
196 | then |
---|
197 | begin=1 |
---|
198 | # the form of this line is "&block !" |
---|
199 | block=${line#&} |
---|
200 | block=${block%% *} |
---|
201 | echo "iii : detection of the beginning of ${block}" |
---|
202 | if [ ${blocknum} -eq 1 ] |
---|
203 | then |
---|
204 | iblock=$(( ${iblock} + 1)) |
---|
205 | fileout=${dirout}/b$(echo ${iblock} | awk '{printf "%3.3d", $1}')_${block} |
---|
206 | else |
---|
207 | fileout="${dirout}/${block}" |
---|
208 | fi |
---|
209 | # check for ${fileout} |
---|
210 | if [ -f ${fileout} ] |
---|
211 | then |
---|
212 | echo "eee : ${fileout} already exists" |
---|
213 | exit 1 |
---|
214 | fi |
---|
215 | # write the previous line and the current line |
---|
216 | echo "${memoline}" > ${fileout} |
---|
217 | echo "${line}" >> ${fileout} |
---|
218 | fi |
---|
219 | # if current line contains "/" there will be no more lines to put in fileout |
---|
220 | echo ${line} | grep -q "^/$" |
---|
221 | if [ ${?} -eq 0 ] |
---|
222 | then |
---|
223 | echo "iii : detection of the end of ${block}" |
---|
224 | echo "${line}" >> ${fileout} |
---|
225 | fileout="/dev/null" |
---|
226 | fi |
---|
227 | # if current line is between "&" and "/", it should be written in fileout |
---|
228 | # if current line is before the first &, it won't be written |
---|
229 | if [ "${fileout}" != "/dev/null" ] |
---|
230 | then |
---|
231 | if [ ${begin} -eq 0 ] |
---|
232 | then |
---|
233 | echo "${line}" >> ${fileout} |
---|
234 | fi |
---|
235 | fi |
---|
236 | # keep memory of this current line to use it if the next one is the |
---|
237 | # beginning of a block |
---|
238 | memoline=${line} |
---|
239 | begin=0 |
---|
240 | # next line |
---|
241 | il=$(( ${il} + 1 )) |
---|
242 | done |
---|
243 | # |
---|
244 | echo "iii : ${filein} is split in ${dirout}" |
---|
245 | # |
---|
246 | # end |
---|
247 | exit 0 |
---|