-
Notifications
You must be signed in to change notification settings - Fork 1
/
airnow_loadDaily.R
203 lines (164 loc) · 4.96 KB
/
airnow_loadDaily.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
#' @export
#' @importFrom dplyr across
#'
#' @title Load daily AirNow monitoring data
#'
#' @param parameterName One of the EPA AQS criteria parameter names.
#' @param archiveBaseUrl Base URL for monitoring v2 data files.
#' @param archiveBaseDir Local base directory for monitoring v2 data files.
#' @param QC_negativeValues Type of QC to apply to negative values.
#'
#' @return A \emph{mts_monitor} object with AirNow data. (A list with
#' \code{meta} and \code{data} dataframes.)
#'
#' @description Loads pre-generated .rda files containing hourly
#' AirNow data.
#'
#' If \code{archiveDataDir} is defined, data will be loaded from this local
#' archive. Otherwise, data will be loaded from the monitoring data repository
#' maintained by the USFS AirFire team.
#'
#' The files loaded by this function are updated once per day and
#' contain data for the previous 45 days.
#'
#' For the most recent data in the last 10 days, use \code{airnow_loadLatest()}.
#'
#' For data extended more than 45 days into the past, use \code{airnow_loadAnnual()}.
#'
#' Pre-processed AirNow exists for the following parameters:
#' \enumerate{
# #' \item{BARPR}
# #' \item{BC}
# #' \item{CO}
# #' \item{NO}
# #' \item{NO2}
# #' \item{NO2Y}
# #' \item{NO2X}s
# #' \item{NOX}
# #' \item{NOOY}
# #' \item{OC}
# #' \item{OZONE}
# #' \item{PM10}
#' \item{PM2.5}
#' \item{PM2.5_nowcast}
# #' \item{PRECIP}
# #' \item{RHUM}
# #' \item{SO2}
# #' \item{SRAD}
# #' \item{TEMP}
# #' \item{UV-AETH}
# #' \item{WD}
# #' \item{WS}
#' }
#'
#' @seealso \code{\link{airnow_loadAnnual}}
#' @seealso \code{\link{airnow_loadLatest}}
#' @seealso \code{\link{airnow_loadMonthly}}
#'
#' @examples
#' \dontrun{
#' library(AirMonitor)
#'
#' # Fail gracefully if any resources are not available
#' try({
#'
#' airnow_loadDaily() \%>\%
#' monitor_filter(stateCode == "WA") \%>\%
#' monitor_leaflet()
#'
#' }, silent = FALSE)
#' }
airnow_loadDaily <- function(
archiveBaseUrl = paste0(
"https://airfire-data-exports.s3.us-west-2.amazonaws.com/",
"monitoring/v2"
),
archiveBaseDir = NULL,
QC_negativeValues = c("zero", "na", "ignore"),
parameterName = "PM2.5"
) {
# ----- Validate parameters --------------------------------------------------
MazamaCoreUtils::stopIfNull(parameterName)
QC_negativeValues <- match.arg(QC_negativeValues)
if ( is.null(archiveBaseUrl) && is.null(archiveBaseDir) )
stop("one of 'archiveBaseUrl' or 'archiveBaseDir' must be defined")
# Parameter code
validParameterNames <- c(
# "BARPR",
# "BC",
# "CO",
# "NO",
# "NO2",
# "NO2Y",
# "NO2X",
# "NOX",
# "NOOY",
# "OC",
# "OZONE",
# "PM10",
"PM2.5",
"PM2.5_nowcast"
# "PRECIP",
# "RHUM",
# "SO2",
# "SRAD",
# "TEMP",
# "UV-AETH",
# "WD",
# "WS"
)
parameterName <- as.character(parameterName)
if ( !parameterName %in% validParameterNames ) {
stop(sprintf(
"data for parameterName '%s' has not been processed",
parameterName
))
}
# ----- Load data ------------------------------------------------------------
# Create file name and path according to the AirMonitorIngest scheme
if ( is.null(archiveBaseUrl) ) {
dataUrl <- NULL
} else {
dataUrl <- file.path(archiveBaseUrl, "daily/data")
}
if ( is.null(archiveBaseDir) ) {
dataDir <- NULL
} else {
dataDir <- file.path(archiveBaseDir, "daily/data")
}
metaFileName <- sprintf("airnow_%s_daily_meta.rda", parameterName)
dataFileName <- sprintf("airnow_%s_daily_data.rda", parameterName)
meta <- MazamaCoreUtils::loadDataFile(metaFileName, dataUrl, dataDir)
data <- MazamaCoreUtils::loadDataFile(dataFileName, dataUrl, dataDir)
# Guarantee that 'meta' and 'data' match
ids <- names(data)[-1]
meta <-
meta %>%
dplyr::filter(.data$deviceDeploymentID %in% ids)
# Guarantee presence of fullAQSID
if ( !"fullAQSID" %in% names(meta) ) meta$fullAQSID <- NA_character_
data <-
data %>%
dplyr::select(dplyr::all_of(c("datetime", meta$deviceDeploymentID))) %>%
# Replace any NaN that snuck in
dplyr::mutate(across(tidyselect::vars_select_helpers$where(is.numeric), function(x) ifelse(is.nan(x), NA, x)))
# Create monitor object
monitor <- list(meta = meta, data = data)
monitor <- structure(monitor, class = c("mts_monitor", "mts", class(monitor)))
MazamaTimeSeries::mts_check(monitor)
# ----- Apply QC -------------------------------------------------------------
if ( QC_negativeValues == "zero" ) {
monitor <- monitor_replaceValues(monitor, data < 0, 0)
} else if ( QC_negativeValues == "na" ) {
monitor <- monitor_replaceValues(monitor, data < 0, as.numeric(NA))
}
# ----- Return ---------------------------------------------------------------
return(monitor)
}
# ===== DEBUG ==================================================================
if ( FALSE ) {
parameterName <- "PM2.5"
archiveBaseUrl <- "https://data-monitoring1.airfire.org/monitoring-v2"
archiveBaseDir <- NULL
QC_negativeValues = "zero"
}