1 !***********************************************************************
2 !* GNU Lesser General Public License
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
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.
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
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 !> @defgroup fm_util_mod fm_util_mod
20 !> @ingroup field_manager
21 !> @brief This module provides utility routines for the field manager.
23 !> Routines for error catching, reporting and
24 !! termination while interfacing with the field manager.
25 !> @author Richard D. Slater
27 !> @addtogroup fm_util_mod
31 use field_manager_mod, only: fm_string_len, fm_field_name_len, fm_type_name_len
32 use field_manager_mod, only: fm_get_type, fm_get_index, fm_get_length
33 use field_manager_mod, only: fm_get_current_list, fm_new_list, fm_change_list, fm_loop_over_list
34 use field_manager_mod, only: fm_new_value, fm_get_value
35 use field_manager_mod, only: fm_exists, fm_dump_list
36 use fms_mod, only: FATAL, stdout
37 use mpp_mod, only: mpp_error
38 use platform_mod, only: r4_kind, r8_kind, FMS_PATH_LEN
44 public fm_util_start_namelist
45 public fm_util_end_namelist
46 public fm_util_check_for_bad_fields
47 public fm_util_set_caller
48 public fm_util_reset_caller
49 public fm_util_set_no_overwrite
50 public fm_util_reset_no_overwrite
51 public fm_util_set_good_name_list
52 public fm_util_reset_good_name_list
53 public fm_util_get_length
54 public fm_util_get_integer
55 public fm_util_get_logical
56 public fm_util_get_real
57 public fm_util_get_string
58 public fm_util_get_integer_array
59 public fm_util_get_logical_array
60 public fm_util_get_real_array
61 public fm_util_get_string_array
62 public fm_util_set_value
63 public fm_util_set_value_integer_array
64 public fm_util_set_value_real_array
65 public fm_util_set_value_logical_array
66 public fm_util_set_value_string_array
67 public fm_util_set_value_integer
68 public fm_util_set_value_logical
69 public fm_util_set_value_real
70 public fm_util_set_value_string
71 !public fm_util_get_index
72 public fm_util_get_index_list
73 public fm_util_get_index_string
79 character(len=128), public :: fm_util_default_caller = ' '
85 character(len=48), parameter :: mod_name = 'fm_util_mod'
91 character(len=128) :: save_default_caller = ' '
92 character(len=128) :: default_good_name_list = ' '
93 character(len=128) :: save_default_good_name_list = ' '
94 logical :: default_no_overwrite = .false.
95 logical :: save_default_no_overwrite = .false.
96 character(len=FMS_PATH_LEN) :: save_current_list
97 character(len=FMS_PATH_LEN) :: save_path
98 character(len=FMS_PATH_LEN) :: save_name
99 ! Include variable "version" to be written to log file.
100 #include<file_version.h>
103 ! Interface definitions for overloaded routines
106 !interface fm_util_get_value !{
107 !module procedure fm_util_get_value_integer
108 !module procedure fm_util_get_value_logical
109 !module procedure fm_util_get_value_real
110 !module procedure fm_util_get_value_string
111 !module procedure fm_util_get_value_integer_array
112 !module procedure fm_util_get_value_logical_array
113 !module procedure fm_util_get_value_real_array
114 !module procedure fm_util_get_value_string_array
119 interface fm_util_set_value_real
120 module procedure fm_util_set_value_real_r4
121 module procedure fm_util_set_value_real_r8
122 end interface fm_util_set_value_real
124 interface fm_util_set_value_real_array
125 module procedure fm_util_set_value_real_array_r4
126 module procedure fm_util_set_value_real_array_r8
127 end interface fm_util_set_value_real_array
129 !> @ingroup fm_util_mod
130 interface fm_util_set_value !{
131 module procedure fm_util_set_value_integer_array
132 module procedure fm_util_set_value_real_array_r4
133 module procedure fm_util_set_value_real_array_r8
134 module procedure fm_util_set_value_logical_array
135 module procedure fm_util_set_value_string_array
136 module procedure fm_util_set_value_real_r4
137 module procedure fm_util_set_value_real_r8
138 module procedure fm_util_set_value_integer
139 module procedure fm_util_set_value_logical
140 module procedure fm_util_set_value_string
143 !interface fm_util_get_index !{
144 !module procedure fm_util_get_index_list
145 !module procedure fm_util_get_index_string
148 !> @addtogroup fm_util_mod
153 !> Set the default value for the optional "caller" variable used in many of these
154 !! subroutines. If the argument is blank, then set the default to blank, otherwise
155 !! the deault will have brackets placed around the argument.
156 subroutine fm_util_set_caller(caller) !{
164 character(len=*), intent(in) :: caller
171 ! save the default caller string
174 save_default_caller = fm_util_default_caller
177 ! set the default caller string
180 if (caller .eq. ' ') then !{
181 fm_util_default_caller = ' '
183 fm_util_default_caller = '[' // trim(caller) // ']'
188 end subroutine fm_util_set_caller !}
190 !#######################################################################
192 !> Reset the default value for the optional "caller" variable used in many of these
193 !! subroutines to blank.
194 subroutine fm_util_reset_caller !{
207 ! reset the default caller string
210 fm_util_default_caller = save_default_caller
211 save_default_caller = ' '
215 end subroutine fm_util_reset_caller !}
217 !#######################################################################
219 !> Set the default value for the optional "good_name_list" variable used in many of these
221 subroutine fm_util_set_good_name_list(good_name_list) !{
229 character(len=*), intent(in) :: good_name_list
236 ! save the default good_name_list string
239 save_default_good_name_list = default_good_name_list
242 ! set the default good_name_list string
245 default_good_name_list = good_name_list
249 end subroutine fm_util_set_good_name_list !}
251 !#######################################################################
253 !> Reset the default value for the optional "good_name_list" variable used in many of these
254 !! subroutines to the saved value.
255 subroutine fm_util_reset_good_name_list !{
268 ! reset the default good_name_list string
271 default_good_name_list = save_default_good_name_list
272 save_default_good_name_list = ' '
276 end subroutine fm_util_reset_good_name_list !}
278 !#######################################################################
280 !> Set the default value for the optional "no_overwrite" variable used in some of these
282 subroutine fm_util_set_no_overwrite(no_overwrite) !{
290 logical, intent(in) :: no_overwrite
297 ! save the default no_overwrite string
300 save_default_no_overwrite = default_no_overwrite
303 ! set the default no_overwrite value
306 default_no_overwrite = no_overwrite
310 end subroutine fm_util_set_no_overwrite !}
312 !#######################################################################
314 !> Reset the default value for the optional "no_overwrite" variable used in some of these
315 !! subroutines to false.
316 subroutine fm_util_reset_no_overwrite !{
329 ! reset the default no_overwrite value
332 default_no_overwrite = save_default_no_overwrite
333 save_default_no_overwrite = .false.
337 end subroutine fm_util_reset_no_overwrite !}
339 !#######################################################################
341 !> Check for unrecognized fields in a list
342 subroutine fm_util_check_for_bad_fields(list, good_fields, caller) !{
350 character(len=*), intent(in) :: list
351 character(len=*), intent(in), dimension(:) :: good_fields
352 character(len=*), intent(in), optional :: caller
358 character(len=48), parameter :: sub_name = 'fm_util_check_for_bad_fields'
364 logical :: fm_success
367 integer :: list_length
368 integer :: good_length
369 character(len=fm_type_name_len) :: typ
370 character(len=fm_field_name_len) :: name
372 character(len=256) :: error_header
373 character(len=256) :: warn_header
374 character(len=256) :: note_header
375 character(len=128) :: caller_str
381 ! set the caller string and headers
384 if (present(caller)) then !{
385 caller_str = '[' // trim(caller) // ']'
387 caller_str = fm_util_default_caller
390 error_header = '==>Error from ' // trim(mod_name) // &
391 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
392 warn_header = '==>Warning from ' // trim(mod_name) // &
393 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
394 note_header = '==>Note from ' // trim(mod_name) // &
395 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
398 ! check that a list is given (fatal if not)
401 if (list .eq. ' ') then !{
402 write (out_unit,*) trim(error_header) // ' Empty list given'
403 call mpp_error(FATAL, trim(error_header) // ' Empty list given')
407 ! Check that we have been given a list
410 if (fm_get_type(list) .ne. 'list') then !{
411 write (out_unit,*) trim(error_header) // ' Not given a list: ' // trim(list)
412 call mpp_error(FATAL, trim(error_header) // ' Not given a list: ' // trim(list))
416 ! Get the list length
419 list_length = fm_get_length(list)
420 if (list_length .lt. 0) then !{
421 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(list))
425 ! Get the number of good fields
428 good_length = size(good_fields)
430 if (list_length .lt. good_length) then !{
433 ! If the list length is less than the number of good fields this is an error
434 ! as the list should be fully populated and we'll check which extra fields
435 ! are given in good_fields
438 write (out_unit,*) trim(error_header), ' List length < number of good fields (', &
439 list_length, ' < ', good_length, ') in list ', trim(list)
442 write (out_unit,*) 'The list contains the following fields:'
443 fm_success= fm_dump_list(list, .false.)
445 write (out_unit,*) 'The supposed list of good fields is:'
446 do i = 1, good_length !{
447 if (fm_exists(trim(list) // '/' // good_fields(i))) then !{
448 write (out_unit,*) 'List field: "', trim(good_fields(i)), '"'
450 write (out_unit,*) 'EXTRA good field: "', trim(good_fields(i)), '"'
455 call mpp_error(FATAL, trim(error_header) // &
456 ' List length < number of good fields for list: ' // trim(list))
458 elseif (list_length .gt. good_length) then !}{
461 ! If the list length is greater than the number of good fields this is an error
462 ! as the there should not be any more fields than those given in the good fields list
463 ! and we'll check which extra fields are given in the list
466 write (out_unit,*) trim(warn_header), 'List length > number of good fields (', &
467 list_length, ' > ', good_length, ') in list ', trim(list)
469 write (out_unit,*) trim(error_header), ' Start of list of fields'
470 do while (fm_loop_over_list(list, name, typ, ind)) !{
472 do i = 1, good_length !{
473 found = found .or. (name .eq. good_fields(i))
476 write (out_unit,*) 'Good list field: "', trim(name), '"'
478 write (out_unit,*) 'EXTRA list field: "', trim(name), '"'
481 write (out_unit,*) trim(error_header), ' End of list of fields'
483 call mpp_error(FATAL, trim(error_header) // &
484 ' List length > number of good fields for list: ' // trim(list))
489 ! If the list length equals the number of good fields then all is good
494 end subroutine fm_util_check_for_bad_fields !}
496 !#######################################################################
498 !> Get the length of an element of the Field Manager tree
499 function fm_util_get_length(name, caller) &
500 result (field_length) !{
508 integer :: field_length
514 character(len=*), intent(in) :: name
515 character(len=*), intent(in), optional :: caller
521 character(len=48), parameter :: sub_name = 'fm_util_get_length'
527 character(len=256) :: error_header
528 character(len=256) :: warn_header
529 character(len=256) :: note_header
530 character(len=128) :: caller_str
533 ! set the caller string and headers
536 if (present(caller)) then !{
537 caller_str = '[' // trim(caller) // ']'
539 caller_str = fm_util_default_caller
542 error_header = '==>Error from ' // trim(mod_name) // &
543 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
544 warn_header = '==>Warning from ' // trim(mod_name) // &
545 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
546 note_header = '==>Note from ' // trim(mod_name) // &
547 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
550 ! check that a name is given (fatal if not)
553 if (name .eq. ' ') then !{
554 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
558 ! Get the field's length
561 field_length = fm_get_length(name)
562 if (field_length .lt. 0) then !{
563 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
568 end function fm_util_get_length !}
570 !#######################################################################
572 !> Get the index of an element of a string in the Field Manager tree
573 function fm_util_get_index_string(name, string, caller) &
588 character(len=*), intent(in) :: name
589 character(len=*), intent(in) :: string
590 character(len=*), intent(in), optional :: caller
596 character(len=48), parameter :: sub_name = 'fm_util_get_index_string'
602 character(len=256) :: error_header
603 character(len=256) :: warn_header
604 character(len=256) :: note_header
605 character(len=128) :: caller_str
606 character(len=32) :: index_str
607 character(len=fm_type_name_len) :: fm_type
608 character(len=fm_string_len) :: fm_string
613 ! set the caller string and headers
616 if (present(caller)) then !{
617 caller_str = '[' // trim(caller) // ']'
619 caller_str = fm_util_default_caller
622 error_header = '==>Error from ' // trim(mod_name) // &
623 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
624 warn_header = '==>Warning from ' // trim(mod_name) // &
625 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
626 note_header = '==>Note from ' // trim(mod_name) // &
627 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
630 ! check that a name is given (fatal if not)
633 if (name .eq. ' ') then !{
634 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
638 ! Check the field's type and get the index
642 fm_type = fm_get_type(name)
643 if (fm_type .eq. 'string') then !{
644 length = fm_get_length(name)
645 if (length .lt. 0) then !{
646 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
648 if (length .gt. 0) then !{
650 if (.not. fm_get_value(name, fm_string, index = i)) then !{
651 write (index_str,*) '(', i, ')'
652 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name) // trim(index_str))
654 if (fm_string .eq. string) then !{
660 elseif (fm_type .eq. ' ') then !}{
661 call mpp_error(FATAL, trim(error_header) // ' Array does not exist: ' // trim(name))
663 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
666 !if (fm_index .eq. 0) then !{
667 !call mpp_error(FATAL, trim(error_header) // ' "' // trim(string) // '" does not exist in ' // trim(name))
672 end function fm_util_get_index_string !}
674 !#######################################################################
676 !> Get the length of an element of the Field Manager tree
677 function fm_util_get_index_list(name, caller) &
692 character(len=*), intent(in) :: name
693 character(len=*), intent(in), optional :: caller
699 character(len=48), parameter :: sub_name = 'fm_util_get_index_list'
705 character(len=256) :: error_header
706 character(len=256) :: warn_header
707 character(len=256) :: note_header
708 character(len=128) :: caller_str
709 character(len=fm_type_name_len) :: fm_type
712 ! set the caller string and headers
715 if (present(caller)) then !{
716 caller_str = '[' // trim(caller) // ']'
718 caller_str = fm_util_default_caller
721 error_header = '==>Error from ' // trim(mod_name) // &
722 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
723 warn_header = '==>Warning from ' // trim(mod_name) // &
724 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
725 note_header = '==>Note from ' // trim(mod_name) // &
726 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
729 ! check that a name is given (fatal if not)
732 if (name .eq. ' ') then !{
733 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
737 ! Check the field's type and get the index
741 fm_type = fm_get_type(name)
742 if (fm_type .eq. 'list') then !{
743 fm_index = fm_get_index(name)
744 if (fm_index .le. 0) then !{
745 call mpp_error(FATAL, trim(error_header) // ' List does not exist: ' // trim(name))
747 elseif (fm_type .eq. ' ') then !}{
748 call mpp_error(FATAL, trim(error_header) // ' List does not exist: ' // trim(name))
750 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
756 end function fm_util_get_index_list !}
759 !#######################################################################
761 !> Get an integer value from the Field Manager tree.
762 function fm_util_get_integer_array(name, caller) &
771 integer, pointer, dimension(:) :: array
777 character(len=*), intent(in) :: name
778 character(len=*), intent(in), optional :: caller
784 character(len=48), parameter :: sub_name = 'fm_util_get_integer_array'
790 character(len=256) :: error_header
791 character(len=256) :: warn_header
792 character(len=256) :: note_header
793 character(len=128) :: caller_str
794 character(len=32) :: index_str
795 character(len=fm_type_name_len) :: fm_type
802 ! set the caller string and headers
805 if (present(caller)) then !{
806 caller_str = '[' // trim(caller) // ']'
808 caller_str = fm_util_default_caller
811 error_header = '==>Error from ' // trim(mod_name) // &
812 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
813 warn_header = '==>Warning from ' // trim(mod_name) // &
814 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
815 note_header = '==>Note from ' // trim(mod_name) // &
816 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
819 ! check that a name is given (fatal if not)
822 if (name .eq. ' ') then !{
823 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
826 fm_type = fm_get_type(name)
827 if (fm_type .eq. 'integer') then !{
828 length = fm_get_length(name)
829 if (length .lt. 0) then !{
830 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
832 if (length .gt. 0) then !{
833 allocate(array(length))
835 if (.not. fm_get_value(name, array(i), index = i)) then !{
836 write (index_str,*) '(', i, ')'
837 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name) // trim(index_str))
841 elseif (fm_type .eq. ' ') then !}{
842 call mpp_error(FATAL, trim(error_header) // ' Array does not exist: ' // trim(name))
844 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
849 end function fm_util_get_integer_array !}
851 !#######################################################################
853 !> Get a logical value from the Field Manager tree.
854 function fm_util_get_logical_array(name, caller) &
863 logical, pointer, dimension(:) :: array
869 character(len=*), intent(in) :: name
870 character(len=*), intent(in), optional :: caller
876 character(len=48), parameter :: sub_name = 'fm_util_get_logical_array'
882 character(len=256) :: error_header
883 character(len=256) :: warn_header
884 character(len=256) :: note_header
885 character(len=128) :: caller_str
886 character(len=32) :: index_str
887 character(len=fm_type_name_len) :: fm_type
894 ! set the caller string and headers
897 if (present(caller)) then !{
898 caller_str = '[' // trim(caller) // ']'
900 caller_str = fm_util_default_caller
903 error_header = '==>Error from ' // trim(mod_name) // &
904 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
905 warn_header = '==>Warning from ' // trim(mod_name) // &
906 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
907 note_header = '==>Note from ' // trim(mod_name) // &
908 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
911 ! check that a name is given (fatal if not)
914 if (name .eq. ' ') then !{
915 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
918 fm_type = fm_get_type(name)
919 if (fm_type .eq. 'logical') then !{
920 length = fm_get_length(name)
921 if (length .lt. 0) then !{
922 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
924 if (length .gt. 0) then !{
925 allocate(array(length))
927 if (.not. fm_get_value(name, array(i), index = i)) then !{
928 write (index_str,*) '(', i, ')'
929 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name) // trim(index_str))
933 elseif (fm_type .eq. ' ') then !}{
934 call mpp_error(FATAL, trim(error_header) // ' Array does not exist: ' // trim(name))
936 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
941 end function fm_util_get_logical_array !}
943 !#######################################################################
945 !> Get a real value from the Field Manager tree.
946 function fm_util_get_real_array(name, caller) &
955 real(r8_kind), pointer, dimension(:) :: array
961 character(len=*), intent(in) :: name
962 character(len=*), intent(in), optional :: caller
968 character(len=48), parameter :: sub_name = 'fm_util_get_real_array'
974 character(len=256) :: error_header
975 character(len=256) :: warn_header
976 character(len=256) :: note_header
977 character(len=128) :: caller_str
978 character(len=32) :: index_str
979 character(len=fm_type_name_len) :: fm_type
986 ! set the caller string and headers
989 if (present(caller)) then !{
990 caller_str = '[' // trim(caller) // ']'
992 caller_str = fm_util_default_caller
995 error_header = '==>Error from ' // trim(mod_name) // &
996 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
997 warn_header = '==>Warning from ' // trim(mod_name) // &
998 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
999 note_header = '==>Note from ' // trim(mod_name) // &
1000 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1003 ! check that a name is given (fatal if not)
1006 if (name .eq. ' ') then !{
1007 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1010 fm_type = fm_get_type(name)
1011 if (fm_type .eq. 'real') then !{
1012 length = fm_get_length(name)
1013 if (length .lt. 0) then !{
1014 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
1016 if (length .gt. 0) then !{
1017 allocate(array(length))
1019 if (.not. fm_get_value(name, array(i), index = i)) then !{
1020 write (index_str,*) '(', i, ')'
1021 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name) // trim(index_str))
1025 elseif (fm_type .eq. ' ') then !}{
1026 call mpp_error(FATAL, trim(error_header) // ' Array does not exist: ' // trim(name))
1028 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
1033 end function fm_util_get_real_array !}
1035 !#######################################################################
1038 !> Get a string value from the Field Manager tree.
1039 function fm_util_get_string_array(name, caller) &
1048 character(len=fm_string_len), pointer, dimension(:) :: array
1054 character(len=*), intent(in) :: name
1055 character(len=*), intent(in), optional :: caller
1061 character(len=48), parameter :: sub_name = 'fm_util_get_string_array'
1067 character(len=256) :: error_header
1068 character(len=256) :: warn_header
1069 character(len=256) :: note_header
1070 character(len=128) :: caller_str
1071 character(len=32) :: index_str
1072 character(len=fm_type_name_len) :: fm_type
1079 ! set the caller string and headers
1082 if (present(caller)) then !{
1083 caller_str = '[' // trim(caller) // ']'
1085 caller_str = fm_util_default_caller
1088 error_header = '==>Error from ' // trim(mod_name) // &
1089 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1090 warn_header = '==>Warning from ' // trim(mod_name) // &
1091 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1092 note_header = '==>Note from ' // trim(mod_name) // &
1093 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1096 ! check that a name is given (fatal if not)
1099 if (name .eq. ' ') then !{
1100 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1103 fm_type = fm_get_type(name)
1104 if (fm_type .eq. 'string') then !{
1105 length = fm_get_length(name)
1106 if (length .lt. 0) then !{
1107 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
1109 if (length .gt. 0) then !{
1110 allocate(array(length))
1112 if (.not. fm_get_value(name, array(i), index = i)) then !{
1113 write (index_str,*) '(', i, ')'
1114 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name) // trim(index_str))
1118 elseif (fm_type .eq. ' ') then !}{
1119 call mpp_error(FATAL, trim(error_header) // ' Array does not exist: ' // trim(name))
1121 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
1126 end function fm_util_get_string_array !}
1128 !#######################################################################
1130 !> Get an integer value from the Field Manager tree.
1131 function fm_util_get_integer(name, caller, index, default_value, scalar) &
1146 character(len=*), intent(in) :: name
1147 character(len=*), intent(in), optional :: caller
1148 integer, intent(in), optional :: index
1149 integer, intent(in), optional :: default_value
1150 logical, intent(in), optional :: scalar
1156 character(len=48), parameter :: sub_name = 'fm_util_get_integer'
1162 character(len=256) :: error_header
1163 character(len=256) :: warn_header
1164 character(len=256) :: note_header
1165 character(len=128) :: caller_str
1167 character(len=fm_type_name_len) :: fm_type
1168 integer :: field_length
1171 ! set the caller string and headers
1174 if (present(caller)) then !{
1175 caller_str = '[' // trim(caller) // ']'
1177 caller_str = fm_util_default_caller
1180 error_header = '==>Error from ' // trim(mod_name) // &
1181 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1182 warn_header = '==>Warning from ' // trim(mod_name) // &
1183 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1184 note_header = '==>Note from ' // trim(mod_name) // &
1185 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1188 ! check that a name is given (fatal if not)
1191 if (name .eq. ' ') then !{
1192 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1196 ! Check whether we require a scalar (length=1) and return
1197 ! an error if we do, and it isn't
1200 if (present(scalar)) then !{
1202 field_length = fm_get_length(name)
1203 if (field_length .lt. 0) then !{
1204 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
1205 elseif (field_length .gt. 1) then !}{
1206 call mpp_error(FATAL, trim(error_header) // trim(name) // ' not scalar')
1215 if (present(index)) then !{
1217 if (index .le. 0) then !{
1218 call mpp_error(FATAL, trim(error_header) // ' Index not positive')
1224 fm_type = fm_get_type(name)
1225 if (fm_type .eq. 'integer') then !{
1226 if (.not. fm_get_value(name, ival, index = index_t)) then !{
1227 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name))
1229 elseif (fm_type .eq. ' ' .and. present(default_value)) then !}{
1230 ival = default_value
1231 elseif (fm_type .eq. ' ') then !}{
1232 call mpp_error(FATAL, trim(error_header) // ' Field does not exist: ' // trim(name))
1234 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
1239 end function fm_util_get_integer !}
1241 !#######################################################################
1243 !> Get a logical value from the Field Manager tree.
1244 function fm_util_get_logical(name, caller, index, default_value, scalar) &
1259 character(len=*), intent(in) :: name
1260 character(len=*), intent(in), optional :: caller
1261 integer, intent(in), optional :: index
1262 logical, intent(in), optional :: default_value
1263 logical, intent(in), optional :: scalar
1269 character(len=48), parameter :: sub_name = 'fm_util_get_logical'
1275 character(len=256) :: error_header
1276 character(len=256) :: warn_header
1277 character(len=256) :: note_header
1278 character(len=128) :: caller_str
1280 character(len=fm_type_name_len) :: fm_type
1281 integer :: field_length
1284 ! set the caller string and headers
1287 if (present(caller)) then !{
1288 caller_str = '[' // trim(caller) // ']'
1290 caller_str = fm_util_default_caller
1293 error_header = '==>Error from ' // trim(mod_name) // &
1294 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1295 warn_header = '==>Warning from ' // trim(mod_name) // &
1296 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1297 note_header = '==>Note from ' // trim(mod_name) // &
1298 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1301 ! check that a name is given (fatal if not)
1304 if (name .eq. ' ') then !{
1305 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1309 ! Check whether we require a scalar (length=1) and return
1310 ! an error if we do, and it isn't
1313 if (present(scalar)) then !{
1315 field_length = fm_get_length(name)
1316 if (field_length .lt. 0) then !{
1317 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
1318 elseif (field_length .gt. 1) then !}{
1319 call mpp_error(FATAL, trim(error_header) // trim(name) // ' not scalar')
1328 if (present(index)) then !{
1330 if (index .le. 0) then !{
1331 call mpp_error(FATAL, trim(error_header) // ' Index not positive')
1337 fm_type = fm_get_type(name)
1338 if (fm_type .eq. 'logical') then !{
1339 if (.not. fm_get_value(name, lval, index = index_t)) then !{
1340 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name))
1342 elseif (fm_type .eq. ' ' .and. present(default_value)) then !}{
1343 lval = default_value
1344 elseif (fm_type .eq. ' ') then !}{
1345 call mpp_error(FATAL, trim(error_header) // ' Field does not exist: ' // trim(name))
1347 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
1352 end function fm_util_get_logical !}
1354 !#######################################################################
1357 !> Get a real value from the Field Manager tree.
1358 function fm_util_get_real(name, caller, index, default_value, scalar) &
1367 real(r8_kind) :: rval
1373 character(len=*), intent(in) :: name
1374 character(len=*), intent(in), optional :: caller
1375 integer, intent(in), optional :: index
1376 real(r8_kind), intent(in), optional :: default_value
1377 logical, intent(in), optional :: scalar
1383 character(len=48), parameter :: sub_name = 'fm_util_get_real'
1389 character(len=256) :: error_header
1390 character(len=256) :: warn_header
1391 character(len=256) :: note_header
1392 character(len=128) :: caller_str
1394 character(len=fm_type_name_len) :: fm_type
1395 integer :: field_length
1399 ! set the caller string and headers
1402 if (present(caller)) then !{
1403 caller_str = '[' // trim(caller) // ']'
1405 caller_str = fm_util_default_caller
1408 error_header = '==>Error from ' // trim(mod_name) // &
1409 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1410 warn_header = '==>Warning from ' // trim(mod_name) // &
1411 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1412 note_header = '==>Note from ' // trim(mod_name) // &
1413 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1416 ! check that a name is given (fatal if not)
1419 if (name .eq. ' ') then !{
1420 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1424 ! Check whether we require a scalar (length=1) and return
1425 ! an error if we do, and it isn't
1428 if (present(scalar)) then !{
1430 field_length = fm_get_length(name)
1431 if (field_length .lt. 0) then !{
1432 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
1433 elseif (field_length .gt. 1) then !}{
1434 call mpp_error(FATAL, trim(error_header) // trim(name) // ' not scalar')
1443 if (present(index)) then !{
1445 if (index .le. 0) then !{
1446 call mpp_error(FATAL, trim(error_header) // ' Index not positive')
1452 fm_type = fm_get_type(name)
1453 if (fm_type .eq. 'real') then !{
1454 if (.not. fm_get_value(name, rval, index = index_t)) then !{
1455 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name))
1457 else if (fm_type .eq. 'integer') then
1458 if (.not. fm_get_value(name, ivalue, index = index_t)) then
1459 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name))
1461 rval = real(ivalue,r8_kind)
1462 elseif (fm_type .eq. ' ' .and. present(default_value)) then !}{
1463 rval = default_value
1464 elseif (fm_type .eq. ' ') then !}{
1465 call mpp_error(FATAL, trim(error_header) // ' Field does not exist: ' // trim(name))
1467 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
1472 end function fm_util_get_real !}
1475 !#######################################################################
1478 !> Get a string value from the Field Manager tree.
1479 function fm_util_get_string(name, caller, index, default_value, scalar) &
1488 character(len=fm_string_len) :: sval
1494 character(len=*), intent(in) :: name
1495 character(len=*), intent(in), optional :: caller
1496 integer, intent(in), optional :: index
1497 character(len=*), intent(in), optional :: default_value
1498 logical, intent(in), optional :: scalar
1504 character(len=48), parameter :: sub_name = 'fm_util_get_string'
1510 character(len=256) :: error_header
1511 character(len=256) :: warn_header
1512 character(len=256) :: note_header
1513 character(len=128) :: caller_str
1515 character(len=fm_type_name_len) :: fm_type
1516 integer :: field_length
1519 ! set the caller string and headers
1522 if (present(caller)) then !{
1523 caller_str = '[' // trim(caller) // ']'
1525 caller_str = fm_util_default_caller
1528 error_header = '==>Error from ' // trim(mod_name) // &
1529 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1530 warn_header = '==>Warning from ' // trim(mod_name) // &
1531 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1532 note_header = '==>Note from ' // trim(mod_name) // &
1533 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1536 ! check that a name is given (fatal if not)
1539 if (name .eq. ' ') then !{
1540 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1544 ! Check whether we require a scalar (length=1) and return
1545 ! an error if we do, and it isn't
1548 if (present(scalar)) then !{
1550 field_length = fm_get_length(name)
1551 if (field_length .lt. 0) then !{
1552 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
1553 elseif (field_length .gt. 1) then !}{
1554 call mpp_error(FATAL, trim(error_header) // trim(name) // ' not scalar')
1563 if (present(index)) then !{
1565 if (index .le. 0) then !{
1566 call mpp_error(FATAL, trim(error_header) // ' Index not positive')
1572 fm_type = fm_get_type(name)
1573 if (fm_type .eq. 'string') then !{
1574 if (.not. fm_get_value(name, sval, index = index_t)) then !{
1575 call mpp_error(FATAL, trim(error_header) // ' Problem getting ' // trim(name))
1577 elseif (fm_type .eq. ' ' .and. present(default_value)) then !}{
1578 sval = default_value
1579 elseif (fm_type .eq. ' ') then !}{
1580 call mpp_error(FATAL, trim(error_header) // ' Field does not exist: ' // trim(name))
1582 call mpp_error(FATAL, trim(error_header) // ' Wrong type for ' // trim(name) // ', found (' // trim(fm_type) // ')')
1587 end function fm_util_get_string !}
1589 !#######################################################################
1591 !> Set an integer array in the Field Manager tree.
1592 subroutine fm_util_set_value_integer_array(name, ival, length, caller, no_overwrite, good_name_list) !{
1600 character(len=*), intent(in) :: name
1601 integer, intent(in) :: length
1602 integer, intent(in) :: ival(length)
1603 character(len=*), intent(in), optional :: caller
1604 logical, intent(in), optional :: no_overwrite
1605 character(len=*), intent(in), optional :: good_name_list
1611 character(len=48), parameter :: sub_name = 'fm_util_set_value_integer_array'
1617 character(len=256) :: error_header
1618 character(len=256) :: warn_header
1619 character(len=256) :: note_header
1620 character(len=128) :: caller_str
1621 character(len=32) :: str_error
1622 integer :: field_index
1623 integer :: field_length
1625 logical :: no_overwrite_use
1626 character(len=FMS_PATH_LEN) :: good_name_list_use
1630 ! set the caller string and headers
1633 if (present(caller)) then !{
1634 caller_str = '[' // trim(caller) // ']'
1636 caller_str = fm_util_default_caller
1639 error_header = '==>Error from ' // trim(mod_name) // &
1640 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1641 warn_header = '==>Warning from ' // trim(mod_name) // &
1642 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1643 note_header = '==>Note from ' // trim(mod_name) // &
1644 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1647 ! check that a name is given (fatal if not)
1650 if (name .eq. ' ') then !{
1651 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1655 ! check that the length is non-negative
1658 if (length .lt. 0) then !{
1659 call mpp_error(FATAL, trim(error_header) // ' Negative array length')
1663 ! check for whether to overwrite existing values
1666 if (present(no_overwrite)) then !{
1667 no_overwrite_use = no_overwrite
1669 no_overwrite_use = default_no_overwrite
1673 ! check for whether to save the name in a list
1676 if (present(good_name_list)) then !{
1677 good_name_list_use = good_name_list
1679 good_name_list_use = default_good_name_list
1683 ! write the data array
1686 if (length .eq. 0) then !{
1687 if (.not. (no_overwrite_use .and. fm_exists(name))) then !{
1688 field_index = fm_new_value(name, 0, index = 0)
1689 if (field_index .le. 0) then !{
1690 write (str_error,*) ' with length = ', length
1691 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
1695 if (no_overwrite_use .and. fm_exists(name)) then !{
1696 field_length = fm_get_length(name)
1697 if (field_length .lt. 0) then !{
1698 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
1700 do n = field_length + 1, length !{
1701 field_index = fm_new_value(name, ival(n), index = n)
1702 if (field_index .le. 0) then !{
1703 write (str_error,*) ' with index = ', n
1704 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
1708 field_index = fm_new_value(name, ival(1))
1709 if (field_index .le. 0) then !{
1710 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name))
1713 field_index = fm_new_value(name, ival(n), index = n)
1714 if (field_index .le. 0) then !{
1715 write (str_error,*) ' with index = ', n
1716 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
1723 ! Add the variable name to the list of good names, to be used
1724 ! later for a consistency check
1727 if (good_name_list_use .ne. ' ') then !{
1728 if (fm_exists(good_name_list_use)) then !{
1729 add_name = fm_util_get_index_string(good_name_list_use, name, &
1730 caller = caller_str) .le. 0 ! true if name does not exist in string array
1732 add_name = .true. ! always add to new list
1734 if (add_name .and. fm_exists(name)) then !{
1735 if (fm_new_value(good_name_list_use, name, append = .true., create = .true.) .le. 0) then !{
1736 call mpp_error(FATAL, trim(error_header) // &
1737 ' Could not add ' // trim(name) // ' to "' // trim(good_name_list_use) // '" list')
1744 end subroutine fm_util_set_value_integer_array !}
1746 !#######################################################################
1748 !> Set a logical array in the Field Manager tree.
1749 subroutine fm_util_set_value_logical_array(name, lval, length, caller, no_overwrite, good_name_list) !{
1757 character(len=*), intent(in) :: name
1758 integer, intent(in) :: length
1759 logical, intent(in) :: lval(length)
1760 character(len=*), intent(in), optional :: caller
1761 logical, intent(in), optional :: no_overwrite
1762 character(len=*), intent(in), optional :: good_name_list
1768 character(len=48), parameter :: sub_name = 'fm_util_set_value_logical_array'
1774 character(len=256) :: error_header
1775 character(len=256) :: warn_header
1776 character(len=256) :: note_header
1777 character(len=128) :: caller_str
1778 character(len=32) :: str_error
1779 integer :: field_index
1780 integer :: field_length
1782 logical :: no_overwrite_use
1783 character(len=FMS_PATH_LEN) :: good_name_list_use
1787 ! set the caller string and headers
1790 if (present(caller)) then !{
1791 caller_str = '[' // trim(caller) // ']'
1793 caller_str = fm_util_default_caller
1796 error_header = '==>Error from ' // trim(mod_name) // &
1797 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1798 warn_header = '==>Warning from ' // trim(mod_name) // &
1799 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1800 note_header = '==>Note from ' // trim(mod_name) // &
1801 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1804 ! check that a name is given (fatal if not)
1807 if (name .eq. ' ') then !{
1808 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1812 ! check that the length is non-negative
1815 if (length .lt. 0) then !{
1816 call mpp_error(FATAL, trim(error_header) // ' Negative array length')
1820 ! check for whether to overwrite existing values
1823 if (present(no_overwrite)) then !{
1824 no_overwrite_use = no_overwrite
1826 no_overwrite_use = default_no_overwrite
1830 ! check for whether to save the name in a list
1833 if (present(good_name_list)) then !{
1834 good_name_list_use = good_name_list
1836 good_name_list_use = default_good_name_list
1840 ! write the data array
1843 if (length .eq. 0) then !{
1844 if (.not. (no_overwrite_use .and. fm_exists(name))) then !{
1845 field_index = fm_new_value(name, .false., index = 0)
1846 if (field_index .le. 0) then !{
1847 write (str_error,*) ' with length = ', length
1848 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
1852 if (no_overwrite_use .and. fm_exists(name)) then !{
1853 field_length = fm_get_length(name)
1854 if (field_length .lt. 0) then !{
1855 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
1857 do n = field_length + 1, length !{
1858 field_index = fm_new_value(name, lval(n), index = n)
1859 if (field_index .le. 0) then !{
1860 write (str_error,*) ' with index = ', n
1861 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
1865 field_index = fm_new_value(name, lval(1))
1866 if (field_index .le. 0) then !{
1867 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name))
1870 field_index = fm_new_value(name, lval(n), index = n)
1871 if (field_index .le. 0) then !{
1872 write (str_error,*) ' with index = ', n
1873 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
1880 ! Add the variable name to the list of good names, to be used
1881 ! later for a consistency check
1884 if (good_name_list_use .ne. ' ') then !{
1885 if (fm_exists(good_name_list_use)) then !{
1886 add_name = fm_util_get_index_string(good_name_list_use, name, &
1887 caller = caller_str) .le. 0 ! true if name does not exist in string array
1889 add_name = .true. ! always add to new list
1891 if (add_name .and. fm_exists(name)) then !{
1892 if (fm_new_value(good_name_list_use, name, append = .true., create = .true.) .le. 0) then !{
1893 call mpp_error(FATAL, trim(error_header) // &
1894 ' Could not add ' // trim(name) // ' to "' // trim(good_name_list_use) // '" list')
1901 end subroutine fm_util_set_value_logical_array !}
1903 !#######################################################################
1905 !> Set a string array in the Field Manager tree.
1906 subroutine fm_util_set_value_string_array(name, sval, length, caller, no_overwrite, good_name_list) !{
1914 character(len=*), intent(in) :: name
1915 integer, intent(in) :: length
1916 character(len=*), intent(in) :: sval(length)
1917 character(len=*), intent(in), optional :: caller
1918 logical, intent(in), optional :: no_overwrite
1919 character(len=*), intent(in), optional :: good_name_list
1925 character(len=48), parameter :: sub_name = 'fm_util_set_value_string_array'
1931 character(len=256) :: error_header
1932 character(len=256) :: warn_header
1933 character(len=256) :: note_header
1934 character(len=128) :: caller_str
1935 character(len=32) :: str_error
1936 integer :: field_index
1937 integer :: field_length
1939 logical :: no_overwrite_use
1940 character(len=FMS_PATH_LEN) :: good_name_list_use
1944 ! set the caller string and headers
1947 if (present(caller)) then !{
1948 caller_str = '[' // trim(caller) // ']'
1950 caller_str = fm_util_default_caller
1953 error_header = '==>Error from ' // trim(mod_name) // &
1954 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1955 warn_header = '==>Warning from ' // trim(mod_name) // &
1956 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1957 note_header = '==>Note from ' // trim(mod_name) // &
1958 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
1961 ! check that a name is given (fatal if not)
1964 if (name .eq. ' ') then !{
1965 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
1969 ! check that the length is non-negative
1972 if (length .lt. 0) then !{
1973 call mpp_error(FATAL, trim(error_header) // ' Negative array length')
1977 ! check for whether to overwrite existing values
1980 if (present(no_overwrite)) then !{
1981 no_overwrite_use = no_overwrite
1983 no_overwrite_use = default_no_overwrite
1987 ! check for whether to save the name in a list
1990 if (present(good_name_list)) then !{
1991 good_name_list_use = good_name_list
1993 good_name_list_use = default_good_name_list
1997 ! write the data array
2000 if (length .eq. 0) then !{
2001 if (.not. (no_overwrite_use .and. fm_exists(name))) then !{
2002 field_index = fm_new_value(name, ' ', index = 0)
2003 if (field_index .le. 0) then !{
2004 write (str_error,*) ' with length = ', length
2005 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2009 if (no_overwrite_use .and. fm_exists(name)) then !{
2010 field_length = fm_get_length(name)
2011 if (field_length .lt. 0) then !{
2012 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
2014 do n = field_length + 1, length !{
2015 field_index = fm_new_value(name, sval(n), index = n)
2016 if (field_index .le. 0) then !{
2017 write (str_error,*) ' with index = ', n
2018 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2022 field_index = fm_new_value(name, sval(1))
2023 if (field_index .le. 0) then !{
2024 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name))
2027 field_index = fm_new_value(name, sval(n), index = n)
2028 if (field_index .le. 0) then !{
2029 write (str_error,*) ' with index = ', n
2030 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2037 ! Add the variable name to the list of good names, to be used
2038 ! later for a consistency check
2041 if (good_name_list_use .ne. ' ') then !{
2042 if (fm_exists(good_name_list_use)) then !{
2043 add_name = fm_util_get_index_string(good_name_list_use, name, &
2044 caller = caller_str) .le. 0 ! true if name does not exist in string array
2046 add_name = .true. ! always add to new list
2048 if (add_name .and. fm_exists(name)) then !{
2049 if (fm_new_value(good_name_list_use, name, append = .true., create = .true.) .le. 0) then !{
2050 call mpp_error(FATAL, trim(error_header) // &
2051 ' Could not add ' // trim(name) // ' to "' // trim(good_name_list_use) // '" list')
2058 end subroutine fm_util_set_value_string_array !}
2060 !#######################################################################
2062 !> Set an integer value in the Field Manager tree.
2063 subroutine fm_util_set_value_integer(name, ival, caller, index, append, no_create, &
2064 no_overwrite, good_name_list) !{
2072 character(len=*), intent(in) :: name
2073 integer, intent(in) :: ival
2074 character(len=*), intent(in), optional :: caller
2075 integer, intent(in), optional :: index
2076 logical, intent(in), optional :: append
2077 logical, intent(in), optional :: no_create
2078 logical, intent(in), optional :: no_overwrite
2079 character(len=*), intent(in), optional :: good_name_list
2085 character(len=48), parameter :: sub_name = 'fm_util_set_value_integer'
2091 character(len=256) :: error_header
2092 character(len=256) :: warn_header
2093 character(len=256) :: note_header
2094 character(len=128) :: caller_str
2095 character(len=32) :: str_error
2096 integer :: field_index
2097 logical :: no_overwrite_use
2098 integer :: field_length
2099 character(len=FMS_PATH_LEN) :: good_name_list_use
2104 ! set the caller string and headers
2107 if (present(caller)) then !{
2108 caller_str = '[' // trim(caller) // ']'
2110 caller_str = fm_util_default_caller
2113 error_header = '==>Error from ' // trim(mod_name) // &
2114 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2115 warn_header = '==>Warning from ' // trim(mod_name) // &
2116 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2117 note_header = '==>Note from ' // trim(mod_name) // &
2118 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2121 ! check that a name is given (fatal if not)
2124 if (name .eq. ' ') then !{
2125 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
2129 ! check that append and index are not both given
2132 if (present(index) .and. present(append)) then !{
2133 call mpp_error(FATAL, trim(error_header) // ' Append and index both given as arguments')
2137 ! check for whether to overwrite existing values
2140 if (present(no_overwrite)) then !{
2141 no_overwrite_use = no_overwrite
2143 no_overwrite_use = default_no_overwrite
2147 ! check for whether to save the name in a list
2150 if (present(good_name_list)) then !{
2151 good_name_list_use = good_name_list
2153 good_name_list_use = default_good_name_list
2156 if (present(no_create)) then !{
2157 create = .not. no_create
2158 if (no_create .and. (present(append) .or. present(index))) then !{
2159 call mpp_error(FATAL, trim(error_header) // &
2160 & ' append or index are present when no_create is true for ' // trim(name))
2166 if (present(index)) then !{
2167 if (fm_exists(name)) then !{
2168 field_length = fm_get_length(name)
2169 if (field_length .lt. 0) then !{
2170 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
2172 if (.not. (no_overwrite_use .and. field_length .ge. index)) then !{
2173 field_index = fm_new_value(name, ival, index = index)
2174 if (field_index .le. 0) then !{
2175 write (str_error,*) ' with index = ', index
2176 call mpp_error(FATAL, trim(error_header) // ' Problem overwriting ' // trim(name) // trim(str_error))
2180 field_index = fm_new_value(name, ival, index = index)
2181 if (field_index .le. 0) then !{
2182 write (str_error,*) ' with index = ', index
2183 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2186 elseif (present(append)) then !}{
2187 field_index = fm_new_value(name, ival, append = append)
2188 if (field_index .le. 0) then !{
2189 write (str_error,*) ' with append = ', append
2190 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2193 if (fm_exists(name)) then !{
2194 if (.not. no_overwrite_use) then !{
2195 field_index = fm_new_value(name, ival)
2196 if (field_index .le. 0) then !{
2197 call mpp_error(FATAL, trim(error_header) // ' Problem overwriting ' // trim(name))
2200 elseif (create) then !}{
2201 field_index = fm_new_value(name, ival)
2202 if (field_index .le. 0) then !{
2203 call mpp_error(FATAL, trim(error_header) // ' Problem creating ' // trim(name))
2209 ! Add the variable name to the list of good names, to be used
2210 ! later for a consistency check, unless the field did not exist and we did not create it
2213 if (good_name_list_use .ne. ' ') then !{
2214 if (fm_exists(good_name_list_use)) then !{
2215 add_name = fm_util_get_index_string(good_name_list_use, name, &
2216 caller = caller_str) .le. 0 ! true if name does not exist in string array
2218 add_name = .true. ! always add to new list
2220 if (add_name .and. fm_exists(name)) then !{
2221 if (fm_new_value(good_name_list_use, name, append = .true., create = .true.) .le. 0) then !{
2222 call mpp_error(FATAL, trim(error_header) // &
2223 ' Could not add ' // trim(name) // ' to "' // trim(good_name_list_use) // '" list')
2230 end subroutine fm_util_set_value_integer !}
2232 !#######################################################################
2234 !> Set a logical value in the Field Manager tree.
2235 subroutine fm_util_set_value_logical(name, lval, caller, index, append, no_create, &
2236 no_overwrite, good_name_list) !{
2244 character(len=*), intent(in) :: name
2245 logical, intent(in) :: lval
2246 character(len=*), intent(in), optional :: caller
2247 integer, intent(in), optional :: index
2248 logical, intent(in), optional :: append
2249 logical, intent(in), optional :: no_create
2250 logical, intent(in), optional :: no_overwrite
2251 character(len=*), intent(in), optional :: good_name_list
2257 character(len=48), parameter :: sub_name = 'fm_util_set_value_logical'
2263 character(len=256) :: error_header
2264 character(len=256) :: warn_header
2265 character(len=256) :: note_header
2266 character(len=128) :: caller_str
2267 character(len=32) :: str_error
2268 integer :: field_index
2269 logical :: no_overwrite_use
2270 integer :: field_length
2271 character(len=FMS_PATH_LEN) :: good_name_list_use
2276 ! set the caller string and headers
2279 if (present(caller)) then !{
2280 caller_str = '[' // trim(caller) // ']'
2282 caller_str = fm_util_default_caller
2285 error_header = '==>Error from ' // trim(mod_name) // &
2286 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2287 warn_header = '==>Warning from ' // trim(mod_name) // &
2288 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2289 note_header = '==>Note from ' // trim(mod_name) // &
2290 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2293 ! check that a name is given (fatal if not)
2296 if (name .eq. ' ') then !{
2297 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
2301 ! check that append and index are not both given
2304 if (present(index) .and. present(append)) then !{
2305 call mpp_error(FATAL, trim(error_header) // ' Append and index both given as arguments')
2309 ! check for whether to overwrite existing values
2312 if (present(no_overwrite)) then !{
2313 no_overwrite_use = no_overwrite
2315 no_overwrite_use = default_no_overwrite
2319 ! check for whether to save the name in a list
2322 if (present(good_name_list)) then !{
2323 good_name_list_use = good_name_list
2325 good_name_list_use = default_good_name_list
2328 if (present(no_create)) then !{
2329 create = .not. no_create
2330 if (no_create .and. (present(append) .or. present(index))) then !{
2331 call mpp_error(FATAL, trim(error_header) // &
2332 & ' append or index are present when no_create is true for ' // trim(name))
2338 if (present(index)) then !{
2339 if (fm_exists(name)) then !{
2340 field_length = fm_get_length(name)
2341 if (field_length .lt. 0) then !{
2342 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
2344 if (.not. (no_overwrite_use .and. field_length .ge. index)) then !{
2345 field_index = fm_new_value(name, lval, index = index)
2346 if (field_index .le. 0) then !{
2347 write (str_error,*) ' with index = ', index
2348 call mpp_error(FATAL, trim(error_header) // ' Problem overwriting ' // trim(name) // trim(str_error))
2352 field_index = fm_new_value(name, lval, index = index)
2353 if (field_index .le. 0) then !{
2354 write (str_error,*) ' with index = ', index
2355 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2358 elseif (present(append)) then !}{
2359 field_index = fm_new_value(name, lval, append = append)
2360 if (field_index .le. 0) then !{
2361 write (str_error,*) ' with append = ', append
2362 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2365 if (fm_exists(name)) then !{
2366 if (.not. no_overwrite_use) then !{
2367 field_index = fm_new_value(name, lval)
2368 if (field_index .le. 0) then !{
2369 call mpp_error(FATAL, trim(error_header) // ' Problem overwriting ' // trim(name))
2372 elseif (create) then !}{
2373 field_index = fm_new_value(name, lval)
2374 if (field_index .le. 0) then !{
2375 call mpp_error(FATAL, trim(error_header) // ' Problem creating ' // trim(name))
2381 ! Add the variable name to the list of good names, to be used
2382 ! later for a consistency check, unless the field did not exist and we did not create it
2385 if (good_name_list_use .ne. ' ') then !{
2386 if (fm_exists(good_name_list_use)) then !{
2387 add_name = fm_util_get_index_string(good_name_list_use, name, &
2388 caller = caller_str) .le. 0 ! true if name does not exist in string array
2390 add_name = .true. ! always add to new list
2392 if (add_name .and. fm_exists(name)) then !{
2393 if (fm_new_value(good_name_list_use, name, append = .true., create = .true.) .le. 0) then !{
2394 call mpp_error(FATAL, trim(error_header) // &
2395 ' Could not add ' // trim(name) // ' to "' // trim(good_name_list_use) // '" list')
2402 end subroutine fm_util_set_value_logical !}
2404 !#######################################################################
2405 !> Set a string value in the Field Manager tree.
2406 subroutine fm_util_set_value_string(name, sval, caller, index, append, no_create, &
2407 no_overwrite, good_name_list) !{
2415 character(len=*), intent(in) :: name
2416 character(len=*), intent(in) :: sval
2417 character(len=*), intent(in), optional :: caller
2418 integer, intent(in), optional :: index
2419 logical, intent(in), optional :: append
2420 logical, intent(in), optional :: no_create
2421 logical, intent(in), optional :: no_overwrite
2422 character(len=*), intent(in), optional :: good_name_list
2428 character(len=48), parameter :: sub_name = 'fm_util_set_value_string'
2434 character(len=256) :: error_header
2435 character(len=256) :: warn_header
2436 character(len=256) :: note_header
2437 character(len=128) :: caller_str
2438 character(len=32) :: str_error
2439 integer :: field_index
2440 logical :: no_overwrite_use
2441 integer :: field_length
2442 character(len=FMS_PATH_LEN) :: good_name_list_use
2447 ! set the caller string and headers
2450 if (present(caller)) then !{
2451 caller_str = '[' // trim(caller) // ']'
2453 caller_str = fm_util_default_caller
2456 error_header = '==>Error from ' // trim(mod_name) // &
2457 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2458 warn_header = '==>Warning from ' // trim(mod_name) // &
2459 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2460 note_header = '==>Note from ' // trim(mod_name) // &
2461 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2464 ! check that a name is given (fatal if not)
2467 if (name .eq. ' ') then !{
2468 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
2472 ! check that append and index are not both given
2475 if (present(index) .and. present(append)) then !{
2476 call mpp_error(FATAL, trim(error_header) // ' Append and index both given as arguments')
2480 ! check for whether to overwrite existing values
2483 if (present(no_overwrite)) then !{
2484 no_overwrite_use = no_overwrite
2486 no_overwrite_use = default_no_overwrite
2490 ! check for whether to save the name in a list
2493 if (present(good_name_list)) then !{
2494 good_name_list_use = good_name_list
2496 good_name_list_use = default_good_name_list
2499 if (present(no_create)) then !{
2500 create = .not. no_create
2501 if (no_create .and. (present(append) .or. present(index))) then !{
2502 call mpp_error(FATAL, trim(error_header) // &
2503 & ' append or index are present when no_create is true for ' // trim(name))
2509 if (present(index)) then !{
2510 if (fm_exists(name)) then !{
2511 field_length = fm_get_length(name)
2512 if (field_length .lt. 0) then !{
2513 call mpp_error(FATAL, trim(error_header) // ' Problem getting length of ' // trim(name))
2515 if (.not. (no_overwrite_use .and. field_length .ge. index)) then !{
2516 field_index = fm_new_value(name, sval, index = index)
2517 if (field_index .le. 0) then !{
2518 write (str_error,*) ' with index = ', index
2519 call mpp_error(FATAL, trim(error_header) // ' Problem overwriting ' // trim(name) // trim(str_error))
2523 field_index = fm_new_value(name, sval, index = index)
2524 if (field_index .le. 0) then !{
2525 write (str_error,*) ' with index = ', index
2526 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2529 elseif (present(append)) then !}{
2530 field_index = fm_new_value(name, sval, append = append)
2531 if (field_index .le. 0) then !{
2532 write (str_error,*) ' with append = ', append
2533 call mpp_error(FATAL, trim(error_header) // ' Problem setting ' // trim(name) // trim(str_error))
2536 if (fm_exists(name)) then !{
2537 if (.not. no_overwrite_use) then !{
2538 field_index = fm_new_value(name, sval)
2539 if (field_index .le. 0) then !{
2540 call mpp_error(FATAL, trim(error_header) // ' Problem overwriting ' // trim(name))
2543 elseif (create) then !}{
2544 field_index = fm_new_value(name, sval)
2545 if (field_index .le. 0) then !{
2546 call mpp_error(FATAL, trim(error_header) // ' Problem creating ' // trim(name))
2552 ! Add the variable name to the list of good names, to be used
2553 ! later for a consistency check, unless the field did not exist and we did not create it
2556 if (good_name_list_use .ne. ' ') then !{
2557 if (fm_exists(good_name_list_use)) then !{
2558 add_name = fm_util_get_index_string(good_name_list_use, name, &
2559 caller = caller_str) .le. 0 ! true if name does not exist in string array
2561 add_name = .true. ! always add to new list
2563 if (add_name .and. fm_exists(name)) then !{
2564 if (fm_new_value(good_name_list_use, name, append = .true., create = .true.) .le. 0) then !{
2565 call mpp_error(FATAL, trim(error_header) // &
2566 ' Could not add ' // trim(name) // ' to "' // trim(good_name_list_use) // '" list')
2573 end subroutine fm_util_set_value_string !}
2575 !#######################################################################
2577 !> Start processing a namelist
2578 subroutine fm_util_start_namelist(path, name, caller, no_overwrite, check) !{
2586 character(len=*), intent(in) :: path
2587 character(len=*), intent(in) :: name
2588 character(len=*), intent(in), optional :: caller
2589 logical, intent(in), optional :: no_overwrite
2590 logical, intent(in), optional :: check
2596 character(len=48), parameter :: sub_name = 'fm_util_start_namelist'
2602 integer :: namelist_index
2603 character(len=FMS_PATH_LEN) :: path_name
2604 character(len=256) :: error_header
2605 character(len=256) :: warn_header
2606 character(len=256) :: note_header
2607 character(len=128) :: caller_str
2613 ! set the caller string and headers
2616 if (present(caller)) then !{
2617 caller_str = '[' // trim(caller) // ']'
2619 caller_str = fm_util_default_caller
2622 error_header = '==>Error from ' // trim(mod_name) // &
2623 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2624 warn_header = '==>Warning from ' // trim(mod_name) // &
2625 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2626 note_header = '==>Note from ' // trim(mod_name) // &
2627 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2630 ! check that a name is given (fatal if not)
2633 if (name .eq. ' ') then !{
2634 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
2638 ! Concatenate the path and name
2641 if (path .eq. ' ') then !{
2644 path_name = trim(path) // '/' // name
2650 ! set the default caller string, if desired
2653 if (present(caller)) then !{
2654 call fm_util_set_caller(caller)
2656 call fm_util_reset_caller
2660 ! set the default no_overwrite flag, if desired
2663 if (present(no_overwrite)) then !{
2664 call fm_util_set_no_overwrite(no_overwrite)
2666 call fm_util_reset_no_overwrite
2670 ! set the default good_name_list string, if desired
2673 if (present(check)) then !{
2675 call fm_util_set_good_name_list('/ocean_mod/GOOD/namelists/' // trim(path_name) // '/good_list')
2677 call fm_util_reset_good_name_list
2680 call fm_util_reset_good_name_list
2684 ! Process the namelist
2688 write (out_unit,*) trim(note_header), ' Processing namelist ', trim(path_name)
2691 ! Check whether the namelist already exists. If so, then use that one
2694 namelist_index = fm_get_index('/ocean_mod/namelists/' // trim(path_name))
2695 if (namelist_index .gt. 0) then !{
2697 !write (out_unit,*) trim(note_header), ' Namelist already set with index ', namelist_index
2702 ! Set a new namelist and get its index
2705 namelist_index = fm_new_list('/ocean_mod/namelists/' // trim(path_name), create = .true.)
2706 if (namelist_index .le. 0) then !{
2707 call mpp_error(FATAL, trim(error_header) // ' Could not set namelist ' // trim(path_name))
2713 ! Add the namelist name to the list of good namelists, to be used
2714 ! later for a consistency check
2717 if (fm_new_value('/ocean_mod/GOOD/namelists/' // trim(path) // '/good_values', &
2718 name, append = .true., create = .true.) .le. 0) then !{
2719 call mpp_error(FATAL, trim(error_header) // &
2720 ' Could not add ' // trim(name) // ' to "' // trim(path) // '/good_values" list')
2724 ! Change to the new namelist, first saving the current list
2727 save_current_list = fm_get_current_list()
2728 if (save_current_list .eq. ' ') then !{
2729 call mpp_error(FATAL, trim(error_header) // ' Could not get the current list')
2732 if (.not. fm_change_list('/ocean_mod/namelists/' // trim(path_name))) then !{
2733 call mpp_error(FATAL, trim(error_header) // ' Could not change to the namelist ' // trim(path_name))
2738 end subroutine fm_util_start_namelist !}
2740 !#######################################################################
2742 !> Finish up processing a namelist
2743 subroutine fm_util_end_namelist(path, name, caller, check) !{
2751 character(len=*), intent(in) :: path
2752 character(len=*), intent(in) :: name
2753 character(len=*), intent(in), optional :: caller
2754 logical, intent(in), optional :: check
2760 character(len=48), parameter :: sub_name = 'fm_util_end_namelist'
2766 character(len=fm_string_len), pointer, dimension(:) :: good_list => NULL()
2767 character(len=FMS_PATH_LEN) :: path_name
2768 character(len=256) :: error_header
2769 character(len=256) :: warn_header
2770 character(len=256) :: note_header
2771 character(len=128) :: caller_str
2774 ! set the caller string and headers
2777 if (present(caller)) then !{
2778 caller_str = '[' // trim(caller) // ']'
2780 caller_str = fm_util_default_caller
2783 error_header = '==>Error from ' // trim(mod_name) // &
2784 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2785 warn_header = '==>Warning from ' // trim(mod_name) // &
2786 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2787 note_header = '==>Note from ' // trim(mod_name) // &
2788 '(' // trim(sub_name) // ')' // trim(caller_str) // ':'
2791 ! check that a path is given (fatal if not)
2794 if (name .eq. ' ') then !{
2795 call mpp_error(FATAL, trim(error_header) // ' Empty name given')
2799 ! Check that the path ane name match the preceding call to
2800 ! fm_util_start_namelist
2803 if (path .ne. save_path) then !{
2804 call mpp_error(FATAL, trim(error_header) // &
2805 & ' Path "' // trim(path) // '" does not match saved path "' // trim(save_path) // '"')
2806 elseif (name .ne. save_name) then !}{
2807 call mpp_error(FATAL, trim(error_header) // &
2808 & ' Name "' // trim(name) // '" does not match saved name "' // trim(save_name) // '"')
2812 ! Concatenate the path and name
2815 if (path .eq. ' ') then !{
2818 path_name = trim(path) // '/' // name
2824 ! Check for any errors in the number of fields in this list
2827 if (present(check)) then !{
2829 if (caller_str .eq. ' ') then !{
2830 caller_str = trim(mod_name) // '(' // trim(sub_name) // ')'
2832 good_list => fm_util_get_string_array('/ocean_mod/GOOD/namelists/' // trim(path_name) // '/good_list', &
2833 caller = trim(mod_name) // '(' // trim(sub_name) // ')')
2834 if (associated(good_list)) then !{
2835 call fm_util_check_for_bad_fields('/ocean_mod/namelists/' // trim(path_name), good_list, caller = caller_str)
2836 deallocate(good_list)
2838 call mpp_error(FATAL, trim(error_header) // ' Empty "' // trim(path_name) // '" list')
2844 ! Change back to the saved list
2847 if (save_current_list .ne. ' ') then !{
2848 if (.not. fm_change_list(save_current_list)) then !{
2849 call mpp_error(FATAL, trim(error_header) // ' Could not change to the saved list: ' // trim(save_current_list))
2852 save_current_list = ' '
2855 ! reset the default caller string
2858 call fm_util_reset_caller
2861 ! reset the default no_overwrite string
2864 call fm_util_reset_no_overwrite
2867 ! reset the default good_name_list string
2870 call fm_util_reset_good_name_list
2874 end subroutine fm_util_end_namelist !}
2876 #include "fm_util_r4.fh"
2877 #include "fm_util_r8.fh"
2879 end module fm_util_mod !}
2881 ! close documentation grouping