Created
February 4, 2013 22:38
-
-
Save jhamman/4710397 to your computer and use it in GitHub Desktop.
Simple way to find the 1D index location of a single value in a numpy array. I often use this to find the location of a lat/lon pair in numpy arrays.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import numpy as np | |
################################################################################## | |
## Find Indicies | |
## Given an input lat or lon, the function returns the nearest index location | |
################################################################################## | |
def find_nearest(array,value): | |
""" Find the index location in (array) with value nearest to (value)""" | |
idx = (np.abs(array-value)).argmin() | |
return idx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment