diff --git a/CHANGES.rst b/CHANGES.rst index ca35068..1279e40 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 ====== diff --git a/pygeogrids/__init__.py b/pygeogrids/__init__.py index 219e4dc..f05ff0c 100644 --- a/pygeogrids/__init__.py +++ b/pygeogrids/__init__.py @@ -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: diff --git a/pygeogrids/grids.py b/pygeogrids/grids.py index 6b0153d..a80cbe0 100755 --- a/pygeogrids/grids.py +++ b/pygeogrids/grids.py @@ -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) diff --git a/tests/test_grid.py b/tests/test_grid.py index 7b3220d..e518a6b 100755 --- a/tests/test_grid.py +++ b/tests/test_grid.py @@ -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()