Find the interval of the time list containing t, by bisection (times
increasing or decreasing). Returns a TIME_ code, or the number of
iterations (> 0) when found, with times(index) <= t < times(index+1) for
increasing times (>= and > for decreasing). See the TIME_ constants for
index in the other cases.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(Trajectory), | intent(in) | :: | self | |||
| real(kind=wp), | intent(in) | :: | t |
the time |
||
| integer, | intent(out) | :: | index |
the interval's first index (see the TIME_* constants) |
integer function time_index(self, t, index) result(status) class(Trajectory), intent(in) :: self real(wp), intent(in) :: t !! the time integer, intent(out) :: index !! the interval's first index (see the TIME_* constants) integer :: low, high, mid, iter, n real(wp) :: dir, dist n = self%n_times if (n == 0) then index = 0 status = TIME_OUT_OF_RANGE return end if dir = merge(1.0_wp, -1.0_wp, self%t(1) <= self%t(n)) dist = self%time_distance(t) if (dist > 0.0_wp) then index = merge(0, n + 1, dir * t < dir * self%t(1)) status = TIME_OUT_OF_RANGE return else if (dist == 0.0_wp) then index = merge(1, n, t == self%t(1)) status = TIME_AT_BOUND return end if low = 1 high = n do iter = 1, 64 mid = low + (high - low) / 2 if (dir * t < dir * self%t(mid)) then high = mid else low = mid end if if (high - low <= 1) then index = low status = iter return end if end do index = low status = TIME_NOT_FOUND end function time_index