forked from Lukas-Vollmerhaus/weather-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_gfs_10day.py
executable file
·151 lines (121 loc) · 5.72 KB
/
alert_gfs_10day.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /usr/bin/env python3
import os,sys
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from selenium import webdriver
from selenium.webdriver.common.by import By
import numpy as np
import argparse
from slack_sdk import WebClient
CSV_PATH = "/home/lukas/Downloads/SpotWx Forecast.csv"
MAX_CLOUD_THRESHOLD = 50 #percent
MIN_TEMPERATURE_THRESHOLD = 0 #celcius
MAX_TEMPERATURE_THRESHOLD = 30
MAX_PRECIP_THRESHOLD = 0 #mm rain // cm snow
MAX_WIND_THRESHOLD = 10 #km/h
brick_tamland = "xoxb-7015922744519-7028590296021-UE547noeSdKPQPYvam2qnVmt"
def slack_alert(message):
weather_alert_bot_token = brick_tamland
channel_name = "weather-alert"
client = WebClient(token=weather_alert_bot_token)
result = client.chat_postMessage(channel=channel_name,text=message)
return result
def slack_update(message):
weather_alert_bot_token = brick_tamland
channel_name = "weather-update"
client = WebClient(token=weather_alert_bot_token)
result = client.chat_postMessage(channel=channel_name,text=message)
return result
def slack_error(message):
weather_alert_bot_token = brick_tamland
channel_name = "error-message"
client = WebClient(token=weather_alert_bot_token)
result = client.chat_postMessage(channel=channel_name,text=message)
return result
def get_csv(URL_STRING):
try:
driver = webdriver.Firefox()
driver.get(URL_STRING)
csv_button = driver.find_element(By.CLASS_NAME,"buttons-csv")
csv_button.click()
driver.quit()
except Exception as e:
print("Error clicking button: %s" % (e))
return(1)
try:
data = pd.read_csv(CSV_PATH)
except Exception as e:
print("Error reading CSV file: %s" % (e))
data['DATETIME'] = pd.to_datetime(data['DATETIME'],format="%Y/%m/%d %H:%M") #convert datetine column to datet time object
#print(data.to_string())
os.remove(CSV_PATH)
return(data)
def find_weekends(data):
index = 0
weekend_data_list = []
weekend_found = False
start_weekend_list = []
for day in data['DATETIME']:
if day.weekday() == 5 or day.weekday() == 6: #saturday
if not weekend_found:
start_weekend_list.append(index)
weekend_found = True
weekend_data_list.append(index)
if weekend_found:
if (index - start_weekend_list[-1]) >= 48: #48 data points per weekend for GFS 10day
weekend_found = False
index = index + 1
return(weekend_data_list,start_weekend_list,index)
def evaluate_thresholds(weekend_data):
weekend_good = True
avg_cloud = np.mean(weekend_data['CLOUD']) #avg cloud cover'
weekend_good = weekend_good and (avg_cloud <= MAX_CLOUD_THRESHOLD)
avg_temp = np.mean(weekend_data['TMP']) #avg temp
weekend_good = weekend_good and (avg_temp < MAX_TEMPERATURE_THRESHOLD) and (avg_temp > MIN_TEMPERATURE_THRESHOLD)
avg_ws = np.mean(weekend_data['WS']) #avg windspeed at ground
weekend_good = weekend_good and (avg_ws <= MAX_WIND_THRESHOLD)
total_precip = weekend_data.iloc[-1]['APCP'] - weekend_data.iloc[1]['APCP'] #total precip for weekend
weekend_good = weekend_good and (total_precip <= MAX_PRECIP_THRESHOLD)
slack_update("cloud %f temp %f ws %f precip %f" % (avg_cloud,avg_temp,avg_ws,total_precip))
return(weekend_good)
def assess_weekends(data,weekend_data_list,start_weekend_list,index):
second_weekend_partial = False
if start_weekend_list[0] != 0: #weekend isn't today
first_weekend_index = start_weekend_list[0]
weekend_data = data.iloc[first_weekend_index:first_weekend_index+48]
print("First weekend")
first_weekend_good = evaluate_thresholds(weekend_data)
first_weekend_date = weekend_data['DATETIME'][first_weekend_index]
else:
first_weekend_good = False #don't report a weekend thats already happening
print("first weekend happening now")
second_weekend_index = start_weekend_list[1]
if index - second_weekend_index < 48: #partial weekend
second_weekend_partial = True
weekend_data = data.iloc[second_weekend_index:]
print("Second weekend")
second_weekend_good = evaluate_thresholds(weekend_data)
second_weekend_date = weekend_data['DATETIME'][second_weekend_index]
else:
weekend_data = data.iloc[second_weekend_index:]
print("Second weekend")
second_weekend_good = evaluate_thresholds(weekend_data)
second_weekend_date = weekend_data['DATETIME'][second_weekend_index]
return(first_weekend_good,first_weekend_date,second_weekend_good,second_weekend_date, second_weekend_partial)
def Alert_GFS_10day(location):
GFS_10DAY = "https://spotwx.com/products/grib_index.php?model=gfs_pgrb2_0p25_f&lat=XXXX&lon=YYYY&tz=America/Edmonton&display=table"
slack_update(location.name + " GFS")
slack_update(GFS_10DAY)
GFS_10DAY = GFS_10DAY.replace("XXXX",str(location.lat))
GFS_10DAY = GFS_10DAY.replace("YYYY",str(location.long))
print(GFS_10DAY)
data = get_csv(GFS_10DAY)
weekend_data_list,start_weekend_list,index = find_weekends(data)
first_weekend,first_weekend_date, second_weekend, second_weekend_date, second_weekend_partial = assess_weekends(data,weekend_data_list,start_weekend_list,index)
print("GFS %s First weekend is good? %r, Second weekend is good? %r, Second weekend is partial? %r" % (location.name,first_weekend,second_weekend,second_weekend_partial))
return first_weekend, first_weekend_date, second_weekend, second_weekend_date, second_weekend_partial
if (__name__ == '__Alert_GFS_10day__'):
main_exit = Alert_GFS_10day()
sys.exit(main_exit)