FV3 Bundle
ncdw_climsg.F90
Go to the documentation of this file.
1 ! nc_diag_write - NetCDF Layer Diag Writing Module
2 ! Copyright 2015 Albert Huang - SSAI/NASA for NASA GSFC GMAO (610.1).
3 !
4 ! Licensed under the Apache License, Version 2.0 (the "License");
5 ! you may not use this file except in compliance with the License.
6 ! You may obtain a copy of the License at
7 !
8 ! http://www.apache.org/licenses/LICENSE-2.0
9 !
10 ! Unless required by applicable law or agreed to in writing, software
11 ! distributed under the License is distributed on an "AS IS" BASIS,
12 ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 ! implied. See the License for the specific language governing
14 ! permissions and limitations under the License.
15 !
16 ! command line message printing module - ncdw_climsg
17 !
19  ! Module that provides command line message printing support.
20  !
21  ! This module has all of the subroutines needed to print various
22  ! types of command line messages.
23  !
24  ! Message types include:
25  ! -> Errors - errors that occur within the application. Errors
26  ! will always result in the program exiting (via stop). If
27  ! ANSI colors are enabled, this will show up in all red.
28  !
29  ! -> Warnings - warnings that occur within the application. This
30  ! will show a warning, but allow the program to continue. If
31  ! ANSI colors are enabled, this will show up in yellow (or
32  ! orange, depending on your terminal colors).
33  !
34  ! -> Info - information about the application's progress. These
35  ! tend to be verbose, hence the option to toggle them on and
36  ! off. By default, they are turned off.
37  !
38  ! -> Action - debug information that displays key subroutines and
39  ! their arguments at the start of the subroutine. These are
40  ! very verbose, hence the option to toggle them on and off.
41  !
42  ! In addition, since these are placed in front of subroutines,
43  ! they require a compile time flag to turn on, since they take
44  ! processing time.
45  !
46  ! By default, due to the high verbosity, they are off.
47  !
48  ! -> Debug - debug information about the application in general.
49  ! These are extremely verbose, and can only be turned on with
50  ! a compile time flag.
51  !
52 
53  ! Load our numerical types from kinds - we just need our standard
54  ! integer type, i_long
55  use ncd_kinds, only: i_long
56 
57  use netcdf, only: nf90_noerr, nf90_strerror
58 
59  implicit none
60 
61  ! Whether to enable info message printing or not.
62  ! By default, this is set to FALSE.
63  logical :: nclayer_enable_info = .false.
64 
65  ! Whether to enable action message printing or not.
66  ! By default, this is set to FALSE.
67  !
68  ! Note that even if this is set to TRUE, action message support
69  ! must be enabled at compile time for messages to be printed.
70  logical :: nclayer_enable_action = .false.
71 
72  contains
73  ! Display a given error message, and exit.
74  !
75  ! Display a specified error message, and exit.
76  !
77  ! If ANSI colors are enabled at compile time, the entire message
78  ! will be printed in red.
79  !
80  ! If error tracebacks are enabled, this will attempt to generate
81  ! a traceback of the error before terminating.
82  !
83  ! Args:
84  ! err (character(len=*)): the error to display.
85  !
86  ! Raises:
87  ! This is the error subroutine that exits, so it is
88  ! basically... an error itself. So indeed, this WILL result
89  ! in an error, no matter what!
90  !
91  ! Although unlikely, other errors may indirectly occur.
92  ! They may be general storage errors, or even a bug.
93  ! These errors are likely to crash the program in unexpected
94  ! ways...
95  !
96  subroutine nclayer_error(err)
97  character(len=*), intent(in) :: err
98 #ifdef ERROR_TRACEBACK
99  integer(i_long) :: div0
100 #endif
101  write(*, "(A)") " ** ERROR: " // err
102 #ifdef ERROR_TRACEBACK
103  write(*, "(A)") " ** Failed to process data/write NetCDF4."
104  write(*, "(A)") " (Traceback requested, triggering div0 error...)"
105  div0 = 1 / 0
106 #else
107  stop " ** Failed to process data/write NetCDF4."
108 #endif
109  end subroutine nclayer_error
110 
111  ! Display a given warning message.
112  !
113  ! Display a specified warning message.
114  !
115  ! If ANSI colors are enabled at compile time, the entire message
116  ! will be printed in yellow or orange, depending on how your
117  ! terminal displays colors.
118  !
119  ! Args:
120  ! warn (character(len=*)): the warning to display.
121  !
122  ! Raises:
123  ! Although unlikely, other errors may indirectly occur.
124  ! They may be general storage errors, or even a bug.
125  ! These errors are likely to crash the program in unexpected
126  ! ways...
127  !
128  subroutine nclayer_warning(warn)
129  character(len=*), intent(in) :: warn
130  write(*, "(A)") " ** WARNING: " // warn
131  end subroutine nclayer_warning
132 
133  ! Set whether to display action messages or not.
134  !
135  ! This sets the flag on whether to display action messages or
136  ! not.
137  !
138  ! If the provided argument is TRUE, action messages will be
139  ! displayed. Otherwise, they will be hidden, even if action
140  ! message calls are made.
141  !
142  ! Args:
143  ! action_on_off (logical): boolean indicating whether to
144  ! display action messages or not. If TRUE, action
145  ! messages will be displayed. Otherwise, they will be
146  ! hidden.
147  !
148  ! Raises:
149  ! Although unlikely, other errors may indirectly occur.
150  ! They may be general storage errors, or even a bug.
151  ! These errors are likely to crash the program in unexpected
152  ! ways...
153  !
154  subroutine nc_set_action_display(action_on_off)
155  logical :: action_on_off
156 #ifdef ENABLE_ACTION_MSGS
157  character(len=1000) :: action_str
158 
159  if (nclayer_enable_action) then
160  write(action_str, "(A, L, A)") "nc_set_action_display(action_on_off = ", action_on_off, ")"
161  call nclayer_actionm(trim(action_str))
162  end if
163 #endif
164  nclayer_enable_action = action_on_off
165  end subroutine nc_set_action_display
166 
167 #ifdef ENABLE_ACTION_MSGS
168  ! Display a given action message.
169  !
170  ! Display a specified action message.
171  !
172  ! The messages displayed here are intended to be debug messages
173  ! indicating the subroutine that was called, along with the
174  ! arguments provided for the subroutine, if any.
175  ! (Hence, the "action" message.)
176  !
177  ! An example of such a message:
178  ! nc_set_action_display(action_on_off = T)
179  !
180  ! Although other kinds of messages can be printed via action
181  ! messages, it's strongly recommended to only print subroutine
182  ! and/or function calls here.
183  !
184  ! If ANSI colors are enabled at compile time, the entire message
185  ! will be printed in cyan (light blue).
186  !
187  ! Args:
188  ! act (character(len=*)): the action message to display.
189  !
190  ! Raises:
191  ! Although unlikely, other errors may indirectly occur.
192  ! They may be general storage errors, or even a bug.
193  ! These errors are likely to crash the program in unexpected
194  ! ways...
195  !
196  subroutine nclayer_actionm(act)
197  character(len=*), intent(in) :: act
198  if (nclayer_enable_action) &
199  write(*, "(A)") " ** ACTION: " // act
200  end subroutine nclayer_actionm
201 #endif
202 
203  ! Set whether to display informational messages or not.
204  !
205  ! This sets the flag on whether to display information messages
206  ! or not.
207  !
208  ! If the provided argument is TRUE, informational messages will
209  ! be displayed. Otherwise, they will be hidden, even if info
210  ! message calls are made.
211  !
212  ! Args:
213  ! info_on_off (logical): boolean indicating whether to
214  ! display informational messages or not. If TRUE,
215  ! informational messages will be displayed. Otherwise,
216  ! they will be hidden.
217  !
218  ! Raises:
219  ! Although unlikely, other errors may indirectly occur.
220  ! They may be general storage errors, or even a bug.
221  ! These errors are likely to crash the program in unexpected
222  ! ways...
223  !
224  subroutine nc_set_info_display(info_on_off)
225  logical :: info_on_off
226 #ifdef ENABLE_ACTION_MSGS
227  character(len=1000) :: action_str
228 
229  if (nclayer_enable_action) then
230  write(action_str, "(A, L, A)") "nc_set_info_display(info_on_off = ", info_on_off, ")"
231  call nclayer_actionm(trim(action_str))
232  end if
233 #endif
234  nclayer_enable_info = info_on_off
235  end subroutine nc_set_info_display
236 
237  ! Display a given information message.
238  !
239  ! Display a specified information message.
240  !
241  ! If ANSI colors are enabled at compile time, the entire message
242  ! will be printed in blue.
243  !
244  ! Args:
245  ! ifo (character(len=*)): the information message to
246  ! display.
247  !
248  ! Raises:
249  ! Although unlikely, other errors may indirectly occur.
250  ! They may be general storage errors, or even a bug.
251  ! These errors are likely to crash the program in unexpected
252  ! ways...
253  !
254  subroutine nclayer_info(ifo)
255  character(len=*), intent(in) :: ifo
256  if (nclayer_enable_info) &
257  write(*, "(A)") " ** INFO: " // ifo
258  end subroutine nclayer_info
259 
260 #ifdef _DEBUG_MEM_
261  ! Display a given debug message.
262  !
263  ! Display a specified debug message. This subroutine is only
264  ! enabled when _DEBUG_MEM_ is defined at compile time.
265  ! Otherwise, this subroutine will not exist.
266  !
267  ! Therefore, any calls to this subroutine must have the
268  ! '#ifdef _DEBUG_MEM_' and #endif surrounding it.
269  !
270  ! Args:
271  ! dbg (character(len=*)): the debug message to display.
272  !
273  ! Raises:
274  ! Although unlikely, other errors may indirectly occur.
275  ! They may be general storage errors, or even a bug.
276  ! These errors are likely to crash the program in unexpected
277  ! ways...
278  !
279  subroutine nclayer_debug(dbg)
280  character(len=*), intent(in) :: dbg
281  write(*, "(A, A)") "D: ", dbg
282  end subroutine nclayer_debug
283 #endif
284 
285  ! Check whether a NetCDF operation completed successfully or
286  ! not, and if not, display the corresponding error message.
287  !
288  ! Given the NetCDF error code integer, determine whether the
289  ! corresponding NetCDF operation succeeded or not. If it failed,
290  ! display the corresponding error message and exit.
291  !
292  ! Args:
293  ! status (integer(i_long)): NetCDF error code to check.
294  !
295  ! Raises:
296  ! Although unlikely, other errors may indirectly occur.
297  ! They may be general storage errors, NetCDF errors, or even
298  ! a bug. See the called subroutines' documentation for
299  ! details.
300  !
301  subroutine nclayer_check(status)
302  integer(i_long), intent(in) :: status
303 
304  if(status /= nf90_noerr) then
305  call nclayer_error(trim(nf90_strerror(status)))
306  end if
307  end subroutine nclayer_check
308 end module ncdw_climsg
integer, parameter, public i_long
Definition: ncd_kinds.F90:47
logical nclayer_enable_action
Definition: ncdw_climsg.F90:70
subroutine nc_set_action_display(action_on_off)
subroutine nclayer_check(status)
subroutine nc_set_info_display(info_on_off)
subroutine nclayer_warning(warn)
subroutine nclayer_error(err)
Definition: ncdw_climsg.F90:97
subroutine nclayer_info(ifo)
logical nclayer_enable_info
Definition: ncdw_climsg.F90:63
subroutine nclayer_actionm(act)