In [3]:
import pandas as pd, json, numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [4]:
SC=json.loads(file('../json/SC2.json','r').read())
citysave=json.loads(file('../json/citysave4.json','r').read())
citycoords=json.loads(file('../json/citycoords.json','r').read())
unicities=json.loads(file('../json/unicities.json','r').read())
cc=json.loads(file('../json/cluster.json','r').read())
I3=json.loads(file('../json/I3.json','r').read())
In [9]:
error=[]
In [73]:
SC['World']=[[],[]]
I3['World']='WD'
In [77]:
for w in {'United Kingdom'}:#SC:
if len(SC[w])>0:
print w,
#read mdf data
cpath=I3[w].lower()
cpath='../countries/'+cpath
flights=json.loads(file(cpath+'/json/flights.json','r').read())
#create newdata
newdata={}
apconv={}
for g in citysave:
h=citysave[g]['city']
k=h+'('+str(citysave[g]['coords'][0])+', '+str(citysave[g]['coords'][1])+')'
apconv[h]=k
if k not in newdata: newdata[k]={}
newdata[k]['coords']=citysave[g]['coords']
newdata[k]['country']=citysave[g]['country']
newdata[k]['count']=0
#parse
for c in flights:
for airport in flights[c]:
if airport in cluster.values():
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']
else: error.append(airport)
#clean up
for i in list(newdata.keys()):
if newdata[i]['count']==0: newdata.pop(i);
file(cpath+'/json/newdata1a.json','w').write(json.dumps(newdata))
#E
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],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'Flights':np.round(newdata[j][i]['airports'][k]['airlines'][m]['7freq'],1)})
file(cpath+'/json/e.json','w').write(json.dumps(F))
#F
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],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'Flights':np.round(newdata[j][i]['airports'][k]['airlines'][m]['7freq'],1)})
#tests
F.append({'to':j[:j.find('(')],'from':cc[i],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':u'Total',u'Flights':0})
F.append({'to':j[:j.find('(')],'from':cc[i],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':u'Total','al':m,u'Flights':0})
F.append({'to':j[:j.find('(')],'from':cc[i],'fp':'from '+cc[i]+' '+i,'country':u'Total','ap':j[:j.find('(')]+' '+str(k),'al':m,u'Flights':0})
F.append({'to':j[:j.find('(')],'from':u'Total','country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'Flights':0})
F.append({'to':u'Total','from':cc[i],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'Flights':0})
file(cpath+'/json/f.json','w').write(json.dumps(F))
#G
F=[]
typ=w+' 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],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'Flights':np.round(newdata[j][i]['airports'][k]['airlines'][m]['7freq'],1)})
#tests
F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':u'Total',u'Flights':0})
F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':u'Total','al':m,u'Flights':0})
F.append({'type':typ,'to':j[:j.find('(')],'from':cc[i],'fp':'from '+cc[i]+' '+i,'country':u'Total','ap':j[:j.find('(')]+' '+str(k),'al':m,u'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'Flights':0})
F.append({'type':typ,'to':u'Total','from':cc[i],'fp':'from '+cc[i]+' '+i,'country':newdata[j]['country'],'ap':j[:j.find('(')]+' '+str(k),'al':m,u'Flights':0})
file(cpath+'/json/g.json','w').write(json.dumps(F))
#country
newdatar={}
for g in citysave:
k=citysave[g]['country']
if k not in newdatar: newdatar[k]={}
newdatar[k]['count']=0
for c in flights:
for airport in flights[c]:
if airport in unicities:
k=unicities[airport]
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(cpath+'/json/newdata2a.json','w').write(json.dumps(newdatar))
In [15]:
for w in SC:
if len(SC[w])>0:
print w,
#read mdf data
cpath=I3[w].lower()
cpath='../countries/'+cpath
order=SC[w]
file(cpath+'/json/cityorder.json','w').write(json.dumps(order))
file(cpath+'/json/cities.json','w').write(json.dumps({o:cc[o] for o in order}))
file(cpath+'/json/colors.json','w').write(json.dumps({o:'#fd8d3c' for o in order}))
In [64]:
co=[]
for i in np.sort(SC.keys()):
if len(SC[i])>0:
cpath=I3[i].lower()
co.append({i:cpath})
#co.append({cnc.loc['pretty'][I3[i]]:cpath})
In [ ]:
file('../json/iso2c.json','w').write(json.dumps(co))
In [69]:
co=[]
for i in np.sort(SC.keys()):
if len(SC[i])>0:
cpath=I3[i].lower()
co.append({cpath:i})
#co.append({cnc.loc['pretty'][I3[i]]:cpath})
In [70]:
file('../json/riso2c.json','w').write(json.dumps(co))
In [66]:
cnc_path='../../universal/countries/'
cnc=pd.read_csv(cnc_path+'cnc.csv',header=1).set_index('ISO2')
In [67]:
co={}
for i in np.sort(SC.keys()):
if len(SC[i])>0:
cpath=cnc.loc['pretty'][I3[i]]
co[i]={"name":cpath,"iso2":I3[i]}
In [68]:
file('../json/pretty.json','w').write(json.dumps(co))