Debian Codegraph @

src/prompting.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2013 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !This is a small f90 module containing a generic subroutine for prompting
24 !integer, real, double and logical variables and strings.
25 !I use them quite often and found the solution below very useful.
26 !
27 !The general syntax is:
28 !
29 ! call prompt(PROMPT,VAR,...)
30 !
31 !Action: writes the string PROMPT on the terminal plus the current
32 ! value of the variable VAR and reads VAR. If default
33 ! is pressed instead of a new value,
34 ! the variable VAR stays untouched.
35 !
36 !In addition there are a few optional parameter to the routine like
37 !setting defaults or limits etc...
38 !
39 !In principle three f90 features are used which are not available
40 !in f77: recursion, non-advancing I/O and overloading.
41 !
42 !A detailed description of the syntax can be found in the header
43 !of the module below.
44
45 !---------------------------- please cut here -------------------------------------
46
47 !
48 ! f90 Module 'prompting'
49 !
50 ! Definition of Generic Subroutine: prompt
51 !
52 ! Syntax: prompt(text, value, [min], [max])
53 ! text character string
54 ! value integer, real or double
55 ! min, max allowed range of same type as value (optional)
56 ! [DJP] min2, max2 allowed 2nd range of same type as value (optional)
57 !
58 ! prompt(text, string, [length], [case])
59 ! text character string
60 ! string character string
61 ! length length of string (optional on return)
62 ! case option
63 ! 1 -> convert string to lower case
64 ! 2 -> convert string to upper case
65 ! lower=1, upper=2 are defined public
66 ! within this module
67 !
68 ! prompt(text, value, [default])
69 ! text character string
70 ! value logical
71 ! default logical (optional)
72 ! will always overwrite the current value
73 !
74 ! Author: Th. S. Ullrich, University Heidelberg
75 ! e-mail: [email protected]
76 ! Last mod: 18 Aug 94
77 !
78 ! Changes by D.Price, University of Exeter, [email protected]:
79 ! 19/10/04 : problem with if (present(min) .and. min < newvalue)
80 ! on some compilers
81 !
82 ! 31/10/06: D. Price:
83 ! Function print_logical added for displaying logicals: takes in a logical
84 ! variable and returns a string 'on' or 'off' as appropriate.
85 !
86 ! 20/06/07: D. Price:
87 ! Default part of prompt changed from "<cr>=" to the more human "default="
88 ! Also the character string prompt puts the default value in quotes
89 !
90 ! 09/05/08: D. Price:
91 ! String prompt accepts "blank" to set empty string, unless optional
92 ! argument noblank is set to .true.
93 !
94 ! 06/02/09: D. Price, Monash University, [email protected]
95 ! Added optional "mask" argument to print_logical routine
96 !
97 ! 27/01/10: D. Price:
98 ! Added optional "list" argument to string_prompt routine, now recursive
99 !
100 ! 24/02/10: D. Price:
101 ! When noblank=.true., string prompt does not accept blank string
102 ! (e.g. where it is the default input) and gives an error message
103 !
104 ! 23/07/10: D. Price:
105 ! Integer prompt accepts 2nd sub-range [min:max] [min2:max2]
106 !
107 ! 06/05/11: D. Price:
108 ! Added prompt for integer arrays
109 !
110 ! 21/08/12: D. Price:
111 ! Real/double prompting interfaces compile and work with -r8
112 !
113 ! 08/02/13: D. Price
114 ! Integer prompt enforces default value to be between min and max
115 !
116 ! 25/06/13: D. Price
117 ! Real and double prompts print values using scientific notation 1.e8 instead of 0.1e9
118 !
119 module prompting
120 implicit none
121 private
122
123 !
124 ! Options for string prompting routine
125 !
126
127 integer, parameter, public :: lower = 1, upper = 2
128
129 !
130 ! Create generic name 'prompt'
131 !
132
133 interface prompt
134 module procedure &
135 integer_prompt, real_prompt, string_prompt, double_prompt, logical_prompt, intarr_prompt
136 end interface
137 public :: prompt,print_logical,print_logicals
138
139 contains
140
141 !
142 ! Integer prompting routine
143 !
144
145 recursive subroutine integer_prompt(text, value, min, max, min2, max2)
146 character(len=*), intent(in) :: text
147 integer, intent(inout) :: value
148 integer :: newvalue
149 character(len=64) :: string
150 character(len=16) :: chmin, chmax, chmin2, chmax2
151 integer :: ios, ierr
152 integer, optional, intent(in) :: min, max, min2, max2
153 logical :: error
154
155
156 chmin = ''
157 chmax = ''
158 chmin2 = ''
159 chmax2 = ''
160 error = .false.
161
162 !
163 ! Make sure default argument is within range!
164 !
165 if (present(min)) then
166 if (value < min .and. .not. (present(min2) .or. present(max2))) value = min
167 endif
168 if (present(max)) then
169 if (value > max .and. .not. (present(min2) .or. present(max2))) value = max
170 endif
171
172 !
173 ! Pack arguments in strings for compact and nicer prompt
174 !
175 write(string,*) value
176 if (present(min)) write(chmin,"(g10.0)") min
177 if (present(max)) write(chmax,"(g10.0)") max
178 if (present(min2)) write(chmin2,"(g10.0)") min2
179 if (present(max2)) write(chmax2,"(g10.0)") max2
180 !
181 ! Write prompt string to terminal
182 !
183
184 if (present(min).or.present(max)) then
185 if (present(min2).or.present(max2)) then
186 write(*,"(a,1x,'([',a,':',a,',',a,':',a,'],',1x,'default=',a,'):',1x)",advance='no') &
187 trim(adjustl(text)), trim(adjustl(chmin)), &
188 trim(adjustl(chmax)),trim(adjustl(chmin2)),&
189 trim(adjustl(chmax2)),trim(adjustl(string))
190 else
191 write(*,"(a,1x,'([',a,':',a,'],',1x,'default=',a,'):',1x)",advance='no') &
192 trim(adjustl(text)), trim(adjustl(chmin)), &
193 trim(adjustl(chmax)), trim(adjustl(string))
194 endif
195 else
196 write(*,"(a,1x,'(default=',a,'):',1x)",advance='no') &
197 trim(adjustl(text)), trim(adjustl(string))
198 endif
199
200 !
201 ! Read new value, quit and keep old value if zero sized string
202 !
203
204 read(*,"(a)",iostat=ierr) string
205 if (len(trim(adjustl(string))) == 0) return
206 read(string,"(g10.0)",iostat=ios) newvalue
207
208 !
209 ! Check if new string is of right type and within given range
210 !
211
212 if (ios /= 0) then
213 print "(a)", "Error, not an integer number"
214 error = .true.
215 else
216 if (present(min)) then
217 if (newvalue < min) then
218 if (present(max2)) then
219 if (newvalue > max2) then
220 print "(a)", "Error, value out of range"
221 error = .true.
222 elseif (newvalue < min2) then
223 print "(a)", "Error, value out of range"
224 error = .true.
225 endif
226 else
227 print "(a)", "Error, value out of range"
228 error = .true.
229 endif
230 endif
231 endif
232 if (present(max)) then
233 if (newvalue > max) then
234 if (present(min2)) then
235 if (newvalue < min2) then
236 print "(a)", "Error, value out of range"
237 error = .true.
238 elseif (newvalue > max2) then
239 print "(a)", "Error, value out of range"
240 error = .true.
241 endif
242 else
243 print "(a)", "Error, value out of range"
244 error = .true.
245 endif
246 endif
247 endif
248 endif
249
250 !
251 ! Assign new value if everything is ok, else prompt again
252 !
253
254 if (error) then
255 call integer_prompt(text, value, min, max, min2, max2)
256 else
257 value = newvalue
258 endif
259
260 end subroutine integer_prompt
261
262
263 !
264 ! Real prompting routine
265 !
266
267 recursive subroutine real_prompt(text, value, min, max)
268 integer, parameter :: sp = selected_real_kind(p=6)
269 character(len=*), intent(in) :: text
270 real(kind=sp), intent(inout) :: value
271 real(kind=sp) :: newvalue
272 character(len=64) :: string
273 character(len=16) :: chmin, chmax
274 integer :: ios
275 real(kind=sp), optional, intent(in) :: min, max
276 logical :: error
277
278
279 chmin = ''
280 chmax = ''
281 error = .false.
282
283 !
284 ! Pack arguments in strings for compact and nicer prompt
285 !
286
287 if (abs(value) < 0.1 .or. abs(value) >= 1.d4) then
288 write(string,"(es13.4)") value
289 else
290 write(string,"(g13.4)") value
291 endif
292 if (present(min)) write(chmin,"(g13.4)") min
293 if (present(max)) write(chmax,"(g13.4)") max
294
295 !
296 ! Write prompt string to terminal
297 !
298
299 if (present(min).or.present(max)) then
300 write(*,"(a,1x,'([',a,':',a,'],',1x,'default=',a,'):',1x)",advance='no') &
301 trim(adjustl(text)), trim(adjustl(chmin)), &
302 trim(adjustl(chmax)), trim(adjustl(string))
303 else
304 write(*,"(a,1x,'(default=',a,'):',1x)",advance='no') &
305 trim(adjustl(text)), trim(adjustl(string))
306 endif
307
308 !
309 ! Read new value, quit and keep old value if zero sized string
310 !
311
312 read(*,"(a)") string
313 if (len(trim(adjustl(string))) == 0) return
314 read(string,*,iostat=ios) newvalue
315
316 !
317 ! Check if new string is of right type and within given range
318 !
319
320 if (ios /= 0) then
321 print "(a)", "Error, not a real number"
322 error = .true.
323 else
324 if (present(min)) then
325 if (newvalue < min) then
326 print "(a)", "Error, value out of range"
327 error = .true.
328 endif
329 endif
330 if (present(max)) then
331 if (newvalue > max) then
332 print "(a)", "Error, value out of range"
333 error = .true.
334 endif
335 endif
336 endif
337
338 !
339 ! Assign new value if everything is ok, else prompt again
340 !
341
342 if (error) then
343 call real_prompt(text, value, min, max)
344 else
345 value = newvalue
346 endif
347
348 end subroutine real_prompt
349
350
351 !
352 ! Double precision prompting routine
353 !
354
355 recursive subroutine double_prompt(text, value, min, max)
356 integer, parameter :: db = kind(0.d0)
357 character(len=*), intent(in) :: text
358 real(kind=db), intent(inout) :: value
359 real(kind=db) :: newvalue
360 character(len=64) :: string
361 character(len=16) :: chmin, chmax
362 integer :: ios
363 real(kind=db), optional, intent(in) :: min, max
364 logical :: error
365
366
367 chmin = ''
368 chmax = ''
369 error = .false.
370
371 !
372 ! Pack arguments in strings for compact and nicer prompt
373 !
374
375 if (abs(value) < 0.1d0 .or. abs(value) >= 1.d4) then
376 write(string,"(es13.4)") value
377 else
378 write(string,"(g13.4)") value
379 endif
380 if (present(min)) write(chmin,"(g13.4)") min
381 if (present(max)) write(chmax,"(g13.4)") max
382
383 !
384 ! Write prompt string to terminal
385 !
386
387 if (present(min).or.present(max)) then
388 write(*,"(a,1x,'([',a,':',a,'],',1x,'default=',a,'):',1x)",advance='no') &
389 trim(adjustl(text)), trim(adjustl(chmin)), &
390 trim(adjustl(chmax)), trim(adjustl(string))
391 else
392 write(*,"(a,1x,'(default=',a,'):',1x)",advance='no') &
393 trim(adjustl(text)), trim(adjustl(string))
394 endif
395
396 !
397 ! Read new value, quit and keep old value if zero sized string
398 !
399
400 read(*,"(a)") string
401 if (len(trim(adjustl(string))) == 0) return
402 read(string,*,iostat=ios) newvalue
403
404 !
405 ! Check if new string is of right type and within given range
406 !
407
408 if (ios /= 0) then
409 print "(a)", "Error, not a real number"
410 error = .true.
411 else
412 if (present(min)) then
413 if (newvalue < min) then
414 print "(a)", "Error, value out of range"
415 error = .true.
416 endif
417 endif
418 if (present(max)) then
419 if (newvalue > max) then
420 print "(a)", "Error, value out of range"
421 error = .true.
422 endif
423 endif
424 endif
425
426 !
427 ! Assign new value if everything is ok, else prompt again
428 !
429
430 if (error) then
431 call double_prompt(text, value, min, max)
432 else
433 value = newvalue
434 endif
435
436 end subroutine double_prompt
437
438
439
440 !
441 ! Logical prompting routine
442 !
443
444 recursive subroutine logical_prompt(text, lvalue, default)
445 character(len=*), intent(in) :: text
446 logical, intent(inout) :: lvalue
447 logical, optional, intent(in) :: default
448 character(len=32) :: string
449
450 !
451 ! If present, set default
452 !
453
454 if (present(default)) lvalue = default
455
456 !
457 ! Default answer yes/no
458 !
459
460 if (lvalue) then
461 string='yes'
462 else
463 string='no'
464 endif
465
466
467 !
468 ! Write prompt string to terminal
469 !
470
471 write(*,"(a,1x,'(default=',a,'):',1x)",advance='no') &
472 trim(adjustl(text)), trim(adjustl(string))
473
474 !
475 ! Read new value, quit and keep old value if zero sized string
476 !
477
478 read(*,"(a)") string
479 if (len(trim(adjustl(string))) == 0) return
480
481 !
482 ! Translate answer in .true./.false., if invalid prompt again
483 !
484
485 select case (adjustl(string))
486 case ('y')
487 lvalue = .true.
488 case ('yes')
489 lvalue = .true.
490 case ('on')
491 lvalue = .true.
492 case ('t')
493 lvalue = .true.
494 case ('true')
495 lvalue = .true.
496 case ('n')
497 lvalue = .false.
498 case ('no')
499 lvalue = .false.
500 case ('off')
501 lvalue = .false.
502 case ('f')
503 lvalue = .false.
504 case ('false')
505 lvalue = .false.
506 case default
507 print "(a)", "Error, answer y(es)/t(rue)/on or n(o)/f(alse)/off"
508 call logical_prompt(text, lvalue, default)
509 end select
510
511 end subroutine logical_prompt
512
513
514 !
515 ! String prompting routine
516 !
517
518 recursive subroutine string_prompt(text, string, length, case, noblank, list)
519 character(len=*), intent(in) :: text
520 character(len=*), intent(inout) :: string
521 character(len=128) :: newstring
522 integer, optional, intent(out) :: length
523 integer, optional, intent(in) :: case
524 logical, optional, intent(in) :: noblank
525 integer :: is, ia, i
526 integer, parameter :: aoffset = 32
527 logical :: allowblank,inlist
528 character(len=*), dimension(:), intent(in), optional :: list
529
530 !
531 ! Write prompt string to terminal
532 !
533 if (present(noblank)) then
534 allowblank = .not.noblank
535 else
536 allowblank = .true.
537 endif
538 if (allowblank .and. len_trim(adjustl(string)) > 0) then
539 write(*,"(a,1x,'(blank=""blank"",default=""',a,'""):',1x)",advance='no') &
540 trim(adjustl(text)), trim(adjustl(string))
541 else
542 write(*,"(a,1x,'(default=""',a,'""):',1x)",advance='no') &
543 trim(adjustl(text)), trim(adjustl(string))
544 endif
545 !
546 ! Read new value, quit and keep old value if zero sized string
547 !
548
549 read(*,"(a)") newstring
550 if (len_trim(newstring) > len(string)) then
551 print "(a)", "Warning: string too long, will be truncated"
552 endif
553 if (allowblank .and. trim(adjustl(newstring))=='blank') then
554 string = ' '
555 elseif ( len_trim(adjustl(newstring)) /= 0 ) then
556 string = newstring
557 elseif ( .not.allowblank .and. len_trim(adjustl(string))==0 ) then
558 print "(a)", "Error, cannot enter blank string"
559 if (present(list)) then
560 call string_prompt(text,string,noblank=.not.allowblank,list=list)
561 else
562 call string_prompt(text,string,noblank=.not.allowblank)
563 endif
564 endif
565 if (present(length)) length = len_trim(string)
566
567 !
568 ! Convert string to upper/lower case if requested
569 !
570
571 if (present(case)) then
572 if (case == upper) then
573 do is = 1, len(string)
574 ia = iachar(string(is:is))
575 if (ia >= iachar('a').and.ia <= iachar('z')) &
576 string(is:is) = achar(ia-aoffset)
577 enddo
578 endif
579 if (case == lower) then
580 do is = 1, len(string)
581 ia = iachar(string(is:is))
582 if (ia >= iachar('A').and.ia <= iachar('Z')) &
583 string(is:is) = achar(ia+aoffset)
584 enddo
585 endif
586 endif
587
588 if (present(list)) then
589 inlist = .false.
590 do i=1,size(list)
591 if (trim(adjustl(list(i)))==trim(adjustl(string))) inlist = .true.
592 enddo
593 if (.not.inlist) then
594 print "(a)", "Error, value not in list"
595 call string_prompt(text,string,noblank=.not.allowblank,list=list)
596 endif
597 endif
598
599 end subroutine string_prompt
600
601 !
602 ! Integer array prompting routine (D. Price)
603 !
604
605 recursive subroutine intarr_prompt(text, value, nvalues, min, max)
606 character(len=*), intent(in) :: text
607 integer, dimension(:), intent(inout) :: value
608 integer, intent(inout) :: nvalues
609 integer, dimension(size(value)) :: newvalue
610 character(len=64) :: valstring
611 character(len=120) :: string
612 character(len=16) :: chmin, chmax
613 integer :: ios
614 integer, optional, intent(in) :: min, max
615 logical :: error
616 integer :: ival,nvaluesnew
617
618 chmin = ''
619 chmax = ''
620 error = .false.
621
622 !
623 ! Pack arguments in strings for compact and nicer prompt
624 !
625 string = ' '
626 do ival=1,nvalues-1
627 write(valstring,*,iostat=ios) value(ival)
628 string = trim(string)//trim(adjustl(valstring))//','
629 enddo
630 if (nvalues > 0) then
631 write(valstring,*,iostat=ios) value(nvalues)
632 endif
633 string = trim(string)//trim(adjustl(valstring))
634
635 if (present(min)) write(chmin,"(g10.0)") min
636 if (present(max)) write(chmax,"(g10.0)") max
637 !
638 ! Write prompt string to terminal
639 !
640
641 if (present(min).or.present(max)) then
642 write(*,"(a,1x,'([',a,':',a,'],',1x,'default=',a,'):',1x)",advance='no') &
643 trim(adjustl(text)), trim(adjustl(chmin)), &
644 trim(adjustl(chmax)), trim(adjustl(string))
645 else
646 write(*,"(a,1x,'(default=',a,'):',1x)",advance='no') &
647 trim(adjustl(text)), trim(adjustl(string))
648 endif
649
650 !
651 ! Read new value, quit and keep old value if zero sized string
652 !
653
654 read(*,"(a)") string
655 if (len(trim(adjustl(string))) == 0) return
656 !
657 !--register how many new values read
658 !
659 newvalue = -huge(0)
660 read(string,*,iostat=ios) newvalue(:)
661 nvaluesnew = 0
662 do ival=1,size(newvalue)
663 if (newvalue(ival) /= -huge(0)) nvaluesnew = nvaluesnew + 1
664 enddo
665
666 !
667 ! Check if new string is of right type and within given range
668 !
669
670 if (nvaluesnew <= 0) then
671 print "(a)", "Error, no integer numbers could be read"
672 error = .true.
673 else
674 if (present(min)) then
675 if (any(newvalue(1:nvaluesnew) < min)) then
676 print "(a)", "Error, value(s) out of range (min)"
677 error = .true.
678 endif
679 endif
680 if (present(max)) then
681 if (any(newvalue(1:nvaluesnew) > max)) then
682 print "(a)", "Error, value(s) out of range (max)"
683 error = .true.
684 endif
685 endif
686 endif
687
688 !
689 ! Assign new value if everything is ok, else prompt again
690 !
691
692 if (error) then
693 call intarr_prompt(text, value, nvalues, min, max)
694 else
695 value = newvalue
696 nvalues = nvaluesnew
697 endif
698
699 end subroutine intarr_prompt
700
701 !
702 ! Routine added by D.Price (31/10/06)
703 ! Takes in a logical variable and returns a string 'on' or 'off' as appropriate
704 !
705
706 function print_logical(lvalue,mask)
707 logical, intent(in) :: lvalue
708 logical, intent(in), optional :: mask
709 character(len=3) :: print_logical
710 logical :: maskval
711
712 maskval = .true.
713 if (present(mask)) maskval = mask
714
715 if (maskval) then
716 if (lvalue) then
717 print_logical = 'ON'
718 else
719 print_logical = 'OFF'
720 endif
721 else
722 print_logical = ''
723 endif
724
725 end function print_logical
726
727 function print_logicals(lvalues,mask)
728 logical, intent(in) :: lvalues(:)
729 logical, intent(in), optional :: mask(:)
730 character(len=4*size(lvalues)) :: print_logicals
731 logical :: maskval
732 integer :: i
733
734 print_logicals = ''
735 do i=1,size(lvalues)
736 maskval = .true.
737 if (present(mask)) maskval = mask(i)
738 if (maskval) then
739 if (lvalues(i)) then
740 print_logicals = trim(print_logicals)//'ON,'
741 else
742 print_logicals = trim(print_logicals)//'OFF,'
743 endif
744 endif
745 enddo
746 ! delete last comma
747 i = index(print_logicals,',',back=.true.)
748 if (i > 1) print_logicals = print_logicals(1:i-1)
749
750 end function print_logicals
751
752 end module prompting