-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeatherTracker.py
More file actions
117 lines (98 loc) · 3.72 KB
/
WeatherTracker.py
File metadata and controls
117 lines (98 loc) · 3.72 KB
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
116
117
import requests
import pandas as pd
from datetime import datetime, timedelta
import pytz
from astral import LocationInfo
from astral.sun import sun
from dotenv import load_dotenv
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Setup
stations = {'Tallahassee Airport': 'KTLH',
'Ft Myers Page Field': 'KFMY'}
url = f"https://api.weather.gov/stations/{stations['Ft Myers Page Field']}/observations"
headers = {"User-Agent": "weather-observer"}
# Fetch observations
print("Fetching weather data...")
response = requests.get(url, headers=headers)
data = response.json()
observations = data.get("features", [])
# Parse records
long, lat = observations[0]['geometry']['coordinates']
records = []
for obs in observations:
props = obs["properties"]
record = {
'station_name': props.get('stationName'),
"station_id": props.get("stationId"),
"timestamp_utc": pd.to_datetime(props.get("timestamp")),
"temperature_c": props.get("temperature", {}).get("value"),
"wind_speed_kph": props.get("windSpeed", {}).get("value"),
"precip_mm": props.get("precipitationLastHour", {}).get("value"),
'description': props.get("textDescription"),
'source': props.get("@id")
}
records.append(record)
df = pd.DataFrame(records)
# Convert to local time (Eastern Time)
eastern = pytz.timezone("US/Eastern")
df["timestamp_local"] = df["timestamp_utc"].dt.tz_convert(eastern)
# Convert wind speed from kph to mph
df["wind_speed_mph"] = df["wind_speed_kph"] * 0.621371
# Convert temperature from Celsius to Fahrenheit
df["temperature_f"] = df["temperature_c"] * 9/5 + 32
# Get sunset time using astral
city = LocationInfo(latitude=lat, longitude=long)
now_local = datetime.now(eastern)
yesterday = now_local - timedelta(days=1)
sun_times = sun(city.observer, date=yesterday.date(), tzinfo=eastern)
start_time = sun_times["sunset"]
end_time = start_time + timedelta(hours=5)
# Filter for 5 hours after sunset
mask = (df["timestamp_local"] >= start_time) & (df["timestamp_local"] <= end_time)
evening_df = df.loc[mask]
# # log the weather data if scheduling locally
# log_file = 'weatherLogs.csv'
# if not os.path.exists(log_file):
# evening_df.to_csv(log_file, index=False)
# else:
# evening_df.to_csv(log_file, mode='a', header=False, index=False)
# Compile weather ranges
airmin = evening_df['temperature_f'].min()
airmax = evening_df['temperature_f'].max()
windmin = evening_df['wind_speed_mph'].min()
windmax = evening_df['wind_speed_mph'].max()
precipsum = evening_df['precip_mm'].sum()
# Determine if it's a pass or fail based on USFWS standards
if airmin < 60 or windmax > 9 or precipsum > 0:
status = 'FAIL'
else:
status = 'PASS'
# Setup email
print("Preparing to send email...")
load_dotenv()
senderaddr = os.getenv('senderaddr')
apppw = os.getenv('apppw')
receiveraddr = 'bfethe@earthresources.us' #NOTE Update to whatever emails you want to send to
message = MIMEMultipart()
message['From'] = senderaddr
message['To'] = receiveraddr
message['Subject'] = f'Bat Survey Weather Alert {now_local.strftime("%Y-%m-%d")} - {status}'
body = f'''Below are the weather summary metrics:
Temp (F): {airmin} - {airmax}
Wind (mph): {windmin} - {windmax}
Precip (mm): {precipsum}
To see how this code was developed and scheduled, please visit the GitHub repository: https://github.com/EarthResources/WeatherTracker
'''
message.attach(MIMEText(body, 'plain'))
# Send the email
try:
with smtplib.SMTP('smtp.gmail.com', 587) as session:
session.starttls()
session.login(user=senderaddr, password=apppw)
session.send_message(message)
print('Email sent!')
except Exception as e:
print(f"Failed to send email: {e}")