-
Notifications
You must be signed in to change notification settings - Fork 0
/
radar-term.py
115 lines (97 loc) · 3.72 KB
/
radar-term.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/python
'''
pip install geopy
pip install rich_pixels
pip install rich
pip install tabulate
'''
from geopy.geocoders import Nominatim
import json
import os
from PIL import Image
from rich.console import Console
from rich_pixels import Pixels
from rich_pixels._renderer import HalfcellRenderer
import requests
from tabulate import tabulate
print('''
__________ .___ ___________
\______ \_____ __| _/____ _______ \__ ___/__________ _____
| _/\__ \ / __ |\__ \\\_ __ \ ______ | |_/ __ \_ __ \/ \
| | \ / __ \_/ /_/ | / __ \| | \/ /_____/ | |\ ___/| | \/ Y Y \\
|____|_ /(____ /\____ |(____ /__| |____| \___ >__| |__|_| /
\/ \/ \/ \/ \/ \/
''')
os.system('')
gl = Nominatim(user_agent='radar-term')
console = Console()
files = []
# Prompt for zip code.
while True:
zip = input('Enter your zip code (q to quit): ')
#Validate input.
if zip.lower() == 'q':
break
elif len(zip) > 5 or len(zip) < 5:
print('Must be 5 digits. Try again.')
elif zip.isdigit() is False:
print('Must enter numbers only. Try again.')
else:
# Convert zip into lat, lon
loc = gl.geocode(zip, country_codes='us')
if loc == None:
print('Invalid zip code. Try again.')
continue
lat = loc.latitude
lon = loc.longitude
# Get local radar station
nws_url = 'https://api.weather.gov'
response = requests.get(f'{nws_url}/points/{lat},{lon}').json()
radar_stn = response['properties']['radarStation']
wfo = response['properties']['gridId']
forecast_url = response['properties']['forecast']
# Get radar image
radar_url = f'https://radar.weather.gov/ridge/standard/{radar_stn}_0.gif'
response = requests.get(radar_url)
with open('radar.gif', 'wb') as f:
f.write(response.content)
# Convert GIF to ANSI
width, height = os.get_terminal_size()
renderer = HalfcellRenderer()
with Image.open('radar.gif', 'r') as image_png:
x, y = image_png.size
ratio = (x / y)
height = int(ratio * width)
image_ansi = Pixels.from_image(image_png, resize=(width, height), renderer=renderer)
console.print(image_ansi)
# Get forecast
response = requests.get(forecast_url).json()
# Parse forecast data
names = []
temps = []
pops = []
table1 = [[], [], []]
table2 = [[], [], []]
for period in response['properties']['periods']:
if len(table1[0]) < 7:
table1[0].append(period['name'])
temp = period['temperature']
table1[1].append(f'{temp}°')
pop = period['probabilityOfPrecipitation']['value']
if pop is not None:
table1[2].append(f'{pop}%')
else:
table2[0].append(period['name'])
temp = period['temperature']
table2[1].append(f'{temp}°')
pop = period['probabilityOfPrecipitation']['value']
if pop is not None:
table2[2].append(f'{pop}%')
# Display forecast
rowIDs = ['Temp', 'PoP']
print(tabulate(table1, headers='firstrow', tablefmt='grid', showindex=rowIDs,
stralign='center', numalign='center'))
print(tabulate(table2, headers='firstrow', tablefmt='grid', showindex=rowIDs,
stralign='center', numalign='center'))
# Clean up
os.remove('radar.gif')