Mercurial > hg > GlobalNeighbors
diff globalneighbors/grid.py @ 0:5dba84370182
initial commit; half-working prototype
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 24 Jun 2017 12:03:39 -0700 |
parents | |
children | 1b94f3bf97e5 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/globalneighbors/grid.py Sat Jun 24 12:03:39 2017 -0700 @@ -0,0 +1,37 @@ +import math + +class LatLonGrid(object): + + lat_range = (-90., 90) + lon_range = (-180., 180) # however, this wraps around + + def __init__(self, nlat, nlon): + self.n = (nlat, nlon) + self.range = (self.lat_range, self.lon_range) + self.d = ((self.lat_range[-1] - self.lat_range[0])/self.n[0], + (self.lon_range[-1] - self.lon_range[0])/self.n[1]) + self.create_grid() + + def create_grid(self): + self.grid = [[] for _ in xrange(self.n[-1]) + for _ in xrange(self.n[0])] + + def add(self, geoid, lat, lon): + latlon = (lat, lon) + self[self.index(lat, lon)].append(geoid) + + def __getitem__(self, index): + """ + index -- 2-tuple or list of i and j indices + """ + import pdb; pdb.set_trace() + return self.grid[index[0]][index[1]] + + def index(self, lat, lon): + return [int(math.floor((val-self.range[i][0])/self.d[i])) + for i, val in enumerate((lat, lon))] + + def neighbors(self, i, j): + """ + return neighbors of points i, j + """