-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateAirSensor_latest_exec.R
executable file
·216 lines (175 loc) · 5.6 KB
/
createAirSensor_latest_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
#!/usr/local/bin/Rscript
# This Rscript will process archived 'pat' data files into a single 'airsensor'
# airsensor_<collection-id>_latest7.rda file containing hourly aggregated pm25
# data for all sensors.
#
# 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 {
# Set up OptionParser
library(optparse)
option_list <- list(
optparse::make_option(
c("-o","--archiveBaseDir"),
default = getwd(),
help = "Output 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_latest_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_latest_",opt$collectionName,"_TRACE.log")),
debugLog = file.path(opt$logDir, paste0("createAirSensor_latest_",opt$collectionName,"_DEBUG.log")),
infoLog = file.path(opt$logDir, paste0("createAirSensor_latest_",opt$collectionName,"_INFO.log")),
errorLog = file.path(opt$logDir, paste0("createAirSensor_latest_",opt$collectionName,"_ERROR.log"))
)
# For use at the very end
errorLog <- file.path(opt$logDir, paste0("createAirSensor_latest_",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_latest_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 AirSensor objects ----------------------------------------------
# Create directory if it doesn't exist
tryCatch(
expr = {
outputDir <- file.path(opt$archiveBaseDir, "airsensor", "latest")
if ( !dir.exists(outputDir) ) {
dir.create(outputDir, recursive = TRUE)
}
logger.info("Output directory: %s", outputDir)
},
error = function(e) {
msg <- paste('Error validating output directory: ', e)
logger.fatal(msg)
stop(msg)
}
)
# Load PAS object
tryCatch(
expr = {
logger.info('Loading PAS data...')
pas <- pas_load()
},
error = function(e) {
msg <- paste('Fatal PAS Load Execution: ', e)
logger.fatal(msg)
stop(msg)
}
)
# Capture Unique IDs
tryCatch(
expr = {
# Get time series unique identifiers
deviceDeploymentIDs <-
pas %>%
pas_getDeviceDeploymentIDs()
},
error = function(e) {
msg <- paste('deviceDeploymentID not found: ', e)
logger.fatal(msg)
stop(msg)
}
)
logger.info('Creating airsensors...')
# Init counts
successCount <- 0
count <- 0
dataList <- list()
for ( deviceDeploymentID in deviceDeploymentIDs ) {
count <- count + 1
# Debug info
logger.debug(
"%4d/%d pat_createAirSensor(id = '%s')",
count,
length(deviceDeploymentIDs),
deviceDeploymentID
)
# Load the pat data, convert to an airsensor and add to dataList
dataList[[deviceDeploymentID]] <- tryCatch(
expr = {
pat_loadLatest(id = deviceDeploymentID) %>%
pat_createAirSensor(
FUN = AirSensor::PurpleAirQC_hourly_AB_01
)
},
error = function(e) {
logger.warn('Unable to load PAT data for %s ', deviceDeploymentID)
NULL
}
)
}
# Combine the airsensors to a single ws_monitor opbject and save
tryCatch(
expr = {
logger.info('Combining airsensors...')
airsensor <- PWFSLSmoke::monitor_combine(dataList)
class(airsensor) <- c("airsensor", "ws_monitor", "list")
logger.info('Combined successfully...')
filename <- paste0("airsensor_", opt$collectionName, "_latest7.rda")
filepath <- file.path(outputDir, filename)
save(list = "airsensor", file = filepath)
logger.info("Saved: %s", filename)
},
error = function(e) {
msg <- paste("Error creating latest AirSensor file: ", e)
logger.fatal(msg)
}
)
# Guarantee that the errorLog exists
if ( !file.exists(errorLog) ) dummy <- file.create(errorLog)
logger.info("Completed successfully!")