-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
409 lines (357 loc) · 14.2 KB
/
functions.py
File metadata and controls
409 lines (357 loc) · 14.2 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import folium
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
def view_station_date_ranges(df: pd.DataFrame, metric: str) -> pd.DataFrame:
"""
Returns a data frame that shows the min and max date for each
station in the data.
Args:
df (data frame) - raw weather data
metric (string) - choose a metric in the weather data: TMAX, TMIN, etc.
Returns:
return_df (data frame) - filtered data frame that meets the criteria
"""
# Filter out stations that do not have non-null data for the metric
df = df[~df[metric].isnull()]
station_list = pd.DataFrame()
for station in df.STATION.unique():
a = df[df["STATION"] == station]
a = a.assign(min_date=pd.to_datetime(min(a["DATE"])))
a = a.assign(max_date=pd.to_datetime(max(a["DATE"])))
station_list = pd.concat([station_list, a])
return_df = (
station_list[["STATION", "NAME", "min_date", "max_date"]]
.drop_duplicates()
.sort_values("STATION")
.reset_index(drop=True)
)
return return_df
def map_stations(
df: pd.DataFrame,
default_lat: float = 45.9,
default_long: float = -122.3,
default_zoom: float = 9,
) -> folium.map:
"""
Maps the stations on a folium map
Args:
df (data frame) - geospatial data by station, such as latitude
default_lat (float) - the latitude that the map starts at
default_long (float) - the longitude that the map starts at
default_zoom (float) - the zoom that the map starts at
Returns:
m (folium.Map) - displays the results_df on a folium map
"""
# Create the folium map and center on the default location
m = folium.Map(
[default_lat, default_long],
zoom_start=default_zoom,
)
# Add darkgreen circles for destinations
for j in range(len(df)):
folium.CircleMarker(
location=[df.iloc[j]["LATITUDE"], df.iloc[j]["LONGITUDE"]],
tooltip=df.iloc[j][["STATION", "ELEVATION"]],
color="darkblue",
fill=True,
fill_opacity=0.7,
radius=8,
).add_to(m)
folium.TileLayer("OpenTopoMap").add_to(m)
folium.TileLayer("OpenStreetMap").add_to(m)
folium.LayerControl().add_to(m)
return m
def plot_temp_compare(df: pd.DataFrame, metric: str, year: int):
"""
Plots of the temperature for two locations, on a lineplot, for a year
Args:
df (data frame) - weather data
metric (string) - choose a metric in the weather data: TMAX, TMIN, or TAVG
year (integer) - filter on a year
Returns:
p (figure) - plot of the temperature comparison for a year
"""
# Stack the metric of the locations; make the wide data long
x = pd.melt(
df[df["year"] == year],
id_vars="DATE",
value_vars=list(df.columns[df.columns.str.contains(metric)]),
)
x["DATE"] = pd.to_datetime(x["DATE"])
ax = sns.set(rc={"figure.figsize": (30, 20)})
p = sns.lineplot(x, x="DATE", y="value", hue="variable", linewidth=4)
ax = p.tick_params(axis="x", labelsize=20)
ax = p.tick_params(axis="y", labelsize=20)
ax = p.set_xlabel("\n Months \n", fontsize=20, rotation=0)
ax = p.set_ylabel("\n Temparature (F) \n", fontsize=20, rotation=90)
ax = p.set_title("\nTemperature Comparison %s\n" % year, fontsize=30)
ax = plt.xticks(rotation=90)
ax = plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m"))
ax = plt.legend(fontsize=20)
return p
def plot_monthly_temp_plots(
df: pd.DataFrame, metric: str, ylim_low: int = -20, ylim_high: int = 120
):
"""
Plots of the temperature for two locations, on a lineplot, for a year
Args:
df (data frame) - weather data
metric (string) - choose a metric in the weather data: TMAX, TMIN, or TAVG
ylim_low (integer) - the lowest range on the y-axis plot
ylim_high (integer) - the higher range on the y-axis plot
Returns:
p (figure) - plot of the temperature comparison for a year
"""
assert df.TMAX.min() > ylim_low
assert df.TMAX.max() < ylim_high
p = plt.figure(figsize=(15, 5))
p = sns.boxenplot(data=df, x="month", y="TMAX", linewidth=1.5)
p = plt.title("\nDaily %s Temperatures\n" % metric, fontsize=20)
p = plt.xlabel(" ", fontsize=15)
p = plt.xticks(
ticks=np.arange(0, 12),
fontsize=15,
labels=[
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
],
)
p = plt.ylabel("\n%s Temperature (°F)\n" % metric, fontsize=15)
p = plt.ylim(ylim_low, ylim_high)
p = plt.show()
return p
def ideal_temp(
df: pd.DataFrame, tmin_or_tmax: str, ideal_min_limit: int, ideal_max_limit: int
) -> pd.DataFrame:
"""
If we defined ideal weather as days where the high temperature is between X and Y degrees,
then how many days per year of this ideal high temperature happen per location?
Displays the number of days per year where the maximum temperature is between ideal_min and ideal_max.
Args:
df (DataFrame): DataFrame containing temperature data with columns for TMAX or TMIN at different locations.
ideal_min_limit (int): Minimum temperature for ideal weather.
ideal_max_limit (int): Maximum temperature for ideal weather.
Returns:
DataFrame: A DataFrame with the number of ideal weather days per year for both locations
and the difference in counts.
"""
ideal_weather1 = df[
(df[tmin_or_tmax] >= ideal_min_limit) & (df[tmin_or_tmax] <= ideal_max_limit)
]
ideal_weather2 = ideal_weather1.groupby(["NAME", "year"], as_index=False).agg(
{"DATE": "count"}
)
ideal_weather2 = ideal_weather2.rename(columns={"DATE": "ideal_days"})
ideal_weather_avg = (
ideal_weather2.groupby("NAME")
.agg({"ideal_days": "mean"})
.sort_values("ideal_days", ascending=False)
)
# Prepare data for seaborn barplot
p = plt.figure(figsize=(10, 6))
p = sns.barplot(data=ideal_weather2, x="year", y="ideal_days", hue="NAME")
p = plt.xlabel("Year")
p = plt.ylabel(
"Number of Days per Year (%i°F to %i°F)" % (ideal_min_limit, ideal_max_limit)
)
p = plt.title(
"\nNumber of Great Weather Days (%s from %i°F to %i°F) \n"
% (tmin_or_tmax, ideal_min_limit, ideal_max_limit)
)
p = plt.legend(loc="lower left")
p = plt.tight_layout()
p = plt.show()
return p, ideal_weather_avg
def tmin_annual_plot(df: pd.DataFrame) -> pd.DataFrame:
"""
Plots the minimum TMIN temperature per year per station
Args:
df (DataFrame): DataFrame containing TMIN temperature data
Returns:
p (plot): plot of TMIN for each station, over time
tmin (DataFrame): data to use dor the USDA plant hardiness zones plot
"""
# Pivot df to see the minimum temperature by year for each station
tmin = df.pivot_table(
index="year", columns="NAME", values="TMIN", aggfunc="min"
).reset_index()
# Calculate the USDA hardiness zone for each station and year for all columns except the first (year)
for col in tmin.columns[1:]:
zone_col = f"{col.replace('TMIN_', '').replace('_', ' ')} Hardiness Zone"
tmin[zone_col] = tmin[col].rolling(window=30, min_periods=30).mean()
# Store the number of stations
n = len(df["STATION"].unique())
# Create a list of the column names for all_locations
all_locations = list(tmin.columns[1 : n + 1])
# Make the wide format long, for the seaborn lineplot
tmin_long = tmin.reset_index().melt(
id_vars="year", value_vars=all_locations, var_name="station", value_name="tmin"
)
# Create the plot
sns.set_theme(style="whitegrid")
# sns.set_theme(style="darkgrid")
# plt.style.use('dark_background')
p = plt.figure(figsize=(15, 5))
p = sns.lineplot(
data=tmin_long,
x="year",
y="tmin",
hue="station",
# palette=['green', 'orange', 'blue'],
marker="o",
markersize=5,
linewidth=2,
style="station",
dashes=False,
# legend='full',
)
p = plt.title("\nMinimum Temperature by Year and Station\n")
p = plt.ylim(-30, 60)
p = plt.xlabel("\nYear", fontsize=12, rotation=0)
p = plt.ylabel("\nMinimum Temperature per Year (°F)\n", fontsize=12, rotation=90)
p = plt.legend(loc="center left", bbox_to_anchor=(0, 0.9))
# Suppress printouts
p = plt.show()
return p, tmin
def usda_plant_hardiness_zone(df: pd.DataFrame, legend_location: str) -> pd.DataFrame:
"""
A USDA plant hardiness zone is defined by the minimum annual temperature at a station, averaged over the last 30 years. For example, by this metric:
* USDA Zone 8a falls within 10°F and 15°F
* USDA Zone 8b falls within 15°F and 20°F
* USDA Zone 9a falls within 20°F and 25°F
Args:
df (DataFrame): DataFrame containing TMIN temperature data
legend_location (string): Allows input fto change where the legend is located
Returns:
p (plot): plot of USDA plant hardiness zones for each station, over time
"""
# Count the number of stations
n = int((len(df.columns) - 1) / 2)
# Make the wide format long, for the seaborn lineplot
usdahz = (
df.reset_index()
.melt(
id_vars="year",
value_vars=list(df.columns[n + 1 :]),
var_name="station",
value_name="USDA Hardiness Zone",
)
.dropna()
)
sns.set_theme(style="whitegrid")
p = plt.figure(figsize=(15, 5))
p = sns.lineplot(
data=usdahz,
x="year",
y="USDA Hardiness Zone",
hue="station",
# palette=['green', 'blue', 'orange'],
marker="o",
markersize=5,
linewidth=2,
style="station",
dashes=False,
)
# Superimpose colored bands for USDA zones
# https://colorbrewer2.org/#type=sequential&scheme=YlOrBr&n=3
p.axhspan(-20, -15, color="#9ecae1", alpha=0.15, label="Zone 5a (-20 to -15°F)")
p.axhspan(-15, -10, color="#3182bd", alpha=0.15, label="Zone 5b (-15 to -10°F)")
p.axhspan(-10, -5, color="#c2e699", alpha=0.15, label="Zone 6a (-10 to -5°F)")
p.axhspan(-5, 0, color="#78c679", alpha=0.15, label="Zone 6b (-5 to 0°F)")
p.axhspan(0, 5, color="#31a354", alpha=0.15, label="Zone 7a (0 to 5°F)")
p.axhspan(5, 10, color="#006837", alpha=0.15, label="Zone 7b (5 to 10°F)")
p.axhspan(10, 15, color="#fef0d9", alpha=0.15, label="Zone 8a (10 to 15°F)")
p.axhspan(15, 20, color="#fdcc8a", alpha=0.15, label="Zone 8b (15 to 20°F)")
p.axhspan(20, 25, color="#fc8d59", alpha=0.15, label="Zone 9a (20 to 25°F)")
p.axhspan(25, 30, color="#e34a33", alpha=0.15, label="Zone 9b (25 to 30°F)")
p.axhspan(30, 35, color="#b30000", alpha=0.15, label="Zone 10a (30 to 35°F)")
# Add zone labels
year_label = 2024
p.text(x=year_label, y=-17, s="5a", color="black", fontsize=14, va="center")
p.text(x=year_label, y=-12, s="5b", color="black", fontsize=14, va="center")
p.text(x=year_label, y=-7, s="6a", color="black", fontsize=14, va="center")
p.text(x=year_label, y=-2, s="6b", color="black", fontsize=14, va="center")
p.text(x=year_label, y=3, s="7a", color="black", fontsize=14, va="center")
p.text(x=year_label, y=8, s="7b", color="black", fontsize=14, va="center")
p.text(x=year_label, y=13, s="8a", color="black", fontsize=14, va="center")
p.text(year_label, 18, "8b", color="black", fontsize=14, va="center")
p.text(year_label, 23, "9a", color="black", fontsize=14, va="center")
p.text(year_label, 28, "9b", color="black", fontsize=14, va="center")
p.text(year_label, 33, "10a", color="black", fontsize=14, va="center")
plt.title("\nUSDA Hardiness Zone by Year\n")
plt.ylim(-20, 35)
p.set_yticks(np.arange(-20, 36, 5))
p.set_yticklabels(
[
"-20°F",
"-15°F",
"-10°F",
"5°F",
"0°F",
"5°F",
"10°F",
"15°F",
"20°F",
"25°F",
"30°F",
"35°F",
]
)
p.set_xlabel("\nYear", fontsize=12, rotation=0)
p.set_ylabel(
"\n30-year-Average-Minimum Temperature (°F)\n", fontsize=12, rotation=90
)
plt.legend(list(df.columns[-n:]), loc=legend_location)
return plt.show()
def non_ideal_temp_days(df, tmin_threshold, tmax_threshold):
"""
Calculate the average annual number of non-ideal temperature days for each location.
Args:
df (pd.DataFrame): DataFrame with temperature data
tmin_threshold (float): Maximum temperature threshold for too cold days
tmax_threshold (float): Minimum temperature threshold for too hot days
Returns:
pd.DataFrame: DataFrame with average annual non-ideal temperature days for each location.
"""
# too_cold
too_cold = df[(df["TMIN"] <= tmin_threshold)]
too_cold_yearly = too_cold.groupby(["NAME", "year"], as_index=False).agg(
{"DATE": "count"}
)
too_cold_yearly_avg = too_cold_yearly.groupby(["NAME"], as_index=False).agg(
{"DATE": "mean"}
)
too_cold_yearly_avg = too_cold_yearly_avg.rename(
columns={"DATE": "avg_days_too_cold"}
)
# too_hot
too_hot = df[df["TMAX"] >= tmax_threshold]
too_hot_yearly = too_hot.groupby(["NAME", "year"], as_index=False).agg(
{"DATE": "count"}
)
too_hot_yearly_avg = too_hot_yearly.groupby(["NAME"], as_index=False).agg(
{"DATE": "mean"}
)
too_hot_yearly_avg = too_hot_yearly_avg.rename(columns={"DATE": "avg_days_too_hot"})
too = too_cold_yearly_avg.merge(
too_hot_yearly_avg,
on=["NAME"],
how="inner",
)
too["avg_days_too_cold"] = round(too["avg_days_too_cold"], 0).astype(int)
too["avg_days_too_hot"] = round(too["avg_days_too_hot"], 0).astype(int)
too["non_ideal_days"] = too["avg_days_too_cold"] + too["avg_days_too_hot"]
return too.sort_values(by="non_ideal_days")