-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1 - dataprep - weather.R
185 lines (142 loc) · 5.29 KB
/
1 - dataprep - weather.R
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
# Set up environment -----------------------------------------------------------
## Libraries
"openxlsx" %>=>% libInstall %!=>% library(.., char = T)
"reshape2" %>=>% libInstall %!=>% library(.., char = T)
# Import data ------------------------------------------------------------------
## Load excel files
datafile <- paste0(getwd(), "/Data/data.xlsx")
"w2015" %=>% read.xlsx(datafile, .., na.strings = "NA") %->% weather2015
"w2016" %=>% read.xlsx(datafile, .., na.strings = "NA") %->% weather2016
"w2017" %=>% read.xlsx(datafile, .., na.strings = "NA") %->% weather2017
## Experiment start date
experimentStart <- c(
`2015` = as.Date("2015-8-26"),
`2016` = as.Date("2016-3-15"),
`2017` = as.Date("2017-4-3")
)
## Merge datasets from all experiments
list(weather2015, weather2016, weather2017) %>=>%
{
if (any(sapply(.., x ->> names(x) != names(..[[1]])))) stop("!")
} %=>%
do.call(rbind, ..) %=>%
..[complete.cases(..), ] %->%
weatherImp %=>%
rm("weather2015", "weather2016", "weather2017", envir = globalenv())
# Data manipulation ------------------------------------------------------------
## Function
metric <- function(data, unit) {
data %=>% seq_along %=>% lapply(.., x ->> {
switch(unit[x],
"F" = data[, x] %=>% (.. - 32) * 5 / 9,
"in" = data[, x] %=>% (.. * 25.4),
"mph" = data[, x] %=>% (.. * 16 / 36),
data[, x]
)
}) %=>%
do.call(data.frame, ..) %=>%
setNames(.., names(data))
}
## Convert units to metric
weatherUnits <- c(rep("N", 7), "F", "%", "in", "mph", "F", "F", "F", "W/m^2")
weatherImp %=>% metric(.., weatherUnits) %->% weatherAll
# Get date and time of day to calculate daily averages -------------------------
weatherAll %<=>%
mutate(
..,
date = as.Date(paste(Year, Month, Day), "%Y %b %d") -
ifelse(paste0(Hour, Min, Meridian) == "120AM", 1, 0),
time = ifelse(
(Hour %in% 9:11 & Meridian == "PM") |
(!Hour %in% c(7:11) & Meridian == "AM"),
"night", "day"
)
)
# Daily aggregates -------------------------------------------------------------
## Daily weather Max
weatherMax <- gather(
weatherAll,
Rain + Temp.60cm + Temp.2m + Wind + Humidity + Dew.Point ~ date,
max
)
## Daily weather Min
weatherMin <- gather(
weatherAll,
Temp.60cm + Temp.2m + Wind + Humidity + Dew.Point ~ date,
min
)
## Daily weather Sum
weatherSum <- gather(weatherAll, Rain ~ date, sum)
## Daily weather Average
weatherMean <- gather(
weatherAll,
Temp.60cm + Temp.2m + Wind + Humidity + Dew.Point + Temp.Wet + Radiation ~
date,
mean
)
## Daily and Nightly Temperature means
weatherTemp2m <- cast(weatherAll, date ~ time, mean, "Temp.2m")
weatherTemp60cm <- cast(weatherAll, date ~ time, mean, "Temp.60cm")
# Merge aggregated dataframes---------------------------------------------------
weatherMerge <- Reduce(
.(x, y) ->> merge(x, y, by = "date", all.x = T),
list(
weatherMean,
weatherSum,
weatherMin,
weatherMax,
weatherTemp2m,
weatherTemp60cm
)
)
# Scaling weather values -------------------------------------------------------
## Calculate scaled values and scale for reverting
weatherMerge %=>%
..[, sapply(.., is.numeric)] %=>%
data.frame(mean = colMeans(..), sd = apply(.., 2, sd)) %->%
weatherScale
## Merge scaled value and unscaled date
weatherMerge %=>%
data.frame(
date = ..[, !sapply(.., is.numeric)],
scale(..[, sapply(.., is.numeric)])
) %->%
weather
# Weekly averages with daily lags ----------------------------------------------
byWeek <- function(.x, .weeks) {
.weeks %=>% names %=>% strsplit(.., "\\.") %=>>% tail(.., 1) %->% func
mean <- function(...) base::mean(c(...)) #base mean doesn't handle input
seq_along(.weeks) %=>%
sapply(.., y ->> do.call(func[[y]], list(.weeks[.x, y]))) %=>%
t
}
## Function for calculation weekly values
slidingAverage <- function(.data, lag = 0, weeks) {
row0 <- which(.data$date == experimentStart[substring(.data$date, 1, 4)])
dataWeeks <- .data[, sapply(.data, is.numeric)]
lapply(lag, i ->> { # for each lag period
((min(weeks) - 1) * 7 + 1):(max(weeks) * 7) %=>%
c(.. - i + row0) %=>%
split(.., sort(.. %% (max(weeks) - min(weeks) + 1))) %=>>%
byWeek(.., dataWeeks) %=>% # compute weekly values
do.call(rbind.data.frame, ..) %=>%
data.frame(.data$date[row0 + weeks * 7], .., row.names = NULL) %=>%
setNames(.., names(.data))
})
}
## Split by year and do sliding average, then merge
split(weather, substring(weather$date, 1, 4)) %=>>%
slidingAverage(.., lag = 0:25, weeks = 3:9) %=>%
do.call(mapply, c(list(rbind, SIMPLIFY = F), ..)) %->%
env
# Cleaning up ------------------------------------------------------------------
## Save dataframes as R object
c("weather", "env", "weatherScale") %=>%
save(list = .., file = paste0(getwd(), "/Data/weather.rda"))
## Remove old dataframes
rm(
"weatherAll", "weatherMean", "weatherSum", "weatherMin",
"weatherMax", "weatherTemp2m", "weatherTemp60cm", "weatherMerge"
)
## Load saved .rda
"/Data/weather.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())