Exemplo n.º 1
0
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
Exemplo n.º 2
0
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)

#get bike data
bikes_mtx, bikes_metadata = opensensor.getTFLBikes((datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y%m%d"),end_date)


coverage_day = LAQN_mtx['NO2'].transpose().apply(np.isnan).sum()/LAQN_mtx['NO2'].transpose().apply(np.isnan).count() 
NO2 = LAQN_mtx['NO2'][coverage_day < 0.2]

coverage_sensor = NO2.apply(np.isnan).sum()/NO2.apply(np.isnan).count() 

NO2 = NO2.transpose()[coverage_sensor < 0.2].transpose()

mean_daily_variance = NO2.groupby(lambda x: (x.date)).std()

for row in mean_daily_variance.index: