ROMS
Loading...
Searching...
No Matches
get_env.F
Go to the documentation of this file.
2!
3!git $Id$
4!================================================== Hernan G. Arango ===
5! Copyright (c) 2002-2025 The ROMS Group !
6! Licensed under a MIT/X style license !
7! See License_ROMS.md !
8!=======================================================================
9! !
10! This module retrieves and decodes system environmental variables !
11! using Fortran 2003 GET_ENVIRONMENT_VARIABLE intrinsic function. !
12! !
13!=======================================================================
14!
15 USE mod_parallel, ONLY : master
16 USE mod_iounits, ONLY : stdout
17!
18 implicit none
19!
20! Define public overloading function.
21!
22 INTERFACE get_env
23 MODULE PROCEDURE get_env_i ! gets integer value
24 MODULE PROCEDURE get_env_l ! gets logical value
25 MODULE PROCEDURE get_env_s ! gets string value
26 END INTERFACE get_env
27!
28 CONTAINS
29!
30 FUNCTION get_env_i (name, value) RESULT (status)
31!
32!***********************************************************************
33! !
34! Reads and decodes environmental variable with an integer value. !
35! !
36!***********************************************************************
37!
38! Imported variable declarations.
39!
40 integer, intent(out) :: value
41 character (len=*), intent(in ) :: name
42!
43! Local variable declaration.
44!
45 integer :: lstr, status
46 character (len=40) :: string
47 character (len=512) :: msg
48!
49!-----------------------------------------------------------------------
50! Read and decode environmental variable. Return "value=-1" if not
51! found.
52!-----------------------------------------------------------------------
53!
54 CALL get_environment_variable (name, string, lstr, status)
55!
56! Convert to integer.
57!
58 IF ((lstr.gt.0).and.(status.eq.0)) THEN
59 READ (string, *, iostat=status, iomsg=msg) value
60 IF (master.and.(status.ne.0)) THEN
61 WRITE (stdout,10) trim(name), trim(string), trim(msg)
62 END IF
63 ELSE
64 status=1
65 value=-1
66 IF (master) THEN
67 WRITE (stdout,20) trim(name)
68 END IF
69 END IF
70!
71 10 FORMAT (/,' GET_ENV_I - Error while converting string to', &
72 & ' integer, name = ',a,', value = ',a,/,13x,'ErrMsg: ',a)
73 20 FORMAT (/,' GET_ENV_I - Cannot find environmental variable', &
74 & ', name = ',a)
75!
76 RETURN
77 END FUNCTION get_env_i
78!
79 FUNCTION get_env_l (name, value) RESULT (status)
80!
81!***********************************************************************
82! !
83! Reads and decodes environmental variable with an logical value. !
84! !
85!***********************************************************************
86!
87! Imported variable declarations.
88!
89 logical, intent(out) :: value
90 character (len=*), intent(in ) :: name
91!
92! Local variable declaration.
93!
94 integer :: lstr, status
95 character (len=40) :: string
96!
97!-----------------------------------------------------------------------
98! Read and decode environmental variable. Return value=.FALSE. if not
99! found.
100!-----------------------------------------------------------------------
101!
102 CALL get_environment_variable (name, string, lstr, status)
103!
104! Convert to logical.
105!
106 value=.false.
107!
108 IF ((lstr.gt.0).and.(status.eq.0)) THEN
109 IF ((string(1:1).eq.'0') .or. &
110 & (string(1:1).eq.'F') .or. &
111 & (string(1:1).eq.'f')) THEN
112 value=.false.
113 ELSE
114 value=.true.
115 END IF
116 END IF
117!
118 RETURN
119 END FUNCTION get_env_l
120!
121 FUNCTION get_env_s (name, value) RESULT (status)
122!
123!***********************************************************************
124! !
125! Reads string environmental variable. !
126! !
127!***********************************************************************
128!
129! Imported variable declarations.
130!
131 character (len=*), intent(in) :: name
132 character (len=*), intent(out) :: value
133!
134! Local variable declaration.
135!
136 integer :: lstr1, lstr2, status
137 character (len=1024) :: string
138!
139!-----------------------------------------------------------------------
140! Read environmental variable. Return a blank space, value=CHAR(32),
141! if not found.
142!-----------------------------------------------------------------------
143!
144 CALL get_environment_variable (name, string, lstr1, status)
145!
146! Load string value.
147!
148 lstr2=len(value)
149!
150 IF (lstr1.gt.lstr2) THEN
151 value=string(1:lstr2)
152 IF (master) THEN
153 WRITE (stdout,10) trim(name), trim(string), lstr2, lstr1
154 END IF
155 ELSE
156 value=string(1:lstr1)
157 END IF
158!
159 10 FORMAT (/,' GET_ENV_S - Error while retrieving enviromental ', &
160 & 'variable, name = ',a,/,13x,"string = '",a,"'",/,13x, &
161 & 'value variable length = ',i0, &
162 & ' is less than the required length = ',i0)
163!
164 RETURN
165 END FUNCTION get_env_s
166!
167 END MODULE get_env_mod
integer function get_env_l(name, value)
Definition get_env.F:80
integer function get_env_i(name, value)
Definition get_env.F:31
integer function get_env_s(name, value)
Definition get_env.F:122
integer stdout
logical master