Skip to content

Commit

Permalink
fix #19 by refactoring iterable checking
Browse files Browse the repository at this point in the history
  • Loading branch information
cpaulik committed May 11, 2015
1 parent 94e221a commit 6f04636
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# v0.1.2 #
- fix issue #19 by refactoring the iterable checking into own function

# v0.1.1 #
- added support for saving more subsets and loading a certain one in/from a netcdf grid file
- fix #15 by setting correct shape for derived cell grids
Expand Down
44 changes: 29 additions & 15 deletions pygeogrids/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,7 @@ def find_nearest_gpi(self, lon, lat, max_dist=np.Inf):
At the moment not on a great circle but in spherical cartesian coordinates
"""
# check if input is iterable
try:
lon[0]
iterable = True
except TypeError:
iterable = False
iterable = _element_iterable(lon)

if self.kdTree is None:
self._setup_kdtree()
Expand Down Expand Up @@ -411,11 +407,8 @@ def gpi2rowcol(self, gpi):
column in 2D array
"""
# check if iterable
try:
gpi[0]
iterable = True
except (TypeError, IndexError):
iterable = False
iterable = _element_iterable(gpi)

gpi = np.atleast_1d(gpi)
if len(self.shape) == 2:
if self.gpidirect:
Expand Down Expand Up @@ -660,11 +653,8 @@ def gpi2cell(self, gpi):
Cell number of GPI.
"""
# check if iterable
try:
gpi[0]
iterable = True
except TypeError:
iterable = False
iterable = _element_iterable(gpi)

gpi = np.atleast_1d(gpi)
if self.gpidirect:
cell = self.arrcell[gpi]
Expand Down Expand Up @@ -914,3 +904,27 @@ def genreg_grid(grd_spc_lat=1, grd_spc_lon=1,
lat_dim = np.arange(maxlat - grd_spc_lat / 2.0, minlat, -grd_spc_lat)

return gridfromdims(lon_dim, lat_dim)


def _element_iterable(el):
"""
Test if a element is iterable
Parameters
----------
el: object
Returns
-------
iterable: boolean
if True then then el is iterable
if Fales then not
"""
try:
el[0]
iterable = True
except (TypeError, IndexError):
iterable = False

return iterable
16 changes: 16 additions & 0 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def test_nearest_neighbor_ndarray(self):
assert lat[0] == 45.5
assert lat[1] == -16.5

def test_nearest_neighbor_numpy_single(self):
gpi, dist = self.grid.find_nearest_gpi(
np.array([145.1, 90.2])[0], np.array([45.8, -16.3])[0])
assert gpi == 16165
lon, lat = self.grid.gpi2lonlat(gpi)
assert lon == 145.5
assert lat == 45.5


class TestCellGrid(unittest.TestCase):

Expand Down Expand Up @@ -100,6 +108,14 @@ def test_gpi2cell_iterable(self):
cell = self.cellgrid.gpi2cell(gpi)
assert np.all(cell == [1043, 2015])

def test_gpi2cell_numpy_single(self):
"""
test if gpi to row column lookup works correctly
"""
gpi = np.array([200, 255])[0]
cell = self.cellgrid.gpi2cell(gpi)
assert cell == 1043

def test_gpi2cell_custom_gpis(self):
"""
test if gpi to row column lookup works correctly
Expand Down

0 comments on commit 6f04636

Please sign in to comment.