84 lines
2.9 KiB
Python
84 lines
2.9 KiB
Python
from weathersettings import (API_BASE_URL, HEADERS, NOMINATIM_URL, logger, requests,
|
|
BASE_URL, station_urls, math, Tuple, List,
|
|
BytesIO, Image, plt)
|
|
from radarlist import NEXRAD_STATIONS
|
|
|
|
|
|
def get_station_urls(station_id):
|
|
urls = station_urls(station_id)
|
|
loop_url = urls[0] if isinstance(urls[0], str) else None
|
|
static_urls = urls[1] if len(urls) > 1 and isinstance(urls[1], list) else []
|
|
return loop_url, static_urls
|
|
|
|
|
|
def find_nearest_nexrad_station(lat: float, lon: float) -> str:
|
|
def distance(lat1, lon1, lat2, lon2):
|
|
R = 6371 # Earth's radius metric system
|
|
dlat = math.radians(lat2 - lat1)
|
|
dlon = math.radians(lon2 - lon1)
|
|
a = (math.sin(dlat/2) * math.sin(dlat/2) +
|
|
math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) *
|
|
math.sin(dlon/2) * math.sin(dlon/2))
|
|
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
|
|
return R * c
|
|
|
|
nearest_station = min(NEXRAD_STATIONS,
|
|
key=lambda x: distance(lat, lon, x[1], x[2]))
|
|
|
|
logger.info(f"Nearest NEXRAD station to {lat}, {lon} is {nearest_station[0]}")
|
|
return nearest_station[0]
|
|
|
|
|
|
def get_radar_image(nearest_station):
|
|
station_id = nearest_station
|
|
loop_url, static_urls = get_station_urls(station_id)
|
|
|
|
image_urls = {'loop': loop_url, 'static': static_urls}
|
|
|
|
return image_urls
|
|
|
|
|
|
def show_radar_image(nearest_station):
|
|
image_urls = get_radar_image(nearest_station)
|
|
logger.info(f"Displaying radar images for station {nearest_station}")
|
|
|
|
# Display loop image
|
|
response = requests.get(image_urls['loop'])
|
|
img = Image.open(BytesIO(response.content))
|
|
plt.figure(figsize=(9, 9))
|
|
plt.imshow(img)
|
|
plt.axis('off')
|
|
plt.title(f"Loop Radar Image for {nearest_station}")
|
|
plt.show()
|
|
|
|
def staticimg(nearest_station):
|
|
image_urls = get_radar_image(nearest_station)
|
|
# Display static images
|
|
for i, url in enumerate(image_urls['static']):
|
|
response = requests.get(url)
|
|
img = Image.open(BytesIO(response.content))
|
|
plt.figure(figsize=(9, 9))
|
|
plt.imshow(img)
|
|
plt.axis('off')
|
|
plt.title(f"Static Radar Image {i+1} for {nearest_station}")
|
|
plt.show()
|
|
|
|
# Save the last displayed image for UI integration
|
|
last_image_path = f"radar_image_{nearest_station}.gif"
|
|
img.save(last_image_path)
|
|
|
|
return last_image_path
|
|
|
|
def save_radar_image(nearest_station):
|
|
image_urls = get_radar_image(nearest_station)
|
|
logger.info(f"Saving radar images for station {nearest_station}")
|
|
for i, url in enumerate(image_urls['static']):
|
|
filename = f"radar_{i}.gif"
|
|
response = requests.get(url)
|
|
with open(filename, 'wb') as f:
|
|
f.write(response.content)
|
|
logger.info(f"Saved image {filename}")
|
|
|
|
logger.info("Radar images saved")
|
|
|