|
| 1 | +module ccpp_scheme_utils |
| 2 | + |
| 3 | + ! Module of utilities available to CCPP schemes |
| 4 | + |
| 5 | + use ccpp_constituent_prop_mod, only: ccpp_model_constituents_t, int_unassigned |
| 6 | + |
| 7 | + implicit none |
| 8 | + private |
| 9 | + |
| 10 | + !! Public interfaces |
| 11 | + public :: ccpp_initialize_constituent_ptr ! Used by framework to initialize |
| 12 | + public :: ccpp_constituent_index ! Lookup index constituent by name |
| 13 | + public :: ccpp_constituent_indices ! Lookup indices of consitutents by name |
| 14 | + |
| 15 | + !! Private module variables & interfaces |
| 16 | + |
| 17 | + ! initialized set to .true. once hash table pointer is initialized |
| 18 | + logical :: initialized = .false. |
| 19 | + type(ccpp_model_constituents_t), pointer :: constituent_obj => NULL() |
| 20 | + |
| 21 | + private :: uninitialized |
| 22 | + |
| 23 | +contains |
| 24 | + |
| 25 | + subroutine uninitialized(caller, errcode, errmsg) |
| 26 | + ! Dummy arguments |
| 27 | + character(len=*), intent(in) :: caller |
| 28 | + integer, optional, intent(out) :: errcode |
| 29 | + character(len=*), optional, intent(out) :: errmsg |
| 30 | + |
| 31 | + if (.not. initialized) then |
| 32 | + if (present(errcode)) then |
| 33 | + errcode = 1 |
| 34 | + end if |
| 35 | + if (present(errmsg)) then |
| 36 | + errmsg = trim(caller)//' FAILED, module not initialized' |
| 37 | + end if |
| 38 | + end if |
| 39 | + end subroutine uninitialized |
| 40 | + |
| 41 | + subroutine ccpp_initialize_constituent_ptr(const_obj) |
| 42 | + ! Dummy arguments |
| 43 | + type(ccpp_model_constituents_t), pointer, intent(in) :: const_obj |
| 44 | + |
| 45 | + if (.not. initialized) then |
| 46 | + constituent_obj => const_obj |
| 47 | + initialized = .true. |
| 48 | + end if |
| 49 | + end subroutine ccpp_initialize_constituent_ptr |
| 50 | + |
| 51 | + subroutine ccpp_constituent_index(standard_name, const_index, errcode, errmsg) |
| 52 | + ! Dummy arguments |
| 53 | + character(len=*), intent(in) :: standard_name |
| 54 | + integer, intent(out) :: const_index |
| 55 | + integer, optional, intent(out) :: errcode |
| 56 | + character(len=*), optional, intent(out) :: errmsg |
| 57 | + |
| 58 | + ! Local variable |
| 59 | + character(len=*), parameter :: subname = 'ccpp_constituent_index' |
| 60 | + |
| 61 | + if (initialized) then |
| 62 | + call constituent_obj%const_index(const_index, standard_name, & |
| 63 | + errcode, errmsg) |
| 64 | + else |
| 65 | + const_index = int_unassigned |
| 66 | + call uninitialized(subname) |
| 67 | + end if |
| 68 | + end subroutine ccpp_constituent_index |
| 69 | + |
| 70 | + subroutine ccpp_constituent_indices(standard_names, const_inds, errcode, errmsg) |
| 71 | + ! Dummy arguments |
| 72 | + character(len=*), intent(in) :: standard_names(:) |
| 73 | + integer, intent(out) :: const_inds(:) |
| 74 | + integer, optional, intent(out) :: errcode |
| 75 | + character(len=*), optional, intent(out) :: errmsg |
| 76 | + |
| 77 | + ! Local variables |
| 78 | + integer :: indx |
| 79 | + character(len=*), parameter :: subname = 'ccpp_constituent_indices' |
| 80 | + |
| 81 | + const_inds = int_unassigned |
| 82 | + if (initialized) then |
| 83 | + if (size(const_inds) < size(standard_names)) then |
| 84 | + errcode = 1 |
| 85 | + errmsg = subname//': const_inds too small' |
| 86 | + else |
| 87 | + do indx = 1, size(standard_names) |
| 88 | + ! For each std name in <standard_names>, find the const. index |
| 89 | + call constituent_obj%const_index(const_inds(indx), & |
| 90 | + standard_names(indx), errcode, errmsg) |
| 91 | + if (errcode /= 0) then |
| 92 | + exit |
| 93 | + end if |
| 94 | + end do |
| 95 | + end if |
| 96 | + else |
| 97 | + call uninitialized(subname) |
| 98 | + end if |
| 99 | + end subroutine ccpp_constituent_indices |
| 100 | + |
| 101 | +end module ccpp_scheme_utils |
0 commit comments