commit
This commit is contained in:
@@ -1,16 +1,11 @@
|
||||
from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
import requests
|
||||
from requests.exceptions import ConnectionError
|
||||
import json
|
||||
from lxml import objectify,etree
|
||||
from lxml import objectify, etree
|
||||
import xml.etree.ElementTree as ET
|
||||
import time
|
||||
from datetime import datetime
|
||||
from rowingdata import rowingdata,geo_distance
|
||||
from rowingdata import rowingdata, geo_distance
|
||||
import arrow
|
||||
import pandas as pd
|
||||
from rowers.models import Rower, Workout
|
||||
@@ -18,33 +13,39 @@ from rowers.models import Rower, Workout
|
||||
from rowsandall_app.settings import FORECAST_IO_KEY
|
||||
|
||||
# Find closest airport
|
||||
def get_airport_code(lat,lon):
|
||||
|
||||
|
||||
def get_airport_code(lat, lon):
|
||||
metardata = pd.read_csv('rowers/data/metarlatlon.csv')
|
||||
deltasq = (lat-metardata.lat)**2+(lon-metardata.lon)**2
|
||||
a = metardata[deltasq == deltasq.min()]
|
||||
airport_code = a.iloc[0]['icao']
|
||||
newlat = a.iloc[0]['lat']
|
||||
newlon = a.iloc[0]['lon']
|
||||
distance = geo_distance(lat,lon,newlat,newlon)
|
||||
return airport_code,newlat,newlon,distance
|
||||
distance = geo_distance(lat, lon, newlat, newlon)
|
||||
return airport_code, newlat, newlon, distance
|
||||
|
||||
# Get weather data from the DarkSky API
|
||||
def get_weather_data(long,lat,unixtime):
|
||||
|
||||
|
||||
def get_weather_data(long, lat, unixtime):
|
||||
url = "https://api.darksky.net/forecast/"+FORECAST_IO_KEY+"/"
|
||||
url += str(long)+","+str(lat)+","+str(unixtime)
|
||||
|
||||
try:
|
||||
s = requests.get(url)
|
||||
except ConnectionError: # pragma: no cover
|
||||
except ConnectionError: # pragma: no cover
|
||||
return 0
|
||||
|
||||
if s.ok:
|
||||
return s.json()
|
||||
else: # pragma: no cover
|
||||
else: # pragma: no cover
|
||||
return 0
|
||||
|
||||
# Get Metar data
|
||||
def get_metar_data(airportcode,unixtime):
|
||||
|
||||
|
||||
def get_metar_data(airportcode, unixtime):
|
||||
|
||||
timestamp = arrow.get(unixtime).isoformat()
|
||||
|
||||
@@ -56,17 +57,16 @@ def get_metar_data(airportcode,unixtime):
|
||||
|
||||
try:
|
||||
s = requests.get(url)
|
||||
except: # pragma: no cover
|
||||
except: # pragma: no cover
|
||||
message = 'Failed to download METAR data'
|
||||
return [0,0,message,'','']
|
||||
return [0, 0, message, '', '']
|
||||
|
||||
|
||||
if s.ok: # pragma: no cover
|
||||
if s.ok: # pragma: no cover
|
||||
try:
|
||||
doc = etree.fromstring(s.content)
|
||||
except AttributeError:
|
||||
message = 'METAR data content error'
|
||||
return [0,0,message,'',0]
|
||||
return [0, 0, message, '', 0]
|
||||
lengte = len(doc.xpath('data/METAR/station_id'))
|
||||
idnr = int(lengte/2)
|
||||
try:
|
||||
@@ -78,8 +78,7 @@ def get_metar_data(airportcode,unixtime):
|
||||
rawtext = doc.xpath('data/METAR/raw_text')[idnr].text
|
||||
except IndexError:
|
||||
message = 'Failed to download METAR data'
|
||||
return [0,0,message,'',timestamp]
|
||||
|
||||
return [0, 0, message, '', timestamp]
|
||||
|
||||
windbearing = float(wind_dir)
|
||||
wind_knots = float(wind_speed)
|
||||
@@ -93,22 +92,21 @@ def get_metar_data(airportcode,unixtime):
|
||||
message = 'Summary for your location at '+timestamp+': '
|
||||
message += 'Temperature '+temp_c+'C/'+temp_f+'F'
|
||||
message += '. Wind: '+str(wind_ms)+' m/s ('+str(wind_knots)+' kt)'
|
||||
message +='. Wind Bearing: '+str(windbearing)+' degrees'
|
||||
message += '. Wind Bearing: '+str(windbearing)+' degrees'
|
||||
# message +='\n'+rawtext
|
||||
|
||||
return [wind_ms,windbearing,message,rawtext,timestamp]
|
||||
return [wind_ms, windbearing, message, rawtext, timestamp]
|
||||
|
||||
|
||||
message = 'Failed to download METAR data' # pragma: no cover
|
||||
return [0,0,message,'',timestamp] # pragma: no cover
|
||||
message = 'Failed to download METAR data' # pragma: no cover
|
||||
return [0, 0, message, '', timestamp] # pragma: no cover
|
||||
|
||||
|
||||
# Get wind data (and translate from knots to m/s)
|
||||
def get_wind_data(lat,long,unixtime):
|
||||
data = get_weather_data(lat,long,unixtime)
|
||||
def get_wind_data(lat, long, unixtime):
|
||||
data = get_weather_data(lat, long, unixtime)
|
||||
summary = ''
|
||||
temperature = 20
|
||||
if data: # pragma: no cover
|
||||
if data: # pragma: no cover
|
||||
try:
|
||||
# we are getting wind in mph
|
||||
windspeed = data['currently']['windSpeed']*0.44704
|
||||
@@ -117,13 +115,11 @@ def get_wind_data(lat,long,unixtime):
|
||||
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
|
||||
@@ -133,7 +129,6 @@ def get_wind_data(lat,long,unixtime):
|
||||
temperature = 'unknown'
|
||||
temperaturec = 'unknown'
|
||||
|
||||
|
||||
try:
|
||||
summary = data['currently']['summary']
|
||||
except KeyError:
|
||||
@@ -157,8 +152,8 @@ def get_wind_data(lat,long,unixtime):
|
||||
message = 'Summary for your location at '+timestamp+': '+summary
|
||||
message += '. Temperature '+str(temperature)+'F/'+str(temperaturec)+'C'
|
||||
|
||||
if data: # pragma: no cover
|
||||
message += '. Wind: '+str(windspeed)+' m/s. Wind Bearing: '+str(windbearing)+' degrees'
|
||||
if data: # pragma: no cover
|
||||
message += '. Wind: ' + \
|
||||
str(windspeed)+' m/s. Wind Bearing: '+str(windbearing)+' degrees'
|
||||
|
||||
|
||||
return [windspeed,windbearing,message,airports,timestamp]
|
||||
return [windspeed, windbearing, message, airports, timestamp]
|
||||
|
||||
Reference in New Issue
Block a user