Skip to content

Commit

Permalink
Add test for cell aware bbox to grid points and ensure gpis dtype.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Paulik committed Aug 14, 2017
1 parent c5a36c1 commit 2416e9c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Expand Up @@ -5,10 +5,13 @@ Changelog
v0.2.4
======

- Add option to subset a grid with a shape file (OGRGeometry) in get_shp_grid_points.
- Add option to subset a grid with a shape file (OGRGeometry) in
get_shp_grid_points.
- Add shapefile module for reading shapefiles from
http://biogeo.ucdavis.edu/data/gadm2.8/gadm28_levels.shp.zip by Global
Administrative Level
- Ensure that `get_bbox_grid_points` returns points while taking cell order into
account.

v0.2.3
======
Expand Down
4 changes: 2 additions & 2 deletions pygeogrids/grids.py
Expand Up @@ -199,7 +199,7 @@ def __init__(self, lon, lat, gpis=None, geodatum='WGS84', subset=None,
self.geodatum = GeodeticDatum(geodatum)

if gpis is None:
self.gpis = np.arange(self.n_gpi)
self.gpis = np.arange(self.n_gpi, dtype=int)
self.gpidirect = True
else:
if lat.shape != gpis.shape:
Expand Down Expand Up @@ -607,7 +607,7 @@ def get_bbox_grid_points(self, latmin=-90, latmax=90, lonmin=-180,
"""

gp_info = np.array(list(self.grid_points()))
gpis = gp_info[:, 0]
gpis = gp_info[:, 0].astype(int)
lons = gp_info[:, 1]
lats = gp_info[:, 2]
index = np.where((lats <= latmax) &
Expand Down
55 changes: 55 additions & 0 deletions tests/test_grid.py
Expand Up @@ -156,6 +156,14 @@ def test_gpi2cell_iterable(self):
cell = self.cellgrid.gpi2cell(gpi)
assert np.all(cell == [1043, 2015])

def test_gpi2cell_numpy(self):
"""
test if gpi to cell lookup works correctly
"""
gpi = np.array([200, 255])
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
Expand Down Expand Up @@ -412,6 +420,52 @@ def test_subgrid_from_gpis(self):
lons_should, lats_should, cells_should, gpis=gpis)
assert subgrid == subgrid_should

def test_get_bbox_grid_points(self):
gpis = self.cellgrid.get_bbox_grid_points(latmin=-10,
latmax=-5,
lonmin=-10,
lonmax=-5)
nptest.assert_allclose(gpis,
np.array([5684, 5685, 5828, 5829,
5540, 5541, 5686, 5830, 5542]))
# gpis should come back sorted by cells
nptest.assert_allclose(self.cellgrid.gpi2cell(gpis),
np.array([1240, 1240, 1240, 1240,
1241, 1241, 1276, 1276, 1277]))
lats, lons = self.cellgrid.get_bbox_grid_points(latmin=-10,
latmax=-5,
lonmin=-10,
lonmax=-5,
coords=True)
lats_should = np.array([-7.5, -7.5, -10., -10.,
-5., -5., -7.5, -10., -5.])
lons_should = np.array([-10., -7.5, -10., -7.5,
-10., -7.5, -5., -5., -5.])
nptest.assert_allclose(lats,
lats_should)
nptest.assert_allclose(lons,
lons_should)
gpis, lats, lons = self.cellgrid.get_bbox_grid_points(latmin=-10,
latmax=-5,
lonmin=-10,
lonmax=-5,
both=True)
lats_should = np.array([-7.5, -7.5, -10., -10.,
-5., -5., -7.5, -10., -5.])
lons_should = np.array([-10., -7.5, -10., -7.5,
-10., -7.5, -5., -5., -5.])
nptest.assert_allclose(lats,
lats_should)
nptest.assert_allclose(lons,
lons_should)
nptest.assert_allclose(gpis,
np.array([5684, 5685, 5828, 5829,
5540, 5541, 5686, 5830, 5542]))
# gpis should come back sorted by cells
nptest.assert_allclose(self.cellgrid.gpi2cell(gpis),
np.array([1240, 1240, 1240, 1240,
1241, 1241, 1276, 1276, 1277]))


def test_setup_grid_with_lists():

Expand Down Expand Up @@ -586,5 +640,6 @@ def test_shpgrid(self):
assert subgrid.activearrlon == 15
assert subgrid.activearrlat == 46


if __name__ == "__main__":
unittest.main()

0 comments on commit 2416e9c

Please sign in to comment.