import pandas as pd, json, numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
flights=json.loads(file('flights_ro.json','r').read())
locations=json.loads(file('locations_ro.json','r').read())
citysave_dest=json.loads(file('citysave_ro_dest.json','r').read())
citysave_arrv=json.loads(file('citysave_ro_arrv.json','r').read())
#example output format
data1a=json.loads(file('data1a.json','r').read())
data2a=json.loads(file('data2a.json','r').read())
cc={
'BCM':u'Bacau',
'SUJ':u'Satu Mare',
'CND':u'Contanta',
'CRA':u'Craiova',
'TSR':u'Timisoara',
'IAS':u'Iasi',
'CLJ':u'Cluj-Napoca',
'SBZ':u'Sibiu',
'OMR':u'Oradea',
'OTP':u'Bucharest',
'SCV':u'Suceava',
'TGM':u'Targu Mures'
}
file("cities_ro.json",'w').write(json.dumps(cc))
citysave={}
for i in list(citysave_dest)+list(citysave_arrv):
if i in citysave_dest:
citysave[i]=citysave_dest[i]
else:
citysave[i]=citysave_arrv[i]
newdata={}
apconv={}
for g in citysave:
k=g+'('+str(citysave[g]['coords'][0])+', '+str(citysave[g]['coords'][1])+')'
apconv[g]=k
if k not in newdata: newdata[k]={}
newdata[k]['coords']=citysave[g]['coords']
newdata[k]['country']=citysave[g]['country']
newdata[k]['count']=0
for c in flights:
for airport in flights[c]:
k=apconv[airport]
newdata[k]['count']+=flights[c][airport]['7freq']
if c not in newdata[k]:
newdata[k][c]={"count":0}
newdata[k][c]['count']+=flights[c][airport]['7freq']
newdata[k][c]['airports']=flights[c][airport]['airports']
#clean up
for i in list(newdata.keys()):
if newdata[i]['count']==0: newdata.pop(i);
F=[]
for j in newdata:
for i in newdata[j]:
if i not in {'country','count','coords'}:
for k in newdata[j][i]['airports']:
for m in newdata[j][i]['airports'][k]['airlines']:
if 'cargo' not in m.lower():
F.append({'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'weekly flights':np.round(newdata[j][i]['airports'][k]['airlines'][m]['7freq'],1)})
#tests
F.append({'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':u'Total',u'weekly flights':0})
F.append({'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'ap':u'Total','al':m,u'weekly flights':0})
F.append({'to':j[:j.find('(')],'from':cc[i],'country':u'Total','ap':j[:j.find('(')]+' '+str(k),'al':m,u'weekly flights':0})
F.append({'to':j[:j.find('(')],'from':u'Total','country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'weekly flights':0})
F.append({'to':u'Total','from':cc[i],'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'weekly flights':0})
file("f_ro.json",'w').write(json.dumps(F))
F=[]
typ=u'Romania flights'
for j in newdata:
for i in newdata[j]:
if i not in {'country','count','coords'}:
for k in newdata[j][i]['airports']:
for m in newdata[j][i]['airports'][k]['airlines']:
if 'cargo' not in m.lower():
F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'weekly flights':np.round(newdata[j][i]['airports'][k]['airlines'][m]['7freq'],1)})
#tests
F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':u'Total',u'weekly flights':0})
F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'country':newdata[j]['country'],'ap':u'Total','al':m,u'weekly flights':0})
F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'country':u'Total','ap':j[:j.find('(')]+' '+str(k),'al':m,u'weekly flights':0})
F.append({'type':typ,'to':j[:j.find('(')],'from':u'Total','country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'weekly flights':0})
F.append({'type':typ,'to':u'Total','from':cc[i],'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'weekly flights':0})
file("g_ro.json",'w').write(json.dumps(F))
s=0
for j in ["CND","TGM","BCM","CRA"]:
newdata[cc[j]+'('+str(locations[j][0])+', '+str(locations[j][1])+')']={j:{'airports': {j: {u'7freq': s,
u'airlines': {u'Airport': {u'7freq': s}}}},
'count': s},
'coords': locations[j],
'count': s,
'country': u'Romania'}
file("newdata1a_ro.json",'w').write(json.dumps(newdata))
data2a
gountrygeo=json.load(file('gountrygeo.json','r'))
gountrygeo["Canada"]=[61, -29]
gountrygeo["United States"]=[57, -29]
gountrygeo["Romania"]=[46.052612, 24.954499]
gountrygeo["Other"]=[58, 44]
gountrygeo["None"]=[0, 0]
gountrygeo[None]=[0, 0]
from pygeocoder import Geocoder
apik='AIzaSyDybC2OroTE_XDJTuxjKruxFpby5VDhEGk'
for c in list(set(citysave[g]['country'] for g in citysave)):
if c not in gountrygeo:
print c
gountrygeo[c]=Geocoder(apik).geocode(c)[0].coordinates
file("gountrygeo.json",'w').write(json.dumps(gountrygeo))
newdatar={}
for g in citysave:
k=citysave[g]['country']
if k not in newdatar: newdatar[k]={}
newdatar[k]['coords']=gountrygeo[k]
newdatar[k]['count']=0
for c in flights:
for airport in flights[c]:
k=citysave[airport]['country']
newdatar[k]['count']+=flights[c][airport]['7freq']
if c not in newdatar[k]:
newdatar[k][c]={"count":0,'cities':{}}
newdatar[k][c]['count']+=flights[c][airport]['7freq']
#if airport not in newdatar['cities'][k][c]['cities']:
newdatar[k][c]['cities'][airport]=flights[c][airport]
file("newdata2a_ro.json",'w').write(json.dumps(newdatar))
colors
order=['OTP','CLJ','TSR','IAS','SBZ','CRA','TGM','SCV','BCM','OMR','SUJ','CND']
ss=[
'#dd1c77',
'#f768a1',
'#fecc5c',
'#fd8d3c',
'#f03b20',
'#2c7fb8',
'#41b6c4',
'#a1dab4',
'#78c679',
'#31a354',
'#006837',
'#1c9099',
'#016c59',
'#993404']
file("colors_ro.json",'w').write(json.dumps({order[i]:ss[i] for i in range(len(order))}))
file("citycoords_ro.json",'w').write(json.dumps({i:locations[i] for i in locations if i in cc}))
file("cityorder_ro.json",'w').write(json.dumps(order))