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
PM25 = LAQN_mtx['PM25'][list(weight[list(LAQN_mtx['PM25'].columns)].index)] PM25 = PM25.ffill().bfill() O3 = LAQN_mtx['O3'][list(weight[list(LAQN_mtx['O3'].columns)].index)] O3 = O3.ffill().bfill() #the AQ vector for my post code myNO2 = (NO2 * NO2_weight).sum(axis = 1) myNO2.plot() #set the decay parameters for the exp moving avgs nslow = 6 nfast = 3 #get the MACD and exp moving avgs Postcode_emafast,Postcode_emaslow,Postcode_macd = opensensor.getMovingAverageConvergence(myNO2, nslow=nslow, nfast=nfast) #plot results fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True) ax1.fill_between(np.asarray(list(range(len(NO2)))), Postcode_emafast, scipy.zeros(len(NO2)), where=Postcode_emafast>=Postcode_emaslow, facecolor='red', interpolate=True) ax1.fill_between(np.asarray(list(range(len(NO2)))), Postcode_emafast, scipy.zeros(len(NO2)), where=Postcode_emafast<=Postcode_emaslow, facecolor='green', interpolate=True) ax1.set_ylabel('NO2') ax2.fill_between(np.asarray(list(range(len(NO2)))), Postcode_emafast, Postcode_emaslow, where=Postcode_emafast>=Postcode_emaslow, facecolor='red', interpolate=True) ax2.fill_between(np.asarray(list(range(len(NO2)))), Postcode_emafast, Postcode_emaslow, where=Postcode_emafast<=Postcode_emaslow, facecolor='green', interpolate=True) ax2.set_ylabel('NO2') plt.show() """