Barak 0.3.2 documentation

barak.utilities.indexnear

«  barak.utilities.get_data_path   ::   Contents   ::   barak.utilities.indgroupby  »

barak.utilities.indexnear

barak.utilities.indexnear(ar, val)[source]

Find the element in an array closest to a given value.

The input array must be sorted lowest to highest. Returns the index of the element with a value closest to the given value.

Parameters :

ar : array_like

Input array. It must be sorted smallest to largest.

val : float

Find the element of ar that is closest to val.

Returns :

index : int

Index of the ar element with the closest value to val.

Examples

>>> wa = np.linspace(4000, 4500, 100)
>>> i = indexnear(wa, 4302.5)
>>> print i, wa[i]
60 4303.03030303
>>> i = indexnear(wa, 4600.0)
>>> print i, wa[i]
99 4500.0
>>> i = indexnear(wa, 3000.0)
>>> print i, wa[i]
0 4000.0

«  barak.utilities.get_data_path   ::   Contents   ::   barak.utilities.indgroupby  »