In [30]:
import pandas as pd, json, numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [31]:
flights=json.loads(file('flights_hu.json','r').read())
locations=json.loads(file('locations_hu.json','r').read())
citysave_dest=json.loads(file('citysave_hu_dest.json','r').read())
citysave_arrv=json.loads(file('citysave_hu_arrv.json','r').read())
#example output format
data1a=json.loads(file('data1a.json','r').read())
data2a=json.loads(file('data2a.json','r').read())

In [32]:
cc={
'DEB':u'Debrecen',
'BUD':u'Budapest'
}

In [33]:
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]

In [34]:
citysave['Cluj-Napoca']['coords']=[46.5385862, 24.5514392]

In [35]:
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

In [36]:
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']

In [37]:
#clean up
for i in list(newdata.keys()):
    if newdata[i]['count']==0: newdata.pop(i);

In [38]:
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})

In [39]:
file("f_hu.json",'w').write(json.dumps(F))

In [40]:
F=[]
typ=u'Hungary 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})

In [41]:
file("g_hu.json",'w').write(json.dumps(F))

In [42]:
s=0
for j in ["DEB","BUD"]:
    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'Hungary'}

In [43]:
file("newdata1a_hu.json",'w').write(json.dumps(newdata))

data2a


In [44]:
gountrygeo=json.load(file('gountrygeo.json','r'))

In [45]:
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]

In [46]:
from pygeocoder import Geocoder
apik='AIzaSyDybC2OroTE_XDJTuxjKruxFpby5VDhEGk'

In [47]:
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

In [48]:
file("gountrygeo.json",'w').write(json.dumps(gountrygeo))

In [49]:
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

In [50]:
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]

In [51]:
file("newdata2a_hu.json",'w').write(json.dumps(newdatar))

colors


In [52]:
file("cities_hu.json",'w').write(json.dumps(cc))

In [53]:
order=['BUD','DEB']

In [54]:
ss={
'DEB':'#1c9099',
'BUD':'#fd8d3c'
}

In [55]:
file("colors_hu.json",'w').write(json.dumps(ss))

In [56]:
file("citycoords_hu.json",'w').write(json.dumps({i:locations[i] for i in locations if i in cc}))

In [57]:
file("cityorder_hu.json",'w').write(json.dumps(order))