Last active
April 16, 2017 01:52
-
-
Save chelsea1992/686f0d9a677d8c35f752a194ba5f42bd to your computer and use it in GitHub Desktop.
process the lax.json File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
import math | |
def convert_item(item): | |
year = str(item['year']).split('-')[0] | |
terminal = item['terminal'] | |
if terminal == 'Terminal 1': | |
terminal = 't1' | |
elif terminal == 'Terminal 2': | |
terminal = 't2' | |
elif terminal == 'Terminal 3': | |
terminal = 't3' | |
elif terminal == 'Terminal 4': | |
terminal = 't4' | |
elif terminal == 'Terminal 5': | |
terminal = 't5' | |
elif terminal == 'Terminal 6': | |
terminal = 't6' | |
elif terminal == 'Tom Bradley International Terminal': | |
terminal = 'tbi' | |
type = str(item['type']).lower() | |
count = int(item['count']) | |
return {'year': year, 'terminal': terminal, 'type': type, 'count': count} | |
if __name__ == "__main__": | |
json_file = sys.argv[1] | |
conditions = sys.argv[2] | |
conditions = conditions.lower() | |
arrival = False | |
departure = False | |
years = set() | |
terminals = set() | |
conditions = conditions.split(' ') | |
for condition in conditions: | |
if condition.startswith('t'): | |
terminals.add(condition) | |
elif condition == 'arrival': | |
arrival = True | |
elif condition == 'departure': | |
departure = True | |
else: | |
years.add(condition) | |
if not arrival and not departure: | |
arrival = True | |
departure = True | |
if len(years) == 0: | |
years = {'2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016'} | |
if len(terminals) == 0: | |
terminals = {'t1', 't2', 't3', 't4', 't5', 't6', 'tbi'} | |
query = { | |
'arrival': arrival, | |
'departure': departure, | |
'years': years, | |
'terminals': terminals | |
} | |
print(query) | |
json_data = json.load(open(json_file))['data'] | |
data = [] | |
for item in json_data: | |
projectedItem = {'year': item[9], 'terminal': item[10], 'type': item[11], 'count': item[13]} | |
convertedItem = convert_item(projectedItem) | |
if convertedItem['terminal'] in ['t1', 't2', 't3', 't4', 't5', 't6', 'tbi']: | |
data.append(convertedItem) | |
counts = [] | |
sum = 0 | |
for item in data: | |
type_flag = (item['type'] == 'arrival' and query['arrival']) or (item['type'] == 'departure' and query['departure']) | |
year_flag = item['year'] in query['years'] | |
terminal_flag = item['terminal'] in query['terminals'] | |
if type_flag and year_flag and terminal_flag: | |
counts.append(item['count']) | |
sum += item['count'] | |
counts = sorted(counts) | |
size = len(counts) | |
min = counts[0] | |
max = counts[size-1] | |
avg = sum*1.0 / size | |
sqrsum = 0 | |
for count in counts: | |
diff = avg - count | |
sqrsum += diff * diff | |
median = counts[size/2] | |
if size % 2 == 0: | |
median = (counts[size/2] + counts[size/2-1]) / 2.0 | |
standard_deviation = math.sqrt(sqrsum*1.0 / size) | |
print(str(min) + ',' + str(max) + ',' + '%.1f' % median + ',' | |
+ '%.2f' % avg + ',' + '%.2f' % standard_deviation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment