diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/globalneighbors/determine_distances.py	Sat Jun 24 12:03:39 2017 -0700
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+
+"""
+determine distances from an output of neighbors
+
+This takes a JSON input file of the format of
+{"geoid": [neighborgeoid, neighborgeoid2, ...]}
+
+and reduces this to a mapping of
+
+{"geoid": [(geoid_remote, distance),
+           (geoid_remote2, distance2),
+           ...]
+ ...
+}
+"""
+
+import argparse
+import json
+import sys
+from .cli import CitiesParser
+from .constants import Rearth
+
+try:
+    # python 2
+    string = (str, unicode)
+except NameError:
+    # python 3
+    string = (str, )
+
+
+def geoids2distances(f, locations, r=Rearth):
+    """convert geoidstodistance with locations mapping"""
+
+    # allow file paths or pointers
+    if isinstance(f, string):
+        with open(f) as f_:
+            return geoids2distances(f_, locations)
+
+    # load the data
+    data = json.loads(f.read())
+
+
+def main(args=sys.argv[1:]):
+    """CLI"""
+
+    # parse command line
+    parser = CitiesParser(description=__doc__)
+    parser.add_argument('neighbors',
+                        type=argparse.FileType('r'),
+                        help="file of nearest neighrbors")
+    options = parser.parse_args(args)
+
+    # read data
+    cities = list(read_cities(options.cities, fields=fields))
+
+    # get locations
+    city_locations = locations(cities)
+
+
+if __name__ == '__main__':
+    main