Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
markyharris committed Jan 4, 2024
1 parent d86f2bb commit d9f060a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 31 deletions.
2 changes: 1 addition & 1 deletion data.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KSEZ
KFLG
1
60
0
Expand Down
7 changes: 3 additions & 4 deletions metar_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
# Metar Display - Mark Harris
# Version 2.1
# UPDATED FAA API 12-2023, https://aviationweather.gov/data/api/
# Part of Epaper Display project found at; https://github.com/markyharris/metar/
#
# Altered from https://github.com/aerodynamics-py/WEATHER_STATION_PI
#
# Added a number of bold fonts
# Added a number of drawing routines for rounded corners, etc.


# Imports
from PIL import Image, ImageDraw, ImageFont, ImageOps
import requests
import urllib.request


# Setup fonts that could be chosen. Default font_choice is #5
# Look in '/usr/share/fonts/truetype/' to see what is installed on
# specific system and change as necessary.
Expand Down Expand Up @@ -63,20 +66,16 @@
class Metar:
def __init__(self, airport):
self.data = requests.get(
# f"https://api.weather.gov/stations/"+airport+"/observations/latest", timeout=5).json()
f"https://aviationweather.gov/api/data/metar?ids="+airport+"&format=json&hours=2.5").json()
self.data2 = requests.get(
# f"https://api.weather.gov/stations/"+airport, timeout=5).json()
f"https://aviationweather.gov/api/data/metar?ids="+airport+"&format=json&hours=2.5").json()
requests.session().close()
pass

def update(self, airport):
self.data = requests.get(
# f"https://api.weather.gov/stations/"+airport+"/observations/latest", timeout=5).json()
f"https://aviationweather.gov/api/data/metar?ids="+airport+"&format=json&hours=2.5").json()
self.data2 = requests.get(
# f"https://api.weather.gov/stations/"+airport, timeout=5).json()
f"https://aviationweather.gov/api/data/metar?ids="+airport+"&format=json&hours=2.5").json()
requests.session().close()
return self.data, self.data2
Expand Down
4 changes: 3 additions & 1 deletion metar_layouts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# metar_layouts.py
# Layouts for Metar Display - Mark Harris
# Version 2.1
# Part of Epaper Display project found at; https://github.com/markyharris/metar/
#
# UPDATED FAA API 12-2023, https://aviationweather.gov/data/api/
#
# Each Layout offers a different look and amount of information.
Expand Down Expand Up @@ -330,7 +332,7 @@ def layout1(display,metar,remarks,print_table,use_remarks,use_disp_format,interv
# Display Wind Speed
windsp,dis_unit = get_wspd(metar,wind_speed_units)

if windsp == "Calm" or float(windsp) < 5.0:
if windsp == "Calm" or windsp == "n/a" or float(windsp) < 5.0:
display.draw_icon(COL2+ICON_OFFSET, LINE3+5, "r", 50, 50, "windvanelow")
elif float(windsp) >= 5.0 and float(windsp) < 15.0:
display.draw_icon(COL2+ICON_OFFSET, LINE3+5, "r", 50, 50, "windvanemed")
Expand Down
2 changes: 2 additions & 0 deletions metar_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# metar_main.py
# E-Paper METAR Display - by Mark Harris
# Version 2.1
# Part of Epaper Display project found at; https://github.com/markyharris/metar/
#
# UPDATED to New FAA API 12-2023, https://aviationweather.gov/data/api/
#
# Thanks to Aerodynamics for providing a great start for this project
Expand Down
32 changes: 13 additions & 19 deletions metar_routines.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# metar_routines.py
# Metar Decoding Routines - Mark Harris
# Version 2.1
# Part of Epaper Display project found at; https://github.com/markyharris/metar/
#
# UPDATED FAA API 12-2023, https://aviationweather.gov/data/api/

# Imports
Expand Down Expand Up @@ -134,9 +136,7 @@ def get_visib(metar,visibility_units): # "Visibility in statute miles, 10+ is gr
if type(metar.data[0]["visib"]) == int or type(metar.data[0]["visib"]) == float:
tmp_vis = float(metar.data[0]["visib"])
else:
# print('metar.data[0]["visib"]',metar.data[0]["visib"]) # debug
tmp_vis = metar.data[0]["visib"].strip('+')
# print(tmp_vis) # debug
tmp_vis = float(tmp_vis)
vis = '{0:.1f}'.format(tmp_vis)
if visibility_units == 1:
Expand All @@ -160,7 +160,6 @@ def get_rawOb(metar): # "Raw text of observation" string
rawmetar = metar.data[0]['rawOb']
else:
rawmetar = 'n/a'
# print ('rawmetar:',rawmetar) # debug
return(rawmetar)

def get_wdir(metar): # "Wind direction in degrees or VRB for variable winds" integer
Expand Down Expand Up @@ -247,7 +246,8 @@ def get_misc(metar): # icaoid,obstime,elev,lat,lon,name = get_misc(metar)
lat = metar.data[0]["lat"] # "Latitude of site in degrees" number
lon = metar.data[0]["lon"] # "Longitude of site in degrees" number
name = metar.data[0]["name"] # "Full name of the site" string
print(icaoid,obstime,elev,lat,lon,name)

print(icaoid,obstime,elev,lat,lon,name) # debug
return(icaoid,obstime,elev,lat,lon,name)


Expand All @@ -261,14 +261,12 @@ def get_ip_address():
return ip_address




# Get Flight Categories for Class B and Class C airports
def get_flightcat():
# api url
# api url; the first 2 are old FAA API's that have been sunsetted. Required significant rewrite to accomodate the new system.
# url = "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=2.5&stationString="
url = "https://aviationweather-cprk.ncep.noaa.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=2.5&stationString="

# url = "https://aviationweather-cprk.ncep.noaa.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=2.5&stationString="
url = "https://aviationweather.gov/api/data/metar?format=xml&hours=2.5&ids=" # Latest API from FAA, https://aviationweather.gov/data/api/
fc_dict = {}
vfr_dict = {}
mvfr_dict = {}
Expand All @@ -282,13 +280,10 @@ def get_flightcat():
url = url+ap+","

content = urllib.request.urlopen(url).read()

root = ET.fromstring(content) #Process XML data returned from FAA
# print(root) # debug

for data in root.iter('data'):
num_results = data.attrib['num_results']
print(num_results)

for metar in root.iter('METAR'):
stationId = metar.find('station_id').text
Expand All @@ -308,6 +303,7 @@ def get_flightcat():
else:
vfr_dict[key] = "VFR"

# print(vfr_dict,mvfr_dict,ifr_dict,lifr_dict) # debug
return(vfr_dict,mvfr_dict,ifr_dict,lifr_dict)


Expand All @@ -327,9 +323,9 @@ def flight_category(metar):
print("num_clouds layers:",len(metar.data[0]['clouds'])) # debug

# Get Cloud Cover
for i in range(len(metar.data[0]['clouds'])): #["properties"]["cloudLayers"])):
sky_condition = metar.data[0]['clouds'][i]['cover'] #["properties"]["cloudLayers"][i]["amount"]
sky_ceiling = metar.data[0]['clouds'][i]['base'] #["properties"]["cloudLayers"][i]["amount"]
for i in range(len(metar.data[0]['clouds'])):
sky_condition = metar.data[0]['clouds'][i]['cover']
sky_ceiling = metar.data[0]['clouds'][i]['base']

if sky_condition == "OVC" or sky_condition == "BKN" or sky_condition == "OVX" or sky_condition == "VV":
if sky_ceiling < 500:
Expand Down Expand Up @@ -395,9 +391,10 @@ def decode_remarks(rawmessage):

# Provides the proper icon to display depending on wind direction
def wind_arrow(deg):
if deg == "000" or deg == "VRB":
if deg == "000" or deg == "VRB" or deg == "n/a":
arrow = "compass"
return arrow

deg = int(deg)
if deg < 30 or deg >= 330:
arrow = "north"
Expand All @@ -421,7 +418,6 @@ def wind_arrow(deg):
return (arrow)



# decodes raw metar string to grab wind direction wind speed, gusts, temperature and baro
# This info is used as backup if the api does not provide this data in its normal response.
def decode_rawmessage(airport_name):
Expand All @@ -439,7 +435,6 @@ def decode_rawmessage(airport_name):
# use either live metar or test_metar from above.
try:
decode = airport_name.split()
# print("***",decode) # debug
except:
print("*decode try failed") # debug
decoded_airport,decoded_time,decoded_wndir,decoded_wnspd,decoded_wngust,decoded_vis,\
Expand Down Expand Up @@ -468,7 +463,6 @@ def decode_rawmessage(airport_name):

# Get Visibility NEEDS WORK
for i in range(len(decode)):
# print("i: ",i) # debug

if len(decode[i]) == 1 and i < len(decode):
decode[i] = decode[i]+" "+decode[i+1]
Expand Down
9 changes: 5 additions & 4 deletions metar_settings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# metar_settings.py
# Metar Display Settings - Mark Harris
# Version 2.1
# Part of Epaper Display project found at; https://github.com/markyharris/metar/
#
# UPDATED FAA API 12-2023, https://aviationweather.gov/data/api/
#
# These settings will be used if script is run with no cmd line arguments

# Default User Settings
airport = "KFLG" # enter default airport identifier to display
use_disp_format = -2 # Choose which display layout to use. -1 = Random layout, -2 = Cycle layouts
interval = 60 # enter default time in seconds between METAR updates - i.e. 3600 = 1 hour
use_disp_format = -2 # Choose which display layout to use. -1 = Random layout, -2 = Cycle layouts
interval = 60 # enter default time in seconds between METAR updates - i.e. 3600 = 1 hour
use_remarks = 0 # 0 = display airport information, 1 = display metar remarks info

# Display Units
Expand All @@ -24,5 +26,4 @@
# For randomly selected airports, put more than 12 in list. 12 will randomly be displayed, 13th will be default
# If you put less than 12 airports in list, it will pad the missing spots with the default airport above.
random_airports = ["KEYW","KFVE","KSEA","KCVG", "KLAS","KCMR","KGRR","KMSN", \
"KLAX","KNBC","KFVE","KBUF"] #, "KTPA", "KLAS","KGEU","KOLS", \
# "KSAN", "KPDX", "KBOI", "KMSP", "KSTL", "KBNA", "KTYS"]
"KLAX","KNBC","KFVE","KBUF"]
11 changes: 11 additions & 0 deletions metar_startup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# metar_startup.py - Mark Harris
# for E-Paper display
# Version 2.1
# Part of Epaper Display project found at; https://github.com/markyharris/metar/
#
# UPDATED FAA API 12-2023, https://aviationweather.gov/data/api/
#
# This script is run only once upon boot/reboot to display the Admin Page's URL.
# rc.local runs this script, then waits for a bit then runs the 'metar_main.py' script and the 'webapp.py' script

# imports
from metar_layouts import *
from metar_routines import *
import time
Expand Down
1 change: 1 addition & 0 deletions templates/metar.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
.myDiv span{
text-align: center;
background: #ffffff;
background-color: transparent;
padding: 6px 6px;
display: block;
width: 120px;
Expand Down
5 changes: 3 additions & 2 deletions webapp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# webapp.py - Mark Harris
# for E-Paper display
# Version 2.1
# Part of Epaper Display project found at; https://github.com/markyharris/metar/
#
# UPDATED FAA API 12-2023, https://aviationweather.gov/data/api/
#
# This will provide a web interface to control the e-Paper display.
Expand Down Expand Up @@ -78,7 +80,6 @@ def metar():

else:
os.system("ps -ef | grep 'metar_main.py' | awk '{print $2}' | xargs sudo kill")
# os.system('sudo python3 ' + PATH + 'metar_main.py '+ data_field1+' '+data_field2+' '+data_field3+' '+data_field4+' &')
os.system('sudo python3 ' + PATH + 'metar_main.py ' + ' ' + data_field1 + ' ' + data_field2 + ' ' + data_field3 + " " + data_field4 \
+ " " + data_field5 + ' ' + data_field6 + ' ' + data_field7 + " " + data_field8 + " " + data_field9 + ' &')

Expand Down Expand Up @@ -138,7 +139,7 @@ def get_data():

# create cmdline command to start the main program using the 'data.txt' variables to kick things off.
print('sudo python3 ' + PATH + 'metar_main.py ' + ' ' + data_field1 + ' ' + data_field2 + ' ' + data_field3 + ' ' + data_field4 \
+ ' ' + data_field5 + ' ' + data_field6 + ' ' + data_field7 + " " + data_field8 + ' ' + data_field9 + ' &') # debug
+ ' ' + data_field5 + ' ' + data_field6 + ' ' + data_field7 + " " + data_field8 + ' ' + data_field9 + ' &\n') # debug

# first run at startup. display web admin ip url for 60 seconds
os.system('sudo python3 ' + PATH + 'metar_main.py ' + ' ' + data_field1 + ' ' + data_field2 + ' ' + data_field3 + ' ' + data_field4 \
Expand Down

0 comments on commit d9f060a

Please sign in to comment.