FV3 Bundle
fms.F90
Go to the documentation of this file.
1 !***********************************************************************
2 !* GNU Lesser General Public License
3 !*
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
5 !*
6 !* FMS is free software: you can redistribute it and/or modify it under
7 !* the terms of the GNU Lesser General Public License as published by
8 !* the Free Software Foundation, either version 3 of the License, or (at
9 !* your option) any later version.
10 !*
11 !* FMS is distributed in the hope that it will be useful, but WITHOUT
12 !* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 !* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 !* for more details.
15 !*
16 !* You should have received a copy of the GNU Lesser General Public
17 !* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
18 !***********************************************************************
19 
20 module fms_mod
21 
22 ! <CONTACT EMAIL="Bruce.Wyman@noaa.gov">
23 ! Bruce Wyman
24 ! </CONTACT>
25 
26 ! <HISTORY SRC="http://www.gfdl.noaa.gov/fms-cgi-bin/cvsweb.cgi/FMS/"/>
27 
28 ! <OVERVIEW>
29 ! The fms module provides routines that are commonly used
30 ! by most FMS modules.
31 ! </OVERVIEW>
32 
33 ! <DESCRIPTION>
34 ! Here is a summary of the functions performed by routines
35 ! in the fms module.
36 !
37 ! 1. Output module version numbers to a common (<TT>log</TT>) file
38 ! using a common format.<BR/>
39 ! 2. Open specific types of files common to many FMS modules.
40 ! These include namelist files, restart files, and 32-bit IEEE
41 ! data files. There also is a matching interface to close the files.
42 ! If other file types are needed the <TT>mpp_open</TT> and <TT>mpp_close</TT>
43 ! interfaces in module <LINK SRC="http://www.gfdl.noaa.gov/fms-cgi-bin/cvsweb.cgi/FMS/shared/mpp/mpp_io.html">mpp_io</LINK> must be used.<BR/>
44 ! 3. Read and write distributed data to simple native unformatted files.
45 ! This type of file (called a restart file) is used to checkpoint
46 ! model integrations for a subsequent restart of the run.<BR/>
47 ! 4. For convenience there are several routines published from
48 ! the <LINK SRC="http://www.gfdl.noaa.gov/fms-cgi-bin/cvsweb.cgi/FMS/shared/mpp/mpp.html">mpp</LINK> module. These are routines for getting processor
49 ! numbers, commonly used I/O unit numbers, error handling, and timing sections of code.
50 ! </DESCRIPTION>
51 
52 !-----------------------------------------------------------------------
53 !
54 ! A collection of commonly used routines.
55 !
56 ! The routines are primarily I/O related, however, there also
57 ! exists several simple miscellaneous utility routines.
58 !
59 !-----------------------------------------------------------------------
60 !
61 ! file_exist Checks the existence of the given file name.
62 !
63 ! check_nml_error Checks the iostat argument that is returned after
64 ! reading a namelist and determines if the error
65 ! code is valid.
66 !
67 ! write_version_number Prints to the log file (or a specified unit)
68 ! the (cvs) version id string and (cvs) tag name.
69 !
70 ! error_mesg Print notes, warnings and error messages,
71 ! terminates program for error messages.
72 ! (use error levels NOTE,WARNING,FATAL)
73 !
74 ! open_namelist_file Opens namelist file for reading only.
75 !
76 ! open_restart_file Opens a file that will be used for reading or writing
77 ! restart files with native unformatted data.
78 !
79 ! open_ieee32_file Opens a file that will be used for reading or writing
80 ! unformatted 32-bit ieee data.
81 !
82 ! close_file Closes a file that was opened using
83 ! open_namelist_file, open_restart_file, or
84 ! open_ieee32_file.
85 !
86 ! set_domain Call this routine to internally store in fms_mod the
87 ! domain2d data type prior to calling the distributed
88 ! data I/O routines read_data and write_data.
89 !
90 ! read_data Reads distributed data from a single threaded file.
91 !
92 ! write_data Writes distributed data to a single threaded file.
93 !
94 ! fms_init Initializes the fms module and also the
95 ! mpp_io module (which initializes all mpp mods).
96 ! Will be called automatically if the user does
97 ! not call it.
98 !
99 ! fms_end Calls mpp exit routines.
100 !
101 ! lowercase Convert character strings to all lower case
102 !
103 ! uppercase Convert character strings to all upper case
104 !
105 ! monotonic_array Determines if the real input array has
106 ! monotonically increasing or decreasing values.
107 !
108 ! string_array_index Match the input character string to a string
109 ! in an array/list of character strings.
110 !
111 !-----------------------------------------------------------------------
112 !---- published routines from mpp_mod ----
113 !
114 ! mpp_error, NOTE, WARNING, FATAL
115 ! mpp_error_state
116 ! mpp_pe, mpp_npes, mpp_root_pe
117 ! stdin, stdout, stderr, stdlog
118 ! mpp_chksum
119 !
120 ! mpp_clock_id, mpp_clock_begin , mpp_clock_end
121 ! MPP_CLOCK_SYNC, MPP_CLOCK_DETAILED
122 ! CLOCK_COMPONENT, CLOCK_SUBCOMPONENT, CLOCK_MODULE_DRIVER,
123 ! CLOCK_MODULE, CLOCK_ROUTINE, CLOCK_LOOP, CLOCK_INFRA
124 !
125 !-----------------------------------------------------------------------
126 
127 use mpp_mod, only: mpp_error, note, warning, fatal, &
128  mpp_set_warn_level, &
129  mpp_transmit, all_pes, &
130  mpp_pe, mpp_npes, mpp_root_pe, &
131  mpp_sync, mpp_chksum, &
132  mpp_clock_begin, mpp_clock_end, &
133  mpp_clock_id, mpp_init, mpp_exit, &
134  mpp_clock_sync, mpp_clock_detailed, &
135  clock_component, clock_subcomponent,&
136  clock_module_driver, clock_module, &
137  clock_routine, clock_loop, &
138  clock_infra, mpp_clock_set_grain, &
139  mpp_set_stack_size, &
140  stdin, stdout, stderr, stdlog, &
141  mpp_error_state, lowercase, &
142  uppercase, mpp_broadcast, input_nml_file
143 
145  mpp_update_domains, global_data_domain, &
146  mpp_domains_init, mpp_domains_exit, &
147  mpp_global_field, mpp_domains_set_stack_size, &
150 
151 use mpp_io_mod, only: mpp_io_init, mpp_open, mpp_close, &
152  mpp_ascii, mpp_native, mpp_ieee32, mpp_netcdf, &
153  mpp_rdonly, mpp_wronly, mpp_append, mpp_overwr, &
154  mpp_sequential, mpp_direct, &
155  mpp_single, mpp_multi, mpp_delete, mpp_io_exit, &
156  fieldtype, mpp_get_atts, mpp_get_info, mpp_get_fields, &
157  do_cf_compliance
158 
166 
168 use constants_mod, only: constants_version=>version !pjp: PI not computed
169 
170 
171 implicit none
172 private
173 
174 ! routines for initialization and termination of module
175 public :: fms_init, fms_end
176 
177 ! routines for opening/closing specific types of file
181 
182 ! routines for reading/writing distributed data
185 public :: get_global_att_value
186 
187 ! routines for get mosaic information
189 
190 ! miscellaneous i/o routines
193 ! i/o routines from fms_io
194 public :: write_version_number
195 
196 ! miscellaneous utilities (non i/o)
197 public :: lowercase, uppercase, string, &
199 
200 ! public mpp interfaces
201 public :: mpp_error, note, warning, fatal, &
202  mpp_error_state, &
203  mpp_pe, mpp_npes, mpp_root_pe, &
204  stdin, stdout, stderr, stdlog, &
205  mpp_chksum
206 public :: mpp_clock_id, mpp_clock_begin, mpp_clock_end
207 public :: mpp_clock_sync, mpp_clock_detailed
208 public :: clock_component, clock_subcomponent, &
209  clock_module_driver, clock_module, &
210  clock_routine, clock_loop, clock_infra
211 ! public mpp-io interfaces
212 public :: do_cf_compliance
213 
214 !Balaji
215 !this is published by fms and applied to any initialized clocks
216 !of course you can go and set the flag to SYNC or DETAILED by hand
217 integer, public :: clock_flag_default
218 
219 ! Namelist read error values
221  INTEGER :: multiplenmlsinfile
222  INTEGER :: badtype1
223  INTEGER :: badtype2
224  INTEGER :: missingvar
225  INTEGER :: notinfile
226  END TYPE nml_errors_type
228 
229 
230 !------ namelist interface -------
231 !------ adjustable severity level for warnings ------
232 
233  logical :: read_all_pe = .true.
234  character(len=16) :: clock_grain = 'NONE', clock_flags='NONE'
235  character(len=8) :: warning_level = 'warning'
236  character(len=64) :: iospec_ieee32 = '-N ieee_32'
237  integer :: stack_size = 0
238  integer :: domains_stack_size = 0
239  logical, public :: print_memory_usage = .false.
240 
241 !------ namelist interface -------
242 
243 ! <NAMELIST NAME="fms_nml">
244 ! <DATA NAME="clock_grain" TYPE="character" DEFAULT="'NONE'">
245 ! The level of clock granularity used for performance timing sections
246 ! of code. Possible values in order of increasing detail are:
247 ! 'NONE', 'COMPONENT', 'SUBCOMPONENT', 'MODULE_DRIVER', 'MODULE', 'ROUTINE',
248 ! 'LOOP', and 'INFRA'. Code sections are defined using routines in MPP
249 ! module: mpp_clock_id, mpp_clock_begin, and mpp_clock_end.
250 ! The fms module makes these routines public.
251 ! A list of timed code sections will be printed to STDOUT.
252 ! See the <LINK SRC="http://www.gfdl.noaa.gov/fms-cgi-bin/cvsweb.cgi/FMS/shared/mpp/mpp.html">MPP</LINK>
253 ! module for more details.
254 ! </DATA>
255 ! <DATA NAME="clock_flags" TYPE="character" DEFAULT="'NONE'">
256 ! Possible values are 'NONE', 'SYNC', or 'DETAILED'.
257 ! SYNC will give accurate information on load balance of the clocked
258 ! portion of code.
259 ! DETAILED also turns on detailed message-passing performance diagnosis.
260 ! Both SYNC and DETAILED will work correctly on innermost clock nest
261 ! and distort outer clocks, and possibly the overall code time.
262 ! See the <LINK SRC="http://www.gfdl.noaa.gov/fms-cgi-bin/cvsweb.cgi/FMS/shared/mpp/mpp.html">MPP</LINK>
263 ! module for more details.
264 ! </DATA>
265 ! <DATA NAME="read_all_pe" TYPE="logical" DEFAULT="true">
266 ! Read global data on all processors extracting local part needed (TRUE) or
267 ! read global data on PE0 and broadcast to all PEs (FALSE).
268 ! </DATA>
269 ! <DATA NAME="warning_level" TYPE="character" DEFAULT="'warning'">
270 ! Sets the termination condition for the WARNING flag to interfaces
271 ! error_mesg/mpp_error. set warning_level = 'fatal' (program crashes for
272 ! warning messages) or 'warning' (prints warning message and continues).
273 ! </DATA>
274 ! <DATA NAME="iospec_ieee32" TYPE="character" DEFAULT="'-N ieee_32'">
275 ! iospec flag used with the open_ieee32_file interface.
276 ! </DATA>
277 ! <DATA NAME="stack_size" TYPE="integer" DEFAULT="0">
278 ! The size in words of the MPP user stack. If stack_size > 0, the following
279 ! MPP routine is called: call mpp_set_stack_size (stack_size). If stack_size
280 ! = 0 (default) then the default size set by mpp_mod is used.
281 ! </DATA>
282 ! <DATA NAME="domains_stack_size" TYPE="integer" DEFAULT="0">
283 ! The size in words of the MPP_DOMAINS user stack. If
284 ! domains_stack_size > 0, the following MPP_DOMAINS routine is called:
285 ! call mpp_domains_set_stack_size (domains_stack_size). If
286 ! domains_stack_size = 0 (default) then the default size set by
287 ! mpp_domains_mod is used.
288 ! </DATA>
289 ! <DATA NAME="print_memory_usage" TYPE="logical" DEFAULT=".FALSE.">
290 ! If set to .TRUE., memory usage statistics will be printed at various
291 ! points in the code. It is used to study memory usage, e.g to detect
292 ! memory leaks.
293 ! </DATA>
294 ! </NAMELIST>
295 
296  namelist /fms_nml/ read_all_pe, clock_grain, clock_flags, &
300 
301 ! ---- private data for check_nml_error ----
302 
303  integer, private :: num_nml_error_codes, nml_error_codes(20)
304  logical, private :: do_nml_error_init = .true.
305  private nml_error_init
306 
307 
308 ! ---- version number -----
309 
310 ! Include variable "version" to be written to log file.
311 #include<file_version.h>
312 
313  logical :: module_is_initialized = .false.
314 
315 
316 contains
317 
318 !#######################################################################
319 
320 ! <SUBROUTINE NAME="fms_init">
321 
322 ! <OVERVIEW>
323 ! Initializes the FMS module and also calls the initialization routines for all
324 ! modules in the MPP package. Will be called automatically if the user does
325 ! not call it.
326 ! </OVERVIEW>
327 ! <DESCRIPTION>
328 ! Initialization routine for the fms module. It also calls initialization routines
329 ! for the mpp, mpp_domains, and mpp_io modules. Although this routine
330 ! will be called automatically by other fms_mod routines, users should
331 ! explicitly call fms_init. If this routine is called more than once it will
332 ! return silently. There are no arguments.
333 ! </DESCRIPTION>
334 ! <TEMPLATE>
335 ! call fms_init ( )
336 ! </TEMPLATE>
337 
338 
339 ! <ERROR MSG="invalid entry for namelist variable warning_level" STATUS="FATAL">
340 ! The namelist variable warning_level must be either 'fatal' or 'warning'
341 ! (case-insensitive).
342 ! </ERROR>
343 ! <ERROR MSG="invalid entry for namelist variable clock_grain" STATUS="FATAL">
344 ! The namelist variable clock_grain must be one of the following values:
345 ! 'NONE', 'COMPONENT', 'SUBCOMPONENT', 'MODULE_DRIVER', 'MODULE', 'ROUTINE',
346 ! 'LOOP', or 'INFRA' (case-insensitive).
347 ! </ERROR>
348 
349 ! initializes the fms module/package
350 ! also calls mpp initialization routines and reads fms namelist
351 
352 subroutine fms_init (localcomm )
353  integer, intent(in), optional :: localcomm
354  integer :: unit, ierr, io
355 
356  if (module_is_initialized) return ! return silently if already called
357  module_is_initialized = .true.
358 !---- initialize mpp routines ----
359  if(present(localcomm)) then
360  call mpp_init(localcomm=localcomm)
361  else
362  call mpp_init()
363  endif
364  call mpp_domains_init
365  call fms_io_init
366 
367 !---- read namelist input ----
368 
369  call nml_error_init ! first initialize namelist iostat error codes
370 
371 #ifdef INTERNAL_FILE_NML
372  read (input_nml_file, fms_nml, iostat=io)
373  ierr = check_nml_error(io,'fms_nml')
374 #else
375  if (file_exist('input.nml')) then
376  unit = open_namelist_file( )
377  ierr=1; do while (ierr /= 0)
378  read (unit, nml=fms_nml, iostat=io, end=10)
379  ierr = check_nml_error(io,'fms_nml') ! also initializes nml error codes
380  enddo
381  10 call mpp_close (unit)
382  endif
383 #endif
384 
385 !---- define mpp stack sizes if non-zero -----
386 
387  if ( stack_size > 0) call mpp_set_stack_size ( stack_size)
388  if (domains_stack_size > 0) call mpp_domains_set_stack_size (domains_stack_size)
389 
390 !---- set severity level for warnings ----
391 
392  select case( trim(lowercase(warning_level)) )
393  case( 'fatal' )
394  call mpp_set_warn_level ( fatal )
395  case( 'warning' )
396  call mpp_set_warn_level ( warning )
397  case default
398  call error_mesg ( 'fms_init', &
399  'invalid entry for namelist variable warning_level', fatal )
400  end select
401 
402 !--- set granularity for timing code sections ---
403 
404  select case( trim(uppercase(clock_grain)) )
405  case( 'NONE' )
406  call mpp_clock_set_grain (0)
407  case( 'COMPONENT' )
408  call mpp_clock_set_grain (clock_component)
409  case( 'SUBCOMPONENT' )
410  call mpp_clock_set_grain (clock_subcomponent)
411  case( 'MODULE_DRIVER' )
412  call mpp_clock_set_grain (clock_module_driver)
413  case( 'MODULE' )
414  call mpp_clock_set_grain (clock_module)
415  case( 'ROUTINE' )
416  call mpp_clock_set_grain (clock_routine)
417  case( 'LOOP' )
418  call mpp_clock_set_grain (clock_loop)
419  case( 'INFRA' )
420  call mpp_clock_set_grain (clock_infra)
421  case default
422  call error_mesg ( 'fms_init', &
423  'invalid entry for namelist variable clock_grain', fatal )
424  end select
425 !Balaji
426  select case( trim(uppercase(clock_flags)) )
427  case( 'NONE' )
429  case( 'SYNC' )
430  clock_flag_default = mpp_clock_sync
431  case( 'DETAILED' )
432  clock_flag_default = mpp_clock_detailed
433  case default
434  call error_mesg ( 'fms_init', &
435  'invalid entry for namelist variable clock_flags', fatal )
436  end select
437 
438 !--- write version info and namelist to logfile ---
439 
440  call write_version_number("FMS_MOD", version)
441  if (mpp_pe() == mpp_root_pe()) then
442  unit = stdlog()
443  write (unit, nml=fms_nml)
444  write (unit,*) 'nml_error_codes=', nml_error_codes(1:num_nml_error_codes)
445  endif
446 
448  call print_memuse_stats('fms_init')
449 
450  call write_version_number("CONSTANTS_MOD", constants_version)
451 
452 end subroutine fms_init
453 ! </SUBROUTINE>
454 
455 !#######################################################################
456 
457 
458 ! <SUBROUTINE NAME="fms_end">
459 
460 ! <OVERVIEW>
461 ! Calls the termination routines for all modules in the MPP package.
462 ! </OVERVIEW>
463 ! <DESCRIPTION>
464 ! Termination routine for the fms module. It also calls destructor routines
465 ! for the mpp, mpp_domains, and mpp_io modules. If this routine is called
466 ! more than once it will return silently. There are no arguments.
467 ! </DESCRIPTION>
468 ! <TEMPLATE>
469 ! call fms_end ( )
470 ! </TEMPLATE>
471 
472 ! terminates the fms module/package
473 ! also calls mpp destructor routines
474 
475 subroutine fms_end ( )
477  if (.not.module_is_initialized) return ! return silently
478 ! call fms_io_exit ! now called from coupler_end
479  call mpp_io_exit
480  call mpp_domains_exit
481  call mpp_exit
482  module_is_initialized =.false.
483 
484 end subroutine fms_end
485 ! </SUBROUTINE>
486 
487 
488 !#######################################################################
489 ! <SUBROUTINE NAME="error_mesg">
490 
491 ! <OVERVIEW>
492 ! Print notes, warnings and error messages; terminates program for warning
493 ! and error messages. (use error levels NOTE,WARNING,FATAL, see example below)
494 ! </OVERVIEW>
495 ! <DESCRIPTION>
496 ! Print notes, warnings and error messages; and terminates the program for
497 ! error messages. This routine is a wrapper around mpp_error, and is provided
498 ! for backward compatibility. This module also publishes mpp_error,
499 ! <B>users should try to use the mpp_error interface</B>.
500 ! </DESCRIPTION>
501 ! <TEMPLATE>
502 ! call error_mesg ( routine, message, level )
503 ! </TEMPLATE>
504 
505 ! <IN NAME="routine" TYPE="character" >
506 ! Routine name where the warning or error has occurred.
507 ! </IN>
508 ! <IN NAME="message" TYPE="character" >
509 ! Warning or error message to be printed.
510 ! </IN>
511 ! <IN NAME="level" TYPE="integer" >
512 ! Level of severity; set to NOTE, WARNING, or FATAL Termination always occurs
513 ! for FATAL, never for NOTE, and is settable for WARNING (see namelist).
514 ! </IN>
515 ! <NOTE>
516 !
517 ! Examples:
518 ! <PRE>
519 ! use fms_mod, only: error_mesg, FATAL, NOTE
520 
521 ! call error_mesg ('fms_mod', 'initialization not called', FATAL)
522 ! call error_mesg ('fms_mod', 'fms_mod message', NOTE)
523 ! </PRE>
524 ! </NOTE>
525 ! wrapper for the mpp error handler
526 ! users should try to use the mpp_error interface
527 
528  subroutine error_mesg (routine, message, level)
529  character(len=*), intent(in) :: routine, message
530  integer, intent(in) :: level
531 
532 ! input:
533 ! routine name of the calling routine (character string)
534 ! message message written to output (character string)
535 ! level set to NOTE, MESSAGE, or FATAL (integer)
536 
537  if (.not.module_is_initialized) call fms_init ( )
538  call mpp_error ( routine, message, level )
539 
540  end subroutine error_mesg
541 ! </SUBROUTINE>
542 
543 !#######################################################################
544 ! <FUNCTION NAME="fms_error_handler">
545 
546 ! <OVERVIEW>
547 ! Facilitates the control of fatal error conditions
548 ! </OVERVIEW>
549 ! <DESCRIPTION>
550 ! When err_msg is present, message is copied into err_msg
551 ! and the function returns a value of .true.
552 ! Otherwise calls mpp_error to terminate execution.
553 ! The intended use is as shown below.
554 ! </DESCRIPTION>
555 ! <TEMPLATE>
556 ! if(fms_error_handler(routine, message, err_msg)) return
557 ! </TEMPLATE>
558 ! <IN NAME="routine" TYPE="character">
559 ! Routine name where the fatal error has occurred.
560 ! </IN>
561 ! <IN NAME="message" TYPE="character">
562 ! fatal error message to be printed.
563 ! </IN>
564 ! <OUT NAME="fms_error_handler" TYPE="logical">
565 ! .true. when err_msg is present
566 ! .false. when err_msg is not present
567 ! </OUT>
568 ! <OUT NAME="err_msg" TYPE="character">
569 ! When err_msg is present: err_msg = message
570 ! </OUT>
571 
572  function fms_error_handler(routine, message, err_msg)
574  logical :: fms_error_handler
575  character(len=*), intent(in) :: routine, message
576  character(len=*), intent(out), optional :: err_msg
577 
578  fms_error_handler = .false.
579  if(present(err_msg)) then
580  err_msg = message
581  fms_error_handler = .true.
582  else
583  call mpp_error(trim(routine),trim(message),fatal)
584  endif
585 
586  end function fms_error_handler
587 ! </FUNCTION>
588 
589 !#######################################################################
590 ! <FUNCTION NAME="check_nml_error">
591 
592 ! <OVERVIEW>
593 ! Checks the iostat argument that is returned after reading a namelist
594 ! and determines if the error code is valid.
595 ! </OVERVIEW>
596 ! <DESCRIPTION>
597 ! The FMS allows multiple namelist records to reside in the same file.
598 ! Use this interface to check the iostat argument that is returned after
599 ! reading a record from the namelist file. If an invalid iostat value
600 ! is detected this routine will produce a fatal error. See the NOTE below.
601 ! </DESCRIPTION>
602 ! <TEMPLATE>
603 ! check_nml_error ( iostat, nml_name )
604 ! </TEMPLATE>
605 
606 ! <IN NAME="iostat" TYPE="integer" >
607 ! The iostat value returned when reading a namelist record.
608 ! </IN>
609 ! <IN NAME="nml_name" TYPE="character" >
610 ! The name of the namelist. This name will be printed if an error is
611 ! encountered, otherwise the name is not used.
612 ! </IN>
613 ! <OUT NAME="" TYPE="integer" >
614 ! This function returns the input iostat value (integer) if it is an
615 ! allowable error code. If the iostat error code is not
616 ! allowable, an error message is printed and the program terminated.
617 ! </OUT>
618 ! <NOTE>
619 ! Some compilers will return non-zero iostat values when reading through
620 ! files with multiple namelist. This routine
621 ! will try skip these errors and only terminate for true namelist errors.
622 !
623 ! Examples
624 !
625 ! The following example checks if a file exists, reads a namelist input
626 ! from that file, and checks for errors in that
627 ! namelist. When the correct namelist is read and it has no errors the
628 ! routine check_nml_error will return zero and the while loop will exit.
629 ! This code segment should be used to read namelist files.
630 ! <PRE>
631 ! integer :: unit, ierr, io
632 !
633 ! if ( file_exist('input.nml') ) then
634 ! unit = open_namelist_file ( )
635 ! ierr=1
636 ! do while (ierr > 0)
637 ! read (unit, nml=moist_processes_nml, iostat=io)
638 ! ierr = check_nml_error(io,'moist_processes_nml')
639 ! enddo
640 ! call close_file (unit)
641 ! endif
642 ! </PRE>
643 ! </NOTE>
644 
645 ! <ERROR MSG="Unknown error while reading namelist ...., (IOSTAT = ####)" STATUS="FATAL">
646 ! There was an error reading the namelist specified. Carefully examine all namelist and variables
647 ! for anything incorrect (e.g. malformed, hidden characters).
648 ! </ERROR>
649 ! <ERROR MSG="Unknown namelist, or mistyped namelist variable in namelist ...., (IOSTAT = ####)" STATUS="FATAL">
650 ! The name list given doesn't exist in the namelist file, or a variable in the namelist is mistyped or isn't a
651 ! namelist variable.
652 ! </ERROR>
653 
654 ! used to check the iostat argument that is
655 ! returned after reading a namelist
656 ! see the online documentation for how this routine might be used
657  INTEGER FUNCTION check_nml_error(IOSTAT, NML_NAME)
658  INTEGER, INTENT(in) :: iostat
659  CHARACTER(len=*), INTENT(in) :: nml_name
660 
661  CHARACTER(len=256) :: err_str
662 
663  IF ( .NOT.module_is_initialized) CALL fms_init()
664 
665  check_nml_error = iostat
666 
667  ! Return on valid IOSTAT values
668  IF ( iostat <= 0 .OR.&
669  & iostat == nml_errors%multipleNMLSinFile .OR.&
670  & iostat == nml_errors%NotInFile) RETURN
671 
672  ! Everything else is a FATAL
673  IF ( (iostat == nml_errors%badType1 .OR. iostat == nml_errors%badType2) .OR. iostat == nml_errors%missingVar ) THEN
674  WRITE (err_str,*) 'Unknown namelist, or mistyped namelist variable in namelist ',trim(nml_name),', (IOSTAT = ',iostat,')'
675  CALL error_mesg ('check_nml_error in fms_mod', err_str, fatal)
676  CALL mpp_sync()
677  ELSE
678  WRITE (err_str,*) 'Unknown error while reading namelist ',trim(nml_name),', (IOSTAT = ',iostat,')'
679  CALL error_mesg ('check_nml_error in fms_mod', err_str, fatal)
680  CALL mpp_sync()
681  END IF
682  END FUNCTION check_nml_error
683 ! </FUNCTION>
684 
685 !-----------------------------------------------------------------------
686 ! private routine for initializing allowable error codes
687 
688  SUBROUTINE nml_error_init
689  ! Determines the IOSTAT error value for some common Namelist errors.
690  ! Also checks if the compiler returns a non-zero status if there are
691  ! multiple namelist records in a single file.
692  INTEGER, PARAMETER :: unit_begin = 20, unit_end = 1024
693  INTEGER :: fileunit, io_stat
694  INTEGER, DIMENSION(5) :: nml_iostats
695  LOGICAL :: opened
696 
697  ! Variables for sample namelists
698  INTEGER :: i1, i2
699  REAL :: r1, r2
700  LOGICAL :: l1
701  namelist /a_nml/ i1, r1
702  namelist /b_nml/ i2, r2, l1
703  namelist /badtype1_nml/ i1, r1
704  namelist /badtype2_nml/ i1, r1
705  namelist /missingvar_nml/ i2, r2
706  namelist /not_in_file_nml/ i2, r2
707 
708  ! Initialize the sample namelist variables
709  i1 = 1
710  i2 = 2
711  r1 = 1.0
712  r2 = 2.0
713  l1 = .false.
714 
715  ! Create a dummy namelist file
716  IF ( mpp_pe() == mpp_root_pe() ) THEN
717  ! Find a free file unit for a scratch file
718  file_opened: DO fileunit = unit_begin, unit_end
719  INQUIRE(unit=fileunit, opened=opened)
720  IF ( .NOT.opened ) EXIT file_opened
721  END DO file_opened
722 
723 #if defined(__PGI) || defined(_CRAYFTN)
724  OPEN (unit=fileunit, file='_read_error.nml', iostat=io_stat)
725 #else
726  OPEN (unit=fileunit, status='SCRATCH', iostat=io_stat)
727 #endif
728 
729  ! Write sample namelist to the SCRATCH file.
730  WRITE (unit=fileunit, nml=a_nml, iostat=io_stat)
731  WRITE (unit=fileunit, nml=b_nml, iostat=io_stat)
732  WRITE (unit=fileunit, iostat=io_stat, fmt='(/,"&badType1_nml i1=1, r1=''bad'' /",/)')
733  WRITE (unit=fileunit, iostat=io_stat, fmt='(/,"&badType2_nml i1=1, r1=.true. /",/)')
734  WRITE (unit=fileunit, iostat=io_stat, fmt='(/,"&missingVar_nml i2=1, r2=1.0e0, l1=.true. /",/)')
735 
736  ! Rewind for reading
737  rewind(unit=fileunit)
738 
739  ! Read the second namelist from the file -- check for namelist bug
740  READ (unit=fileunit, nml=b_nml, iostat=nml_iostats(1))
741  rewind(unit=fileunit)
742 
743  ! Read in bad type 1 --- Some compilers treat the string cast differently
744  READ (unit=fileunit, nml=badtype1_nml, iostat=nml_iostats(2))
745  rewind(unit=fileunit)
746 
747  ! Read in bad type 2
748  READ (unit=fileunit, nml=badtype2_nml, iostat=nml_iostats(3))
749  rewind(unit=fileunit)
750 
751  ! Read in missing variable/misstyped
752  READ (unit=fileunit, nml=missingvar_nml, iostat=nml_iostats(4))
753  rewind(unit=fileunit)
754 
755  ! Code for namelist not in file
756  READ (unit=fileunit, nml=not_in_file_nml, iostat=nml_iostats(5))
757 
758  ! Done, close file
759  CLOSE (unit=fileunit)
760 
761  ! Some compilers don't handle the type casting as well as we would like.
762  IF ( nml_iostats(2) * nml_iostats(3) .EQ. 0 ) THEN
763  IF ( nml_iostats(2) .NE. 0 .AND. nml_iostats(3) .EQ. 0 ) THEN
764  nml_iostats(3) = nml_iostats(2)
765  ELSE IF ( nml_iostats(2) .EQ. 0 .AND. nml_iostats(3) .NE.0 ) THEN
766  nml_iostats(2) = nml_iostats(3)
767  ELSE
768  nml_iostats(2) = nml_iostats(4)
769  nml_iostats(2) = nml_iostats(4)
770  END IF
771  END IF
772  END IF
773 
774  ! Broadcast nml_errors
775  CALL mpp_broadcast(nml_iostats,5,mpp_root_pe())
776  nml_errors%multipleNMLSinFile = nml_iostats(1)
777  nml_errors%badType1 = nml_iostats(2)
778  nml_errors%badType2 = nml_iostats(3)
779  nml_errors%missingVar = nml_iostats(4)
780  nml_errors%NotInFile = nml_iostats(5)
781 
782  do_nml_error_init = .false.
783  END SUBROUTINE nml_error_init
784 
785 !#######################################################################
786 
787 
788 ! <FUNCTION NAME="string_array_index">
789 
790 ! <OVERVIEW>
791 ! match the input character string to a string
792 ! in an array/list of character strings
793 ! </OVERVIEW>
794 ! <DESCRIPTION>
795 ! Tries to find a match for a character string in a list of character strings.
796 ! The match is case sensitive and disregards blank characters to the right of
797 ! the string.
798 ! </DESCRIPTION>
799 ! <TEMPLATE>
800 ! string_array_index ( string, string_array [, index] )
801 ! </TEMPLATE>
802 
803 ! <IN NAME="string" TYPE="character(len=*), scalar" >
804 ! Character string of arbitrary length.
805 ! </IN>
806 ! <IN NAME="string_array" TYPE="character(len=*)" DIM="(:)">
807 ! Array/list of character strings.
808 ! </IN>
809 ! <OUT NAME="index" TYPE="integer" >
810 ! The index of string_array where the first match was found. If
811 ! no match was found then index = 0.
812 ! </OUT>
813 ! <OUT NAME="string_array_index" TYPE="logical" >
814 ! If an exact match was found then TRUE is returned, otherwise FALSE is returned.
815 ! </OUT>
816 ! <NOTE>
817 ! Examples
818 ! <PRE>
819 ! string = "def"
820 ! string_array = (/ "abcd", "def ", "fghi" /)
821 
822 ! string_array_index ( string, string_array, index )
823 
824 ! Returns: TRUE, index = 2
825 ! </PRE>
826 ! </NOTE>
827 ! match the input character string to a string
828 ! in an array/list of character strings
829 
830 function string_array_index ( string, string_array, index ) result (found)
831 character(len=*), intent(in) :: string, string_array(:)
832 integer, optional, intent(out) :: index
833 logical :: found
834 integer :: i
835 
836 ! initialize this function to false
837 ! loop thru string_array and exit when a match is found
838 
839  found = .false.
840  if (present(index)) index = 0
841 
842  do i = 1, size(string_array(:))
843  ! found a string match ?
844  if ( trim(string) == trim(string_array(i)) ) then
845  found = .true.
846  if (present(index)) index = i
847  exit
848  endif
849  enddo
850 
851 end function string_array_index
852 ! </FUNCTION>
853 
854 !#######################################################################
855 
856 ! <FUNCTION NAME="monotonic_array">
857 
858 ! <OVERVIEW>
859 ! Determines if a real input array has monotonically increasing or
860 ! decreasing values.
861 ! </OVERVIEW>
862 ! <DESCRIPTION>
863 ! Determines if the real input array has monotonically increasing or
864 ! decreasing values.
865 ! </DESCRIPTION>
866 ! <TEMPLATE>
867 ! monotonic_array ( array [, direction] )
868 ! </TEMPLATE>
869 
870 ! <IN NAME="array" TYPE="real" DIM="(:)">
871 ! An array of real values. If the size(array) < 2 this function
872 ! assumes the array is not monotonic, no fatal error will occur.
873 ! </IN>
874 ! <OUT NAME="direction" TYPE="integer" >
875 ! If the input array is:
876 ! >> monotonic (small to large) then direction = +1.
877 ! >> monotonic (large to small) then direction = -1.
878 ! >> not monotonic then direction = 0.
879 ! </OUT>
880 ! <OUT NAME="monotonic_array" TYPE="logical" >
881 ! If the input array of real values either increases or decreases monotonically
882 ! then TRUE is returned, otherwise FALSE is returned.
883 ! </OUT>
884 ! determines if the real input array has
885 ! monotonically increasing or decreasing values
886 
887 function monotonic_array ( array, direction )
888 real, intent(in) :: array(:)
889 integer, intent(out), optional :: direction
890 logical :: monotonic_array
891 integer :: i
892 
893 ! initialize
894  monotonic_array = .false.
895  if (present(direction)) direction = 0
896 
897 ! array too short
898  if ( size(array(:)) < 2 ) return
899 
900 ! ascending
901  if ( array(1) < array(size(array(:))) ) then
902  do i = 2, size(array(:))
903  if (array(i-1) < array(i)) cycle
904  return
905  enddo
906  monotonic_array = .true.
907  if (present(direction)) direction = +1
908 
909 ! descending
910  else
911  do i = 2, size(array(:))
912  if (array(i-1) > array(i)) cycle
913  return
914  enddo
915  monotonic_array = .true.
916  if (present(direction)) direction = -1
917  endif
918 
919 end function monotonic_array
920 ! </FUNCTION>
921 
922 end module fms_mod
923 ! <INFO>
924 ! <BUG>
925 ! Namelist error checking may not work correctly with some compilers.
926 !
927 ! Users should beware when mixing Fortran reads and read_data calls. If a
928 ! Fortran read follows read_data and namelist variable read_all_pe = FALSE
929 ! (not the default), then the code will fail. It is safest if Fortran reads
930 ! precede calls to read_data.
931 ! </BUG>
932 ! <ERROR MSG="unexpected EOF" STATUS="FATAL">
933 ! An unexpected end-of-file was encountered in a read_data call.
934 ! You may want to use the optional end argument to detect the EOF.
935 ! </ERROR>
936 ! <NOTE>
937 ! 1) If the <B>MPP</B> or <B>MPP_DOMAINS</B> stack size is exceeded the
938 ! program will terminate after printing the required size.
939 !
940 ! 2) When running on a very small number of processors or for high
941 ! resolution models the default domains_stack_size will
942 ! probably be insufficient.
943 !
944 ! 3) The following performance routines in the <B>MPP</B> module are published by this module.
945 !<PRE>
946 ! mpp_clock_id, mpp_clock_begin, mpp_clock_end
947 !</PRE>
948 ! and associated parameters that are published:
949 !<PRE>
950 ! MPP_CLOCK_SYNC, MPP_CLOCK_DETAILED, CLOCK_COMPONENT, CLOCK_SUBCOMPONENT,
951 ! CLOCK_MODULE_DRIVER, CLOCK_MODULE, CLOCK_ROUTINE, CLOCK_LOOP, CLOCK_INFRA
952 !</PRE>
953 !
954 ! 4) Here is an example of how to time a section of code.<BR/>
955 !<PRE>
956 ! use fms_mod, only: mpp_clock_id, mpp_clock_begin, &
957 ! mpp_clock_end. MPP_CLOCK_SYNC, &
958 ! CLOCK_MODULE_DRIVER
959 ! integer :: id_mycode
960 !
961 ! id_mycode = mpp_clock_id ('mycode loop', flags=MPP_CLOCK_SYNC, grain=CLOCK_MODULE_DRIVER)
962 ! call mpp_clock_begin (id_mycode)
963 ! :
964 ! :
965 ! ~~ this code will be timed ~~
966 ! :
967 ! :
968 ! call mpp_clock_end (id_mycode)
969 ! </PRE>
970 ! Note: <TT>CLOCK_MODULE_DRIVER</TT> can be replaced with
971 ! CLOCK_COMPONENT, CLOCK_SUBCOMPONENT, CLOCK_MODULE_DRIVER, CLOCK_MODULE, CLOCK_ROUTINE,
972 ! CLOCK_LOOP, or CLOCK_INFRA.
973 !
974 ! </NOTE>
975 ! <FUTURE>
976 ! NetCDF facilities for reading and writing restart files and (IEEE32)
977 ! data files.
978 ! </FUTURE>
979 ! <FUTURE>
980 ! May possible split the FMS module into two modules.
981 !
982 ! i.general utilities (FMS_MOD) <BR/>
983 ! ii.I/O utilities (FMS_IO_MOD)
984 ! </FUTURE>
985 ! </INFO>
986 
subroutine, public print_memuse_stats(text, unit, always)
Definition: memutils.F90:282
Definition: fms.F90:20
character(len=16) clock_flags
Definition: fms.F90:234
subroutine, public set_domain(Domain2)
Definition: fms_io.F90:7401
integer function, public open_direct_file(file, action, recl)
Definition: fms_io.F90:7280
logical module_is_initialized
Definition: fms.F90:313
logical function, public file_exist(file_name, domain, no_domain)
Definition: fms_io.F90:8246
logical function, public fms_error_handler(routine, message, err_msg)
Definition: fms.F90:573
subroutine, public write_version_number(version, tag, unit)
Definition: fms_io.F90:8604
character(len=8) warning_level
Definition: fms.F90:235
Definition: mpp.F90:39
integer function, public check_nml_error(IOSTAT, NML_NAME)
Definition: fms.F90:658
logical, private do_nml_error_init
Definition: fms.F90:304
subroutine, private nml_error_init
Definition: fms.F90:689
subroutine, public memutils_init(print_flag)
Definition: memutils.F90:55
character(len=input_str_length), dimension(:), allocatable, target, public input_nml_file
Definition: mpp.F90:1378
integer, public clock_flag_default
Definition: fms.F90:217
integer function, public open_ieee32_file(file, action)
Definition: fms_io.F90:7321
logical function, public monotonic_array(array, direction)
Definition: fms.F90:888
subroutine, public fms_init(localcomm)
Definition: fms.F90:353
logical read_all_pe
Definition: fms.F90:233
character(len=16) clock_grain
Definition: fms.F90:234
subroutine, public get_domain_decomp(x, y)
Definition: fms_io.F90:7471
subroutine, public fms_io_init()
Definition: fms_io.F90:638
subroutine, public field_size(filename, fieldname, siz, field_found, domain, no_domain)
Definition: fms_io.F90:4941
type(nml_errors_type), save nml_errors
Definition: fms.F90:227
integer, dimension(20), private nml_error_codes
Definition: fms.F90:303
subroutine, public nullify_domain()
Definition: fms_io.F90:7421
integer function, public open_namelist_file(file)
Definition: fms_io.F90:7204
integer domains_stack_size
Definition: fms.F90:238
integer stack_size
Definition: fms.F90:237
subroutine, public fms_end()
Definition: fms.F90:476
subroutine, public fms_io_exit()
Definition: fms_io.F90:750
integer function, public open_restart_file(file, action)
Definition: fms_io.F90:7248
character(len=64) iospec_ieee32
Definition: fms.F90:236
integer, private num_nml_error_codes
Definition: fms.F90:303
integer function, public open_file(file, form, action, access, threading, recl, dist)
Definition: fms_io.F90:7552
subroutine, public get_mosaic_tile_grid(grid_file, mosaic_file, domain, tile_count)
Definition: fms_io.F90:7850
logical function, public string_array_index(string, string_array, index)
Definition: fms.F90:831
logical, public print_memory_usage
Definition: fms.F90:239
subroutine, public error_mesg(routine, message, level)
Definition: fms.F90:529
subroutine, public close_file(unit, status, dist)
Definition: fms_io.F90:7363
logical function, public field_exist(file_name, field_name, domain, no_domain)
Definition: fms_io.F90:8298