Private
Public Access
1
0
Files
rowsandall/rowers/weather.py
2017-01-15 16:06:14 +01:00

73 lines
1.9 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
# Get weather data from the DarkSky API
def get_weather_data(long,lat,unixtime):
url = "https://api.darksky.net/forecast/"+FORECAST_IO_KEY+"/"
url += str(long)+","+str(lat)+","+str(unixtime)
s = requests.get(url)
if s.ok:
return s.json()
else:
return 0
# Get wind data (and translate from knots to m/s)
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']
# Temp is given in Fahrenheit, so convert to Celsius for Europeans
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]