comparison globalneighbors/distance.py @ 22:e69cb496324e

we have a data dump
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Jun 2017 17:45:19 -0700
parents 811adc9736eb
children
comparison
equal deleted inserted replaced
21:22c384fe954d 22:e69cb496324e
156 k) 156 k)
157 157
158 return neighbors 158 return neighbors
159 159
160 160
161 def write_neighbors(fp, neighbors):
162 for key, value in neighbors.iteritems():
163 fp.write("{key} {value}\n".format(key=key,
164 value=json.dumps(value)))
165
161 def main(args=sys.argv[1:]): 166 def main(args=sys.argv[1:]):
162 """CLI""" 167 """CLI"""
163 168
164 # parse command line arguments 169 # parse command line arguments
165 description = """write nearest neighborfiles""" 170 description = """write nearest neighborfiles"""
177 parser.add_argument('-k', dest='k', 182 parser.add_argument('-k', dest='k',
178 type=int, default=50, 183 type=int, default=50,
179 help="number of neighbors to determine [DEFAULT: %(default)s]") 184 help="number of neighbors to determine [DEFAULT: %(default)s]")
180 options = parser.parse_args(args) 185 options = parser.parse_args(args)
181 186
182 # parse cities
183 cities = list(read_cities(options.cities, fields=fields))
184
185 # get locations 187 # get locations
186 city_locations = locations(cities) 188 city_locations = locations(read_cities(options.cities, fields=fields))
189 options.cities.close()
190 options.output.close()
191
187 192
188 # calculate neighbors 193 # calculate neighbors
189 neighbors = calculate_neighbors(city_locations, 194 neighbors = calculate_neighbors(city_locations,
190 k=options.k, 195 k=options.k,
191 lat_tol=options.latlon[0], 196 lat_tol=options.latlon[0],
193 output=options.output_counter) 198 output=options.output_counter)
194 199
195 # output 200 # output
196 print ("Outputting neighbors") 201 print ("Outputting neighbors")
197 sys.stdout.flush() 202 sys.stdout.flush()
198 options.output.write(json.dumps(neighbors)) 203 import pdb; pdb.set_trace()
204 with open(options.output.name, 'w') as f:
205 f.write(json.dumps(neighbors))
199 206
200 if __name__ == '__main__': 207 if __name__ == '__main__':
201 main() 208 main()