view globalneighbors/determine_distances.py @ 6:316e1d54ffd4

move to jinja templates
author Jeff Hammel <k0scist@gmail.com>
date Sat, 24 Jun 2017 15:47:59 -0700
parents 5dba84370182
children
line wrap: on
line source

#!/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