Private
Public Access
1
0
Files
rowsandall/rowers/weather.py~
sanderroosendaal 3eed3cc3e7 Uploading files
2016-10-30 17:39:32 +01:00

70 lines
1.8 KiB
Python

import requests
import json
import time
from datetime import datetime
from rowingdata import rowingdata
import pandas as pd
from rowers.models import Rower, Workout
from rowsandall_app.settings import FORECAST_IO_KEY
def get_weather_data(long,lat,unixtime):
url = "https://api.forecast.io/forecast/"+FORECAST_IO_KEY+"/"
url += str(long)+","+str(lat)+","+str(unixtime)
s = requests.get(url)
if s.ok:
return s.json()
else:
return 0
def get_wind_data(lat,long,unixtime):
data = get_weather_data(lat,long,unixtime)
if data:
try:
# we are getting wind in mph
windspeed = data['currently']['windSpeed']*0.44704
windbearing = data['currently']['windBearing']
except KeyError:
windspeed = 0
windbearing = 0
try:
airports = data['flags']['madis-stations']
except KeyError:
airports = ['unknown']
try:
temperature = data['currently']['temperature']
temperaturec = (temperature-32.)*(5./9.)
temperaturec = int(10*temperaturec)/10.
except KeyError:
temperature = 'unknown'
temperaturec = 'unknown'
try:
summary = data['currently']['summary']
except KeyError:
summary = 'unknown'
else:
windspeed = 0
windbearing = 0
message = 'Not able to get weather data'
# apply Hellman's coefficient for neutral air above human inhabitated areas
windspeed = windspeed*(0.1)**0.34
windspeed = 0.01*int(100*windspeed)
timestamp = datetime.utcfromtimestamp(unixtime).isoformat()+'Z'
message = 'Summary for your location at '+timestamp+': '+summary
message += '. Temperature '+str(temperature)+'F/'+str(temperaturec)+'C'
if data:
message += '. Wind: '+str(windspeed)+' m/s. Wind Bearing: '+str(windbearing)+' degrees'
return [windspeed,windbearing,message,airports,timestamp]