Skip to content

Commit

Permalink
Remove clipping from lonlat2cell (#77)
Browse files Browse the repository at this point in the history
When using the current implementation of `lonlat2cell` with cellsize < 1, the clipping leads to very large cells. For example, with `cellsize=0.5` all points with lat>0 and lon>0 will be mapped to the same cell, because in this case `(lat+90)/cellsize = (lat+90)*2 > 180` and similarly `(lon+180)/cellsize = (lon+180)*2 > 360`.
When using cellsize >= 1, clipping is only necessary if latitude and longitude are not in (-90, 90) and (-180, 180), respectively, but this is already enforced in the BasicGrid init, so it shouldn't be necessary here.
  • Loading branch information
s-scherrer authored Nov 11, 2022
1 parent d54ea73 commit 9e4ccfb
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/pygeogrids/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -1224,12 +1224,8 @@ def lonlat2cell(lon, lat, cellsize=5.0, cellsize_lon=None, cellsize_lat=None):
cellsize_lon = cellsize
if cellsize_lat is None:
cellsize_lat = cellsize
y = np.clip(
np.floor((np.double(lat) + (np.double(90.0) + 1e-9)) / cellsize_lat), 0, 180
)
x = np.clip(
np.floor((np.double(lon) + (np.double(180.0) + 1e-9)) / cellsize_lon), 0, 360
)
y = np.floor((np.double(lat) + (np.double(90.0) + 1e-9)) / cellsize_lat)
x = np.floor((np.double(lon) + (np.double(180.0) + 1e-9)) / cellsize_lon)
cells = np.int32(x * (np.double(180.0) / cellsize_lat) + y)

max_cells = (np.double(180.0) / cellsize_lat) * (np.double(360.0)) / cellsize_lon
Expand Down

0 comments on commit 9e4ccfb

Please sign in to comment.