Skip to content

Instantly share code, notes, and snippets.

@jfbourdon
Created October 19, 2018 18:37
Show Gist options
  • Save jfbourdon/52ddb9259302a0cc3ff13779245d5020 to your computer and use it in GitHub Desktop.
Save jfbourdon/52ddb9259302a0cc3ff13779245d5020 to your computer and use it in GitHub Desktop.
zvalue_from_index for KRSTN
def _zvalue_from_index(arr, ind):
"""private helper function to work around the limitation of np.choose() by employing np.take()
arr has to be a 3D array
ind has to be a 2D array containing values for z-indicies to take from arr
See: http://stackoverflow.com/a/32091712/4169585
This is faster and more memory efficient than using the ogrid based solution with fancy indexing.
"""
# get number of rows and columns
_,nR,nC = arr.shape
# get linear indices and extract elements with np.take()
idx = nC*nR*ind + nC*np.arange(nR)[:,None] + np.arange(nC)
return np.take(arr, idx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment