1 | #! /bin/sh |
---|
2 | # |
---|
3 | #+ |
---|
4 | # |
---|
5 | # .. program:: correct_terminology.sh |
---|
6 | # |
---|
7 | # .. _correct_terminology.sh: |
---|
8 | # |
---|
9 | # ======================= |
---|
10 | # correct_terminology.sh |
---|
11 | # ======================= |
---|
12 | # |
---|
13 | # SYNOPSIS |
---|
14 | # ======== |
---|
15 | # |
---|
16 | # .. code-block:: bash |
---|
17 | # |
---|
18 | # correct_terminology.sh [--debug] -good goodend -bad badend -d directory |
---|
19 | # |
---|
20 | # DESCRIPTION |
---|
21 | # =========== |
---|
22 | # |
---|
23 | # .. option:: --debug |
---|
24 | # |
---|
25 | # If this option is set, :samp:`ncdump -v tt` will be added to log file |
---|
26 | # |
---|
27 | # .. option:: -good goodend |
---|
28 | # |
---|
29 | # good end part of the name |
---|
30 | # |
---|
31 | # .. option:: -bad badend |
---|
32 | # |
---|
33 | # bad end part of the name |
---|
34 | # |
---|
35 | # .. option:: -d <dirin> |
---|
36 | # |
---|
37 | # base location of the dataset to be changed |
---|
38 | # |
---|
39 | # Modify filenames in the dataset based under dirin parameter |
---|
40 | # |
---|
41 | # Log file is written on |
---|
42 | # :file:`${PROJECT_LOG}/correct_terminology.sh.log.{YYYYMMDDTHHMMSS}Z`. |
---|
43 | # |
---|
44 | # .. graphviz:: |
---|
45 | # |
---|
46 | # digraph correct_terminology { |
---|
47 | # |
---|
48 | # correct_terminology [shape=box, |
---|
49 | # fontname=Courier, |
---|
50 | # color=blue, |
---|
51 | # URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/correct_terminology.sh", |
---|
52 | # label="${PROJECT}/src/correct_terminology.sh"]; |
---|
53 | # |
---|
54 | # {filein} -> {correct_terminology} -> {fileout} |
---|
55 | # |
---|
56 | # } |
---|
57 | # |
---|
58 | # EXAMPLES |
---|
59 | # ======== |
---|
60 | # |
---|
61 | # To modify files under |
---|
62 | # :file:`/usr/lodyc/incas/fplod/tropflux_d/to_be_published/` because files |
---|
63 | # are not named |
---|
64 | # :file:`var_tropflux_1d_${yyyy}_{yyyy}.nc` |
---|
65 | # but |
---|
66 | # :file:`var_tropflux_1d_{yyyy}_{month}{yyyy}.nc`: |
---|
67 | # |
---|
68 | # .. code-block:: bash |
---|
69 | # |
---|
70 | # correct_terminology.sh -d /usr/lodyc/incas/fplod/tropflux_d/to_be_published/ -good "1979_2013" -bad "1979_march2013" |
---|
71 | # |
---|
72 | # And look at log file with : |
---|
73 | # |
---|
74 | # .. code-block:: bash |
---|
75 | # |
---|
76 | # tlogd.sh correct_terminology |
---|
77 | # |
---|
78 | # SEE ALSO |
---|
79 | # ======== |
---|
80 | # |
---|
81 | # :ref:`updatedata` |
---|
82 | # |
---|
83 | # :ref:`project_profile.sh` |
---|
84 | # |
---|
85 | # TODO |
---|
86 | # ==== |
---|
87 | # |
---|
88 | # coding rules |
---|
89 | # |
---|
90 | # overwrite : may be an other option to avoid scratch |
---|
91 | # |
---|
92 | # debug not used |
---|
93 | # |
---|
94 | # build file name according to time in the file instead of parameter |
---|
95 | # |
---|
96 | # EVOLUTIONS |
---|
97 | # ========== |
---|
98 | # |
---|
99 | # $Id$ |
---|
100 | # |
---|
101 | # $URL$ |
---|
102 | # |
---|
103 | # - fplod 20130726T112836Z cratos.locean-ipsl.upmc.fr (Linux) |
---|
104 | # |
---|
105 | # * creation |
---|
106 | # |
---|
107 | #- |
---|
108 | system=$(uname) |
---|
109 | case "${system}" in |
---|
110 | AIX|IRIX64) |
---|
111 | echo "www : no specific posix checking" |
---|
112 | date_cmd=date |
---|
113 | ;; |
---|
114 | Darwin) |
---|
115 | set -o posix |
---|
116 | date_cmd=gdate |
---|
117 | ;; |
---|
118 | Linux) |
---|
119 | set -o posix |
---|
120 | date_cmd=date |
---|
121 | ;; |
---|
122 | *) |
---|
123 | set -o posix |
---|
124 | ;; |
---|
125 | esac |
---|
126 | unset system |
---|
127 | # |
---|
128 | LANG=POSIX |
---|
129 | # |
---|
130 | set -u |
---|
131 | # |
---|
132 | command=$(basename ${0}) |
---|
133 | log_date=$(date -u +"%Y%m%dT%H%M%SZ") |
---|
134 | # |
---|
135 | usage=" Usage : ${command} [--debug] -d dirin -good yyyymmdd_yyyymmdd -bad anything" |
---|
136 | # |
---|
137 | hostname=$(hostname) |
---|
138 | # |
---|
139 | # default |
---|
140 | debug=0 |
---|
141 | # |
---|
142 | minargcount=0 |
---|
143 | if [ ${#} -lt ${minargcount} ] |
---|
144 | then |
---|
145 | echo "${command} : eee : not enough arguments" |
---|
146 | echo "${usage}" |
---|
147 | exit 1 |
---|
148 | fi |
---|
149 | # |
---|
150 | while [ ${#} -gt 0 ] |
---|
151 | do |
---|
152 | case ${1} in |
---|
153 | -good) |
---|
154 | goodend=${2} |
---|
155 | shift |
---|
156 | ;; |
---|
157 | -bad) |
---|
158 | badend=${2} |
---|
159 | shift |
---|
160 | ;; |
---|
161 | -d) |
---|
162 | dirin=${2} |
---|
163 | shift |
---|
164 | ;; |
---|
165 | --debug) |
---|
166 | debug=1 |
---|
167 | ;; |
---|
168 | *) |
---|
169 | # anything else |
---|
170 | echo "${command} : eee : unknown option ${1}" |
---|
171 | echo "${command} : eee : ${usage}" |
---|
172 | exit 1 |
---|
173 | ;; |
---|
174 | esac |
---|
175 | # next flag |
---|
176 | shift |
---|
177 | done |
---|
178 | # |
---|
179 | # check parameters |
---|
180 | if [ ! -d ${dirin} ] |
---|
181 | then |
---|
182 | echo " eee : ${dirin} not found" |
---|
183 | exit 1 |
---|
184 | fi |
---|
185 | # |
---|
186 | # check for permission on dirin |
---|
187 | if [ ! -x ${dirin} ] |
---|
188 | then |
---|
189 | echo " eee : ${dirin} not reachable" |
---|
190 | exit 1 |
---|
191 | fi |
---|
192 | #++ check good validity |
---|
193 | # |
---|
194 | # check for ${PROJECT_LOG} definition |
---|
195 | if [ "${PROJECT_LOG}" = "" ] |
---|
196 | then |
---|
197 | echo "${command} : ${LINENO} : eee : \${PROJECT_LOG} not defined" |
---|
198 | exit 1 |
---|
199 | fi |
---|
200 | # |
---|
201 | # check for ${PROJECT_LOG} existence |
---|
202 | if [ ! -d ${PROJECT_LOG} ] |
---|
203 | then |
---|
204 | echo "${command} : eee : ${PROJECT_LOG} not found" |
---|
205 | exit 1 |
---|
206 | fi |
---|
207 | # |
---|
208 | # check for permission access on PROJECT_LOG |
---|
209 | if [ ! -x ${PROJECT_LOG} ] |
---|
210 | then |
---|
211 | echo "${command} : eee : ${PROJECT_LOG} not reachable" |
---|
212 | exit 1 |
---|
213 | fi |
---|
214 | # |
---|
215 | # check for write permission on PROJECT_LOG |
---|
216 | if [ ! -w ${PROJECT_LOG} ] |
---|
217 | then |
---|
218 | echo "${command} : eee : ${PROJECT_LOG} not writable" |
---|
219 | exit 1 |
---|
220 | fi |
---|
221 | # |
---|
222 | log=${PROJECT_LOG}/$(basename ${0} .sh).log.${log_date} |
---|
223 | echo "[Context]" 1>> ${log} |
---|
224 | echo "command=$(basename ${0})" 1>>${log} |
---|
225 | echo "hostname=${hostname}" 1>> ${log} |
---|
226 | echo "runtime=${log_date}" 1>> ${log} |
---|
227 | echo "log=${log}" 1>> ${log} |
---|
228 | unset log_date |
---|
229 | # |
---|
230 | echo "" 1>> ${log} |
---|
231 | echo "[Parameters]" 1>> ${log} |
---|
232 | echo "dirin=${dirin}" 1>> ${log} |
---|
233 | echo "good=${goodend}" 1>> ${log} |
---|
234 | echo "bad=${badend}" 1>> ${log} |
---|
235 | echo "" 1>> ${log} |
---|
236 | # |
---|
237 | # build list files |
---|
238 | varlist="lhf lwr netflux q2m shf sst swr t2m tau taux tauy ws" |
---|
239 | period_list="daily monthly" |
---|
240 | list_filein="" |
---|
241 | for var in ${varlist} |
---|
242 | do |
---|
243 | for period in ${period_list} |
---|
244 | do |
---|
245 | if [ "${period}" == "daily" ] |
---|
246 | then |
---|
247 | suffix=1d |
---|
248 | else |
---|
249 | suffix=1m |
---|
250 | fi |
---|
251 | filein=${dirin}/${period}/${var}_tropflux_${suffix}_${badend}.nc |
---|
252 | if [ ! -f ${filein} ] |
---|
253 | then |
---|
254 | echo " eee : ${filein} not found" 1>> ${log} |
---|
255 | exit 1 |
---|
256 | else |
---|
257 | list_filein=${list_filein}" ${filein}" |
---|
258 | fi |
---|
259 | done |
---|
260 | unset period |
---|
261 | done |
---|
262 | unset period_list |
---|
263 | unset var |
---|
264 | unset varlist |
---|
265 | # |
---|
266 | # rename all files |
---|
267 | for filein in ${list_filein} |
---|
268 | do |
---|
269 | echo "iii : rename ${filein}" >> ${log} 2>&1 |
---|
270 | bfileout=$(basename ${filein} ${badend}.nc)"${goodend}.nc" |
---|
271 | fileout=$(dirname ${filein})/${bfileout} |
---|
272 | unset bfileout |
---|
273 | mv ${filein} ${fileout} >> ${log} 2>&1 |
---|
274 | status=${?} |
---|
275 | if [ ${status} -ne 0 ] |
---|
276 | then |
---|
277 | echo "eee : pb with mv" >> ${log} 2>&1 |
---|
278 | ncdump -h ${filein} >> ${log} 2>&1 |
---|
279 | exit 1 |
---|
280 | fi |
---|
281 | unset status |
---|
282 | unset fileout |
---|
283 | done |
---|
284 | unset filein |
---|
285 | # |
---|
286 | # cleaning |
---|
287 | #++ |
---|
288 | #++ set |
---|
289 | # end |
---|
290 | exit 0 |
---|