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