forked from bcgov/aq-level2-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_01-importData.Rmd
More file actions
403 lines (341 loc) · 11.2 KB
/
Copy path_01-importData.Rmd
File metadata and controls
403 lines (341 loc) · 11.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
---
title: "Import and Prep Data"
output: html_notebook
---
```{r setup}
library(tidyverse)
library(magrittr)
library(envair)
```
```{r declareParameters}
# YEAR TO VALIDATE:
yearToValidate<-2022
parameters <- tolower(
c(
"CO",
"H2S",
"HF",
"HUMIDITY",
"NO",
"NO2",
"NOX",
"O3",
"PM10",
"PM25",
"SO2",
"TEMP_MEAN",
"TRS",
"WDIR_VECT",
"WSPD_SCLR"
)
)
```
```{r importUnverifiedData}
data<-readr::read_rds("unverified_data.rds")
system.time({
data<-envair::importBC_data(
parameter_or_station = parameters,
years=2022,
use_openairformat = FALSE
) %>%
dplyr::distinct(.) %>%
dplyr::filter(lubridate::year(DATE_PST) == 2022)
})
# look for duplicates by validation status. data in yearToValidate should only have
# validation status 0
data %>%
dplyr::distinct(year=lubridate::year(DATE_PST),
STATION_NAME_FULL,
PARAMETER,
VALIDATION_STATUS) %>% utils::View(.)
# remove STATION_NAME that are NA's:
data %<>%
dplyr::filter(!is.na(STATION_NAME))
# look for duplicates
duplicates<-data %>%
dplyr::group_by(DATE_PST,
STATION_NAME_FULL,
STATION_NAME,
PARAMETER,
VALIDATION_STATUS,
INSTRUMENT) %>%
dplyr::summarise(n = dplyr::n(),
.groups = "drop") %>%
dplyr::filter(n > 1L) %>%
dplyr::left_join(.,
data,
by=c("DATE_PST",
"STATION_NAME_FULL",
"STATION_NAME",
"PARAMETER",
"VALIDATION_STATUS",
"INSTRUMENT"))%>%
dplyr::arrange(DATE_PST,
STATION_NAME,
STATION_NAME_FULL,
PARAMETER) %>%
dplyr::select(DATE_PST,
STATION_NAME,
STATION_NAME_FULL,
PARAMETER,
INSTRUMENT,
RAW_VALUE,
everything())
# Merritt Nicola Ave MAML has been updated (data was not padding and first started in feb),
#so replace in data:
merrittMAML<-envair::importBC_data(parameter_or_station="Merritt Nicola Ave MAML",
year=2022,
use_openairformat = FALSE)
merrittMAML %<>% dplyr::filter(lubridate::year(DATE_PST)==2022)
merrittMAML %>%
dplyr::filter(is.na(INSTRUMENT))
data %<>%
# remove previously imported merritt maml data for 2022
dplyr::filter(!(STATION_NAME %in% "Merritt Nicola Ave MAML" &
lubridate::year(DATE_PST)==2022)) %>%
# bind updated merrit maml data
dplyr::bind_rows(.,
merrittMAML)
# port edward sunset drive has some instrument == NA's (with all NA data)
#remove it
data %<>%
dplyr::filter(!(STATION_NAME %in% "Port Edward Sunset Drive" &
is.na(INSTRUMENT) &
lubridate::year(DATE_PST)==yearToValidate))
readr::write_rds(data,"unverified_data.rds")
```
```{r importPrevYrWind}
# import previous x years wind data for comparison
prevYrWind <- envair::importBC_data(
parameter_or_station = c("wdir_vect",
"wspd_sclr"),
years = (yearToValidate - 6):(yearToValidate - 1),
use_openairformat = FALSE
)
#save prevYrWind
readr::write_rds(prevYrWind,
"prevYrWind_raw.rds")
```
```{r prevYrWindYears}
prevYrWind<-readr::read_rds("prevYrWind_raw.rds")
# check what years were imported
prevYrWind %>%
dplyr::distinct(year=lubridate::year(DATE_PST)) %>%
dplyr::arrange(year)# imports data in years that were not requested
prevYrWind %<>% # filter for last X years of data (double check):
dplyr::filter(lubridate::year(DATE_PST) %in% (yearToValidate - 6):(yearToValidate - 1)) %>%
dplyr::distinct(.)
#save prevYrWind
readr::write_rds(prevYrWind,
"prevYrWind.rds")
```
```{r prevYrWindNAs}
prevYrWind<-readr::read_rds("prevYrWind.rds")
# there are NA's for STATION_NAME_FULL and for VALIDATION_STATUS
prevYrWind %>%
dplyr::filter(is.na(STATION_NAME_FULL) & is.na(VALIDATION_STATUS)) #5,468,551 rows
# if it's the same number as is.na(STATION_NAME_FULL) | is.na(VALIDATION_STATUS),
# then it's always both of them that are NA
prevYrWind %>%
dplyr::filter(is.na(STATION_NAME_FULL) | is.na(VALIDATION_STATUS)) #5,468,551 rows
# the above shows that STATION_NAME_FULL is always NA when VALIDATION_STATUS is NA
# summary of when these NA's occur:
prevYrWind %>%
dplyr::filter(is.na(STATION_NAME_FULL) & is.na(VALIDATION_STATUS)) %>%
dplyr::group_by(year=lubridate::year(DATE_PST),
STATION_NAME_FULL,
STATION_NAME,
PARAMETER) %>%
dplyr::summarise(`# NA's`=n()) # there are lots of them at different stations,
# in different years and for both WDIR_VECT and WSPD_SCLR
# filter out these NA's
prevYrWind %<>%
dplyr::filter(!(is.na(STATION_NAME_FULL) & is.na(VALIDATION_STATUS))) #reduces
# data set by ~5 million obs!
#save prevYrWind
readr::write_rds(prevYrWind,
"prevYrWind.rds")
```
```{r prevYrWindValidationStatus}
prevYrWind<-readr::read_rds("prevYrWind.rds")
# look for duplicates by validation status. years 2016-2021 should only have
# validation status = Level 2
doubleValidationStatus<-prevYrWind %>%
dplyr::distinct(STATION_NAME_FULL,
STATION_NAME,
PARAMETER,
year=lubridate::year(DATE_PST),
VALIDATION_STATUS,
INSTRUMENT) %>%
dplyr::arrange(year,STATION_NAME_FULL,PARAMETER,INSTRUMENT) %>%
dplyr::group_by(STATION_NAME_FULL,
STATION_NAME,
PARAMETER,
INSTRUMENT,
year) %>%
dplyr::filter(n()>1)
# which years does this happen for?
doubleValidationStatus %>%
dplyr::ungroup() %>%
dplyr::distinct(year) # only 2021
# look at the double validation status in the hourly data:
duplicates <- prevYrWind %>%
dplyr::group_by(DATE_PST,
STATION_NAME_FULL,
STATION_NAME,
PARAMETER,
VALIDATION_STATUS,
INSTRUMENT) %>%
dplyr::summarise(n = dplyr::n(),
.groups = "drop") %>%
dplyr::filter(n > 1L) %>%
dplyr::left_join(
.,
prevYrWind,
by = c(
"DATE_PST",
"STATION_NAME_FULL",
"STATION_NAME",
"PARAMETER",
"VALIDATION_STATUS",
"INSTRUMENT"
)
) %>%
dplyr::arrange(DATE_PST,
STATION_NAME,
STATION_NAME_FULL,
PARAMETER) %>%
dplyr::select(
DATE_PST,
STATION_NAME,
STATION_NAME_FULL,
PARAMETER,
INSTRUMENT,
RAW_VALUE,
everything()
)
# no duplicates!
# look at an example to see if validation status changes part-way through the yr
courtenay<-prevYrWind %>%
dplyr::filter(STATION_NAME == "Courtenay Elementary School")
ggplot2::ggplot(courtenay,
aes(x=DATE_PST,
y=RAW_VALUE,
color=VALIDATION_STATUS)) +
ggplot2::geom_point() +
ggplot2::facet_wrap(~PARAMETER)
courtenay %>%
dplyr::group_by(PARAMETER,
VALIDATION_STATUS) %>%
dplyr::summarise(start=min(DATE_PST),
end=max(DATE_PST))
# all of the level 0 validation status in 2021 occurs jan 1 00:00, but not at all stations
prevYrWind %>%
dplyr::filter(lubridate::year(DATE_PST)==2021) %>%
dplyr::group_by(STATION_NAME_FULL,
STATION_NAME,
PARAMETER,
VALIDATION_STATUS) %>%
dplyr::summarise(start=min(DATE_PST),
end=max(DATE_PST),
.groups = "drop") %>%
dplyr::arrange(STATION_NAME,
STATION_NAME_FULL,
PARAMETER,
VALIDATION_STATUS) %>% #utils::View(.)
dplyr::ungroup() %>%
dplyr::filter(VALIDATION_STATUS=="Level 2") %>%
dplyr::distinct(STATION_NAME_FULL,start,end)
# fix - make all data in 2021 have validation status = level 2
prevYrWind %<>%
dplyr::mutate(VALIDATION_STATUS=dplyr::if_else(
lubridate::year(DATE_PST)==2021,
"Level 2",
VALIDATION_STATUS
))
#save prevYrWind
readr::write_rds(prevYrWind,
"prevYrWind.rds")
```
```{r prevYrWind_60issue}
prevYrWind<-readr::read_rds("prevYrWind.rds")
# have a closer look at station_name_full
summary_stnNameFull<-prevYrWind %>%
dplyr::group_by(STATION_NAME,
STATION_NAME_FULL,
PARAMETER,INSTRUMENT) %>%
dplyr::summarise(start=min(DATE_PST),
end=max(DATE_PST),
n=n(),
`values`=sum(!is.na(RAW_VALUE)),
`NAs`=sum(is.na(RAW_VALUE)),
`allNAs`=n()==sum(is.na(RAW_VALUE)))
utils::View(summary_stnNameFull)
# find combo's that are all NA's and remove them from prevYrWind:
prevYrWind<-summary_stnNameFull %>%
dplyr::filter(allNAs==TRUE) %>% #utils::View(.)
dplyr::select(STATION_NAME,
STATION_NAME_FULL,
PARAMETER,
INSTRUMENT) %>%
dplyr::anti_join(
prevYrWind,
.,
by=c("STATION_NAME",
"STATION_NAME_FULL",
"PARAMETER",
"INSTRUMENT")
)
#save prevYrWind
readr::write_rds(prevYrWind,
"prevYrWind.rds")
# # # # # FIND DUPLICATES
windDuplicates<-prevYrWind %>%
dplyr::group_by(DATE_PST, STATION_NAME, PARAMETER) %>%
dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
dplyr::filter(n > 1L) %>%
dplyr::left_join(.,
prevYrWind,
by=c("DATE_PST","STATION_NAME","PARAMETER")) # no duplicates!!!!
```
```{r data}
# bind the two data sets together, call it data
data<-dplyr::bind_rows(readr::read_rds("unverified_data.rds"),
readr::read_rds("prevYrWind.rds")
) %>%
dplyr::distinct(.)
```
```{r saveExternally}
# save list of stations in yearToValidate with data:
allStations<-data %>%
dplyr::filter(lubridate::year(DATE_PST) == yearToValidate) %>%
dplyr::group_by(STATION_NAME) %>%
dplyr::filter(!is.na(RAW_VALUE)) %>%
dplyr::distinct(STATION_NAME) %>%
dplyr::arrange(STATION_NAME)
# save allStations as rds
readr::write_rds(allStations,
"allStations.rds")
# read in allStations
# allStations<-readr::read_rds(file="allStations.rds")
# save allStations as csv
readr::write_csv(allStations,
"allStations.csv")
# create preppedData folder if it doesn't already exist
ifelse(dir.exists(file.path("./preppedData")),
"preppedData folder already exists",
dir.create(file.path("./preppedData"
)))
# Save each station to preppedData folder
purrr::walk(readr::read_rds("allStations.rds")$STATION_NAME,
function(x) {
# test
# x<-"Abbotsford A Columbia Street"
# end test
stnData <- data %>%
dplyr::filter(STATION_NAME == x)
readr::write_rds(stnData,
file.path("./preppedData",
stringr::str_c(x, ".rds", collapse = "")))
})
```