module unittestArrayMod ! Provides utility functions for working with array inputs to (or outputs from) ! subroutines. ! ! The routines here assume that the subgrid structure has been set up via ! unittestSubgridMod. use shr_kind_mod, only : r8 => shr_kind_r8, i4 => shr_kind_i4 use shr_infnan_mod, only : nan => shr_infnan_nan, assignment(=) use unittestSubgridMod, only : bounds implicit none private save ! Create a gridcell-level array filled with a single value; the type of the array is the ! same as the type of the input value. If no value is given, an r8 array is returned, ! filled with NaNs. public :: grc_array ! Create a column-level array filled with a single value; the type of the array is the ! same as the type of the input value. If no value is given, an r8 array is returned, ! filled with NaNs. public :: col_array ! Create a patch-level array filled with a single value; the type of the array is the ! same as the type of the input value. If no value is given, an r8 array is returned, ! filled with NaNs. public :: patch_array ! Converts an array of logicals to a same-size array of integers: false => 0, true => 1 public :: logical_array_to_int interface grc_array module procedure grc_array_uninit !TYPE logical,int,double module procedure grc_array_{TYPE} end interface grc_array interface col_array module procedure col_array_uninit !TYPE logical,int,double module procedure col_array_{TYPE} end interface col_array interface patch_array module procedure patch_array_uninit !TYPE logical,int,double module procedure patch_array_{TYPE} end interface patch_array contains !----------------------------------------------------------------------- pure function grc_array_uninit() result(grc_array) ! ! !DESCRIPTION: ! Creates a gridcell-level array of r8 values. All elements are set to NaN. ! ! !USES: ! ! !ARGUMENTS: real(r8), allocatable :: grc_array(:) ! function result ! ! !LOCAL VARIABLES: character(len=*), parameter :: subname = 'grc_array_uninit' !----------------------------------------------------------------------- allocate(grc_array(bounds%begg:bounds%endg)) grc_array(:) = nan end function grc_array_uninit !----------------------------------------------------------------------- !TYPE logical,int,double pure function grc_array_{TYPE}(val) result(grc_array) ! ! !DESCRIPTION: ! Creates a gridcell-level array with the same type as val. All elements are set to val. ! ! !USES: ! ! !ARGUMENTS: {VTYPE}, allocatable :: grc_array(:) ! function result {VTYPE}, intent(in) :: val ! all elements in grc_array are set to val ! ! !LOCAL VARIABLES: character(len=*), parameter :: subname = 'grc_array_{TYPE}' !----------------------------------------------------------------------- allocate(grc_array(bounds%begg:bounds%endg)) grc_array(:) = val end function grc_array_{TYPE} !----------------------------------------------------------------------- pure function col_array_uninit() result(col_array) ! ! !DESCRIPTION: ! Creates a column-level array of r8 values. All elements are set to NaN. ! ! !USES: ! ! !ARGUMENTS: real(r8), allocatable :: col_array(:) ! function result ! ! !LOCAL VARIABLES: character(len=*), parameter :: subname = 'col_array_uninit' !----------------------------------------------------------------------- allocate(col_array(bounds%begc:bounds%endc)) col_array(:) = nan end function col_array_uninit !----------------------------------------------------------------------- !TYPE logical,int,double pure function col_array_{TYPE}(val) result(col_array) ! ! !DESCRIPTION: ! Creates a column-level array with the same type as val. All elements are set to val. ! ! !USES: ! ! !ARGUMENTS: {VTYPE}, allocatable :: col_array(:) ! function result {VTYPE}, intent(in) :: val ! all elements in col_array are set to val ! ! !LOCAL VARIABLES: character(len=*), parameter :: subname = 'col_array_{TYPE}' !----------------------------------------------------------------------- allocate(col_array(bounds%begc:bounds%endc)) col_array(:) = val end function col_array_{TYPE} !----------------------------------------------------------------------- pure function patch_array_uninit() result(patch_array) ! ! !DESCRIPTION: ! Creates a patch-level array of r8 values. All elements are set to NaN. ! ! !USES: ! ! !ARGUMENTS: real(r8), allocatable :: patch_array(:) ! function result ! ! !LOCAL VARIABLES: character(len=*), parameter :: subname = 'patch_array_uninit' !----------------------------------------------------------------------- allocate(patch_array(bounds%begp:bounds%endp)) patch_array(:) = nan end function patch_array_uninit !----------------------------------------------------------------------- !TYPE logical,int,double pure function patch_array_{TYPE}(val) result(patch_array) ! ! !DESCRIPTION: ! Creates a patch-level array with the same type as val. All elements are set to val. ! ! !USES: ! ! !ARGUMENTS: {VTYPE}, allocatable :: patch_array(:) ! function result {VTYPE}, intent(in) :: val ! all elements in patch_array are set to val ! ! !LOCAL VARIABLES: character(len=*), parameter :: subname = 'patch_array_{TYPE}' !----------------------------------------------------------------------- allocate(patch_array(bounds%begp:bounds%endp)) patch_array(:) = val end function patch_array_{TYPE} !----------------------------------------------------------------------- function logical_array_to_int(logical_array) result(int_array) ! ! !DESCRIPTION: ! Converts an array of logicals to a same-size array of integers: false => 0, true => 1 ! ! This can be useful for doing assertions on arrays, since pFUnit doesn't support ! assertions of equality of logical arrays. ! ! !ARGUMENTS: logical, intent(in) :: logical_array(:) integer :: int_array(size(logical_array)) ! function result ! ! !LOCAL VARIABLES: character(len=*), parameter :: subname = 'logical_array_to_int' !----------------------------------------------------------------------- where (logical_array) int_array = 1 elsewhere int_array = 0 end where end function logical_array_to_int end module unittestArrayMod