-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtNrInstrument_data_processing_code.R
More file actions
486 lines (368 loc) · 16.8 KB
/
Copy pathtNrInstrument_data_processing_code.R
File metadata and controls
486 lines (368 loc) · 16.8 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#Written by Melodie Lao (github.com/data-lao) & Leigh Creilley
#feel free to email data-lao/VandenBoer group if you come across any errors or require help in troubleshooting your tNr instrument
#packages used
library(xts)
library(zoo)
library(data.table)
library(lubridate)
library(tidyverse)
library(padr)
rm(list=ls(all=TRUE)) #clear all previous variables in environment
# 1. Data Processing
# Import Data ------------------------------------------------------------
setwd("C:/Users/your_drivename_here")
tnrdata <- read.csv(file.choose(), sep = "\t") #load csv file
## Modify Dataset ------------------------------------------------------------
tnrdata <- tnrdata[ -c(5:6)] #removes v4 and MFC2 flow which are column 5 and 6
tnrdata$date <- dmy_hms(tnrdata$datetime)
colnames(tnrdata) <- c("datetime",
"V1",
"V2",
"V3",
"NO",
"NO2",
"NOx",
"date")
tnrdata$Vt <- rowSums(tnrdata[2:4])
tnrdata <- tnrdata %>%
mutate(tnr_cycle = case_when(
Vt == 0 ~ "tNr",
Vt == 2 ~ "NH3",
Vt == 1 ~ "HONO",
Vt == 3 ~ "NOx"))
#add a cycle count up column.
#cycle defined as a measurement mode, i.e. NOx, HONO, tNr or NH3
tnrdata$Vt<- rowSums(tnrdata[2:4])
foo<-rle(tnrdata$Vt) #creates cycle based on total valve change
foo$values <- 1:length(foo$values)
tnrdata$cycle <- inverse.rle(foo)
ncyc = max(tnrdata$cycle)
TD <- subset(tnrdata, select = c(8,9,5,6,7,10,11)) #re-arrange columns to a new dataframe
TD <- data.table(TD)
# double check if cycle numbers are uneven
TD %>%
select(tnr_cycle, cycle) %>%
table()
options(max.print=999999)
#applied voltage corrections to instrument
TD2 <- TD %>%
mutate(TD, NOcorr = (1.0018*TD$NO) - 0.8569) %>%
mutate(TD, NO2corr = (1.0018*TD$NO2) - 0.8469)
TD2 <- mutate(TD2, NOxcorr = TD2$NOcorr + TD2$NO2corr)
TD3 <- subset(TD2, select = c("date", "Vt", "NOcorr", "NO2corr", "NOxcorr", "tnr_cycle", "cycle"))
#rearrange where number of cycles become columns
pivot_wider(TD3, names_from = cycle, values_from = NOcorr ) -> totcycl_TD
#create new df per each cycle
for(i in unique(TD3$cycle)) {
nam <- paste("TD", i, sep = "_")
assign(nam, TD3[TD3$cycle == i, ])
}
# CYCLE 1 ------------------------------------------------------------
#the length of cycle 1 is not divisible by 60, ruining the averaged start time
#Remove the first rows by nth to make it divisible by 60 for time averaging to a min
rn <- nrow(TD_1) #rn = row number
rm <- rn %% 60 #rm = remainder of row number when divided by 60
TD_1 <- tail(TD_1, -rm)
# CLEAN/AVG ------------------------------------------------------------
## Avg 1 min ------------------------------------------------------------
list_TD <- mget(ls(pattern = 'TD_')) #makes a list of all df with cycles
#apply averaging of every 60 rows to list
AVG_list <- map(list_TD, ~ .x %>%
group_by(date = cut(date, breaks = "60 sec")) %>%
summarize(NO = mean(NOcorr),
NO2 = mean(NO2corr),
NOx = mean(NOxcorr),
Vt = mean(Vt),
cycle = mean(cycle))
)
#NOTE: averaged times are different by seconds in cycle 1 to other cycles due to uneven lengths
## Remove Rows ------------------------------------------------------------
AVG_list2 <- lapply(AVG_list, tail, -1) #removes first 60 rows in this list
#maybe better to replace c(5,6,7) with replace_na by tail, -1
## Combine Data ------------------------------------------------------------
CL <- bind_rows(AVG_list2, .id = "column_label") #combines list in one df, sort by date
CL <- arrange(CL, cycle) #rearrange by date
CLtot <- CL %>%
mutate(tnr_cycle = case_when(
Vt == 0 ~ "tNr",
Vt == 2 ~ "NH3",
Vt == 1 ~ "HONO",
Vt == 3 ~ "NOx"))
#create new df of each tnr_cycle
for(i in unique(CLtot$tnr_cycle)) {
nam1 <- paste("CL", i, sep = "_")
assign(nam1, CLtot[CLtot$tnr_cycle == i, ])
}
# CALCULATIONS ------------------------------------------------------------
## Linear Interp ------------------------------------------------------------
### HONO ------------------------------------------------------------
CL_HONO[['date']] <- as.POSIXct(CL_HONO[['date']],
format = "%Y-%m-%d %H:%M:%S")
#aggregate if the time series don't match up (seconds skipped,etc)
#datetime variable contains missing values, they are left in place in the df with thicken
CL_HONO2 <- thicken(CL_HONO, '1 min')
CL_HONO2 <- CL_HONO2[ -c(2)]
CL_HONO2[['date_min']] <- as.POSIXct(CL_HONO2[['date_min']],
format = "%Y-%m-%d %H:%M:%S")
LI_HONO <- CL_HONO2 %>%
pad(group = 'tnr_cycle', interval = 'min') %>% # Explicitly fill by 1 min
fill_by_value(0)
LI_HONO_2 <- LI_HONO
LI_HONO_2$NO <- na.approx(LI_HONO$NO)
LI_HONO_2$NO2 <- na.approx(LI_HONO$NO2)
LI_HONO_2$NOx <- na.approx(LI_HONO$NOx)
### NOx ------------------------------------------------------------
CL_NOx[['date']] <- as.POSIXct(CL_NOx[['date']],
format = "%Y-%m-%d %H:%M:%S")
CL_NOx2 <- thicken(CL_NOx, '1 min')
CL_NOx2 <- CL_NOx2[ -c(2)]
CL_NOx2[['date_min']] <- as.POSIXct(CL_NOx2[['date_min']],
format = "%Y-%m-%d %H:%M:%S")
LI_NOX <- CL_NOx2 %>%
pad(group = 'tnr_cycle', interval = 'min') %>% # Explicitly fill by 1 min
fill_by_value(0)
LI_NOX_2 <- LI_NOX
LI_NOX_2$NO <- na.approx(LI_NOX$NO)
LI_NOX_2$NO2 <- na.approx(LI_NOX$NO2)
LI_NOX_2$NOx <- na.approx(LI_NOX$NOx)
### NH3 ------------------------------------------------------------
CL_NH3[['date']] <- as.POSIXct(CL_NH3[['date']],
format = "%Y-%m-%d %H:%M:%S")
CL_NH3_2 <- thicken(CL_NH3, '1 min')
CL_NH3_2 <- CL_NH3_2[ -c(2)]
CL_NH3_2[['date_min']] <- as.POSIXct(CL_NH3_2[['date_min']],
format = "%Y-%m-%d %H:%M:%S")
LI_NH3 <- CL_NH3_2 %>%
pad(group = 'tnr_cycle', interval = 'min') %>% # Explicitly fill by 1 min
fill_by_value(0)
LI_NH3_2 <- LI_NH3
LI_NH3_2$NO <- na.approx(LI_NH3$NO)
LI_NH3_2$NO2 <- na.approx(LI_NH3$NO2)
LI_NH3_2$NOx <- na.approx(LI_NH3$NOx)
### tNr ------------------------------------------------------------
CL_tNr[['date']] <- as.POSIXct(CL_tNr[['date']],
format = "%Y-%m-%d %H:%M:%S")
CL_tNr2 <- thicken(CL_tNr, '1 min')
CL_tNr2 <- CL_tNr2[ -c(2)]
CL_tNr2[['date_min']] <- as.POSIXct(CL_tNr2[['date_min']],
format = "%Y-%m-%d %H:%M:%S")
LI_TNR <- CL_tNr2 %>%
pad(group = 'tnr_cycle', interval = 'min') %>% # Explicitly fill by 1 min
fill_by_value(0)
LI_TNR_2 <- LI_TNR
LI_TNR_2$NO <- na.approx(LI_TNR$NO)
LI_TNR_2$NO2 <- na.approx(LI_TNR$NO2)
LI_TNR_2$NOx <- na.approx(LI_TNR$NOx)
## No Interpolation ------------------------------------------------------------
NL_HONO <- merge(LI_HONO, LI_NOX, by = "date_min", all = TRUE)
NL_NH3 <- merge(LI_TNR, LI_NH3, by = "date_min", all = TRUE)
NL_Nr <- merge(NL_HONO, NL_NH3, by = "date_min", all = TRUE)
#merge columns again for vt and cycles
NL_Nr2 <- NL_Nr
NL_Nr2$vt = NL_Nr2$Vt.x.x
NL_Nr2$vt[!is.na(NL_Nr2$Vt.y.x)] = NL_Nr$Vt.y.x[!is.na(NL_Nr$Vt.y.x)]
NL_Nr2$vt[!is.na(NL_Nr2$Vt.x.y)] = NL_Nr$Vt.x.y[!is.na(NL_Nr$Vt.x.y)]
NL_Nr2$vt[!is.na(NL_Nr2$Vt.y.y)] = NL_Nr$Vt.y.y[!is.na(NL_Nr$Vt.y.y)]
NL_Nr2$cycle = NL_Nr2$cycle.x.x
NL_Nr2$cycle[!is.na(NL_Nr2$cycle.x.y)] = NL_Nr$cycle.x.y[!is.na(NL_Nr$cycle.x.y)]
NL_Nr2$cycle[!is.na(NL_Nr2$cycle.y.x)] = NL_Nr$cycle.y.x[!is.na(NL_Nr$cycle.y.x)]
NL_Nr2$cycle[!is.na(NL_Nr2$cycle.y.y)] = NL_Nr$cycle.y.y[!is.na(NL_Nr$cycle.y.y)]
NL_Nr2 <- NL_Nr2[ -c(2,6:9,13:16,20:23, 27:29)]
colnames(NL_Nr2) <- c("date_min",
"HONO.NO",
"HONO.NO2",
"HONO.NOx",
"NOx.NO",
"NOx.NO2",
"NOx.NOx",
"tnr.NO",
"tnr.NO2",
"tnr.NOx",
"nh3.NO",
"nh3.NO2",
"nh3.NOx",
"vt",
'cycle')
#compile original data with accurate datetime averaged to 1 min
O_HONO <- merge(CL_HONO, CL_NOx, by = "date", all = TRUE)
O_NH3 <- merge(CL_tNr, CL_NH3, by = "date", all = TRUE)
O_Nr <- merge(O_HONO, O_NH3, by = "date", all = TRUE)
O_Nr2 <- O_Nr
O_Nr2$vt = O_Nr2$Vt.x.x
O_Nr2$vt[!is.na(O_Nr2$Vt.y.x)] = O_Nr$Vt.y.x[!is.na(O_Nr$Vt.y.x)]
O_Nr2$vt[!is.na(O_Nr2$Vt.x.y)] = O_Nr$Vt.x.y[!is.na(O_Nr$Vt.x.y)]
O_Nr2$vt[!is.na(O_Nr2$Vt.y.y)] = O_Nr$Vt.y.y[!is.na(O_Nr$Vt.y.y)]
O_Nr2$cycle = O_Nr2$cycle.x.x
O_Nr2$cycle[!is.na(O_Nr2$cycle.x.y)] = O_Nr$cycle.x.y[!is.na(O_Nr$cycle.x.y)]
O_Nr2$cycle[!is.na(O_Nr2$cycle.y.x)] = O_Nr$cycle.y.x[!is.na(O_Nr$cycle.y.x)]
O_Nr2$cycle[!is.na(O_Nr2$cycle.y.y)] = O_Nr$cycle.y.y[!is.na(O_Nr$cycle.y.y)]
O_Nr2 <- O_Nr2[ -c(2,6:9,13:16,20:23, 27:29)]
colnames(O_Nr2) <- c("date_min",
"HONO.NO",
"HONO.NO2",
"HONO.NOx",
"NOx.NO",
"NOx.NO2",
"NOx.NOx",
"tnr.NO",
"tnr.NO2",
"tnr.NOx",
"nh3.NO",
"nh3.NO2",
"nh3.NOx",
"vt",
'cycle')
## HONO-NO2 ------------------------------------------------------------
#here we take the avg data and calc HONO and NH3 by differnce using the nearest points
#1st HONO calculation from NOx
#merge files based on base
F_HONO <- merge(LI_HONO_2, LI_NOX_2, by = "date_min", all = TRUE)
# .x columns are HONO pathway, .y columns are true NOx pathway
F_HONO$cycle = F_HONO$cycle.x
F_HONO$cycle[!is.na(F_HONO$cycle.y)] = F_HONO$cycle.y[!is.na(F_HONO$cycle.y)]
F_HONO <- F_HONO[ -c(2,6,7,9,13,14)]
#new df, subtract differences to form new column, HONO
G_HONO <- mutate(F_HONO, HONO = NO2.x-NO2.y)
hono <- subset(G_HONO, select = c("date_min", "HONO" , "NOx.x", "NO2.x", "cycle"))
nox <- subset(F_HONO, select = c("date_min", "NOx.y", "NO2.y", "NO.y"))
colnames(hono) <- c("date_min", "HONO", "NOx.star", "NO2.star", "cycle")
colnames(nox) <- c("date_min", "NOx.true", "NO2.true", "NO")
tp1 <- full_join(nox, hono, by = "date_min")
## tNr-NH3 ------------------------------------------------------------
F_NH3 <- merge(LI_TNR_2, LI_NH3_2, by = "date_min", all = TRUE)
# .x columns are TNr pathway, .y columns are NH3 pathway
F_NH3$cycle = F_NH3$cycle.x
F_NH3$cycle[!is.na(F_NH3$cycle.y)] = F_NH3$cycle.y[!is.na(F_NH3$cycle.y)]
F_NH3 <- F_NH3[ -c(2,6,7,9,13,14)]
G_NH3 <- mutate(F_NH3, NH3 = NOx.x-NOx.y)
#NH3 calculated using NOx
nh3 <- subset(G_NH3, select = c("date_min", "NH3", "cycle"))
tnr <- subset(F_NH3, select = c("date_min", "NOx.x"))
colnames(tnr) <- c("date_min", "tnr")
tp2 <- full_join(tnr, nh3, by = "date_min")
#if we want to offload specific data frames
# write.csv(TD2,file = file.choose(new = T))
#recombine four datasets(NOx, HONO, tNr, NH3)
all <- full_join(tp1, tp2, by = "date_min")
all <- arrange(all, date_min)
all$cycle = all$cycle.x
all$cycle[!is.na(all$cycle.y)] = all$cycle.y[!is.na(all$cycle.y)]
all <- all[-c(8,11)]
# NO2 as true NO2, NO2.star is NO2 + Nr...etc
all2 <- all %>%
group_by(date = cut (date_min, breaks = "5 min")) %>%
summarize(NOx.true = mean(NOx.true, na.rm = TRUE),
NOx.star = mean(NOx.star, na.rm = TRUE),
HONO = mean(HONO, na.rm = TRUE),
NO2.true = mean(NO2.true, na.rm = TRUE),
NO2.star = mean(NO2.star, na.rm = TRUE),
NO = mean(NO, na.rm = TRUE),
NH3 = mean(NH3, na.rm = TRUE),
tnr = mean(tnr, na.rm = TRUE))
# GRAPHS ------------------------------------------------------------
summary(all)
# quick check with a scatter plot
all %>%
gather("key", "value", -date_min) %>%
ggplot(aes (x = date_min,
y = value,
color = key)) +
# geom_line()
geom_point()
all.f %>%
gather("key", "value", -date_min) %>%
ggplot(aes (x = date_min,
y = value,
color = key)) +
# geom_line()
geom_point()
all2 %>%
gather("key", "value", -date) %>%
ggplot(aes (x = date,
y = value,
color = key)) +
geom_point()
NL_Nr2 %>%
gather("key", "value", -date_min) %>%
ggplot(aes (x = date_min,
y = value,
color = key)) +
geom_point()
#offload selective files
# write.csv(NL_NH3, file = file.choose(new = T))
# FINAL DATA ------------------------------------------------------------
#saves the final processed data csv file in your directory
#measured and interpolated data averaged to 1 min
outfile_final=(paste(sub('.csv', '', infile),"_1min_processed_final_data.csv",sep=""))
print(outfile_final)
write.csv(all, outfile_final, row.names=F)
#the measured and interpolated data is averaged to 5 min
outfile_final2=(paste(sub('.csv', '', infile),"_5min_processed_final_data.csv",sep=""))
print(outfile_final)
write.csv(all2, outfile_final2, row.names=F)
#measured data with no interpolation, with rounded datetimes per min
outfile_final3=(paste(sub('.csv', '', infile),"_1min_measurements_no_lin_interp.csv",sep=""))
print(outfile_final)
write.csv(NL_Nr2, outfile_final3, row.names=F)
#measured data with no interpolation, with the exact averaged datetime in 1 min
outfile_final4=(paste(sub('.csv', '', infile),"_1min_ORIGINAL_datetime_measurements.csv",sep=""))
print(outfile_final4)
write.csv(O_Nr2, outfile_final4, row.names=F)
# Compile All ------------------------------------------------------------
# use this code if you want to compile all daily datasets into weekly
library(readr)
library(gridExtra)
library(ggpubr)
library(lmodel2)
rm(list=ls(all=TRUE)) #clear all previous variables
setwd("C:/Users/folder_name_here")
# 1 MIN BIND ------------------------------------------------------------
#first 1 min interpolated and measured data
data.path.1min="C:/Users/your_drive_name_here"
# load files into a list
filenames.1min=list.files(path=data.path.1min, pattern = "Total*",full.names=TRUE)
d.1min <- rbindlist(lapply(filenames.1min, fread, header=T,blank.lines.skip=TRUE))
d.1min$date.m <- ymd_hms(d.1min$date_min)
## Measured only 1 min data ------------------------------------------------------------
data.path.1min.m="C:/Users/drive_name_here"
## Combine All ------------------------------------------------------------
filenames.1min.m=list.files(path=data.path.1min.m, pattern = "Total*",full.names=TRUE)
d.1min.m <- rbindlist(lapply(filenames.1min.m, fread, header=T,blank.lines.skip=TRUE))
d.1min.m$date <- ymd_hms(d.1min.m$date_min)
#select true NOx, NO2 star and tnr measurements
d.1min.meas <- select(d.1min.m, date, NOx.NO, NOx.NO2, NOx.NOx, HONO.NO2, tnr.NOx, nh3.NOx, vt)
colnames(d.1min.meas) <- c("date", "NO.true.meas", "NO2.true.meas", "NOx.true.meas", "NO2.star.meas", "tnr.meas", "tnr.nh3.meas", "Vt")
#combine 1 min files
tp1 <- cbind(d.1min.meas, d.1min)
all.1min <- select(tp1, -c("cycle"))
## quick graph check
all.1min %>%
gather("key", "value", -date) %>%
ggplot(aes (x = date,
y = value,
color = key)) +
geom_point()
ggplot() +
geom_point(data = all.1min, aes(x=date, y=tnr, colour = group), colour = "green")+
geom_point(data = all.1min, aes(x=date, y=tnr.meas, colour = group), colour = "gold")+
geom_point(data = all.1min, aes(x=date, y=NH3, colour = group), colour = "red")+
geom_point(data = all.1min, aes(x=date, y=tnr.nh3.meas, colour = group), colour = "purple")+
theme(legend.position = "right")+
theme_light()
#write csv file
write.csv(all.1min, "tNr_processed_1_min_data.csv", row.names = F)
#5 MIN WEEKLY ------------------------------------------------------------
#5 min data interpolated and measured data
#note no measurements only file for 5 min data
data.path.5min="drive_name_here"
# FINAL CSV ------------------------------------------------------------
filenames.5min=list.files(path=data.path.5min, pattern = "Total*",full.names=TRUE)
d.5min <- rbindlist(lapply(filenames.5min, fread, header=T,blank.lines.skip=TRUE))
d.5min$date <- ymd_hms(d.5min$date)
d.5min %>%
gather("key", "value", -date) %>%
ggplot(aes (x = date,
y = value,
color = key)) +
geom_point()
write.csv(d.5min, "5_min_averaged_data", row.names = F)