def get_barometer(API_KEY,myPostCode,sensor_locations): opensensor = London(API_KEY) weight = opensensor.get_weights(myPostCode,sensor_locations) #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(hours=-8)).strftime("%Y-%m-%dT%H:00:00.000Z") #the last date defaults to today end_date = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d") NO2 = pandas.DataFrame() for row in weight.index: mtx = opensensor.getAirQuality(start=start_date,end=end_date,location = row).get('NO2') if mtx is not None: NO2[row] = mtx[row] #grab the NO2 data where we bhave the intersect of weights and NO2 weight = weight[list(NO2.columns)]/weight[list(NO2.columns)].sum() #sort the matrices NO2 = NO2.reindex_axis(sorted(NO2.columns), axis=1) weight = weight.reindex(sorted(weight.index)) #ffill and bfill NA's NO2 = NO2.ffill().bfill() #the AQ vector for my post code myAQ = (NO2 * weight).sum(axis = 1) #set the decay parameters for the exp moving avgs nslow = 6 nfast = 3 #get the MACD and exp moving avgs NO2_emafast,NO2_emaslow,NO2_macd = opensensor.getMovingAverageConvergence(myAQ, nslow=nslow, nfast=nfast) #make into 1/0 signal and get the last reading signal = (-NO2_macd/abs(NO2_macd))[len(NO2_macd) - 1] return signal
#how many days we want to look back over time lookback = 20 #the file that holds all the meta data for LAQN sensors LAQN_file = '/home/troups/osio/AQ/Data/LAQN.csv' Barometer_file = 'Barometer.csv' #API Key comes from your account on the opensensors platform API_KEY = '' #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(days=-lookback)).strftime("%Y%m%d") #the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #create instance of Air Quality Object opensensor = London(API_KEY) #Get the meta data not on opensensors at the moment with open(LAQN_file) as f: reader = csv.reader(f) LAQN_locations = dict((rows[0],[float(rows[1]),float(rows[2])]) for rows in reader) def moving_average_convergence(x, nslow=8, nfast=4): """ wrap the method in the London object so we can use apply """ NO2_emafast,NO2_emaslow,NO2_macd = opensensor.getMovingAverageConvergence(x, nslow, nfast) return pandas.Series(NO2_macd) #get the list of LAQN measures LAQN_mtx = opensensor.getAirQuality(start=start_date,end=end_date)
import pandas import numpy as np import datetime import matplotlib.pyplot as plt import scipy lookback = 2 #API Key comes from your account on the opensensors platform API_KEY = '' #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(days=-lookback)).strftime("%Y%m%d") #the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #create instance of Air Quality Object opensensor = London(API_KEY) #get the tube data mtx = opensensor.getTFLTube(start_date,end_date) #add the index as a column so we can pivot mtx['datetime'] = mtx.index #pivot the data mtx = mtx.pivot(index='datetime', columns='name', values='severity') #reset the index mtx.set_index(pandas.DatetimeIndex(mtx.index),inplace=True) #make sure everything is numeric mtx = mtx.convert_objects(convert_numeric=True)
from London import * import datetime #API Key comes from your account on the opensensors platform API_KEY = '' #the days to look back lookback = 2 #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(days=-lookback)).strftime("%Y%m%d") #the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #create instance of Air Quality Object opensensor = London(API_KEY) AQ_mtx = opensensor.getAirQuality(start=start_date,end=end_date) Bike_mtx, Bike_meta_data = opensensor.getTFLBikes(start_date,end_date) Roads_mtx, Roads_meta_data = opensensor.getTFLRoads(start_date,end_date) Tubes_mtx = opensensor.getTFLTube(start_date,end_date)
from London import * import datetime #API Key comes from your account on the opensensors platform API_KEY = '36a1dd80-b5f4-48ce-8dce-50b50905f0fd' #the days to look back lookback = 20 #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(days=-lookback)).strftime("%Y%m%d") #the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #create instance of Air Quality Object opensensor = London(API_KEY) Bike_mtx, Bike_meta_data = opensensor.getTFLBikes(start_date,end_date) Tubes_mtx = opensensor.getTFLTube(start_date,end_date) Biketopics = json_normalize(opensensor.getTopicsForOrg('Tfl')) Biketopics = Biketopics[Biketopics['topic'].str.contains("/orgs/tfl/bikes/")]
from London import * import pandas import datetime lookback = 2 #API Key comes from your account on the opensensors platform API_KEY = '' #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(days=-lookback)).strftime("%Y%m%d") #the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #create instance of Air Quality Object opensensor = London(API_KEY) roads_mtx_raw, roads_meta_data = opensensor.getTFLRoads(start_date,end_date) roads_mtx = roads_mtx_raw roads_mtx['datetime'] = roads_mtx.index roads_mtx['severity'][roads_mtx['severity'] == 'Closure'] = 3 roads_mtx['severity'][roads_mtx['severity'] == 'Severe'] = 2 roads_mtx['severity'][roads_mtx['severity'] == 'Serious'] = 1 roads_mtx['severity'][roads_mtx['severity'] == 'Good'] = 0 roads_mtx = roads_mtx.convert_objects(convert_numeric=True) roads_mtx = roads_mtx[~roads_mtx.duplicated(['name', 'datetime', 'severity'])]
LAQN_file = '\Data\LAQN.csv' #API Key comes from your account on the opensensors platform API_KEY = '' #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(days=-lookback)).strftime("%Y%m%d") #the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #Get the meta data not on opensensors at the moment with open(LAQN_file) as f: reader = csv.reader(f) LAQN_locations = dict((rows[0],[float(rows[1]),float(rows[2])]) for rows in reader) #create instance of Air Quality Object opensensor = London(API_KEY) #set my postcode myPostCode = 'E1W 2PA' #get the geo data for my postcode geo_meta_data=opensensor.getPostcode(myPostCode) #get the distance from all the LAQN sensors from my postcode distance = pandas.Series(dict((k, opensensor.getDistance(v,[geo_meta_data['geo']['lng'], geo_meta_data['geo']['lat']])) for k, v in LAQN_locations.items())) #work out the weight for each LAQN sensor weight = np.exp(-2*distance)/sum(np.exp(-2*distance)) distance = pandas.concat([distance, weight], axis=1) distance.columns = ['distance','weight'] #get the list of LAQN measures LAQN_mtx = opensensor.getAirQuality(start=start_date,end=end_date)
from London import * import pandas import datetime lookback = 2 #API Key comes from your account on the opensensors platform API_KEY = '' #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(days=-lookback)).strftime("%Y%m%d") #the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #create instance of Air Quality Object opensensor = London(API_KEY) #initiated the list of days for which well have a list of measures BikeData = pandas.DataFrame(columns = ['name', 'level_1', 'value']) BikeMetaData = {} todays_matrix, todays_metadata = opensensor.getTFLBikes(start_date,end_date) #lose dupes todays_matrix = todays_matrix[~todays_matrix.duplicated(['name', 'level_1', 'value'])] #lose NA todays_matrix = todays_matrix.dropna(subset=['name', 'level_1', 'value'], how='any') #reset the index todays_matrix = todays_matrix.reset_index(drop=True) #pivot into all the locations bike_mtx = todays_matrix.pivot(index='level_1', columns='name', values='value')
How Use the London city class Get the data from Opensensors.io API Plot """ #API Key comes from your account on the opensensors platform API_KEY = '' #the first date we try and get data start_date = (datetime.datetime.now() + datetime.timedelta(days=-lookback)).strftime("%Y%m%d") #the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #create instance of Air Quality Object opensensor = London(API_KEY) #get the raw roads data roads_mtx_raw, roads_meta_data = opensensor.getTFLRoads(start_date,end_date) roads_mtx = roads_mtx_raw #Add a datatime column needed to pivot roads_mtx['datetime'] = roads_mtx.index #Change text columns to numeric grade roads_mtx['severity'][roads_mtx['severity'] == 'Closure'] = 3 roads_mtx['severity'][roads_mtx['severity'] == 'Severe'] = 2 roads_mtx['severity'][roads_mtx['severity'] == 'Serious'] = 1 roads_mtx['severity'][roads_mtx['severity'] == 'Good'] = 0
#make into 1/0 signal and get the last reading signal = (-NO2_macd/abs(NO2_macd))[len(NO2_macd) - 1] return signal #Get the meta data not on opensensors at the moment #this is the list of locations for the London Air Quality Network with open(LAQN_file) as f: reader = csv.reader(f) LAQN_locations = dict((rows[0],[float(rows[1]),float(rows[2])]) for rows in reader) #Create a dictionary to put the barometer readings into barometer = {} opensensor = London(API_KEY) myPostCode = 'E14 8EJ' #get the index of locations that are important for our post code weights = opensensor.getWeights(myPostCode,LAQN_locations).index #set the org since we need to get all mesages (easier than creating more than one SSE stream) org ='london-air-quality-network' #make sure the messages stream is dead messages = None #set the SSE stream in motion messages = SSEClient('https://realtime.opensensors.io/v1/public/events/orgs/'+org+'/topics?api-key='+ API_KEY) for msg in messages: #get the data for the message outputMsg = msg.data #sometimes we seem to get empty messages to check there is indeed something there