FV3 Bundle
gsw_util_indx.f90
Go to the documentation of this file.
1 !==========================================================================
2 pure function gsw_util_indx (x, z, kstart) result(ki)
3 !==========================================================================
4 !
5 ! Finds the index of the value in a monotonically increasing array
6 !
7 ! x : array of monotonically increasing values
8 ! z : value to be indexed
9 ! kstart : (optional) restrict search to x(kstart:)
10 !
11 ! ki : index k : if x(k) <= z < x(k+1), or
12 ! n-1 : if z = x(n)
13 !--------------------------------------------------------------------------
14 
15 use gsw_mod_kinds
16 
17 implicit none
18 
19 real (r8), intent(in) :: x(:), z
20 integer, intent(in), optional :: kstart
21 
22 integer :: ki
23 
24 integer :: ku, kl, km, n
25 
26 n = size(x)
27 
28 if ((z .gt. x(1)) .and. (z .lt. x(n))) then
29 
30  if (present(kstart)) then
31  kl = kstart
32  else
33  kl = 1
34  end if
35 
36  ku = n
37  do while (ku-kl .gt. 1)
38  km = (ku + kl) / 2
39  if (z .gt. x(km)) then
40  kl = km
41  else
42  ku = km
43  endif
44  end do
45  ki = kl
46  if (z .eq. x(ki+1)) ki = ki + 1
47 
48 elseif (z .le. x(1)) then
49 
50  ki = 1
51 
52 else !if (z.ge.x(n)) then - removed (GBH 3/6/2015) so z=NaN has somewhere to go (otherwise ki is undefined and gives segmentation fault)
53 
54  ki = n-1
55 
56 end if
57 
58 return
59 end function
60 
61 !--------------------------------------------------------------------------
62 
pure integer function gsw_util_indx(x, z, kstart)