Skip to content

Instantly share code, notes, and snippets.

@jhamman
Created February 4, 2013 22:38
Show Gist options
  • Save jhamman/4710397 to your computer and use it in GitHub Desktop.
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.
#!/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