forked from hydro-lab/wind-power
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwind-rose.r
More file actions
80 lines (73 loc) · 3.23 KB
/
wind-rose.r
File metadata and controls
80 lines (73 loc) · 3.23 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
## Wind data from Pittsburgh International Airport.
## Example of rose plot of wind direction
library(tidyverse) # includes ggplot and readr commands
library(RColorBrewer) # used for wind rose with color code by speed
## Pittsburgh International Airport (PIT)
pit <- read_csv("https://duq.box.com/shared/static/i9qlh63qdzf5hqvf40dphkbwkh93o2h0.csv")
pit.dir <- pit$`Wind Dir` # degrees
pit.spd <- 0.51444444444 * pit$`Wind Speed` # converted to m/s from knots, per http://www.climate.psu.edu/data/current/help.php
## Heinz Field (HFP)
hfp <- read_csv("https://duq.box.com/shared/static/2cs6xi81xtcmq4mmi46t0v0ev4f2mehs.csv")
hfp.dir <- hfp$`Wind Vane` # degrees
hfp.spd <- 0.44704 * hfp$Anemometer # converted to m/s from mph, per https://allegheny.weatherstem.com/pitt
## OLD METHOD - no speed binning
# br <- 10*(c(0:36)) # This array constructs the bins in degrees
# h <- hist(hfp.dir, breaks = br)
## Make rose plot, based on:
## https://stackoverflow.com/questions/39024758/how-to-use-r-package-circular-to-make-rose-plot-of-histogram-data-on-360/39025913
## https://stackoverflow.com/questions/50163352/plot-wind-rose-in-r
# angle <- h$mids
# count <- h$counts
# y <- data.frame(angle, count)
# ggplot(y, aes(x = angle, y = count)) +
# labs(caption = "Heinz Field") +
# geom_col(fill = "steelblue", color = "steelblue") +
# coord_polar(theta = "x", start = 0) +
# scale_x_continuous(breaks = seq(0, 360, 45)) +
# theme_linedraw() +
# theme(axis.title = element_blank(), panel.ontop = TRUE, panel.background = element_blank())
## NEW METHOD - with speed binning
## sort data:
d <- pit.dir
s <- pit.spd
speed.bins <- ceiling(max(s))
speed.bins <- 6
wind <- array(0, dim = c(36,speed.bins))
for (i in 1:(length(s))) {
# speed.index <- ceiling(s[i])
## for use when speed categories exceed colormap (>9)
if (s[i] <= 2) {
speed.index <- 1
} else if (s[i] <= 4) {
speed.index <- 2
} else if (s[i] <= 6) {
speed.index <- 3
} else if (s[i] <= 8) {
speed.index <- 4
} else if (s[i] <= 10) {
speed.index <- 5
} else {
speed.index <- 6
}
wind[ceiling(d[i]/10),speed.index] <- wind[ceiling(d[i]/10),speed.index] + 1
}
## Now, form long array rather than wide:
wind.long <- array(NA, dim = 36*speed.bins)
for (i in 1:speed.bins) {
for (j in 1:36) {
wind.long[(36*(i-1))+j] <- wind[j,i]
}
}
speeds <- c(rep("0-2",36), rep("2-4",36), rep("4-6",36), rep("6-8",36), rep("8-10",36), rep("above 10",36)) # be sure to fill in as many as the wind bins in "wind" allocation
directions <- rep(5+10*(c(0:35)), speed.bins)
rose <- data.frame(directions, speeds, wind.long)
ggplot(rose, aes(fill = fct_rev(speeds), x = directions, y = wind.long)) +
labs(caption = "Pittsburgh International Airport") +
geom_bar(position="stack", stat="identity") +
scale_fill_brewer("Speed (m/s)", palette = "Blues") +
coord_polar(theta = "x", start = 0) +
scale_x_continuous(breaks = seq(0, 360, 45)) +
theme_linedraw() +
theme(axis.title = element_blank(), panel.ontop = TRUE, panel.background = element_blank())
# caption = "Pittsburgh International Airport" +
# caption = "Heinz Field" +