subroutine check_int_range(values, name, range, index)
! Check if the integer array values is within (/lower, upper/) range and terminate
! with name if not. values must be a 1D array but a single
! value can also be checked by passing (/value/) and an optional index to report.
! Examples:
! call check_int_range((/1, 2, 3/), "array", (/0, 2/))
! call check_int_range((/3/), "val", (/0, 2/))
! call check_int_range((/3/), "array", (/0, 2/), index=100)
integer, intent(in) :: values(:)
character(*), intent(in) :: name
integer, intent(in) :: range(2)
integer, intent(in), optional :: index
integer i, nv, counter
if (range(1) >= range(2)) &
call log_error("check_int_range", "range is not (/lower, upper/) values", &
ints=range)
counter = 0
nv = size(values)
do i = 1, nv
if (values(i) < range(1) .or. values(i) > range(2)) then
! Only print value if not single cell array
if (nv > 1) call log_warn('check_int_range', name, i1=i, int=values(i))
counter = counter + 1
endif
end do
! Array with counted out of range values, indeces reported above
if (counter > 0 .and. nv > 1) call out_of_range_error(name, int_range=range, counter=counter)
! Single value with index
if (counter == 1 .and. present(index)) &
call out_of_range_error(name, int_range=range, int_value=values(1), index=index)
! Single value without index
if (counter == 1) call out_of_range_error(name, int_range=range, int_value=values(1))
end subroutine check_int_range