comparison globalneighbors/determine_distances.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
comparison
equal deleted inserted replaced
-1:000000000000 0:5dba84370182
1 #!/usr/bin/env python
2
3 """
4 determine distances from an output of neighbors
5
6 This takes a JSON input file of the format of
7 {"geoid": [neighborgeoid, neighborgeoid2, ...]}
8
9 and reduces this to a mapping of
10
11 {"geoid": [(geoid_remote, distance),
12 (geoid_remote2, distance2),
13 ...]
14 ...
15 }
16 """
17
18 import argparse
19 import json
20 import sys
21 from .cli import CitiesParser
22 from .constants import Rearth
23
24 try:
25 # python 2
26 string = (str, unicode)
27 except NameError:
28 # python 3
29 string = (str, )
30
31
32 def geoids2distances(f, locations, r=Rearth):
33 """convert geoidstodistance with locations mapping"""
34
35 # allow file paths or pointers
36 if isinstance(f, string):
37 with open(f) as f_:
38 return geoids2distances(f_, locations)
39
40 # load the data
41 data = json.loads(f.read())
42
43
44 def main(args=sys.argv[1:]):
45 """CLI"""
46
47 # parse command line
48 parser = CitiesParser(description=__doc__)
49 parser.add_argument('neighbors',
50 type=argparse.FileType('r'),
51 help="file of nearest neighrbors")
52 options = parser.parse_args(args)
53
54 # read data
55 cities = list(read_cities(options.cities, fields=fields))
56
57 # get locations
58 city_locations = locations(cities)
59
60
61 if __name__ == '__main__':
62 main