def Producer(): moList = [] #build the list to be serialized for i in location: #iterate through all the locations city, postcode, lat, lon = club_location( i) # import location details from 'reference.py' x = M.nearest_loc_forecast( float(lat), float(lon), metoffer.THREE_HOURLY) #location specific, 3 hourly forecast y = metoffer.parse_val(x) myDict = y.data #data retrieved from MetOffice tempDict = {} for m in myDict: #iterate through all the days itemp = { m['timestamp'][0]: { 'temp': m['Temperature'][0], 'ftemp': m['Feels Like Temperature'][0], 'weathertype': weathertype[str(m['Weather Type'][0])], 'gwind': m['Wind Gust'][0], 'swind': m['Wind Speed'][0], 'precip_prob': m['Precipitation Probability'][0], 'rain': '', 'location': i } } tempDict.update(itemp) moList.append( tempDict) #list of dictionaries from a single API request return moList
def background(self): import metoffer api_key = "f2ef8ecf-175f-491d-aea1-93a38209bd55" M = metoffer.MetOffer(api_key) #x = M.nearest_loc_forecast(51.4033, -0.3375, metoffer.THREE_HOURLY) x = M.nearest_loc_forecast(51.5252257441084, -0.134831964969635, metoffer.DAILY) self.y = metoffer.parse_val(x) self.tagline = "Live from the Met Office"
def getTemperatures(): print "getTemperatures start" api_key = '35d53d57-9aa0-45de-9772-0871f0ceffda' M = metoffer.MetOffer(api_key) x = M.nearest_loc_forecast(52.2793,-1.5803, metoffer.THREE_HOURLY) y = metoffer.parse_val(x) print "data retrieved" for i in y.data: print("{} - {}".format(i["timestamp"][0].strftime("%d %b, %H:%M"), i["Temperature"][0])) print "getTemperatures end" return y.data
def met(): #retrieve 5-day information key = '568fbf63-75c5-451e-a282-1388c877e60b' try: M = metoffer.MetOffer(key) x = M.nearest_loc_forecast(53.4840, -2.2440, metoffer.THREE_HOURLY) y = metoffer.parse_val(x) weatherData = [y.name] #weather forecast data for i in y.data: a = ("{}-{}".format(i["timestamp"][0].strftime("%D %b, %H:%M"), metoffer.WEATHER_CODES[i["Weather Type"][0]]) + " " + str(i["Temperature"][0])) weatherData.append(a) return weatherData except Exception, e: return 0
def checkWeather(): global weatherType global weatherTemp global weatherPrecip global weatherHumidity print "Checking The Weather" locationForecast = M.nearest_loc_forecast(51.4033, -0.3375, metoffer.THREE_HOURLY) parsed = metoffer.parse_val(locationForecast) timeNow = time.strftime("%H") dateNow = time.strftime("%d %b") for i in parsed.data: #if timeNow == i["timestamp"][0].strftime("%d %b %H"): # print "GOT IT" #print i["timestamp"][0].strftime("%d %b %H") if dateNow == i["timestamp"][0].strftime("%d %b"): plusOne = int(timeNow) -1 plusTwo = int(timeNow) -2 if timeNow == i["timestamp"][0].strftime("%H"): print "the hour is the same on this day, so happy!" print i["timestamp"][0].strftime("%d %b %H:%M") weatherType = i["Weather Type"][0] weatherTemp = i["Temperature"][0] weatherPrecip = i["Precipitation Probability"][0] weatherHumidity = i["Screen Relative Humidity"][0] elif str(plusOne) == i["timestamp"][0].strftime("%H"): print "the hour is +1 on the same day, Happy this is the right one" print i["timestamp"][0].strftime("%d %b %H:%M") weatherType = i["Weather Type"][0] weatherTemp = i["Temperature"][0] weatherPrecip = i["Precipitation Probability"][0] weatherHumidity = i["Screen Relative Humidity"][0] elif str(plusTwo) == i["timestamp"][0].strftime("%H"): print "the hour is +2 on the same day, Happy this is the right one" print i["timestamp"][0].strftime("%d %b %H:%M") weatherType = i["Weather Type"][0] weatherTemp = i["Temperature"][0] weatherPrecip = i["Precipitation Probability"][0] weatherHumidity = i["Screen Relative Humidity"][0]
def getforecast(self): # 354077 is ID for Wantage try: self.data = metoffer.parse_val(self.M.loc_forecast("354077", metoffer.THREE_HOURLY)).data except: print datetime.datetime.now().ctime() traceback.print_exc() self.temps = [t["Temperature"][0] for t in self.data] self.times = [(t["timestamp"][0] - datetime.datetime.now()).total_seconds() for t in self.data] self.types = [t["Weather Type"][0] for t in self.data] # 0 clear night, 1 sunny day, 3 partially cloudy day, 7 cloudy f = numpy.interp([3600 * 5, 3600 * 6], self.times, self.temps) self.futuretemp = f[0] self.futuregrad = f[1] - f[0] self.futuretype = self.types[(numpy.abs(numpy.array(self.times) - 3600 * 5)).argmin()] xiv.queue("future_temp", round(float(self.futuretemp), 2)) xiv.queue("future_grad", round(float(self.futuregrad), 2)) xiv.queue("future_type", self.futuretype)
def __init__( self, sLocation, dLatitude, dLongitude, sMetApiKey, sMeasurementType): # This is run when an onject is first created self.sLocation = sLocation self.dLatitude = dLatitude self.dLongitude = dLongitude self.sMetApiKey = sMetApiKey self.sMeasurementType = sMeasurementType oMet = metoffer.MetOffer(sMetApiKey) if self.sMeasurementType == 'nearest_loc_obs': self.dicWeatherData = oMet.nearest_loc_obs( self.dLatitude, self.dLongitude ) # This finds the nearest observation station of which there are not that many so your data may be inaccruate. if self.sMeasurementType == 'nearest_loc_forecast': self.dicWeatherData = oMet.nearest_loc_forecast( dLatitude, dLongitude, metoffer.THREE_HOURLY ) # This gives forecast data for the given area. self.oWeatherReport = metoffer.parse_val(self.dicWeatherData)
import metoffer api_key = '7fe18ea6-ca0d-4952-b166-aa0a0ae19ffb' M = metoffer.MetOffer(api_key) x = M.nearest_loc_forecast(51.215340, -0.602149, metoffer.THREE_HOURLY) y = metoffer.parse_val(x) z = y.data[0] print(z)
def getNearestLocationData(lat, lon, apikey): M = metoffer.MetOffer(apikey) data = M.nearest_loc_forecast(lat, lon, metoffer.THREE_HOURLY) data = metoffer.parse_val(data) return data