added METAR
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import requests
|
||||
import json
|
||||
from lxml import objectify,etree
|
||||
import xml.etree.ElementTree as ET
|
||||
import time
|
||||
from datetime import datetime
|
||||
from rowingdata import rowingdata,geo_distance
|
||||
@@ -31,6 +33,52 @@ def get_weather_data(long,lat,unixtime):
|
||||
else:
|
||||
return 0
|
||||
|
||||
# Get Metar data
|
||||
def get_metar_data(airportcode,unixtime):
|
||||
|
||||
timestamp = datetime.utcfromtimestamp(unixtime).isoformat()+'Z'
|
||||
|
||||
url = "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&startTime="
|
||||
url += str(unixtime-3600)
|
||||
url += "&endTime="
|
||||
url += str(unixtime+3600)
|
||||
url += "&stationString="+airportcode
|
||||
|
||||
s = requests.get(url)
|
||||
|
||||
if s.ok:
|
||||
doc = etree.fromstring(s.content)
|
||||
id = doc.xpath('data/METAR/station_id')[0].text
|
||||
temp_c = doc.xpath('data/METAR/temp_c')[0].text
|
||||
wind_dir = doc.xpath('data/METAR/wind_dir_degrees')[0].text
|
||||
wind_speed = doc.xpath('data/METAR/wind_speed_kt')[0].text
|
||||
timestamp = doc.xpath('data/METAR/observation_time')[0].text
|
||||
rawtext = doc.xpath('data/METAR/raw_text')[0].text
|
||||
|
||||
print temp_c,wind_dir,wind_speed
|
||||
|
||||
windbearing = float(wind_dir)
|
||||
wind_knots = float(wind_speed)
|
||||
wind_ms = 0.514444*wind_knots
|
||||
wind_ms = int(10*wind_ms)/10.
|
||||
|
||||
temperaturec = float(temp_c)
|
||||
temp_f = 32.+(9./5.)*temperaturec
|
||||
temp_f = str(int(10*temp_f)/10.)
|
||||
|
||||
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 +='\n'+rawtext
|
||||
|
||||
return [wind_ms,windbearing,message,rawtext,timestamp]
|
||||
|
||||
|
||||
message = 'Failed to download METAR data'
|
||||
return [0,0,message,'',timestamp]
|
||||
|
||||
|
||||
# Get wind data (and translate from knots to m/s)
|
||||
def get_wind_data(lat,long,unixtime):
|
||||
data = get_weather_data(lat,long,unixtime)
|
||||
|
||||
Reference in New Issue
Block a user