1 | MODULE strings_mod |
---|
2 | |
---|
3 | IMPLICIT NONE |
---|
4 | |
---|
5 | PRIVATE |
---|
6 | PUBLIC :: maxlen, init_printout, msg, fmsg, get_in, lunout, prt_level |
---|
7 | PUBLIC :: strLower, strHead, strStack, strCount, strReduce, strClean, strIdx |
---|
8 | PUBLIC :: strUpper, strTail, strStackm, strParse, strReplace, strFind, find, cat |
---|
9 | PUBLIC :: dispTable, dispOutliers, dispNameList |
---|
10 | PUBLIC :: is_numeric, bool2str, int2str, real2str, dble2str |
---|
11 | PUBLIC :: reduceExpr, str2bool, str2int, str2real, str2dble |
---|
12 | PUBLIC :: addQuotes, testFile, checkList, removeComment, test |
---|
13 | |
---|
14 | INTERFACE get_in; MODULE PROCEDURE getin_s, getin_i, getin_r, getin_l; END INTERFACE get_in |
---|
15 | INTERFACE msg; MODULE PROCEDURE msg_1, msg_m; END INTERFACE msg |
---|
16 | INTERFACE fmsg; MODULE PROCEDURE fmsg_1, fmsg_m; END INTERFACE fmsg |
---|
17 | INTERFACE strHead; MODULE PROCEDURE strHead_1, strHead_m; END INTERFACE strHead |
---|
18 | INTERFACE strTail; MODULE PROCEDURE strTail_1, strTail_m; END INTERFACE strTail |
---|
19 | INTERFACE strClean; MODULE PROCEDURE strClean_1, strClean_m; END INTERFACE strClean |
---|
20 | INTERFACE strReduce; MODULE PROCEDURE strReduce_1, strReduce_2; END INTERFACE strReduce |
---|
21 | INTERFACE strIdx; MODULE PROCEDURE strIdx_1, strIdx_m; END INTERFACE strIdx |
---|
22 | INTERFACE strCount; MODULE PROCEDURE strCount_m1, strCount_11, strCount_1m; END INTERFACE strCount |
---|
23 | INTERFACE strParse; MODULE PROCEDURE strParse_1, strParse_m; END INTERFACE strParse |
---|
24 | INTERFACE strReplace; MODULE PROCEDURE strReplace_1, strReplace_m; END INTERFACE strReplace |
---|
25 | INTERFACE cat; MODULE PROCEDURE horzcat_s1, horzcat_i1, horzcat_r1, & |
---|
26 | ! horzcat_d1, horzcat_dm, |
---|
27 | horzcat_sm, horzcat_im, horzcat_rm; END INTERFACE cat |
---|
28 | INTERFACE find; MODULE PROCEDURE strFind, find_int, find_boo; END INTERFACE find |
---|
29 | INTERFACE dispOutliers; MODULE PROCEDURE dispOutliers_1, dispOutliers_2; END INTERFACE dispOutliers |
---|
30 | INTERFACE reduceExpr; MODULE PROCEDURE reduceExpr_1, reduceExpr_m; END INTERFACE reduceExpr |
---|
31 | INTERFACE addQuotes; MODULE PROCEDURE addQuotes_1, addQuotes_m; END INTERFACE addQuotes |
---|
32 | INTERFACE testFile; MODULE PROCEDURE testFile_1, testFile_m; END INTERFACE testFile |
---|
33 | |
---|
34 | INTEGER, PARAMETER :: maxlen = 256 !--- Standard maximum length for strings |
---|
35 | INTEGER, SAVE :: lunout = 6 !--- Printing unit (default: 6, ie. on screen) |
---|
36 | INTEGER, SAVE :: prt_level = 1 !--- Printing level (default: 1, ie. print all) |
---|
37 | |
---|
38 | CONTAINS |
---|
39 | |
---|
40 | LOGICAL FUNCTION test(lcond, lout) RESULT(lerr) |
---|
41 | LOGICAL, INTENT(IN) :: lcond |
---|
42 | LOGICAL, INTENT(OUT) :: lout |
---|
43 | lerr = lcond; lout = lcond |
---|
44 | END FUNCTION test |
---|
45 | |
---|
46 | !============================================================================================================================== |
---|
47 | SUBROUTINE init_printout(lunout_, prt_level_) |
---|
48 | INTEGER, INTENT(IN) :: lunout_, prt_level_ |
---|
49 | lunout = lunout_ |
---|
50 | END SUBROUTINE init_printout |
---|
51 | !============================================================================================================================== |
---|
52 | |
---|
53 | |
---|
54 | !============================================================================================================================== |
---|
55 | !=== Same as getin ; additional last argument: the default value. |
---|
56 | !============================================================================================================================== |
---|
57 | SUBROUTINE getin_s(nam, val, def) |
---|
58 | USE ioipsl_getincom, ONLY: getin |
---|
59 | CHARACTER(LEN=*), INTENT(IN) :: nam |
---|
60 | CHARACTER(LEN=*), INTENT(INOUT) :: val |
---|
61 | CHARACTER(LEN=*), INTENT(IN) :: def |
---|
62 | val = def; CALL getin(nam, val) |
---|
63 | IF(val/=def) WRITE(lunout,*)TRIM(nam)//' = '//TRIM(val) |
---|
64 | END SUBROUTINE getin_s |
---|
65 | !============================================================================================================================== |
---|
66 | SUBROUTINE getin_i(nam, val, def) |
---|
67 | USE ioipsl_getincom, ONLY: getin |
---|
68 | CHARACTER(LEN=*), INTENT(IN) :: nam |
---|
69 | INTEGER, INTENT(INOUT) :: val |
---|
70 | INTEGER, INTENT(IN) :: def |
---|
71 | val = def; CALL getin(nam, val) |
---|
72 | IF(val/=def) WRITE(lunout,*)TRIM(nam)//' = '//TRIM(int2str(val)) |
---|
73 | END SUBROUTINE getin_i |
---|
74 | !============================================================================================================================== |
---|
75 | SUBROUTINE getin_r(nam, val, def) |
---|
76 | USE ioipsl_getincom, ONLY: getin |
---|
77 | CHARACTER(LEN=*), INTENT(IN) :: nam |
---|
78 | REAL, INTENT(INOUT) :: val |
---|
79 | REAL, INTENT(IN) :: def |
---|
80 | val = def; CALL getin(nam, val) |
---|
81 | IF(val/=def) WRITE(lunout,*)TRIM(nam)//' = '//TRIM(real2str(val)) |
---|
82 | END SUBROUTINE getin_r |
---|
83 | !============================================================================================================================== |
---|
84 | SUBROUTINE getin_l(nam, val, def) |
---|
85 | USE ioipsl_getincom, ONLY: getin |
---|
86 | CHARACTER(LEN=*), INTENT(IN) :: nam |
---|
87 | LOGICAL, INTENT(INOUT) :: val |
---|
88 | LOGICAL, INTENT(IN) :: def |
---|
89 | val = def; CALL getin(nam, val) |
---|
90 | IF(val.NEQV.def) WRITE(lunout,*)TRIM(nam)//' = '//TRIM(bool2str(val)) |
---|
91 | END SUBROUTINE getin_l |
---|
92 | !============================================================================================================================== |
---|
93 | |
---|
94 | |
---|
95 | !============================================================================================================================== |
---|
96 | !=== Display one or several messages, one each line, starting with the current routine name "modname". |
---|
97 | !============================================================================================================================== |
---|
98 | SUBROUTINE msg_1(str, modname, ll, unit) |
---|
99 | !--- Display a simple message "str". Optional parameters: |
---|
100 | ! * "modname": module name, displayed in front of the message (with ": " separator) if present. |
---|
101 | ! * "ll": message trigger ; message is displayed only if ll==.TRUE. |
---|
102 | ! * "unit": write unit (by default: "lunout") |
---|
103 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
104 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: modname |
---|
105 | LOGICAL, OPTIONAL, INTENT(IN) :: ll |
---|
106 | INTEGER, OPTIONAL, INTENT(IN) :: unit |
---|
107 | CHARACTER(LEN=maxlen) :: subn |
---|
108 | INTEGER :: unt |
---|
109 | subn = ''; IF(PRESENT(modname)) subn = modname |
---|
110 | IF(PRESENT(ll)) THEN; IF(.NOT.ll) RETURN; END IF |
---|
111 | unt = lunout; IF(PRESENT(unit)) unt = unit |
---|
112 | IF(subn == '') WRITE(unt,'(a)') str !--- Simple message |
---|
113 | IF(subn /= '') WRITE(unt,'(a)') TRIM(subn)//': '//str !--- Routine name provided |
---|
114 | END SUBROUTINE msg_1 |
---|
115 | !============================================================================================================================== |
---|
116 | SUBROUTINE msg_m(str, modname, ll, unit, nmax) |
---|
117 | !--- Same as msg_1 with multiple strings that are stacked (separator: coma) on up to "nmax" full lines. |
---|
118 | CHARACTER(LEN=*), INTENT(IN) :: str(:) |
---|
119 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: modname |
---|
120 | LOGICAL, OPTIONAL, INTENT(IN) :: ll |
---|
121 | INTEGER, OPTIONAL, INTENT(IN) :: unit |
---|
122 | INTEGER, OPTIONAL, INTENT(IN) :: nmax |
---|
123 | CHARACTER(LEN=maxlen), ALLOCATABLE :: s(:) |
---|
124 | CHARACTER(LEN=maxlen) :: subn |
---|
125 | INTEGER :: unt, nmx, k |
---|
126 | LOGICAL :: l |
---|
127 | subn = ''; IF(PRESENT(modname)) subn = modname |
---|
128 | l = .TRUE.; IF(PRESENT(ll)) l = ll |
---|
129 | unt = lunout; IF(PRESENT(unit)) unt = unit |
---|
130 | nmx = 128; IF(PRESENT(nmax)) nmx = nmax |
---|
131 | s = strStackm(str, ', ', nmx) |
---|
132 | DO k=1,SIZE(s); CALL msg_1(s(k), subn, l, unt); END DO |
---|
133 | END SUBROUTINE msg_m |
---|
134 | !============================================================================================================================== |
---|
135 | LOGICAL FUNCTION fmsg_1(str, modname, ll, unit) RESULT(l) |
---|
136 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
137 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: modname |
---|
138 | LOGICAL, OPTIONAL, INTENT(IN) :: ll |
---|
139 | INTEGER, OPTIONAL, INTENT(IN) :: unit |
---|
140 | CHARACTER(LEN=maxlen) :: subn |
---|
141 | INTEGER :: unt |
---|
142 | subn = ''; IF(PRESENT(modname)) subn = modname |
---|
143 | l = .TRUE.; IF(PRESENT(ll)) l = ll |
---|
144 | unt = lunout; IF(PRESENT(unit)) unt = unit |
---|
145 | CALL msg_1(str, subn, l, unt) |
---|
146 | END FUNCTION fmsg_1 |
---|
147 | !============================================================================================================================== |
---|
148 | LOGICAL FUNCTION fmsg_m(str, modname, ll, unit, nmax) RESULT(l) |
---|
149 | CHARACTER(LEN=*), INTENT(IN) :: str(:) |
---|
150 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: modname |
---|
151 | LOGICAL, OPTIONAL, INTENT(IN) :: ll |
---|
152 | INTEGER, OPTIONAL, INTENT(IN) :: unit |
---|
153 | INTEGER, OPTIONAL, INTENT(IN) :: nmax |
---|
154 | CHARACTER(LEN=maxlen) :: subn |
---|
155 | INTEGER :: unt, nmx |
---|
156 | subn = ''; IF(PRESENT(modname)) subn = modname |
---|
157 | l = .TRUE.; IF(PRESENT(ll)) l = ll |
---|
158 | unt = lunout; IF(PRESENT(unit)) unt = unit |
---|
159 | nmx = 128; IF(PRESENT(nmax)) nmx = nmax |
---|
160 | CALL msg_m(str, subn, l, unt, nmx) |
---|
161 | END FUNCTION fmsg_m |
---|
162 | !============================================================================================================================== |
---|
163 | |
---|
164 | |
---|
165 | !============================================================================================================================== |
---|
166 | !=== Lower/upper case conversion function. ==================================================================================== |
---|
167 | !============================================================================================================================== |
---|
168 | ELEMENTAL CHARACTER(LEN=maxlen) FUNCTION strLower(str) RESULT(out) |
---|
169 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
170 | INTEGER :: k |
---|
171 | out = str |
---|
172 | DO k=1,LEN_TRIM(str) |
---|
173 | IF(str(k:k)>='A' .AND. str(k:k)<='Z') out(k:k)=ACHAR(IACHAR(str(k:k))+32) |
---|
174 | END DO |
---|
175 | END FUNCTION strLower |
---|
176 | !============================================================================================================================== |
---|
177 | ELEMENTAL CHARACTER(LEN=maxlen) FUNCTION strUpper(str) RESULT(out) |
---|
178 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
179 | INTEGER :: k |
---|
180 | out = str |
---|
181 | DO k=1,LEN_TRIM(str) |
---|
182 | IF(str(k:k)>='a' .AND. str(k:k)<='z') out(k:k)=ACHAR(IACHAR(str(k:k))-32) |
---|
183 | END DO |
---|
184 | END FUNCTION strUpper |
---|
185 | !============================================================================================================================== |
---|
186 | |
---|
187 | |
---|
188 | !============================================================================================================================== |
---|
189 | !=== Extract the substring in front of the last (first if lFirst==TRUE) occurrence of separator "sep" in "str" ================ |
---|
190 | !=== Examples for str='a_b_c' and sep='_' and the bash command with the same effect: ================ |
---|
191 | !=== * strHead(..,.FALSE.) = 'a' ${str%%$sep*} ================ |
---|
192 | !=== * strHead(..,.TRUE.) = 'a_b' ${str%$sep*} ================ |
---|
193 | !============================================================================================================================== |
---|
194 | CHARACTER(LEN=maxlen) FUNCTION strHead_1(str,sep,lFirst) RESULT(out) |
---|
195 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
196 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep |
---|
197 | LOGICAL, OPTIONAL, INTENT(IN) :: lFirst |
---|
198 | LOGICAL :: lf |
---|
199 | lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst |
---|
200 | IF(PRESENT(sep)) THEN |
---|
201 | out = str(1:INDEX(str,sep,.NOT.lf)-1) |
---|
202 | ELSE |
---|
203 | out = str(1:INDEX(str,'/',.NOT.lf)-1) |
---|
204 | END IF |
---|
205 | IF(out == '') out = str |
---|
206 | END FUNCTION strHead_1 |
---|
207 | !============================================================================================================================== |
---|
208 | FUNCTION strHead_m(str,sep,lFirst) RESULT(out) |
---|
209 | CHARACTER(LEN=maxlen), ALLOCATABLE :: out(:) |
---|
210 | CHARACTER(LEN=*), INTENT(IN) :: str(:) |
---|
211 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep |
---|
212 | LOGICAL, OPTIONAL, INTENT(IN) :: lFirst |
---|
213 | LOGICAL :: lf |
---|
214 | INTEGER :: k |
---|
215 | lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst |
---|
216 | IF(PRESENT(sep)) THEN |
---|
217 | out = [(strHead_1(str(k), sep, lf), k=1, SIZE(str))] |
---|
218 | ELSE |
---|
219 | out = [(strHead_1(str(k), lFirst=lf), k=1, SIZE(str))] |
---|
220 | END IF |
---|
221 | END FUNCTION strHead_m |
---|
222 | !============================================================================================================================== |
---|
223 | !=== Extract the substring following the last (first if lFirst==TRUE) occurrence of separator "sep" in "str" ================ |
---|
224 | !=== Examples for str='a_b_c' and sep='_' and the bash command with the same effect: ================ |
---|
225 | !=== * strTail(..,.FALSE.) = 'c' ${str#*$sep} ================ |
---|
226 | !=== * strTail(..,.TRUE.) = 'b_c' ${str##*$sep} ================ |
---|
227 | !============================================================================================================================== |
---|
228 | CHARACTER(LEN=maxlen) FUNCTION strTail_1(str,sep,lFirst) RESULT(out) |
---|
229 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
230 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep |
---|
231 | LOGICAL, OPTIONAL, INTENT(IN) :: lFirst |
---|
232 | LOGICAL :: lf |
---|
233 | lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst |
---|
234 | IF(PRESENT(sep)) THEN |
---|
235 | out = str(INDEX(str,sep,.NOT.lf)+LEN(sep):LEN_TRIM(str)) |
---|
236 | ELSE |
---|
237 | out = str(INDEX(str,'/',.NOT.lf)+1:LEN_TRIM(str)) |
---|
238 | END IF |
---|
239 | IF(out == '') out = str |
---|
240 | END FUNCTION strTail_1 |
---|
241 | !============================================================================================================================== |
---|
242 | FUNCTION strTail_m(str,sep,lFirst) RESULT(out) |
---|
243 | CHARACTER(LEN=maxlen), ALLOCATABLE :: out(:) |
---|
244 | CHARACTER(LEN=*), INTENT(IN) :: str(:) |
---|
245 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep |
---|
246 | LOGICAL, OPTIONAL, INTENT(IN) :: lFirst |
---|
247 | LOGICAL :: lf |
---|
248 | INTEGER :: k |
---|
249 | lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst |
---|
250 | IF(PRESENT(sep)) THEN |
---|
251 | out = [(strTail_1(str(k), sep, lf), k=1, SIZE(str))] |
---|
252 | ELSE |
---|
253 | out = [(strTail_1(str(k), lFirst=lf), k=1, SIZE(str))] |
---|
254 | END IF |
---|
255 | END FUNCTION strTail_m |
---|
256 | !============================================================================================================================== |
---|
257 | |
---|
258 | |
---|
259 | !============================================================================================================================== |
---|
260 | !=== Concatenates the strings "str(:)" with separator "sep" into a single string using a separator (',' by default). ========== |
---|
261 | !============================================================================================================================== |
---|
262 | FUNCTION strStack(str, sep, mask) RESULT(out) |
---|
263 | CHARACTER(LEN=:), ALLOCATABLE :: out |
---|
264 | CHARACTER(LEN=*), INTENT(IN) :: str(:) |
---|
265 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep |
---|
266 | LOGICAL, OPTIONAL, INTENT(IN) :: mask(:) |
---|
267 | CHARACTER(LEN=:), ALLOCATABLE :: s |
---|
268 | INTEGER :: is, i0 |
---|
269 | IF(SIZE(str) == 0) THEN; out = ''; RETURN; END IF |
---|
270 | ALLOCATE(s, SOURCE=', '); IF(PRESENT(sep)) s=sep |
---|
271 | IF(PRESENT(mask)) THEN |
---|
272 | IF(ALL(.NOT.mask)) THEN; out = ''; RETURN; END IF |
---|
273 | i0 = 0; DO WHILE(.NOT.mask(i0+1)); i0 = i0+1; END DO |
---|
274 | out = str(i0); DO is=i0+1,SIZE(str, DIM=1); IF(.NOT.mask(is)) CYCLE; out = TRIM(out)//s//TRIM(str(is)); END DO |
---|
275 | ELSE |
---|
276 | out = str(1); DO is=2,SIZE(str, DIM=1); out = TRIM(out)//s//TRIM(str(is)); END DO |
---|
277 | END IF |
---|
278 | END FUNCTION strStack |
---|
279 | !============================================================================================================================== |
---|
280 | !=== Concatenate the strings "str(:)" with separator "sep" into one or several lines of "nmax" characters max (for display) === |
---|
281 | !============================================================================================================================== |
---|
282 | FUNCTION strStackm(str, sep, nmax) RESULT(out) |
---|
283 | CHARACTER(LEN=maxlen), ALLOCATABLE :: out(:) |
---|
284 | CHARACTER(LEN=*), INTENT(IN) :: str(:) |
---|
285 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep |
---|
286 | INTEGER, OPTIONAL, INTENT(IN) :: nmax |
---|
287 | CHARACTER(LEN=maxlen), ALLOCATABLE :: t(:) |
---|
288 | CHARACTER(LEN=maxlen) :: sp |
---|
289 | INTEGER :: is, ns, no, mx, n |
---|
290 | IF(SIZE(str) == 0) THEN; out = ['']; RETURN; END IF |
---|
291 | sp =', '; IF(PRESENT(sep )) sp = sep |
---|
292 | ns = 2 ; IF(PRESENT(sep )) ns = LEN(sep) |
---|
293 | mx = 256; IF(PRESENT(nmax)) mx = nmax |
---|
294 | no = 1; out = [''] |
---|
295 | DO is = 1, SIZE(str) |
---|
296 | n = LEN_TRIM(str(is)); IF(out(no)/='') n = n+ns+LEN_TRIM(out(no)) !--- Line length after "str(is)" inclusion |
---|
297 | IF(out(no) == '') THEN |
---|
298 | out(no) = str(is) !--- Empty new line: set to "str(is)" |
---|
299 | ELSE IF(n <= mx) THEN |
---|
300 | out(no) = TRIM(out(no))//sp(1:ns)//TRIM(str(is)) !--- Append "str(is)" to the current line |
---|
301 | ELSE |
---|
302 | ALLOCATE(t(no+1)); t(1:no) = out; no=no+1; t(no) = str(is) !--- Full line: "str(si)" put in next line |
---|
303 | CALL MOVE_ALLOC(FROM=t, TO=out) |
---|
304 | END IF |
---|
305 | END DO |
---|
306 | END FUNCTION strStackm |
---|
307 | !============================================================================================================================== |
---|
308 | |
---|
309 | |
---|
310 | !============================================================================================================================== |
---|
311 | !=== String cleaning: replace tabulation by spaces, remove NULL characters and comments. ====================================== |
---|
312 | !============================================================================================================================== |
---|
313 | SUBROUTINE strClean_1(str) |
---|
314 | CHARACTER(LEN=*), INTENT(INOUT) :: str |
---|
315 | INTEGER :: k, n, m |
---|
316 | n = LEN(str) |
---|
317 | DO k = n, 1, -1 |
---|
318 | m = IACHAR(str(k:k)) |
---|
319 | IF(m==9) str(k:k) = ' ' !--- Replace the tabulations with spaces |
---|
320 | IF(m==0) str(k:n) = str(k+1:n)//' ' !--- Remove the NULL characters |
---|
321 | END DO |
---|
322 | m = INDEX(str,'!')-1; IF(m==-1) m = LEN_TRIM(str) !--- Remove end of line comment |
---|
323 | str = ADJUSTL(str(1:m)) |
---|
324 | END SUBROUTINE strClean_1 |
---|
325 | !============================================================================================================================== |
---|
326 | SUBROUTINE strClean_m(str) |
---|
327 | CHARACTER(LEN=*), INTENT(INOUT) :: str(:) |
---|
328 | INTEGER :: k |
---|
329 | DO k = 1, SIZE(str); CALL strClean_1(str(k)); END DO |
---|
330 | END SUBROUTINE strClean_m |
---|
331 | !============================================================================================================================== |
---|
332 | |
---|
333 | |
---|
334 | !============================================================================================================================== |
---|
335 | !=== strReduce_1(str1) : Remove duplicated elements of str1. =========================================================== |
---|
336 | !=== strReduce_2(str1,str2): Append str1 with new elements of str2. =========================================================== |
---|
337 | !============================================================================================================================== |
---|
338 | SUBROUTINE strReduce_1(str, nb) |
---|
339 | CHARACTER(LEN=*), ALLOCATABLE, INTENT(INOUT) :: str(:) |
---|
340 | INTEGER, OPTIONAL, INTENT(OUT) :: nb |
---|
341 | CHARACTER(LEN=maxlen), ALLOCATABLE :: s1(:) |
---|
342 | INTEGER :: k, n, n1 |
---|
343 | IF(PRESENT(nb)) nb = 0 |
---|
344 | CALL MOVE_ALLOC(FROM=str, TO=s1); CALL strClean(s1) |
---|
345 | n1 = SIZE(s1, DIM=1) !--- Total nb. of elements in "s1" |
---|
346 | n = COUNT( [( ALL(s1(1:k-1)/=s1(k)), k=1, n1 )] ) !--- Nb of unique elements in "s1" |
---|
347 | ALLOCATE(str(n)) |
---|
348 | IF(n==0) RETURN |
---|
349 | str(1) = s1(1) |
---|
350 | n=1; DO k=2,n1; IF(ANY(s1(1:k-1)==s1(k))) CYCLE; n=n+1; str(n)=s1(k); END DO |
---|
351 | IF(PRESENT(nb)) nb=n |
---|
352 | END SUBROUTINE strReduce_1 |
---|
353 | !============================================================================================================================== |
---|
354 | SUBROUTINE strReduce_2(str1, str2) |
---|
355 | CHARACTER(LEN=*), ALLOCATABLE, INTENT(INOUT) :: str1(:) |
---|
356 | CHARACTER(LEN=*), INTENT(IN) :: str2(:) |
---|
357 | CHARACTER(LEN=maxlen), ALLOCATABLE :: s1(:), s2(:) |
---|
358 | INTEGER :: k |
---|
359 | IF(SIZE(str2)==0) RETURN |
---|
360 | s2 = str2; CALL strClean(s2) |
---|
361 | IF(.NOT.ALLOCATED(s2)) RETURN |
---|
362 | IF(SIZE(s2) == 0) THEN; DEALLOCATE(s2); RETURN; END IF |
---|
363 | IF(.NOT.ALLOCATED(str1)) THEN |
---|
364 | str1 = s2 |
---|
365 | ELSE IF(SIZE(str1)==0) THEN |
---|
366 | str1 = s2 |
---|
367 | ELSE |
---|
368 | s1 = str1; CALL strClean(s1) |
---|
369 | str1 = [s1, PACK(s2, MASK= [( ALL(s1(:) /= s2(k)), k=1, SIZE(s2) )] ) ] |
---|
370 | END IF |
---|
371 | END SUBROUTINE strReduce_2 |
---|
372 | !============================================================================================================================== |
---|
373 | |
---|
374 | |
---|
375 | !============================================================================================================================== |
---|
376 | !=== GET THE INDEX OF THE FIRST APPEARANCE IN THE STRING VECTOR "str(:)" OF THE STRING(s) "s" ================================= |
---|
377 | !============================================================================================================================== |
---|
378 | INTEGER FUNCTION strIdx_1(str, s) RESULT(out) |
---|
379 | CHARACTER(LEN=*), INTENT(IN) :: str(:), s |
---|
380 | DO out = 1, SIZE(str); IF(str(out) == s) EXIT; END DO |
---|
381 | IF(out == 1+SIZE(str) .OR. SIZE(str)==0) out = 0 |
---|
382 | END FUNCTION strIdx_1 |
---|
383 | !============================================================================================================================== |
---|
384 | FUNCTION strIdx_m(str, s, n) RESULT(out) |
---|
385 | CHARACTER(LEN=*), INTENT(IN) :: str(:), s(:) |
---|
386 | INTEGER, OPTIONAL, INTENT(OUT) :: n |
---|
387 | INTEGER, ALLOCATABLE :: out(:) |
---|
388 | INTEGER :: k |
---|
389 | out = [(strIdx_1(str(:), s(k)), k=1, SIZE(s))] |
---|
390 | IF(PRESENT(n)) n = COUNT(out(:)/=0) |
---|
391 | END FUNCTION strIdx_m |
---|
392 | !============================================================================================================================== |
---|
393 | |
---|
394 | |
---|
395 | !============================================================================================================================== |
---|
396 | !=== GET THE INDEX LIST OF THE ELEMENTS OF "str(:)" EQUAL TO "s" AND OPTIONALY, ITS LENGTH "n" ================================ |
---|
397 | !============================================================================================================================== |
---|
398 | FUNCTION strFind(str, s, n) RESULT(out) |
---|
399 | CHARACTER(LEN=*), INTENT(IN) :: str(:), s |
---|
400 | INTEGER, OPTIONAL, INTENT(OUT) :: n |
---|
401 | INTEGER, ALLOCATABLE :: out(:) |
---|
402 | INTEGER :: k |
---|
403 | out = PACK( [(k, k=1, SIZE(str(:), DIM=1))], MASK = str(:) == s ) |
---|
404 | IF(PRESENT(n)) n = SIZE(out(:), DIM=1) |
---|
405 | END FUNCTION strFind |
---|
406 | !============================================================================================================================== |
---|
407 | FUNCTION find_int(i,j,n) RESULT(out) |
---|
408 | INTEGER, INTENT(IN) :: i(:), j |
---|
409 | INTEGER, OPTIONAL, INTENT(OUT) :: n |
---|
410 | INTEGER, ALLOCATABLE :: out(:) |
---|
411 | INTEGER :: k |
---|
412 | out = PACK( [(k, k=1, SIZE(i(:), DIM=1))], MASK = i(:) == j ) |
---|
413 | IF(PRESENT(n)) n = SIZE(out(:), DIM=1) |
---|
414 | END FUNCTION find_int |
---|
415 | !============================================================================================================================== |
---|
416 | FUNCTION find_boo(l,n) RESULT(out) |
---|
417 | LOGICAL, INTENT(IN) :: l(:) |
---|
418 | INTEGER, OPTIONAL, INTENT(OUT) :: n |
---|
419 | INTEGER, ALLOCATABLE :: out(:) |
---|
420 | INTEGER :: k |
---|
421 | out = PACK( [(k, k=1, SIZE(l(:), DIM=1))], MASK = l(:) ) |
---|
422 | IF(PRESENT(n)) n = SIZE(out(:), DIM=1) |
---|
423 | END FUNCTION find_boo |
---|
424 | !============================================================================================================================== |
---|
425 | |
---|
426 | |
---|
427 | |
---|
428 | !============================================================================================================================== |
---|
429 | !=== GET 1ST APPEARANCE INDEX OF EACH ELEMENT OF "t(:)" IN "s(:)" (UNFOUND: INDEX=0) ========================================== |
---|
430 | !=== OPTIONALY: GET THE NUMBER OF FOUND ELEMENTS "n" ========================================== |
---|
431 | !============================================================================================================================== |
---|
432 | |
---|
433 | |
---|
434 | !============================================================================================================================== |
---|
435 | !=== GET THE INDEX IN "rawList" OF THE 1ST APPEARANCE OF ONE OF THE "del(:)" SEPARATORS (0 IF NONE OF THEM ARE PRESENT) |
---|
436 | !=== IF lSc == .TRUE.: * SKIP HEAD SIGNS OR EXPONENTS SIGNS THAT SHOULD NOT BE CONFUSED WITH SEPARATORS |
---|
437 | !=== * THEN TEST WHETHER THE STRING FROM START TO THE FOUND SEPARATOR IS A CORRECTLY FORMATTED NUMBER |
---|
438 | !============================================================================================================================== |
---|
439 | LOGICAL FUNCTION strIdx_prv(rawList, del, ibeg, idx, idel, lSc) RESULT(lerr) |
---|
440 | CHARACTER(LEN=*), INTENT(IN) :: rawList !--- String in which delimiters have to be identified |
---|
441 | CHARACTER(LEN=*), INTENT(IN) :: del(:) !--- List of delimiters |
---|
442 | INTEGER, INTENT(IN) :: ibeg !--- Start index |
---|
443 | INTEGER, INTENT(OUT) :: idx !--- Index of the first identified delimiter in "rawList" |
---|
444 | INTEGER, INTENT(OUT) :: idel !--- Index of the identified delimiter (0 if idx==0) |
---|
445 | LOGICAL, OPTIONAL, INTENT(IN) :: lSc !--- Care about nbs with front sign or in scient. notation |
---|
446 | |
---|
447 | INTEGER :: idx0 !--- Used to display an identified non-numeric string |
---|
448 | INTEGER, ALLOCATABLE :: ii(:) |
---|
449 | LOGICAL :: ll, ls |
---|
450 | CHARACTER(LEN=maxlen) :: d |
---|
451 | ! modname = 'strIdx' |
---|
452 | lerr = .FALSE. |
---|
453 | idx = strIdx1(rawList, del, ibeg, idel) !--- del(idel) appears in "rawList" at position idx |
---|
454 | IF(.NOT.PRESENT(lSc)) RETURN !--- No need to check exceptions for numbers => finished |
---|
455 | IF(.NOT. lSc ) RETURN !--- No need to check exceptions for numbers => finished |
---|
456 | IF(idx == 0) THEN !--- No element of "del" in "rawList": |
---|
457 | lerr = .NOT.is_numeric(rawList(ibeg:)) !--- String must be a number |
---|
458 | IF(lerr) idx = LEN_TRIM(rawList); RETURN !--- Update idx => rawList(ibeg:idx-1) is the whole string |
---|
459 | END IF |
---|
460 | idx0 = idx |
---|
461 | IF(test(idx==1.AND.INDEX('+-',del(idel))/=0, lerr)) RETURN !--- Front separator different from +/-: error |
---|
462 | IF(idx/=1.AND.is_numeric(rawList(ibeg:idx-1))) RETURN !--- The input string tail is a valid number |
---|
463 | idx = strIdx1(rawList, del, idx+1, idel) !--- => TO THE NEXT DELIMITER |
---|
464 | IF(idx == 0) THEN |
---|
465 | lerr = .NOT.is_numeric(rawList(ibeg:)) !--- No delimiter detected: string must be a number |
---|
466 | IF(lerr) idx = idx0; RETURN |
---|
467 | END IF |
---|
468 | idx0 = idx |
---|
469 | IF(is_numeric(rawList(ibeg:idx-1))) RETURN !--- The input string tail is a valid number |
---|
470 | IF(test( INDEX('eE',rawList(idx-1:idx-1)) /= 0 & !--- Sole possible exception: scientific notation: E+/- |
---|
471 | .OR. INDEX('+-',del(idel)) /=0, lerr)) RETURN |
---|
472 | idx = strIdx1(rawList, del, idx+1, idel) !--- => TO THE NEXT DELIMITER |
---|
473 | IF(idx == 0) THEN |
---|
474 | lerr = .NOT.is_numeric(rawList(ibeg:)) !--- No separator detected: string must be a number |
---|
475 | IF(lerr) idx = idx0; RETURN |
---|
476 | END IF |
---|
477 | lerr = .NOT.is_numeric(rawList(ibeg:idx-1)) |
---|
478 | CONTAINS |
---|
479 | |
---|
480 | INTEGER FUNCTION strIdx1(str, del, ib, id) RESULT(idx) |
---|
481 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
482 | CHARACTER(LEN=*), INTENT(IN) :: del(:) |
---|
483 | INTEGER, INTENT(IN) :: ib |
---|
484 | INTEGER, INTENT(OUT) :: id |
---|
485 | |
---|
486 | INTEGER :: nd, ns, i |
---|
487 | INTEGER, ALLOCATABLE :: ii(:) |
---|
488 | |
---|
489 | nd = SIZE(del) !--- Number of separators |
---|
490 | ns = LEN_TRIM(str) !--- Length of the raw chain |
---|
491 | ii = [(INDEX( str(ib:ns), del(i) ), i = 1, nd)] !--- Determine the next separator start index |
---|
492 | id = MINLOC( ii, MASK = ii /= 0, DIM = 1 ) !--- Current delimiter index in the "delimiter(:)" list |
---|
493 | idx = 0 |
---|
494 | IF(ANY(ii /= 0)) idx = MINVAL( ii, MASK = ii /= 0 ) + ib - 1 !--- Index in "str(1:ns)" of the delimiter first character |
---|
495 | IF(idx == 0) id = 0 |
---|
496 | END FUNCTION strIdx1 |
---|
497 | |
---|
498 | END FUNCTION strIdx_prv |
---|
499 | !============================================================================================================================== |
---|
500 | |
---|
501 | |
---|
502 | !============================================================================================================================== |
---|
503 | !=== Return the index of first appearance of "del" in "str" starting from index "ib" |
---|
504 | !============================================================================================================================== |
---|
505 | INTEGER FUNCTION strIndex(str, del, ib) RESULT(idx) |
---|
506 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
507 | CHARACTER(LEN=*), INTENT(IN) :: del |
---|
508 | INTEGER, INTENT(IN) :: ib |
---|
509 | idx = INDEX( str(ib:LEN_TRIM(str)), del ) + ib -1 |
---|
510 | END FUNCTION strIndex |
---|
511 | !============================================================================================================================== |
---|
512 | |
---|
513 | |
---|
514 | !============================================================================================================================== |
---|
515 | !=== Count the number of elements separated by "delimiter" in list "rawList". ================================================= |
---|
516 | !============================================================================================================================== |
---|
517 | LOGICAL FUNCTION strCount_11(rawList, delimiter, nb, lSc) RESULT(out) |
---|
518 | CHARACTER(LEN=*), INTENT(IN) :: rawList |
---|
519 | CHARACTER(LEN=*), INTENT(IN) :: delimiter |
---|
520 | INTEGER, INTENT(OUT) :: nb |
---|
521 | LOGICAL, OPTIONAL, INTENT(IN) :: lSc |
---|
522 | LOGICAL :: ll |
---|
523 | ll = .FALSE.; IF(PRESENT(lSc)) ll = lSc |
---|
524 | out = strCount_1m(rawList, [delimiter], nb, ll) |
---|
525 | END FUNCTION strCount_11 |
---|
526 | !============================================================================================================================== |
---|
527 | LOGICAL FUNCTION strCount_m1(rawList, delimiter, nb, lSc) RESULT(out) |
---|
528 | CHARACTER(LEN=*), INTENT(IN) :: rawList(:) |
---|
529 | CHARACTER(LEN=*), INTENT(IN) :: delimiter |
---|
530 | INTEGER, ALLOCATABLE, INTENT(OUT) :: nb(:) |
---|
531 | LOGICAL, OPTIONAL, INTENT(IN) :: lSc |
---|
532 | |
---|
533 | LOGICAL :: ll |
---|
534 | INTEGER :: id |
---|
535 | |
---|
536 | ll = .FALSE.; IF(PRESENT(lSc)) ll = lSc .AND. INDEX('+-', delimiter) /= 0 |
---|
537 | out = .TRUE. |
---|
538 | ALLOCATE(nb(SIZE(rawList))) |
---|
539 | DO id = 1, SIZE(rawList) |
---|
540 | out = out .AND. strCount_1m(rawList(id), [delimiter], nb(id), ll) |
---|
541 | END DO |
---|
542 | END FUNCTION strCount_m1 |
---|
543 | !============================================================================================================================== |
---|
544 | LOGICAL FUNCTION strCount_1m(rawList, delimiter, nb, lSc) RESULT(lerr) |
---|
545 | CHARACTER(LEN=*), INTENT(IN) :: rawList |
---|
546 | CHARACTER(LEN=*), INTENT(IN) :: delimiter(:) |
---|
547 | INTEGER, INTENT(OUT) :: nb |
---|
548 | LOGICAL, OPTIONAL, INTENT(IN) :: lSc |
---|
549 | INTEGER :: ib, ie, jd, nr |
---|
550 | LOGICAL :: ll |
---|
551 | CHARACTER(LEN=1024) :: r |
---|
552 | ! modname = 'strCount' |
---|
553 | lerr = .FALSE. |
---|
554 | ll = .FALSE.; IF(PRESENT(lSc)) ll = lSc |
---|
555 | r = TRIM(ADJUSTL(rawList)) |
---|
556 | nr = LEN_TRIM(r); IF(nr == 0) RETURN |
---|
557 | nb = 1; ib = 1 |
---|
558 | DO |
---|
559 | lerr = strIdx_prv(r, delimiter, ib, ie, jd, ll) |
---|
560 | IF(fmsg('"'//TRIM(r(ib:ie-1))//'" is not numeric', ll=lerr)) RETURN |
---|
561 | IF(jd == 0) EXIT |
---|
562 | ib = ie + LEN(delimiter(jd)) |
---|
563 | DO WHILE(r(ib:ib) == ' ' .AND. ib < nr); ib = ib + 1; END DO !--- Skip spaces before next chain |
---|
564 | nb = nb + 1 |
---|
565 | END DO |
---|
566 | END FUNCTION strCount_1m |
---|
567 | !============================================================================================================================== |
---|
568 | |
---|
569 | |
---|
570 | !============================================================================================================================== |
---|
571 | !=== Purpose: Parse "delimiter"-separated list "rawList" into the pair keys(:), vals(:). ==================================== |
---|
572 | !=== Corresponding "vals" remains empty if the element does not contain "=" sign. ==================================== |
---|
573 | !============================================================================================================================== |
---|
574 | LOGICAL FUNCTION strParse_1(rawList, delimiter, keys, lSc, vals, n) RESULT(lerr) |
---|
575 | CHARACTER(LEN=*), INTENT(IN) :: rawList, delimiter |
---|
576 | CHARACTER(LEN=maxlen), ALLOCATABLE, INTENT(OUT) :: keys(:) |
---|
577 | LOGICAL, OPTIONAL, INTENT(IN) :: lSc !--- Take care about numbers in scientific notation |
---|
578 | CHARACTER(LEN=maxlen), ALLOCATABLE, OPTIONAL, INTENT(OUT) :: vals(:) |
---|
579 | INTEGER, OPTIONAL, INTENT(OUT) :: n |
---|
580 | LOGICAL :: ll |
---|
581 | ! modname = 'strParse' |
---|
582 | ll = .FALSE.; IF(PRESENT(lSc)) ll = lSc |
---|
583 | IF(.NOT.PRESENT(vals)) lerr = strParse_m(rawList, [delimiter], keys, ll) |
---|
584 | IF( PRESENT(vals)) lerr = strParse_m(rawList, [delimiter], keys, ll, vals) |
---|
585 | IF(PRESENT(n)) n = SIZE(keys) |
---|
586 | END FUNCTION strParse_1 |
---|
587 | !============================================================================================================================== |
---|
588 | LOGICAL FUNCTION strParse_m(rawList, delimiter, keys, lSc, vals, n, id) RESULT(lerr) |
---|
589 | CHARACTER(LEN=*), INTENT(IN) :: rawList, delimiter(:) |
---|
590 | CHARACTER(LEN=maxlen), ALLOCATABLE, INTENT(OUT) :: keys(:) !--- Parsed keys vector |
---|
591 | LOGICAL, OPTIONAL, INTENT(IN) :: lSc !--- Take care about numbers in scientific notation |
---|
592 | CHARACTER(LEN=maxlen), OPTIONAL, ALLOCATABLE, INTENT(OUT) :: vals(:) !--- Values for <name>=<value> keys |
---|
593 | INTEGER, OPTIONAL, INTENT(OUT) :: n !--- Length of the parsed vector |
---|
594 | INTEGER, OPTIONAL, ALLOCATABLE, INTENT(OUT) :: id(:) !--- Indexes of the separators in "delimiter(:)" vector |
---|
595 | |
---|
596 | CHARACTER(LEN=1024) :: r |
---|
597 | INTEGER :: nr, ik, nk, ib, ie, jd |
---|
598 | LOGICAL :: ll |
---|
599 | |
---|
600 | ! modname = 'strParse' |
---|
601 | ll = .FALSE.; IF(PRESENT(lSc)) ll = lSc |
---|
602 | IF(test(fmsg("Couldn't parse list: non-numerical strings were found", ll=strCount_1m(rawList, delimiter, nk, ll)),lerr)) RETURN |
---|
603 | |
---|
604 | !--- FEW ALLOCATIONS |
---|
605 | ALLOCATE(keys(nk)) |
---|
606 | IF(PRESENT(vals)) ALLOCATE(vals(nk)) |
---|
607 | IF(PRESENT(id)) ALLOCATE(id(nk-1)) |
---|
608 | IF(PRESENT(n)) n = nk |
---|
609 | |
---|
610 | !--- PARSING |
---|
611 | r = TRIM(ADJUSTL(rawList)) |
---|
612 | nr = LEN_TRIM(r); IF(nr == 0) RETURN |
---|
613 | ib = 1 |
---|
614 | DO ik = 1, nk-1 |
---|
615 | IF(test(fmsg('Non-numeric values found', ll=strIdx_prv(r, delimiter, ib, ie, jd, ll)),lerr)) RETURN |
---|
616 | keys(ik) = r(ib:ie-1) |
---|
617 | IF(PRESENT(vals)) CALL parseKeys(keys(ik), vals(ik)) !--- Parse a <key>=<val> pair |
---|
618 | IF(PRESENT(id )) id(ik) = jd !--- Index in "delimiter(:)" of the "ik"th delimiter |
---|
619 | ib = ie + LEN_TRIM( delimiter(jd) ) !--- Length of the current delimiter |
---|
620 | DO WHILE(r(ib:ib) == ' ' .AND. ib < nr); ib = ib + 1; END DO !--- Skip spaces before next chain |
---|
621 | END DO |
---|
622 | keys(nk) = r(ib:nr) |
---|
623 | IF(PRESENT(vals)) CALL parseKeys(keys(nk), vals(nk)) !--- Parse a <key>=<val> pair |
---|
624 | |
---|
625 | CONTAINS |
---|
626 | |
---|
627 | SUBROUTINE parseKeys(key, val) |
---|
628 | CHARACTER(LEN=*), INTENT(INOUT) :: key |
---|
629 | CHARACTER(LEN=*), INTENT(OUT) :: val |
---|
630 | INTEGER :: ix |
---|
631 | ix = INDEX(key, '='); IF(ix == 0) RETURN !--- First "=" index in "key" |
---|
632 | val = ADJUSTL(key(ix+1:LEN_TRIM(key))) |
---|
633 | key = ADJUSTL(key(1:ix-1)) |
---|
634 | END SUBROUTINE parseKeys |
---|
635 | |
---|
636 | END FUNCTION strParse_m |
---|
637 | !============================================================================================================================== |
---|
638 | |
---|
639 | |
---|
640 | !============================================================================================================================== |
---|
641 | !=== String substitution: replace "key" by "val" each time it appears in "str". |
---|
642 | !============================================================================================================================== |
---|
643 | SUBROUTINE strReplace_1(str, key, val, lsurr) |
---|
644 | CHARACTER(LEN=*), INTENT(INOUT) :: str !--- Main string |
---|
645 | CHARACTER(LEN=*), INTENT(IN) :: key, val !--- "key" will be replaced by "val" |
---|
646 | LOGICAL, OPTIONAL, INTENT(IN) :: lsurr !--- TRUE => key must be surrounded by special characters to be substituted |
---|
647 | |
---|
648 | CHARACTER(LEN=1024) :: s, t |
---|
649 | INTEGER :: i0, ix, nk, ns |
---|
650 | LOGICAL :: lsur, lb, le |
---|
651 | |
---|
652 | lsur = .FALSE.; IF(PRESENT(lsurr)) lsur = lsurr |
---|
653 | nk = LEN_TRIM(key) |
---|
654 | i0 = 1 |
---|
655 | DO |
---|
656 | ns = LEN_TRIM(str) |
---|
657 | ix = INDEX(str(i0:ns), TRIM(key)) !--- First appearance index of "key" in "s", starting from index "i0" |
---|
658 | IF(ix == 0) EXIT |
---|
659 | ix = ix + i0 -1 |
---|
660 | IF(lsur) THEN !--- Key must be surrounded by special characters |
---|
661 | !--- lb=.TRUE.: key is at the very beginning of "str" or located after a special character |
---|
662 | lb = ix ==1; IF(.NOT.lb) lb = INDEX('+-*/()^', str(ix-1 :ix-1 ))/=0 |
---|
663 | !--- le=.TRUE.: key is at the very end of "str" or located before a special character |
---|
664 | le = ix+nk-1==ns; IF(.NOT.le) le = INDEX('+-*/()^', str(ix+nk:ix+nk))/=0 |
---|
665 | IF(.NOT.(lb.AND.le)) THEN; i0 = i0 + nk; CYCLE; END IF |
---|
666 | END IF |
---|
667 | str = str(1:ix-1)//TRIM(val)//str(ix+nk:ns) |
---|
668 | END DO |
---|
669 | END SUBROUTINE strReplace_1 |
---|
670 | !============================================================================================================================== |
---|
671 | SUBROUTINE strReplace_m(str, key, val, lsurr) |
---|
672 | CHARACTER(LEN=*), INTENT(INOUT) :: str(:) !--- Main strings vector |
---|
673 | CHARACTER(LEN=*), INTENT(IN) :: key, val !--- "key" will be replaced by "val" |
---|
674 | LOGICAL, OPTIONAL, INTENT(IN) :: lsurr !--- TRUE => key must be surrounded by special characters to be substituted |
---|
675 | INTEGER :: k |
---|
676 | LOGICAL :: ll |
---|
677 | ll=.FALSE.; IF(PRESENT(lsurr)) ll=lsurr |
---|
678 | DO k=1, SIZE(str); CALL strReplace_1(str(k),key,val,ll); END DO |
---|
679 | END SUBROUTINE strReplace_m |
---|
680 | !============================================================================================================================== |
---|
681 | |
---|
682 | |
---|
683 | !============================================================================================================================== |
---|
684 | !=== Contatenate horizontally scalars/vectors of strings/integers/reals into a vector/array =================================== |
---|
685 | !============================================================================================================================== |
---|
686 | FUNCTION horzcat_s1(s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) RESULT(out) |
---|
687 | CHARACTER(LEN=*), TARGET, INTENT(IN) :: s0 |
---|
688 | CHARACTER(LEN=*), OPTIONAL, TARGET, INTENT(IN) :: s1, s2, s3, s4, s5, s6, s7, s8, s9 |
---|
689 | CHARACTER(LEN=maxlen), ALLOCATABLE :: out(:) |
---|
690 | CHARACTER(LEN=maxlen), POINTER :: s |
---|
691 | LOGICAL :: lv(10) |
---|
692 | INTEGER :: iv |
---|
693 | lv = [ .TRUE. , PRESENT(s1), PRESENT(s2), PRESENT(s3), PRESENT(s4) , & |
---|
694 | PRESENT(s5), PRESENT(s6), PRESENT(s7), PRESENT(s8), PRESENT(s9) ] |
---|
695 | ALLOCATE(out(COUNT(lv))) |
---|
696 | DO iv=1, COUNT(lv) |
---|
697 | SELECT CASE(iv) |
---|
698 | CASE(1); s=> s0; CASE(2); s=> s1; CASE(3); s=> s2; CASE(4); s=> s3; CASE(5); s=> s4 |
---|
699 | CASE(6); s=> s5; CASE(7); s=> s6; CASE(8); s=> s7; CASE(9); s=> s8; CASE(10);s=> s9 |
---|
700 | END SELECT |
---|
701 | out(iv) = s |
---|
702 | END DO |
---|
703 | END FUNCTION horzcat_s1 |
---|
704 | !============================================================================================================================== |
---|
705 | FUNCTION horzcat_sm(s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) RESULT(out) |
---|
706 | CHARACTER(LEN=*), TARGET, DIMENSION(:), INTENT(IN) :: s0 |
---|
707 | CHARACTER(LEN=*), OPTIONAL, TARGET, DIMENSION(:), INTENT(IN) :: s1, s2, s3, s4, s5, s6, s7, s8, s9 |
---|
708 | CHARACTER(LEN=maxlen), ALLOCATABLE :: out(:,:) |
---|
709 | CHARACTER(LEN=maxlen), POINTER :: s(:) |
---|
710 | LOGICAL :: lv(10) |
---|
711 | INTEGER :: nrow, ncol, iv, n |
---|
712 | lv = [ .TRUE. , PRESENT(s1), PRESENT(s2), PRESENT(s3), PRESENT(s4) , & |
---|
713 | PRESENT(s5), PRESENT(s6), PRESENT(s7), PRESENT(s8), PRESENT(s9) ] |
---|
714 | nrow = SIZE(s0); ncol=COUNT(lv) |
---|
715 | ALLOCATE(out(nrow, ncol)) |
---|
716 | DO iv=1, ncol |
---|
717 | SELECT CASE(iv) |
---|
718 | CASE(1); s=> s0; CASE(2); s=> s1; CASE(3); s=> s2; CASE(4); s=> s3; CASE(5); s=> s4 |
---|
719 | CASE(6); s=> s5; CASE(7); s=> s6; CASE(8); s=> s7; CASE(9); s=> s8; CASE(10);s=> s9 |
---|
720 | END SELECT |
---|
721 | n = SIZE(s, DIM=1) |
---|
722 | IF(n/=nrow) THEN; CALL msg("Can't concatenate vectors of differing lengths"); STOP; END IF |
---|
723 | out(:,iv) = s(:) |
---|
724 | END DO |
---|
725 | END FUNCTION horzcat_sm |
---|
726 | !============================================================================================================================== |
---|
727 | FUNCTION horzcat_i1(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) RESULT(out) |
---|
728 | INTEGER, TARGET, INTENT(IN) :: i0 |
---|
729 | INTEGER, OPTIONAL, TARGET, INTENT(IN) :: i1, i2, i3, i4, i5, i6, i7, i8, i9 |
---|
730 | INTEGER, ALLOCATABLE :: out(:) |
---|
731 | INTEGER, POINTER :: i |
---|
732 | LOGICAL :: lv(10) |
---|
733 | INTEGER :: iv |
---|
734 | lv = [ .TRUE. , PRESENT(i1), PRESENT(i2), PRESENT(i3), PRESENT(i4) , & |
---|
735 | PRESENT(i5), PRESENT(i6), PRESENT(i7), PRESENT(i8), PRESENT(i9) ] |
---|
736 | ALLOCATE(out(COUNT(lv))) |
---|
737 | DO iv=1, COUNT(lv) |
---|
738 | SELECT CASE(iv) |
---|
739 | CASE(1); i=> i0; CASE(2); i=> i1; CASE(3); i=> i2; CASE(4); i=> i3; CASE(5); i=> i4 |
---|
740 | CASE(6); i=> i5; CASE(7); i=> i6; CASE(8); i=> i7; CASE(9); i=> i8; CASE(10);i=> i9 |
---|
741 | END SELECT |
---|
742 | out(iv) = i |
---|
743 | END DO |
---|
744 | END FUNCTION horzcat_i1 |
---|
745 | !============================================================================================================================== |
---|
746 | FUNCTION horzcat_im(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9) RESULT(out) |
---|
747 | INTEGER, TARGET, DIMENSION(:), INTENT(IN) :: i0 |
---|
748 | INTEGER, OPTIONAL, TARGET, DIMENSION(:), INTENT(IN) :: i1, i2, i3, i4, i5, i6, i7, i8, i9 |
---|
749 | INTEGER, ALLOCATABLE :: out(:,:) |
---|
750 | INTEGER, POINTER :: i(:) |
---|
751 | LOGICAL :: lv(10) |
---|
752 | INTEGER :: nrow, ncol, iv, n |
---|
753 | lv = [ .TRUE. , PRESENT(i1), PRESENT(i2), PRESENT(i3), PRESENT(i4) , & |
---|
754 | PRESENT(i5), PRESENT(i6), PRESENT(i7), PRESENT(i8), PRESENT(i9) ] |
---|
755 | nrow = SIZE(i0); ncol=COUNT(lv) |
---|
756 | ALLOCATE(out(nrow, ncol)) |
---|
757 | DO iv=1, ncol |
---|
758 | SELECT CASE(iv) |
---|
759 | CASE(1); i=> i0; CASE(2); i=> i1; CASE(3); i=> i2; CASE(4); i=> i3; CASE(5); i=> i4 |
---|
760 | CASE(6); i=> i5; CASE(7); i=> i6; CASE(8); i=> i7; CASE(9); i=> i8; CASE(10);i=> i9 |
---|
761 | END SELECT |
---|
762 | n = SIZE(i, DIM=1) |
---|
763 | IF(n/=nrow) THEN; CALL msg("Can't concatenate vectors of differing lengths"); STOP; END IF |
---|
764 | out(:,iv) = i(:) |
---|
765 | END DO |
---|
766 | END FUNCTION horzcat_im |
---|
767 | !============================================================================================================================== |
---|
768 | FUNCTION horzcat_r1(r0, r1, r2, r3, r4, r5, r6, r7, r8, r9) RESULT(out) |
---|
769 | REAL, TARGET, INTENT(IN) :: r0 |
---|
770 | REAL, OPTIONAL, TARGET, INTENT(IN) :: r1, r2, r3, r4, r5, r6, r7, r8, r9 |
---|
771 | REAL, ALLOCATABLE :: out(:) |
---|
772 | REAL, POINTER :: r |
---|
773 | LOGICAL :: lv(10) |
---|
774 | INTEGER :: iv |
---|
775 | lv = [ .TRUE. , PRESENT(r1), PRESENT(r2), PRESENT(r3), PRESENT(r4) , & |
---|
776 | PRESENT(r5), PRESENT(r6), PRESENT(r7), PRESENT(r8), PRESENT(r9) ] |
---|
777 | ALLOCATE(out(COUNT(lv))) |
---|
778 | DO iv=1, COUNT(lv) |
---|
779 | SELECT CASE(iv) |
---|
780 | CASE(1); r=> r0; CASE(2); r=> r1; CASE(3); r=> r2; CASE(4); r=> r3; CASE(5); r=> r4 |
---|
781 | CASE(6); r=> r5; CASE(7); r=> r6; CASE(8); r=> r7; CASE(9); r=> r8; CASE(10);r=> r9 |
---|
782 | END SELECT |
---|
783 | out(iv) = r |
---|
784 | END DO |
---|
785 | END FUNCTION horzcat_r1 |
---|
786 | !============================================================================================================================== |
---|
787 | FUNCTION horzcat_rm(r0, r1, r2, r3, r4, r5, r6, r7, r8, r9) RESULT(out) |
---|
788 | REAL, TARGET, DIMENSION(:), INTENT(IN) :: r0 |
---|
789 | REAL, OPTIONAL, TARGET, DIMENSION(:), INTENT(IN) :: r1, r2, r3, r4, r5, r6, r7, r8, r9 |
---|
790 | REAL, ALLOCATABLE :: out(:,:) |
---|
791 | REAL, POINTER :: r(:) |
---|
792 | LOGICAL :: lv(10) |
---|
793 | INTEGER :: nrow, ncol, iv, n |
---|
794 | lv = [ .TRUE. , PRESENT(r1), PRESENT(r2), PRESENT(r3), PRESENT(r4) , & |
---|
795 | PRESENT(r5), PRESENT(r6), PRESENT(r7), PRESENT(r8), PRESENT(r9) ] |
---|
796 | nrow = SIZE(r0); ncol=COUNT(lv) |
---|
797 | ALLOCATE(out(nrow, ncol)) |
---|
798 | DO iv=1, ncol |
---|
799 | SELECT CASE(iv) |
---|
800 | CASE(1); r=> r0; CASE(2); r=> r1; CASE(3); r=> r2; CASE(4); r=> r3; CASE(5); r=> r4 |
---|
801 | CASE(6); r=> r5; CASE(7); r=> r6; CASE(8); r=> r7; CASE(9); r=> r8; CASE(10);r=> r9 |
---|
802 | END SELECT |
---|
803 | n = SIZE(r, DIM=1) |
---|
804 | IF(n/=nrow) THEN; CALL msg("Can't concatenate vectors of differing lengths"); STOP; END IF |
---|
805 | out(:,iv) = r(:) |
---|
806 | END DO |
---|
807 | END FUNCTION horzcat_rm |
---|
808 | !============================================================================================================================== |
---|
809 | FUNCTION horzcat_d1(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9) RESULT(out) |
---|
810 | DOUBLE PRECISION, TARGET, INTENT(IN) :: d0 |
---|
811 | DOUBLE PRECISION, OPTIONAL, TARGET, INTENT(IN) :: d1, d2, d3, d4, d5, d6, d7, d8, d9 |
---|
812 | DOUBLE PRECISION, ALLOCATABLE :: out(:) |
---|
813 | DOUBLE PRECISION, POINTER :: d |
---|
814 | LOGICAL :: lv(10) |
---|
815 | INTEGER :: iv |
---|
816 | lv = [ .TRUE. , PRESENT(d1), PRESENT(d2), PRESENT(d3), PRESENT(d4) , & |
---|
817 | PRESENT(d5), PRESENT(d6), PRESENT(d7), PRESENT(d8), PRESENT(d9) ] |
---|
818 | ALLOCATE(out(COUNT(lv))) |
---|
819 | DO iv=1, COUNT(lv) |
---|
820 | SELECT CASE(iv) |
---|
821 | CASE(1); d=> d0; CASE(2); d=> d1; CASE(3); d=> d2; CASE(4); d=> d3; CASE(5); d=> d4 |
---|
822 | CASE(6); d=> d5; CASE(7); d=> d6; CASE(8); d=> d7; CASE(9); d=> d8; CASE(10);d=> d9 |
---|
823 | END SELECT |
---|
824 | out(iv) = d |
---|
825 | END DO |
---|
826 | END FUNCTION horzcat_d1 |
---|
827 | !============================================================================================================================== |
---|
828 | FUNCTION horzcat_dm(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9) RESULT(out) |
---|
829 | DOUBLE PRECISION, TARGET, DIMENSION(:), INTENT(IN) :: d0 |
---|
830 | DOUBLE PRECISION, OPTIONAL, TARGET, DIMENSION(:), INTENT(IN) :: d1, d2, d3, d4, d5, d6, d7, d8, d9 |
---|
831 | DOUBLE PRECISION, ALLOCATABLE :: out(:,:) |
---|
832 | DOUBLE PRECISION, POINTER :: d(:) |
---|
833 | LOGICAL :: lv(10) |
---|
834 | INTEGER :: nrow, ncol, iv, n |
---|
835 | lv = [ .TRUE. , PRESENT(d1), PRESENT(d2), PRESENT(d3), PRESENT(d4) , & |
---|
836 | PRESENT(d5), PRESENT(d6), PRESENT(d7), PRESENT(d8), PRESENT(d9) ] |
---|
837 | nrow = SIZE(d0); ncol=COUNT(lv) |
---|
838 | ALLOCATE(out(nrow, ncol)) |
---|
839 | DO iv=1, ncol |
---|
840 | SELECT CASE(iv) |
---|
841 | CASE(1); d=> d0; CASE(2); d=> d1; CASE(3); d=> d2; CASE(4); d=> d3; CASE(5); d=> d4 |
---|
842 | CASE(6); d=> d5; CASE(7); d=> d6; CASE(8); d=> d7; CASE(9); d=> d8; CASE(10);d=> d9 |
---|
843 | END SELECT |
---|
844 | n = SIZE(d, DIM=1) |
---|
845 | IF(n/=nrow) THEN; CALL msg("Can't concatenate vectors of differing lengths"); STOP; END IF |
---|
846 | out(:,iv) = d(:) |
---|
847 | END DO |
---|
848 | END FUNCTION horzcat_dm |
---|
849 | !============================================================================================================================== |
---|
850 | |
---|
851 | |
---|
852 | !============================================================================================================================== |
---|
853 | !--- Display a clean table composed of successive vectors of same length. |
---|
854 | !=== The profile "p" describe in which order to pick up the columns from "s", "i" and "r" for display. |
---|
855 | !============================================================================================================================== |
---|
856 | LOGICAL FUNCTION dispTable(p, titles, s, i, r, rFmt, nmax, unit, sub) RESULT(lerr) |
---|
857 | CHARACTER(LEN=*), INTENT(IN) :: p !--- DISPLAY MAP: s/i/r |
---|
858 | CHARACTER(LEN=*), INTENT(IN) :: titles(:) !--- TITLES (ONE EACH COLUMN) |
---|
859 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: s(:,:) !--- STRINGS |
---|
860 | INTEGER, OPTIONAL, INTENT(IN) :: i(:,:) !--- INTEGERS |
---|
861 | REAL, OPTIONAL, INTENT(IN) :: r(:,:) !--- REALS |
---|
862 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: rFmt !--- Format for reals |
---|
863 | INTEGER, OPTIONAL, INTENT(IN) :: nmax !--- Display less than "nrow" rows |
---|
864 | INTEGER, OPTIONAL, INTENT(IN) :: unit !--- Output unit (default: screen) |
---|
865 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sub !--- Subroutine name |
---|
866 | |
---|
867 | CHARACTER(LEN=2048) :: row |
---|
868 | CHARACTER(LEN=maxlen) :: rFm, el, subn |
---|
869 | CHARACTER(LEN=maxlen), ALLOCATABLE :: d(:,:) |
---|
870 | CHARACTER(LEN=1) :: s1, sp |
---|
871 | INTEGER :: is, ii, ir, np, nrow, unt, ic |
---|
872 | INTEGER :: ns, ni, nr, ncol, nmx |
---|
873 | INTEGER, ALLOCATABLE :: n(:) |
---|
874 | INTEGER, PARAMETER :: nm=2 !--- Space between values & columns |
---|
875 | LOGICAL :: ls, li, lr |
---|
876 | |
---|
877 | subn = ''; IF(PRESENT(sub)) subn = sub |
---|
878 | rFm = '*'; IF(PRESENT(rFmt)) rFm = rFmt !--- Specified format for reals |
---|
879 | unt = lunout; IF(PRESENT(unit)) unt = unit !--- Specified output unit |
---|
880 | np = LEN_TRIM(p); ns = 0; ni = 0; nr = 0; ncol = 0 |
---|
881 | ls = PRESENT(s); li = PRESENT(i); lr = PRESENT(r) |
---|
882 | lerr = .FALSE.; IF(.NOT.ANY([ls,li,lr])) RETURN !--- Nothing to do |
---|
883 | sp = '|' !--- Separator |
---|
884 | |
---|
885 | !--- CHECK ARGUMENTS COHERENCE |
---|
886 | lerr = np /= SIZE(titles); IF(fmsg('string "pattern" length and titles list mismatch', subn, lerr)) RETURN |
---|
887 | IF(ls) THEN |
---|
888 | ns = SIZE(s, 1); ncol = ncol + SIZE(s, 2); lerr = COUNT([(p(ic:ic)=='s', ic=1, np)]) /= SIZE(s, 2) |
---|
889 | END IF |
---|
890 | IF(li) THEN |
---|
891 | ni = SIZE(i, 1); ncol = ncol + SIZE(i, 2); lerr = COUNT([(p(ic:ic)=='i', ic=1, np)]) /= SIZE(i, 2) |
---|
892 | END IF |
---|
893 | IF(lr) THEN |
---|
894 | nr = SIZE(r, 1); ncol = ncol + SIZE(r, 2); lerr = COUNT([(p(ic:ic)=='r', ic=1, np)]) /= SIZE(r, 2) |
---|
895 | END IF |
---|
896 | IF(fmsg('string "pattern" length and arguments number mismatch', subn, lerr)) RETURN |
---|
897 | lerr = ncol /= SIZE(titles); IF(fmsg('"titles" length and arguments number mismatch', subn, lerr)) RETURN |
---|
898 | lerr = ls.AND.li.AND.ns/=ni; IF(fmsg('string and integer arguments lengths mismatch', subn, lerr)) RETURN |
---|
899 | lerr = ls.AND.lr.AND.ns/=nr; IF(fmsg( 'string and real arguments lengths mismatch', subn, lerr)) RETURN |
---|
900 | lerr = li.AND.lr.AND.ni/=nr; IF(fmsg( 'integer and real arguments lengths mismatch', subn, lerr)) RETURN |
---|
901 | nrow = MAX(ns,ni,nr)+1 |
---|
902 | nmx = nrow; IF(PRESENT(nmax)) nmx = MIN(nmx,nmax+1) |
---|
903 | |
---|
904 | !--- Allocate the assembled quantities array |
---|
905 | ALLOCATE(d(nrow,ncol), n(ncol)) |
---|
906 | |
---|
907 | !--- Assemble the vectors into a strings array in the order indicated by "pattern" |
---|
908 | is = 1; ii = 1; ir = 1 |
---|
909 | DO ic = 1, ncol |
---|
910 | d(1,ic) = TRIM(titles(ic)) |
---|
911 | SELECT CASE(p(ic:ic)) |
---|
912 | CASE('s'); d(2:nmx,ic) = s(:,is) ; is = is + 1 |
---|
913 | CASE('i'); d(2:nmx,ic) = int2str(i(:,ii) ); ii = ii + 1 |
---|
914 | CASE('r'); d(2:nmx,ic) = real2str(r(:,ir),rFm); ir = ir + 1 |
---|
915 | END SELECT |
---|
916 | n(ic)=0; DO ir=1, nmx; n(ic)=MAX(n(ic), LEN_TRIM(d(ir,ic))); END DO |
---|
917 | END DO |
---|
918 | n(:) = n(:) + 2*nm |
---|
919 | |
---|
920 | !--- Display the strings array as a table |
---|
921 | DO ir = 1, nmx; row = '' |
---|
922 | DO ic = 1, ncol; el = d(ir,ic) |
---|
923 | s1 = sp |
---|
924 | row = TRIM(row)//REPEAT(' ',nm)//TRIM(el)//REPEAT(' ',n(ic)-LEN_TRIM(el)-nm)//s1 |
---|
925 | END DO |
---|
926 | nr = LEN_TRIM(row)-1 !--- Final separator removed |
---|
927 | CALL msg(row(1:nr), subn, unit=unt) |
---|
928 | IF(ir /= 1) CYCLE !--- Titles are underlined |
---|
929 | row=''; DO ic=1,ncol; row=TRIM(row)//REPEAT('-',n(ic))//'+'; END DO |
---|
930 | CALL msg(row(1:LEN_TRIM(row)-1), subn, unit=unt) |
---|
931 | END DO |
---|
932 | |
---|
933 | END FUNCTION dispTable |
---|
934 | !============================================================================================================================== |
---|
935 | |
---|
936 | !============================================================================================================================== |
---|
937 | LOGICAL FUNCTION dispNamelist(unt, p, titles, s, i, r, rFmt, llast) RESULT(lerr) |
---|
938 | INTEGER, INTENT(IN) :: unt !--- Output unit |
---|
939 | CHARACTER(LEN=*), INTENT(IN) :: p !--- DISPLAY MAP: s/i/r |
---|
940 | CHARACTER(LEN=*), INTENT(IN) :: titles(:) !--- TITLES (ONE EACH COLUMN) |
---|
941 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: s(:,:) !--- STRINGS |
---|
942 | INTEGER, OPTIONAL, INTENT(IN) :: i(:,:) !--- INTEGERS |
---|
943 | REAL, OPTIONAL, INTENT(IN) :: r(:,:) !--- REALS |
---|
944 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: rFmt !--- Format for reals |
---|
945 | LOGICAL, OPTIONAL, INTENT(IN) :: llast !--- Last variable: no final ',' |
---|
946 | |
---|
947 | CHARACTER(LEN=maxlen) :: rFm, el |
---|
948 | CHARACTER(LEN=maxlen), ALLOCATABLE :: d(:,:) |
---|
949 | CHARACTER(LEN=:), ALLOCATABLE :: sp, row |
---|
950 | INTEGER :: is, ii, ir, nrow, ic |
---|
951 | INTEGER :: ns, ni, nr, ncol, np |
---|
952 | INTEGER, ALLOCATABLE :: n(:) |
---|
953 | LOGICAL :: ls, li, lr, la |
---|
954 | |
---|
955 | ! modname = 'dispNamelist' |
---|
956 | rFm = '*'; IF(PRESENT(rFmt)) rFm = rFmt !--- Specified format for reals |
---|
957 | ls = PRESENT(s); li = PRESENT(i); lr = PRESENT(r) |
---|
958 | lerr = .FALSE.; IF(.NOT.ANY([ls,li,lr])) RETURN !--- Nothing to do |
---|
959 | la = .FALSE.; IF(PRESENT(llast)) la = llast |
---|
960 | |
---|
961 | !--- CHECK ARGUMENTS COHERENCE |
---|
962 | ns = 0; ni = 0; nr = 0; np = LEN_TRIM(p); ncol = 0 |
---|
963 | IF(ls) THEN; ns = SIZE(s, DIM=1); ncol = ncol + SIZE(s, DIM=2) |
---|
964 | lerr = COUNT([(p(ic:ic)=='s', ic=1, np)]) /= SIZE(s, DIM=2) |
---|
965 | END IF |
---|
966 | IF(li) THEN; ni = SIZE(i, DIM=1); ncol = ncol + SIZE(i, DIM=2) |
---|
967 | lerr = COUNT([(p(ic:ic)=='i', ic=1, np)]) /= SIZE(i, DIM=2) |
---|
968 | END IF |
---|
969 | IF(lr) THEN; nr = SIZE(r, DIM=1); ncol = ncol + SIZE(r, DIM=2) |
---|
970 | lerr = COUNT([(p(ic:ic)=='r', ic=1, np)]) /= SIZE(r, DIM=2) |
---|
971 | END IF |
---|
972 | IF(fmsg('string "pattern" length and arguments number mismatch', ll=lerr)) RETURN |
---|
973 | lerr = ncol /= SIZE(titles); IF(fmsg('"titles" length and arguments number mismatch', ll=lerr)) RETURN |
---|
974 | lerr = ls.AND.li.AND.ns/=ni; IF(fmsg('string and integer arguments lengths mismatch', ll=lerr)) RETURN |
---|
975 | lerr = ls.AND.lr.AND.ns/=nr; IF(fmsg( 'string and real arguments lengths mismatch', ll=lerr)) RETURN |
---|
976 | lerr = li.AND.lr.AND.ni/=nr; IF(fmsg( 'integer and real arguments lengths mismatch', ll=lerr)) RETURN |
---|
977 | |
---|
978 | !--- Allocate the assembled quantities array |
---|
979 | nrow = MAX(ns,ni,nr)+1 |
---|
980 | ALLOCATE(d(nrow,ncol), n(ncol)) |
---|
981 | |
---|
982 | !--- Assemble the vectors into a strings array in the order indicated by "pattern" |
---|
983 | is = 1; ii = 1; ir = 1 |
---|
984 | DO ic = 1, ncol |
---|
985 | d(1,ic) = TRIM(titles(ic)) |
---|
986 | SELECT CASE(p(ic:ic)) |
---|
987 | CASE('s'); d(2:nrow,ic) = s(:,is) ; is = is + 1 |
---|
988 | CASE('i'); d(2:nrow,ic) = int2str(i(:,ii) ); ii = ii + 1 |
---|
989 | CASE('r'); d(2:nrow,ic) = real2str(r(:,ir),rFm); ir = ir + 1 |
---|
990 | END SELECT |
---|
991 | n(ic) = 0; DO ir=1, nrow; n(ic)=MAX(n(ic), LEN_TRIM(d(ir,ic))); END DO |
---|
992 | IF(needQuotes(d(2,ic)) .AND. ic/=1) n(ic) = n(ic) + 2 !--- For quotes, using second line only |
---|
993 | END DO |
---|
994 | |
---|
995 | !--- Display the strings array as a table |
---|
996 | DO ir = 1, nrow |
---|
997 | row = ''; sp = ' '; IF(TRIM(d(ir,1)) /= '') sp = ' = ' |
---|
998 | DO ic = 1, ncol |
---|
999 | el = d(ir,ic); IF(ic /= 1) el = addQuotes_1(el) |
---|
1000 | row = row//TRIM(el)//REPEAT(' ',n(ic)-LEN_TRIM(el))//sp |
---|
1001 | sp = ' '; IF(ic /= ncol-1) CYCLE |
---|
1002 | IF(TRIM(d(MIN(ir+1,nrow),1)) /= '' .AND. (ir /= nrow .OR. .NOT.la)) sp = ' , ' |
---|
1003 | END DO |
---|
1004 | WRITE(unt,'(a)')TRIM(row) |
---|
1005 | END DO |
---|
1006 | |
---|
1007 | !--- End of section |
---|
1008 | IF(la) THEN |
---|
1009 | WRITE(unt,'(a)')'/' |
---|
1010 | WRITE(unt,'(a)') |
---|
1011 | END IF |
---|
1012 | |
---|
1013 | END FUNCTION dispNameList |
---|
1014 | !============================================================================================================================== |
---|
1015 | |
---|
1016 | |
---|
1017 | !============================================================================================================================== |
---|
1018 | LOGICAL FUNCTION dispOutliers_1(ll, a, n, err_msg, nam, subn, nmax, unit) RESULT(lerr) |
---|
1019 | ! Display outliers list in tables |
---|
1020 | ! If "nam" is supplied, it means the last index is for tracers => one table each tracer for rank > 2. |
---|
1021 | LOGICAL, INTENT(IN) :: ll(:) !--- Linearized mask of outliers |
---|
1022 | REAL, INTENT(IN) :: a(:) !--- Linearized array of values |
---|
1023 | INTEGER, INTENT(IN) :: n(:) !--- Profile before linearization |
---|
1024 | |
---|
1025 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: err_msg, nam(:), subn !--- Error message, variables and calling subroutine names |
---|
1026 | INTEGER, OPTIONAL, INTENT(IN) :: nmax, unit !--- Maximum number of lines to display (default: all) |
---|
1027 | CHARACTER(LEN=maxlen), ALLOCATABLE :: ttl(:) |
---|
1028 | LOGICAL, ALLOCATABLE :: m(:) |
---|
1029 | INTEGER, ALLOCATABLE :: ki(:), kj(:) |
---|
1030 | INTEGER :: i, j, k, rk, rk1, ib, ie, itr, nm, unt, nmx, nv |
---|
1031 | CHARACTER(LEN=maxlen) :: mes, sub, fm='(f12.9)', v, s |
---|
1032 | CHARACTER(LEN=maxlen), ALLOCATABLE :: vnm(:) |
---|
1033 | |
---|
1034 | lerr = ANY(ll); IF(.NOT.lerr) RETURN !--- No outliers -> finished |
---|
1035 | |
---|
1036 | mes = 'outliers found'; IF(PRESENT(err_msg)) mes = err_msg !--- Error message |
---|
1037 | vnm = ['a']; IF(PRESENT(nam )) vnm = nam !--- Variables names |
---|
1038 | sub = 'dispOutliers'; IF(PRESENT(subn)) sub = subn !--- Calling subroutine name |
---|
1039 | nmx = SIZE(a); IF(PRESENT(nmax)) nmx = MIN(nmx,nmax) !--- Maximum number of lines to print |
---|
1040 | unt = lunout; IF(PRESENT(unit)) unt = unit !--- Unit to print messages |
---|
1041 | |
---|
1042 | rk = SIZE(n); nv = SIZE(vnm) |
---|
1043 | IF(test(fmsg('SIZE(nam) /= 1 and /= last "n" element', sub, nv /= 1 .AND. nv /= n(rk), unt),lerr)) RETURN |
---|
1044 | IF(test(fmsg('ll" and "a" sizes mismatch', sub, SIZE(a) /= SIZE(ll), unt),lerr)) RETURN |
---|
1045 | IF(test(fmsg('profile "n" does not match "a" and "ll', sub, SIZE(a) /= PRODUCT(n), unt),lerr)) RETURN |
---|
1046 | CALL msg(mes, sub, unit=unt) |
---|
1047 | |
---|
1048 | !--- SCALAR CASE: single value to display |
---|
1049 | IF(rk==1.AND.n(1)==1) THEN |
---|
1050 | IF(ll(1)) WRITE(unt,'(a," = ",f12.9)')TRIM(nam(1)),a(1); RETURN |
---|
1051 | END IF |
---|
1052 | |
---|
1053 | rk1 = rk; IF(nv==1) rk1 = rk-1 !--- Rank of each displayed table |
---|
1054 | SELECT CASE(rk1) !--- Indices list |
---|
1055 | CASE(1,2); ki = [ (i,i=1,n(1)) ] |
---|
1056 | CASE(3); ki = [((i,i=1,n(1)),j=1,n(2))]; kj = [((j,i=1,n(1)),j=1,n(2))] |
---|
1057 | CASE DEFAULT; WRITE(unt,*)'Sorry: routine "dispOutliers" is limited to rank 3'; RETURN |
---|
1058 | END SELECT |
---|
1059 | |
---|
1060 | !--- VECTOR CASE: table " name | value " (known names) / ) / " i | a(i) " (unknown names) |
---|
1061 | IF(rk==1) THEN |
---|
1062 | ALLOCATE(ttl(2)); ttl(2) = TRIM(vnm(1))//'(i)'; ttl(1) = 'i' |
---|
1063 | IF(nv == 1) lerr = dispTable('sr', ttl, s=cat(PACK(nam,ll)), r=cat(PACK(a,ll)), rFmt=fm, nmax=nmax) |
---|
1064 | IF(nv /= 1) lerr = dispTable('ir', ['name ','value'], i=cat(PACK(ki,m)), r=cat(PACK(a,ll)), rFmt=fm, nmax=nmax) |
---|
1065 | CALL msg("can't display outliers table", sub, lerr, unt) |
---|
1066 | RETURN |
---|
1067 | END IF |
---|
1068 | |
---|
1069 | !--- OTHER CASES: one table for each tracer (last index) |
---|
1070 | ttl = [(ACHAR(k), k = 105, 104+rk)] !--- Titles list ('i', 'j', 'k'...) |
---|
1071 | s = strStack( ttl(1:rk-1) ) !--- Tracer name dummy indexes: (i, j, k, ... |
---|
1072 | |
---|
1073 | DO itr=1,n(rk) |
---|
1074 | nm = PRODUCT(n(1:rk-1)) !--- number of elements per tracer |
---|
1075 | ie = itr*nm; ib = ie-nm+1; m=ll(ib:ie) !--- section bounds for tracer "itr" ; outlayers mask |
---|
1076 | IF(.NOT.ANY(m)) CYCLE !--- no outlayers for tracer "itr" |
---|
1077 | v = TRIM(vnm(MIN(itr,SIZE(vnm))))//'('//TRIM(s) !--- "<name>(" |
---|
1078 | IF(nv == 1) ttl(rk) = TRIM(v)//','//int2str(itr)//')' !--- "<name>(i,j,itr)" (single name) |
---|
1079 | IF(nv /= 1) ttl(rk) = TRIM(v)//')' !--- "<nam(itr)>(i,j)" (one name each table/itr index) |
---|
1080 | IF(rk==2) lerr = dispTable('ir', ttl, i=cat(PACK(ki,m)), r=cat(PACK(a(ib:ie),m)), rFmt=fm, nmax=nmax) |
---|
1081 | IF(rk==3) lerr = dispTable('iir', ttl, i=cat(PACK(ki,m),PACK(kj,m)), r=cat(PACK(a(ib:ie),m)), rFmt=fm, nmax=nmax) |
---|
1082 | CALL msg("can't display outliers table", sub, lerr, unt) |
---|
1083 | IF(lerr) RETURN |
---|
1084 | END DO |
---|
1085 | END FUNCTION dispOutliers_1 |
---|
1086 | !============================================================================================================================== |
---|
1087 | LOGICAL FUNCTION dispOutliers_2(ll, a, n, err_msg, nam, subn, nmax, unit) RESULT(lerr) |
---|
1088 | ! Display outliers list in tables |
---|
1089 | ! If "nam" is supplied and, it means the last index is for tracers => one table each tracer for rank > 2. |
---|
1090 | LOGICAL, INTENT(IN) :: ll(:) !--- Linearized mask of outliers |
---|
1091 | REAL, INTENT(IN) :: a(:,:) !--- Linearized arrays of values stacked along 2nd dim. |
---|
1092 | INTEGER, INTENT(IN) :: n(:) !--- Profile before linearization |
---|
1093 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: err_msg, nam(:), subn !--- Error message, variables and calling subroutine names |
---|
1094 | INTEGER, OPTIONAL, INTENT(IN) :: nmax, unit !--- Maximum number of lines to display (default: all) |
---|
1095 | |
---|
1096 | CHARACTER(LEN=maxlen) :: mes, sub, fm='(f12.9)', prf |
---|
1097 | CHARACTER(LEN=maxlen), ALLOCATABLE :: ttl(:), vnm(:) |
---|
1098 | LOGICAL, ALLOCATABLE :: m(:) |
---|
1099 | INTEGER, ALLOCATABLE :: ki(:), kj(:), kl(:) |
---|
1100 | INTEGER :: i, j, k, rk, ib, ie, itr, nm, nv, unt, nmx, rk1 |
---|
1101 | REAL, ALLOCATABLE :: val(:,:) |
---|
1102 | |
---|
1103 | lerr = ANY(ll); IF(.NOT.lerr) RETURN !--- No outliers -> finished |
---|
1104 | rk = SIZE(n); nv = SIZE(a,2) |
---|
1105 | mes = 'outliers found'; IF(PRESENT(err_msg)) mes = err_msg !--- Error message |
---|
1106 | vnm = [(ACHAR(k+96),k=1,nv)]; IF(PRESENT(nam )) vnm = nam !--- Variables names |
---|
1107 | sub = 'dispOutliers'; IF(PRESENT(subn)) sub = subn !--- Calling subroutine name |
---|
1108 | nmx = SIZE(a); IF(PRESENT(nmax)) nmx = MIN(nmx,nmax)!--- Maximum number of lines to print |
---|
1109 | unt = lunout; IF(PRESENT(unit)) unt = unit !--- Unit to print messages |
---|
1110 | lerr = SIZE(vnm) /= nv; IF(fmsg('SIZE(nam) /= SIZE(a,2)', sub, lerr, unt)) RETURN |
---|
1111 | lerr = SIZE(a,1) /= SIZE(ll); IF(fmsg('"ll" and "a" sizes mismatch', sub, lerr, unt)) RETURN |
---|
1112 | lerr = SIZE(a,1) /= PRODUCT(n); IF(fmsg('profile "n" does not match "a" and "ll"', sub, lerr, unt)) RETURN |
---|
1113 | |
---|
1114 | SELECT CASE(rk1) !--- Indices list |
---|
1115 | CASE(0); IF(ll(1)) THEN; WRITE(unt,'(a,", ",a," = ",2f12.9)')TRIM(vnm(1)),TRIM(vnm(2)),a(1,1),a(1,2); RETURN; END IF |
---|
1116 | CASE(1); ki = [ (i,i=1,n(1)) ] |
---|
1117 | CASE(2); ki = [ ((i,i=1,n(1)),j=1,n(2))]; kj = [ ((j,i=1,n(1)),j=1,n(2))] |
---|
1118 | CASE(3); ki = [(((i,i=1,n(1)),j=1,n(2)),k=1,n(3))]; kj = [(((j,i=1,n(1)),j=1,n(2)),k=1,n(3))] |
---|
1119 | kl = [(((k,i=1,n(1)),j=1,n(2)),k=1,n(3))] |
---|
1120 | CASE DEFAULT; WRITE(unt,*)'Sorry: routine "dispOutliers_2" is limited to rank 3'; RETURN |
---|
1121 | END SELECT |
---|
1122 | |
---|
1123 | ttl = [(ACHAR(k), k = 105, 104+rk), vnm] !--- Titles list ('i', 'j', 'k'...'var1', 'var2', ...) |
---|
1124 | prf = REPEAT('i',rk)//REPEAT('r',nv) !--- Profile |
---|
1125 | ALLOCATE(val(COUNT(ll),nv)); DO k=1, nv; val(:,k) = PACK(a(:,k),ll); END DO |
---|
1126 | IF(rk == 1) lerr = dispTable(prf, ttl, i = cat(PACK(ki,ll)), r = val, rFmt=fm, nmax=nmax) |
---|
1127 | IF(rk == 2) lerr = dispTable(prf, ttl, i = cat(PACK(ki,ll),PACK(kj,ll)), r = val, rFmt=fm, nmax=nmax) |
---|
1128 | IF(rk == 3) lerr = dispTable(prf, ttl, i = cat(PACK(ki,ll),PACK(kj,ll),PACK(kl,ll)), & |
---|
1129 | r = val, rFmt=fm, nmax=nmax) |
---|
1130 | CALL msg("can't display outliers table", sub, lerr, unt) |
---|
1131 | END FUNCTION dispOutliers_2 |
---|
1132 | !============================================================================================================================== |
---|
1133 | |
---|
1134 | |
---|
1135 | !============================================================================================================================== |
---|
1136 | !=== Reduce an algebrical expression (basic operations and parenthesis) to a single number (string format) ==================== |
---|
1137 | !============================================================================================================================== |
---|
1138 | LOGICAL FUNCTION reduceExpr_1(str, val) RESULT(lerr) |
---|
1139 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
1140 | CHARACTER(LEN=maxlen), INTENT(OUT) :: val |
---|
1141 | |
---|
1142 | CHARACTER(LEN=maxlen) :: v |
---|
1143 | CHARACTER(LEN=1024) :: s, vv |
---|
1144 | CHARACTER(LEN=1024), ALLOCATABLE :: vl(:) |
---|
1145 | INTEGER, ALLOCATABLE :: ip(:) |
---|
1146 | INTEGER :: nl, nn, i, j, im, ix |
---|
1147 | LOGICAL :: ll |
---|
1148 | ! modname = 'reduceExpr_1' |
---|
1149 | s = str |
---|
1150 | |
---|
1151 | !--- Check wether the parenthesis are correctly formed |
---|
1152 | ll = strCount(s,'(',nl) |
---|
1153 | ll = strCount(s,')',nn) |
---|
1154 | lerr = nl /= nn |
---|
1155 | IF(fmsg('Mismatching number of opening and closing parenthesis: '//TRIM(s), ll=lerr)) RETURN |
---|
1156 | nl = 2*nl-1 |
---|
1157 | |
---|
1158 | !--- Build vectors ; vl: parenthesis-separated elements ; ip: parenthesis types (1: opening, 2: closing) |
---|
1159 | ALLOCATE(ip(nl-1),vl(nl)) |
---|
1160 | j = 1; im = 1 |
---|
1161 | DO i = 1, LEN_TRIM(str) |
---|
1162 | ix = INDEX('()', str(i:i)) |
---|
1163 | IF(ix == 0) CYCLE |
---|
1164 | ip(j) = ix |
---|
1165 | vl(j) = str(im:i-1) |
---|
1166 | j = j + 1; im = i + 1 |
---|
1167 | END DO |
---|
1168 | vl(j) = str(im:LEN_TRIM(str)) |
---|
1169 | |
---|
1170 | !--- Search for each opening/closing parenthesis pair |
---|
1171 | DO WHILE(nl > 1) |
---|
1172 | i = 1; DO WHILE(ip(i) /= 1 .OR. ip(i+1) /= 2); i = i + 1; END DO !IF(i > SIZE(ip)+1) EXIT;END DO |
---|
1173 | IF(test(reduceExpr_basic(vl(i+1), v), lerr)) RETURN |
---|
1174 | v = TRIM(vl(i))//TRIM(v); IF(i+2<=nl) v=TRIM(v)//TRIM(vl(i+2)) |
---|
1175 | vv = v//REPEAT(' ',768) |
---|
1176 | IF(i == 1) THEN; ip = ip(3:nl-1); vl = [ vv, vl(4 :nl)] |
---|
1177 | ELSE IF(i == nl-1) THEN; ip = ip(1:nl-2); vl = [vl(1:nl-3), vv ] |
---|
1178 | ELSE; ip = [ip(1: i-1), ip(i+2:nl-1)]; vl = [vl(1: i-1), vv, vl(i+3:nl)]; END IF |
---|
1179 | nl = SIZE(vl) |
---|
1180 | END DO |
---|
1181 | lerr = reduceExpr_basic(vl(1), val) |
---|
1182 | END FUNCTION reduceExpr_1 |
---|
1183 | |
---|
1184 | |
---|
1185 | !============================================================================================================================== |
---|
1186 | !=== Reduce a simple algebrical expression (basic operations, no parenthesis) to a single number (string format) ============== |
---|
1187 | !============================================================================================================================== |
---|
1188 | LOGICAL FUNCTION reduceExpr_basic(str, val) RESULT(lerr) |
---|
1189 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
1190 | CHARACTER(LEN=*), INTENT(OUT) :: val |
---|
1191 | DOUBLE PRECISION, ALLOCATABLE :: vl(:) |
---|
1192 | INTEGER, ALLOCATABLE :: id(:) |
---|
1193 | CHARACTER(LEN=maxlen), ALLOCATABLE :: ky(:) |
---|
1194 | CHARACTER(LEN=1), ALLOCATABLE :: op(:) |
---|
1195 | |
---|
1196 | CHARACTER(LEN=1024) :: s |
---|
1197 | DOUBLE PRECISION :: v, vm, vp |
---|
1198 | INTEGER :: i, ni, io |
---|
1199 | |
---|
1200 | ! modname = 'reduceExpr_basic' |
---|
1201 | lerr = .FALSE. |
---|
1202 | IF(is_numeric(str)) THEN; val=TRIM(str); RETURN; END IF |
---|
1203 | op = ['^','/','*','+','-'] !--- List of recognized operations |
---|
1204 | s = str |
---|
1205 | IF(test(strParse_m(s, op, ky, .TRUE., id = id), lerr)) RETURN !--- Parse the values |
---|
1206 | vl = str2dble(ky) !--- Conversion to doubles |
---|
1207 | lerr = ANY(vl >= HUGE(1.d0)) |
---|
1208 | IF(fmsg('Some values are non-numeric in: '//TRIM(s), ll=lerr)) RETURN !--- Non-numerical values found |
---|
1209 | DO io = 1, SIZE(op) !--- Loop on known operators (order matters !) |
---|
1210 | DO i = SIZE(id), 1, -1 !--- Loop on found operators |
---|
1211 | ni = SIZE(id) |
---|
1212 | IF(id(i) /= io) CYCLE !--- Current found operator is not op(io) |
---|
1213 | vm = vl(i); vp = vl(i+1) !--- Couple of values used for current operation |
---|
1214 | SELECT CASE(io) !--- Perform operation on the two values |
---|
1215 | CASE(1); v = vm**vp !--- ^ |
---|
1216 | CASE(2); v = vm/vp !--- / |
---|
1217 | CASE(3); v = vm*vp !--- * |
---|
1218 | CASE(4); v = vm+vp !--- + |
---|
1219 | CASE(5); v = vm-vp !--- + |
---|
1220 | END SELECT |
---|
1221 | IF(i == ni) THEN; vl = [vl(1:ni-1), v]; ELSE; vl = [vl(1:i-1), v, vl(i+2:ni+1)]; END IF |
---|
1222 | IF(i == ni) THEN; id = id(1:ni-1); ELSE; id = [id(1:i-1), id(i+1:ni )]; END IF |
---|
1223 | END DO |
---|
1224 | END DO |
---|
1225 | val = dble2str(vl(1)) |
---|
1226 | |
---|
1227 | END FUNCTION reduceExpr_basic |
---|
1228 | !============================================================================================================================== |
---|
1229 | |
---|
1230 | !============================================================================================================================== |
---|
1231 | FUNCTION reduceExpr_m(str, val) RESULT(lerr) |
---|
1232 | LOGICAL, ALLOCATABLE :: lerr(:) |
---|
1233 | CHARACTER(LEN=*), INTENT(IN) :: str(:) |
---|
1234 | CHARACTER(LEN=maxlen), ALLOCATABLE, INTENT(OUT) :: val(:) |
---|
1235 | INTEGER :: i |
---|
1236 | ALLOCATE(lerr(SIZE(str)),val(SIZE(str))) |
---|
1237 | lerr(:) = [(reduceExpr_1(str(i), val(i)), i=1, SIZE(str))] |
---|
1238 | END FUNCTION reduceExpr_m |
---|
1239 | !============================================================================================================================== |
---|
1240 | |
---|
1241 | |
---|
1242 | !============================================================================================================================== |
---|
1243 | !=== Check whether a string is a number or not ================================================================================ |
---|
1244 | !============================================================================================================================== |
---|
1245 | ELEMENTAL LOGICAL FUNCTION is_numeric(str) RESULT(out) |
---|
1246 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
1247 | REAL :: x |
---|
1248 | INTEGER :: e |
---|
1249 | CHARACTER(LEN=12) :: fmt |
---|
1250 | IF(TRIM(str) == '') THEN; out = .FALSE.; RETURN; END IF |
---|
1251 | WRITE(fmt,'("(f",i0,".0)")') LEN_TRIM(str) |
---|
1252 | READ(str,fmt,IOSTAT=e) x |
---|
1253 | out = e==0 .AND. INDEX('Ee',str(LEN_TRIM(str):LEN_TRIM(str)))==0 |
---|
1254 | IF(str == '') out = .FALSE. |
---|
1255 | END FUNCTION is_numeric |
---|
1256 | !============================================================================================================================== |
---|
1257 | |
---|
1258 | |
---|
1259 | !============================================================================================================================== |
---|
1260 | !=== Convert a string into a logical/integer integer or an integer/real into a string ========================================= |
---|
1261 | !============================================================================================================================== |
---|
1262 | ELEMENTAL LOGICAL FUNCTION str2bool(str) RESULT(out) |
---|
1263 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
1264 | INTEGER :: ierr |
---|
1265 | READ(str,*,IOSTAT=ierr) out |
---|
1266 | IF(ierr==0) RETURN |
---|
1267 | out = ANY(['t ','true ','.true.','y ','yes ']==strLower(str)) |
---|
1268 | END FUNCTION str2bool |
---|
1269 | !============================================================================================================================== |
---|
1270 | ELEMENTAL INTEGER FUNCTION str2int(str) RESULT(out) |
---|
1271 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
1272 | INTEGER :: ierr |
---|
1273 | READ(str,*,IOSTAT=ierr) out |
---|
1274 | IF(ierr/=0) out = -HUGE(1) |
---|
1275 | END FUNCTION str2int |
---|
1276 | !============================================================================================================================== |
---|
1277 | ELEMENTAL REAL FUNCTION str2real(str) RESULT(out) |
---|
1278 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
1279 | INTEGER :: ierr |
---|
1280 | READ(str,*,IOSTAT=ierr) out |
---|
1281 | IF(ierr/=0) out = -HUGE(1.) |
---|
1282 | END FUNCTION str2real |
---|
1283 | !============================================================================================================================== |
---|
1284 | ELEMENTAL DOUBLE PRECISION FUNCTION str2dble(str) RESULT(out) |
---|
1285 | CHARACTER(LEN=*), INTENT(IN) :: str |
---|
1286 | INTEGER :: ierr |
---|
1287 | READ(str,*,IOSTAT=ierr) out |
---|
1288 | IF(ierr/=0) out = -HUGE(1.d0) |
---|
1289 | END FUNCTION str2dble |
---|
1290 | !============================================================================================================================== |
---|
1291 | ELEMENTAL CHARACTER(LEN=maxlen) FUNCTION bool2str(b) RESULT(out) |
---|
1292 | LOGICAL, INTENT(IN) :: b |
---|
1293 | WRITE(out,*)b |
---|
1294 | out = ADJUSTL(out) |
---|
1295 | END FUNCTION bool2str |
---|
1296 | !============================================================================================================================== |
---|
1297 | ELEMENTAL CHARACTER(LEN=maxlen) FUNCTION int2str(i, nDigits) RESULT(out) |
---|
1298 | INTEGER, INTENT(IN) :: i |
---|
1299 | INTEGER, OPTIONAL, INTENT(IN) :: nDigits |
---|
1300 | WRITE(out,*)i |
---|
1301 | out = ADJUSTL(out) |
---|
1302 | IF(.NOT.PRESENT(nDigits)) RETURN |
---|
1303 | IF(nDigits > LEN_TRIM(out)) out = REPEAT('0', nDigits - LEN_TRIM(out))//TRIM(out) |
---|
1304 | END FUNCTION int2str |
---|
1305 | !============================================================================================================================== |
---|
1306 | ELEMENTAL CHARACTER(LEN=maxlen) FUNCTION real2str(r,fmt) RESULT(out) |
---|
1307 | REAL, INTENT(IN) :: r |
---|
1308 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: fmt |
---|
1309 | IF( PRESENT(fmt)) WRITE(out,fmt)r |
---|
1310 | IF(.NOT.PRESENT(fmt)) WRITE(out, * )r |
---|
1311 | out = ADJUSTL(out) |
---|
1312 | END FUNCTION real2str |
---|
1313 | !============================================================================================================================== |
---|
1314 | ELEMENTAL CHARACTER(LEN=maxlen) FUNCTION dble2str(d,fmt) RESULT(out) |
---|
1315 | DOUBLE PRECISION, INTENT(IN) :: d |
---|
1316 | CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: fmt |
---|
1317 | IF( PRESENT(fmt)) WRITE(out,fmt)d |
---|
1318 | IF(.NOT.PRESENT(fmt)) WRITE(out, * )d |
---|
1319 | out = ADJUSTL(out) |
---|
1320 | END FUNCTION dble2str |
---|
1321 | !============================================================================================================================== |
---|
1322 | |
---|
1323 | |
---|
1324 | !============================================================================================================================== |
---|
1325 | FUNCTION addQuotes_1(s) RESULT(out) |
---|
1326 | CHARACTER(LEN=*), INTENT(IN) :: s |
---|
1327 | CHARACTER(LEN=:), ALLOCATABLE :: out |
---|
1328 | IF(needQuotes(s)) THEN; out = "'"//TRIM(s)//"'"; ELSE; out = s; END IF |
---|
1329 | END FUNCTION addQuotes_1 |
---|
1330 | !============================================================================================================================== |
---|
1331 | FUNCTION addQuotes_m(s) RESULT(out) |
---|
1332 | CHARACTER(LEN=*), INTENT(IN) :: s(:) |
---|
1333 | CHARACTER(LEN=:), ALLOCATABLE :: out(:) |
---|
1334 | INTEGER :: k, n |
---|
1335 | n = MAXVAL(LEN_TRIM(s), MASK=.TRUE.) |
---|
1336 | ALLOCATE(CHARACTER(LEN=n) :: out(SIZE(s))) |
---|
1337 | DO k=1,SIZE(s) |
---|
1338 | IF(needQuotes(s(k))) THEN; out(k) = "'"//TRIM(s(k))//"'"; ELSE; out(k) = s(k); END IF |
---|
1339 | END DO |
---|
1340 | END FUNCTION addQuotes_m |
---|
1341 | !============================================================================================================================== |
---|
1342 | ELEMENTAL LOGICAL FUNCTION needQuotes(s) RESULT(out) |
---|
1343 | CHARACTER(LEN=*), INTENT(IN) :: s |
---|
1344 | CHARACTER(LEN=1) :: b, e |
---|
1345 | out = .TRUE.; IF(TRIM(s) == '') RETURN |
---|
1346 | b = s(1:1); e = s(MAX(1,LEN_TRIM(s)):MAX(1,LEN_TRIM(s))) |
---|
1347 | out = .NOT.is_numeric(s) .AND. (b /= "'" .OR. e /= "'") .AND. ( b /= '"' .OR. e /= '"') |
---|
1348 | END FUNCTION needQuotes |
---|
1349 | !============================================================================================================================== |
---|
1350 | |
---|
1351 | |
---|
1352 | !============================================================================================================================== |
---|
1353 | !=== TEST WHETHER A FILE IS PRESENT OR NOT ==================================================================================== |
---|
1354 | !============================================================================================================================== |
---|
1355 | LOGICAL FUNCTION testFile_1(fname) RESULT(out) |
---|
1356 | CHARACTER(LEN=*), INTENT(IN) :: fname |
---|
1357 | INTEGER :: ierr |
---|
1358 | OPEN(90, FILE=fname, FORM='formatted', STATUS='old', IOSTAT=ierr); CLOSE(99) |
---|
1359 | out = ierr/=0 |
---|
1360 | END FUNCTION testFile_1 |
---|
1361 | !============================================================================================================================== |
---|
1362 | FUNCTION testFile_m(fname) RESULT(out) |
---|
1363 | LOGICAL, ALLOCATABLE :: out(:) |
---|
1364 | CHARACTER(LEN=*), INTENT(IN) :: fname(:) |
---|
1365 | INTEGER :: k |
---|
1366 | out = [(testFile_1(fname(k)), k=1, SIZE(fname))] |
---|
1367 | END FUNCTION testFile_m |
---|
1368 | !============================================================================================================================== |
---|
1369 | |
---|
1370 | |
---|
1371 | !============================================================================================================================== |
---|
1372 | !=== DISPLAY "<message>: the following <items> are <reason>" FOLLOWED BY THE LIST OF <str> FOR WHICH <lerr>==T. =============== |
---|
1373 | !============================================================================================================================== |
---|
1374 | LOGICAL FUNCTION checkList(str, lerr, message, items, reason, nmax) RESULT(out) |
---|
1375 | ! Purpose: Messages in case a list contains wrong elements (indicated by lerr boolean vector). |
---|
1376 | ! Note: Return value "out" is .TRUE. if there are errors (ie at least one element of "lerr" is TRUE). |
---|
1377 | CHARACTER(LEN=*), INTENT(IN) :: str(:) |
---|
1378 | LOGICAL, INTENT(IN) :: lerr(:) |
---|
1379 | CHARACTER(LEN=*), INTENT(IN) :: message, items, reason |
---|
1380 | INTEGER, OPTIONAL, INTENT(IN) :: nmax |
---|
1381 | CHARACTER(LEN=maxlen), ALLOCATABLE :: s(:) |
---|
1382 | INTEGER :: i, nmx |
---|
1383 | nmx = 256; IF(PRESENT(nmax)) nmx=nmax |
---|
1384 | out = ANY(lerr); IF(.NOT.out) RETURN |
---|
1385 | CALL msg(TRIM(message)//': the following '//TRIM(items)//' are '//TRIM(reason)//':') |
---|
1386 | s = strStackm(PACK(str, MASK=lerr), ', ',nmx) |
---|
1387 | DO i=1,SIZE(s,DIM=1); CALL msg(s(i)); END DO |
---|
1388 | END FUNCTION checkList |
---|
1389 | !============================================================================================================================== |
---|
1390 | |
---|
1391 | |
---|
1392 | !============================================================================================================================== |
---|
1393 | !=== Remove comment in line "str", ie all the characters from the first "#" sign found in "str". ============================== |
---|
1394 | !============================================================================================================================== |
---|
1395 | SUBROUTINE removeComment(str) |
---|
1396 | CHARACTER(LEN=*), INTENT(INOUT) :: str |
---|
1397 | INTEGER :: ix |
---|
1398 | ix = INDEX(str,'# '); IF(ix /= 0) str = str(1:ix-1)//REPEAT(' ',LEN(str)-ix+1) |
---|
1399 | END SUBROUTINE removeComment |
---|
1400 | !============================================================================================================================== |
---|
1401 | |
---|
1402 | |
---|
1403 | |
---|
1404 | END MODULE strings_mod |
---|