-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateAirSensor_annual_exec.R
executable file
·302 lines (235 loc) · 9.69 KB
/
createAirSensor_annual_exec.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
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
#!/usr/local/bin/Rscript
# This Rscript will ingest airsensor_~_latest7.rda files and use them to create
# airsensor files for an entire year.
#
# See test/Makefile for testing options
#
# ----- . AirSensor 1.1.x . first pass
VERSION = "0.3.0"
# The following packages are attached here so they show up in the sessionInfo
suppressPackageStartupMessages({
library(MazamaCoreUtils)
library(AirSensor)
})
# ----- Get command line arguments ---------------------------------------------
if ( interactive() ) {
# RStudio session
opt <- list(
archiveBaseDir = file.path(getwd(), "data"),
logDir = file.path(getwd(), "logs"),
collectionName = "scaqmd",
version = FALSE
)
} else {
option_list <- list(
optparse::make_option(
c("-o","--archiveBaseDir"),
default = getwd(),
help = "Output base directory for generated .RData files [default = \"%default\"]"
),
optparse::make_option(
c("-l","--logDir"),
default = getwd(),
help = "Output directory for generated .log file [default=\"%default\"]"
),
optparse::make_option(
c("-n","--collectionName"),
default = "scaqmd",
help = "Name associated with this collection of sensors [default=\"%default\"]"
),
optparse::make_option(
c("-V","--version"),
action = "store_true",
default = FALSE,
help = "Print out version number [default=\"%default\"]"
)
)
# Parse arguments
opt <- optparse::parse_args(optparse::OptionParser(option_list = option_list))
}
# Print out version and quit
if ( opt$version ) {
cat(paste0("createAirSensor_annual_exec.R ",VERSION,"\n"))
quit()
}
# ----- Validate parameters ----------------------------------------------------
if ( dir.exists(opt$archiveBaseDir) ) {
setArchiveBaseDir(opt$archiveBaseDir)
} else {
stop(paste0("archiveBaseDir not found: ", opt$archiveBaseDir))
}
if ( !dir.exists(opt$logDir) )
stop(paste0("logDir not found: ",opt$logDir))
# ----- Set up logging ---------------------------------------------------------
logger.setup(
traceLog = file.path(opt$logDir, paste0("createAirSensor_annual_",opt$collectionName,"_TRACE.log")),
debugLog = file.path(opt$logDir, paste0("createAirSensor_annual_",opt$collectionName,"_DEBUG.log")),
infoLog = file.path(opt$logDir, paste0("createAirSensor_annual_",opt$collectionName,"_INFO.log")),
errorLog = file.path(opt$logDir, paste0("createAirSensor_annual_",opt$collectionName,"_ERROR.log"))
)
# For use at the very end
errorLog <- file.path(opt$logDir, paste0("createAirSensor_annual_",opt$collectionName,"_ERROR.log"))
if ( interactive() ) {
logger.setLevel(TRACE)
}
# Silence other warning messages
options(warn = -1) # -1=ignore, 0=save/print, 1=print, 2=error
# Start logging
logger.info("Running createAirSensor_annual_exec.R version %s",VERSION)
optString <- paste(capture.output(str(opt)), collapse = "\n")
logger.debug("Script options: \n\n%s\n", optString)
sessionString <- paste(capture.output(sessionInfo()), collapse = "\n")
logger.debug("R session:\n\n%s\n", sessionString)
# ----- Create datestamps ------------------------------------------------------
# All datestamps are UTC
timezone <- "UTC"
# Use the the current year and extend by a day to capture all timezones
yearstamp <- lubridate::year(lubridate::now(tzone = timezone))
startstamp <- paste0(yearstamp - 1, "1231")
endstamp <- paste0((yearstamp + 1), "0102")
logger.trace("Setting up data directories")
latestDataDir <- paste0(opt$archiveBaseDir, "/airsensor/latest")
yearDataDir <- paste0(opt$archiveBaseDir, "/airsensor/", yearstamp)
# ------ Create annual airsensor object ----------------------------------------
# Create paths
tryCatch(
expr = {
latest7Path <- file.path(latestDataDir, paste0("airsensor_", opt$collectionName, "_latest7.rda"))
yearPath <- file.path(yearDataDir, paste0("airsensor_", opt$collectionName, "_", yearstamp, ".rda"))
logger.trace("Loading %s", latest7Path)
# Load latest7
if ( file.exists(latest7Path) ) {
latest7 <- get(load(latest7Path))
} else {
err_msg <- paste0("Missing ", latest7Path)
logger.error(err_msg)
stop(err_msg)
}
},
error = function(e) {
msg <- paste("Error loading paths: ", e)
logger.fatal(msg)
stop(msg)
}
)
# Load airsensor files
tryCatch(
expr = {
if ( file.exists(yearPath) ) {
# TODO: We have a basic problem with the pwfsl_closest~ variables.
# TODO: These can change when a new, temporary monitor gets installed.
# TODO: We don't want to have two separate metadata records for a single
# TODO: Sensor as the metadata is supposed to be location-specific and
# TODO: not time-dependent. Unfortunately, the location of the nearest
# TODO: PWFSL monitor is time-dependent and any choice we make will break
# TODO: things like pat_externalFit() for those periods when a temporary
# TODO: monitor is closer than a permanent monitor.
# TODO:
# TODO: Ideally, enhanceSynopticData() would have some concept of
# TODO: "permanent" monitors but this is far beyond what is currently
# TODO: supported.
logger.trace("Loading %s", yearPath)
year <- get(load(yearPath))
# Update year_meta with mutable information
year_meta <- year$meta
for ( index_year in seq_len(nrow(year$meta)) ) {
monitorID <- year_meta$monitorID[index_year]
logger.trace("Updating pwfsl_closestMonitorID for %s", monitorID)
if ( monitorID %in% latest7$meta$monitorID ) {
index_latest7 <- which(latest7$meta$monitorID == monitorID)
year_meta$pwfsl_closestDistance[index_year] <-
latest7$meta$pwfsl_closestDistance[index_latest7]
year_meta$pwfsl_closestMonitorID[index_year] <-
latest7$meta$pwfsl_closestMonitorID[index_latest7]
}
}
# NOTE: If a latest7 file is created with no data, all of the metadata
# NOTE: fields with missing data will be of type "logical". This will
# NOTE: prevent them from being merged with metadata fields of type
# NOTE: character. Here we ensure that everything has the proper type.
logger.trace("Correcting potential 'logical' types in metadata")
latest7_meta <-
latest7$meta %>%
dplyr::mutate_if(is.logical, as.character) %>%
dplyr::mutate_at(
dplyr::vars(longitude, latitude, elevation, pwfsl_closestDistance),
as.numeric
)
year_meta <-
year_meta %>%
dplyr::mutate_if(is.logical, as.character) %>%
dplyr::mutate_at(
dplyr::vars(longitude, latitude, elevation, pwfsl_closestDistance),
as.numeric
)
logger.trace("Combining metadata")
# < NOTE: Despite the efforts above, if a sensor has a new *location* there >
# < NOTE: is nothing we can do to avoid duplicates. So we have to filter for >
# < NOTE: uniqueness at this point as having duplicate monitorIDs in the >
# < NOTE: meta dataframe breaks the data model. >
# NOTE: As of AirSensor 0.8.x, this should no longer be a problem because
# NOTE: the 'monitorID' is a truly unique 'deviceDeploymentID'. But we
# NOTE: leave this here because it doesn't hurt anything.
# Combine meta
suppressMessages({
meta <-
# Join the latest and yearly meta in that order to keep newer locations
dplyr::full_join(latest7_meta, year_meta, by = NULL) %>%
# Remove rows with duplicate monitorID which we use as a unique identifier
dplyr::distinct(monitorID, .keep_all = TRUE)
})
logger.trace("Datetime filtering")
# Strip off data overlap
year_data <-
year$data %>%
dplyr::filter(datetime < latest7$data$datetime[1])
logger.trace("Combining data")
# Combine data
suppressMessages({
data <-
dplyr::full_join(year_data, latest7$data, by = NULL) %>%
dplyr::arrange(datetime)
})
logger.trace("Create airsensor object")
logger.trace("meta$monitorID = %s", paste0(meta$monitorID, collapse = ", "))
logger.trace("names(data) = %s", paste0(names(data), collapse = ", "))
# Create an "airsensor" object
airsensor <- list(
meta = meta,
data = data
)
class(airsensor) <- c("airsensor", "ws_monitor", "list")
logger.trace("Calling PWFSLSmoke::monitor_subset(airsensor)")
# Guarante that the order of meta and data agree
airsensor <- PWFSLSmoke::monitor_subset(airsensor)
# Add "airsensor" class back again
class(airsensor) <- union("airsensor", class(airsensor))
logger.trace("Successfully built the annual airsensor")
} else {
logger.trace("No annual file found. Using latest7.")
airsensor <- latest7 # default when starting from scratch
}
},
error = function(e) {
msg <- paste("Error creating annual airsensor file: ", e)
logger.fatal(msg)
}
)
# ----- Save annual data -------------------------------------------------------
tryCatch(
expr = {
logger.trace("Trim and save %s", yearPath)
# Trim to year boundaries
airsensor <-
PWFSLSmoke::monitor_subset(airsensor, tlim = c(startstamp, endstamp))
save(list = "airsensor", file = yearPath)
},
error = function(e) {
msg <- paste("Error saving annual airsensor file: ", e)
logger.fatal(msg)
stop(e)
}
)
if ( !file.exists(errorLog) )
dummy <- file.create(errorLog)
logger.info("Completed successfully!")