Mercurial > hg > GlobalNeighbors
annotate globalneighbors/grid.py @ 16:4583d0d9331a
stub command line entry for gridding neighbors
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 25 Jun 2017 15:03:54 -0700 |
parents | 21095c9006e5 |
children | 2fef925fbf37 |
rev | line source |
---|---|
16
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
1 #!/usr/bin/env python |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
2 |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
3 """ |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
4 grid locations, for speed and fidelity |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
5 """ |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
6 |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 import math |
16
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
8 import sys |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
9 from .cli import CitiesParser |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
10 from .locations import locations |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 class LatLonGrid(object): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 lat_range = (-90., 90) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 lon_range = (-180., 180) # however, this wraps around |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 def __init__(self, nlat, nlon): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 self.n = (nlat, nlon) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 self.range = (self.lat_range, self.lon_range) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 self.d = ((self.lat_range[-1] - self.lat_range[0])/self.n[0], |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 (self.lon_range[-1] - self.lon_range[0])/self.n[1]) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 self.create_grid() |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 def create_grid(self): |
1 | 25 self.grid = [] |
26 for _ in xrange(self.n[0]): | |
27 self.grid.append([set() for _ in xrange(self.n[-1])]) | |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 def add(self, geoid, lat, lon): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 latlon = (lat, lon) |
1 | 31 self[self.index(lat, lon)].add(geoid) |
0
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 def __getitem__(self, index): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 """ |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
35 index -- 2-tuple or list of i and j indices |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
36 """ |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
37 return self.grid[index[0]][index[1]] |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
38 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
39 def index(self, lat, lon): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
40 return [int(math.floor((val-self.range[i][0])/self.d[i])) |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
41 for i, val in enumerate((lat, lon))] |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
42 |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
43 def neighbors(self, i, j): |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
44 """ |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
45 return neighbors of points i, j |
5dba84370182
initial commit; half-working prototype
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
46 """ |
7
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
47 imat = [i] |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
48 jmat = [j] |
5
7e27e874655b
test a larger grid + move distance insertion to its own function
Jeff Hammel <k0scist@gmail.com>
parents:
1
diff
changeset
|
49 |
7e27e874655b
test a larger grid + move distance insertion to its own function
Jeff Hammel <k0scist@gmail.com>
parents:
1
diff
changeset
|
50 # latitude |
1 | 51 if i: |
5
7e27e874655b
test a larger grid + move distance insertion to its own function
Jeff Hammel <k0scist@gmail.com>
parents:
1
diff
changeset
|
52 imat.append(i-1) |
7
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
53 if i < self.n[0]-1: |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
54 imat.append(i+1) |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
55 |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
56 # longitude: wraps around |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
57 if j: |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
58 jmat.append(j-1) |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
59 else: |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
60 jmat.append(self.n[-1] - 1) |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
61 if j == self.n[-1] - 1: |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
62 jmat.append(0) |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
63 else: |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
64 jmat.append(j+1) |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
65 |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
66 return [(_i, _j) for _i in imat for _j in jmat |
254195d0bac2
partial implementation of autocomplete using jqueryui; easyautocomplete.com may be more what we want
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
67 if (_i,_j) != (i,j)] |
9
638fad06e556
use bisect function; it has been tested faster
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
68 |
638fad06e556
use bisect function; it has been tested faster
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
69 def neighbor_points(self, i, j): |
638fad06e556
use bisect function; it has been tested faster
Jeff Hammel <k0scist@gmail.com>
parents:
7
diff
changeset
|
70 """return all points in a lat-lon box""" |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
9
diff
changeset
|
71 geoids = set() |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
9
diff
changeset
|
72 geoids.update(self[(i,j)]) |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
9
diff
changeset
|
73 for ii, jj in self.neighbors(i,j): |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
9
diff
changeset
|
74 geoids.update(self[ii,jj]) |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
9
diff
changeset
|
75 return geoids |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
9
diff
changeset
|
76 |
11 | 77 |
10
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
9
diff
changeset
|
78 class GriddedLocations(object): |
21ed15391e8a
add a placeholder view for a city based on geoid
Jeff Hammel <k0scist@gmail.com>
parents:
9
diff
changeset
|
79 |
11 | 80 def __init__(self, locations): |
81 raise NotImplementedError('TODO') | |
15
21095c9006e5
city page is now functional + linky
Jeff Hammel <k0scist@gmail.com>
parents:
11
diff
changeset
|
82 |
16
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
83 def main(args=sys.argv[1:]): |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
84 """CLI""" |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
85 |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
86 # parse command line |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
87 parser = CitiesParser(description=__doc__) |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
88 options = parser.parse_args(args) |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
89 |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
90 # read locations |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
91 city_locations = locations(parser.read_cities()) |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
92 |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
93 if __name__ == '__main__': |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
94 main() |
4583d0d9331a
stub command line entry for gridding neighbors
Jeff Hammel <k0scist@gmail.com>
parents:
15
diff
changeset
|
95 |