Skip to content

Instantly share code, notes, and snippets.

@wesleygrimes
Created December 17, 2020 01:13
Show Gist options
  • Save wesleygrimes/8129573b0d6f338aee92da5cd9bfe937 to your computer and use it in GitHub Desktop.
Save wesleygrimes/8129573b0d6f338aee92da5cd9bfe937 to your computer and use it in GitHub Desktop.
Example plot generation
def get_bearwallow_mountain_station_data():
url = 'https://climate.ncsu.edu/cronos/?station=BEAR'
html = urllib.request.urlopen(url)
soup = BeautifulSoup(html, features='html.parser')
output = dict()
output['label'] = 'Bearwallow Mtn 4200ft'
output['temp'] = 0.00
output['success'] = False
try:
Table = str(
soup.find('table', {"class": "CurrentConditions"}).find_all('tr'))
Tr = Table.split(',')
Temp_1 = str(Tr[4])
Temp_2 = Temp_1.split('<')
Temp_3 = str(Temp_2[9])
Temp_4 = Temp_3.split('>')
Temp_5 = str(Temp_4[1])
Temp_6 = Temp_5.split('°')
Temp_7 = str(Temp_6[0])
Temp_8 = Temp_7.strip(' ')
output['temp'] = eval(Temp_8)
output['success'] = True
except:
print("BEAR missing")
return output
import piedmont.services.cronos as cronos
import piedmont.services.rays_weather as rays_weather
import piedmont.services.nc_high_peaks as nc_high_peaks
import numpy as np
import math
import shapefile
import urllib.request
import matplotlib.pyplot as plt
import matplotlib.patheffects as pe
from matplotlib.patches import PathPatch
from matplotlib.collections import LineCollection
from matplotlib import cm
from bs4 import BeautifulSoup
from datetime import datetime
def wncbar():
barnames = []
temperatures = []
south_asheville = cronos.get_south_asheville_station_data()
if south_asheville['success']:
barnames.append(south_asheville['label'])
temperatures.append(south_asheville['temp'])
boone = cronos.get_boone_station_data()
if boone['success']:
barnames.append(boone['label'])
temperatures.append(boone['temp'])
grandfather = cronos.get_grandfather_station_data()
if grandfather['success']:
barnames.append(grandfather['label'])
temperatures.append(grandfather['temp'])
mitchell = cronos.get_mitchell_station_data()
if mitchell['success']:
barnames.append(mitchell['label'])
temperatures.append(mitchell['temp'])
bearwallow_mountain = cronos.get_bearwallow_mountain_station_data()
if bearwallow_mountain['success']:
barnames.append(bearwallow_mountain['label'])
temperatures.append(bearwallow_mountain['temp'])
valle_crucis = rays_weather.get_valle_crucis_station_data()
if valle_crucis['success']:
barnames.append(valle_crucis['label'])
temperatures.append(valle_crucis['temp'])
avery = rays_weather.get_avery_station_data()
for data in avery:
barnames.append(data['label'])
temperatures.append(data['temp'])
mitchell_top = nc_high_peaks.get_mitchell_top_data()
if mitchell_top['success']:
barnames.append(mitchell_top['label'])
temperatures.append(mitchell_top['temp'])
print(temperatures)
fig = plt.figure()
fig.patch.set_facecolor('grey')
ax = plt.axes()
ax.set_facecolor('grey')
# data and bar names
height = temperatures
bars = barnames
y_pos = np.arange(len(bars))
#max/mins & fonts
if len(height) > 0:
maximum = max(height)+3
minimum = min(height)-3
font = {'size': 16, 'color': 'white'}
font2 = {'size': 22, 'color': 'white'}
# color
color_1 = np.array(height)
color_2 = cm.cool(1-(color_1 / float(max(color_1))))
ax.xaxis.label.set_color('white')
ax.spines['bottom'].set_color('white')
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
ax.spines['left'].set_color('white')
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
# horizontal bars
plt.barh(y_pos, height, color=color_2)
# y-axis names
plt.yticks(y_pos, bars, **font)
# x-axis
plt.xlim(minimum, maximum)
# x label
plt.xlabel('Temperature (°F)', **font)
for i, v in enumerate(height):
plt.text(v, i-0.1, str(v), color='white', fontsize='13')
plt.title('Mountains, NC\nVertical Temperature Profile', **font2)
now = datetime.now()
current_time = now.strftime("%I:%M %p")
#plt.text(0.87,0.94,current_time,color='white',size=18,horizontalalignment='center',verticalalignment='center',transform = ax.transAxes)
plt.text(-0.27, -0.12, "Source: NCSCO", color='white', size=16,
horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
plt.text(-0.27, 1.05, "Valid: " + current_time, color='white', size=16,
horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
plt.figtext(0.5, -0.2, "AppalachianWX.com", color='white',
size=10, horizontalalignment='center', transform=ax.transAxes)
# show/save graphic
plt.savefig("output/wncbarplot.png", bbox_inches='tight',
facecolor=fig.get_facecolor())
# plt.show()
elif len(height) == 0:
fig = plt.figure()
fig.patch.set_facecolor('grey')
ax = plt.axes()
ax.set_facecolor('grey')
plt.text(0.5, 0.5, "All Stations Down", color='white', size=22,
horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
plt.savefig("output/wncbarplot.png", bbox_inches='tight',
facecolor=fig.get_facecolor())
import urllib
from bs4 import BeautifulSoup
def get_mitchell_top_data():
url = 'https://nchighpeaks.org/davis/index.html'
html = urllib.request.urlopen(url)
soup = BeautifulSoup(html, features='html.parser')
output = dict()
output['label'] = 'Mt Mitchell #1 6600ft'
output['temp'] = 0.00
output['success'] = False
try:
Table = str(soup.find('table').find_all('tr'))
Tr = Table.split(',')
Temp_1 = str(Tr[0])
Temp_2 = Temp_1.split('<td class="stats_data">')
Temp_3 = str(Temp_2[1])
Temp_4 = Temp_3.split('°F')
Temp_5 = str(Temp_4[0])
Temp = eval(Temp_5)
output['temp'] = eval(Temp)
output['success'] = True
except:
print("Mitchell Top missing")
return output
import urllib
from bs4 import BeautifulSoup
def get_valle_crucis_station_data():
url = 'http://booneweather.com/'
html = urllib.request.urlopen(url)
soup = BeautifulSoup(html, features='html.parser')
Map = str(soup.find('map', {"name": "cc"}))
output = dict()
output['label'] = 'Valle Crucis 2670ft'
output['temp'] = 0.00
output['success'] = False
try:
Area_1 = Map.split('<area')
Area_2 = str(Area_1[3])
Temp_1 = Area_2.split('Temp:&lt;/td&gt;&lt;td&gt;')
Temp_2 = str(Temp_1[1])
Temp_3 = Temp_2.split('°F')
output['temp'] = eval(Temp_3[0])
output['success'] = True
except:
print("Valle Crucis missing")
return output
def get_avery_station_data():
url = 'http://averyweather.com/'
html = urllib.request.urlopen(url)
soup = BeautifulSoup(html, features='html.parser')
Map = str(soup.find('map', {"name": "cc"}))
output = []
linville_output = dict()
linville_output['label'] = 'Linville 3650ft'
linville_output['temp'] = 0.00
linville_output['success'] = False
try:
Area_1 = Map.split('<area')
Area_2 = str(Area_1[5])
Temp_1 = Area_2.split('Temp:&lt;/td&gt;&lt;td&gt;')
Temp_2 = str(Temp_1[1])
Temp_3 = Temp_2.split('°F')
linville_output['temp'] = eval(Temp_3[0])
linville_output['success'] = True
output.append(linville_output)
except:
print("Linville missing")
seven_devils_output = dict()
seven_devils_output['label'] = 'Seven Devils 3940ft'
seven_devils_output['temp'] = 0.00
seven_devils_output['success'] = False
try:
Area_1 = Map.split('<area')
Area_2 = str(Area_1[11])
Temp_1 = Area_2.split('Temp:&lt;/td&gt;&lt;td&gt;')
Temp_2 = str(Temp_1[1])
Temp_3 = Temp_2.split('°F')
seven_devils_output['temp'] = eval(Temp_3[0])
seven_devils_output['success'] = True
output.append(seven_devils_output)
except:
print("Seven Devils missing")
sugar_mountain_output = dict()
sugar_mountain_output['label'] = 'Sugar Mtn 5000ft'
sugar_mountain_output['temp'] = 0.00
sugar_mountain_output['success'] = False
try:
Area_1 = Map.split('<area')
Area_2 = str(Area_1[7])
Temp_1 = Area_2.split('Temp:&lt;/td&gt;&lt;td&gt;')
Temp_2 = str(Temp_1[1])
Temp_3 = Temp_2.split('°F')
sugar_mountain_output['temp'] = eval(Temp_3[0])
sugar_mountain_output['success'] = True
output.append(sugar_mountain_output)
except:
print("Sugar Mountain missing")
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment