Skip to content

Commit

Permalink
Merge pull request #119 from ahaberlie/master
Browse files Browse the repository at this point in the history
Added generic 1d nearest neighbor helper function
  • Loading branch information
dopplershift committed Jan 25, 2016
2 parents 00830e6 + 0db4d47 commit e66c1d2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
57 changes: 53 additions & 4 deletions examples/notebooks/Simple_Sounding.ipynb

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions metpy/calc/tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2008-2015 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause

import numpy as np
from numpy.testing import assert_array_equal

from metpy.calc.tools import resample_nn_1d


def test_resample_nn():
'Test 1d nearest neighbor functionality.'
a = np.arange(5.)
b = np.array([2, 3.8])
truth = np.array([2, 4])

assert_array_equal(truth, resample_nn_1d(a, b))
30 changes: 30 additions & 0 deletions metpy/calc/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2008-2015 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause

import numpy as np


def resample_nn_1d(a, centers):
"""Helper function that returns one-dimensional nearest-neighbor
indexes based on user-specified centers.
Parameters
----------
a : array-like
1-dimensional array of numeric values from which to
extract indexes of nearest-neighbors
centers : array-like
1-dimensional array of numeric values representing a subset of values to approximate
Returns
-------
An array of indexes representing values closest to given array values
"""
ix = []
for center in centers:
index = (np.abs(a - center)).argmin()
if index not in ix:
ix.append(index)
return ix

0 comments on commit e66c1d2

Please sign in to comment.