comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:5dba84370182
1 import math
2
3 class LatLonGrid(object):
4
5 lat_range = (-90., 90)
6 lon_range = (-180., 180) # however, this wraps around
7
8 def __init__(self, nlat, nlon):
9 self.n = (nlat, nlon)
10 self.range = (self.lat_range, self.lon_range)
11 self.d = ((self.lat_range[-1] - self.lat_range[0])/self.n[0],
12 (self.lon_range[-1] - self.lon_range[0])/self.n[1])
13 self.create_grid()
14
15 def create_grid(self):
16 self.grid = [[] for _ in xrange(self.n[-1])
17 for _ in xrange(self.n[0])]
18
19 def add(self, geoid, lat, lon):
20 latlon = (lat, lon)
21 self[self.index(lat, lon)].append(geoid)
22
23 def __getitem__(self, index):
24 """
25 index -- 2-tuple or list of i and j indices
26 """
27 import pdb; pdb.set_trace()
28 return self.grid[index[0]][index[1]]
29
30 def index(self, lat, lon):
31 return [int(math.floor((val-self.range[i][0])/self.d[i]))
32 for i, val in enumerate((lat, lon))]
33
34 def neighbors(self, i, j):
35 """
36 return neighbors of points i, j
37 """