Skip to content

Commit

Permalink
Add reorder_to_cellsize function for reordering CellGrids.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Paulik authored and cpaulik committed Mar 28, 2017
1 parent 88c5e3c commit 7de1428
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ v0.2.3
======

- Fix bug in calc_lut in case of differently ordered subset of a grid.
- Add function to reorder grid based on different cell size. (See grids.reorder_to_cellsize)

v0.2.2
======
Expand Down
2 changes: 1 addition & 1 deletion pygeogrids/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .grids import BasicGrid, CellGrid, genreg_grid, lonlat2cell
from .grids import BasicGrid, CellGrid, genreg_grid, lonlat2cell, reorder_to_cellsize
import pkg_resources

try:
Expand Down
41 changes: 41 additions & 0 deletions pygeogrids/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,3 +1070,44 @@ def _element_iterable(el):
iterable = False

return iterable


def reorder_to_cellsize(grid, cellsize_lat, cellsize_lon):
"""
Reorder grid points in one grid to follow the
ordering of differently sized cells. This is useful if
e.g. a 10x10 degree CellGrid should be traversed
in an order compatible with a 5x5 degree CellGrid.
Parameters
----------
grid: :py:class:`pygeogrids.grids.CellGrid`
input grid
cellsize_lat: float
cellsize in latitude direction
cellsize_lon: float
cellsize in longitude direction
Returns
-------
new_grid: :py:class:`pygeogrids.grids.CellGrid`
output grid with original cell sizes but
different ordering.
"""

cell_grid = grid.to_cell_grid(cellsize_lat=cellsize_lat,
cellsize_lon=cellsize_lon)
cell_sort = np.argsort(cell_grid.arrcell)
new_arrlon = grid.arrlon[cell_sort]
new_arrlat = grid.arrlat[cell_sort]
new_arrcell = grid.arrcell[cell_sort]
new_gpis = grid.gpis[cell_sort]
new_subset = None
if grid.subset is not None:
full_subset = np.zeros(new_arrlon.size)
full_subset[grid.subset] = 1
new_full_subset = full_subset[cell_sort]
new_subset = np.where(new_full_subset == 1)[0]
return CellGrid(new_arrlon, new_arrlat, new_arrcell,
gpis=new_gpis,
subset=new_subset)
20 changes: 20 additions & 0 deletions tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,5 +533,25 @@ def test_genreggrid():
assert lat == 88.5


def test_reorder_to_cellsize():
"""
Test reordering to different cellsize
"""
lons = np.array([-177, -177, -176, -176])
lats = np.array([51, 57, 51, 57])
gpis = np.array([1, 2, 3, 4])
cells = np.array([14, 14, 14, 14])
orig_grid = grids.CellGrid(lons, lats, cells, gpis=gpis)
reordered_grid = grids.reorder_to_cellsize(orig_grid, 5.0, 5.0)
nptest.assert_almost_equal(reordered_grid.gpis,
np.array([1, 3, 2, 4]))
nptest.assert_almost_equal(reordered_grid.arrlon,
np.array([-177, -176, -177, -176]))
nptest.assert_almost_equal(reordered_grid.arrlat,
np.array([51, 51, 57, 57]))
nptest.assert_almost_equal(reordered_grid.arrcell,
np.array([14, 14, 14, 14]))


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

0 comments on commit 7de1428

Please sign in to comment.