Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Suggests:
openair,
ggplot2,
purrr,
magrittr
magrittr,
withr
Depends:
R (>= 3.5.0)
Config/testthat/edition: 3
Expand Down
7 changes: 2 additions & 5 deletions R/cetesb_retrieve.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ cetesb_retrieve <- function(username, password,
if (ncol(dat) != 19){
dat <- data.frame(date = all.dates$date , pol = NA, aqs = aqs_name, # nocov
stringsAsFactors = FALSE) # nocov
message(paste0( # nocov
'No data available for ', # nocov
pol_name, # nocov
". Filling with NA.")) # nocov
padding_out_message_for(pol_name) # nocov
}

if (ncol(dat) == 19) {
Expand All @@ -139,7 +136,7 @@ cetesb_retrieve <- function(username, password,
aqs = aqs_name,
stringsAsFactors = FALSE)
} else {
message(paste0('Download OK ', pol_abr))
download_ok_cetesb_msg(pol_abr)
dat <- data.frame(date = all.dates$date , pol = dat$value ,
aqs = aqs_name, stringsAsFactors = FALSE)
} # nocov end
Expand Down
12 changes: 3 additions & 9 deletions R/cetesb_retrieve_met.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#' @param aqs_code Code of AQS
#' @param start_date Date to start downloading in dd/mm/yyyy
#' @param end_date Date to end downloading in dd/mm/yyyy
#' @param verbose Print query summary
#' @param to_csv Creates a csv file. FALSE by default
#' @param csv_path Path to save the csv file.
#'
Expand All @@ -35,8 +34,8 @@
#' }
cetesb_retrieve_met <- function(username, password,
aqs_code, start_date,
end_date, verbose = TRUE,
to_csv = FALSE, csv_path = ""){
end_date, to_csv = FALSE,
csv_path = ""){

# Check if aqs_code is valid
aqs <- cetesb
Expand All @@ -45,12 +44,7 @@ cetesb_retrieve_met <- function(username, password,
aqs_code <- as.numeric(check_code[2])

# Adding query summary
if (verbose){
message("Your query is:")
message("Parameter: TC, RH, WS, WD, Pressure")
message("Air quality station: ", aqs_name)
message("Period: From ", start_date, " to ", end_date)
}
query_summary(start_date, end_date, aqs_name, "met")

tc <- cetesb_retrieve(username, password, 25,
aqs_code, start_date,
Expand Down
14 changes: 4 additions & 10 deletions R/cetesb_retrieve_met_pol.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#' @param aqs_code Code of AQS
#' @param start_date Date to start downloading in dd/mm/yyyy
#' @param end_date Date to end downloading in dd/mm/yyyy
#' @param verbose Print query summary
#' @param to_csv Creates a csv file. FALSE by default
#' @param csv_path Path to save the csv file.
#'
Expand All @@ -38,8 +37,8 @@
#' }
cetesb_retrieve_met_pol <- function(username, password,
aqs_code, start_date,
end_date, verbose = TRUE,
to_csv = FALSE, csv_path = ""){
end_date, to_csv = FALSE,
csv_path = ""){

# Check if aqs_code is valid
aqs <- cetesb
Expand All @@ -48,13 +47,8 @@ cetesb_retrieve_met_pol <- function(username, password,
aqs_code <- as.numeric(check_code[2])

# Adding query summary
if (verbose){
message("Your query is: ")
message(paste("Parameter: TC, RH, WS, WD, Pressure,",
"O3, NO, NO2, NOX, PM2.5, PM10, CO"))
message("Air quality station: ", aqs_name)
message("Period: From ", start_date, " to ", end_date)
}
query_summary(start_date, end_date, aqs_name, "met_pol")


tc <- cetesb_retrieve(username, password, 25,
aqs_code, start_date,
Expand Down
19 changes: 10 additions & 9 deletions R/cetesb_retrieve_param.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#' @param aqs_code Code of AQS
#' @param start_date Date to start downloading in dd/mm/yyyy
#' @param end_date Date to end downloading in dd/mm/yyyy
#' @param verbose Print query summary
#' @param to_csv Creates a csv file. FALSE by default
#' @param csv_path Path to save the csv file
#'
Expand All @@ -39,8 +38,8 @@
#' }
cetesb_retrieve_param <- function(username, password, parameters,
aqs_code, start_date, end_date,
verbose = TRUE, to_csv = FALSE,
csv_path = ""){
to_csv = FALSE,
csv_path = ""){

# Check if aqs_code is valid
aqs <- cetesb
Expand Down Expand Up @@ -68,12 +67,14 @@ cetesb_retrieve_param <- function(username, password, parameters,


# Adding query summary
if (verbose){
message("Your query is:")
message("Parameter: ", paste(codes_df$name, collapse = ", "))
message("Air quality station: ", aqs_name)
message("Period: From ", start_date, " to ", end_date)
}
query_summary(start_date, end_date, aqs_name, "param",
codes_df$name)
# if (verbose){
# message("Your query is:")
# message("Parameter: ", paste(codes_df$name, collapse = ", "))
# message("Air quality station: ", aqs_name)
# message("Period: From ", start_date, " to ", end_date)
# }

# Downloading data
aqs_data <- lapply(codes_df$code, cetesb_retrieve,
Expand Down
17 changes: 5 additions & 12 deletions R/cetesb_retrieve_pol.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#' @param aqs_code Code of AQS
#' @param start_date Date to start downloading in dd/mm/yyyy
#' @param end_date Date to end downloading in dd/mm/yyyy
#' @param verbose Print query summary
#' @param to_csv Creates a csv file. FALSE by default
#' @param csv_path Path to save the csv file
#'
Expand All @@ -36,8 +35,8 @@
#' }
cetesb_retrieve_pol <- function(username, password,
aqs_code, start_date,
end_date, verbose = TRUE,
to_csv = FALSE, csv_path = ""){
end_date, to_csv = FALSE,
csv_path = ""){

# Check if aqs_code is valid
aqs <- cetesb
Expand All @@ -46,12 +45,7 @@ cetesb_retrieve_pol <- function(username, password,
aqs_code <- as.numeric(check_code[2])

# Adding query summary
if (verbose){
message("Your query is:")
message("Parameter: O3, NO, NO2, NOX, MP2.5, MP10, CO")
message("Air quality station: ", aqs_name)
message("Period: From ", start_date, " to ", end_date)
}
query_summary(start_date, end_date, aqs_name, "pol")

o3 <- cetesb_retrieve(username, password, 63,
aqs_code, start_date,
Expand Down Expand Up @@ -96,9 +90,8 @@ cetesb_retrieve_pol <- function(username, password,
cols_unchange <- -c(1, ncol(all_pol))
all_pol[, cols_unchange] <- sapply(all_pol[, cols_unchange], as.numeric)

message(paste(
"Download complete for", unique(all_pol$aqs)
))
# Download ok msg
download_ok_cetesb_msg('', unique(all_pol$aqs))

if (to_csv){
write_csv(all_pol, aqs_name, start_date, end_date, "POL", csv_path) # nocov
Expand Down
15 changes: 3 additions & 12 deletions R/monitor_ar_retrieve_met.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#' @param start_date Date to start downloading in dd/mm/yyyy.
#' @param end_date Date to end downloading in dd/mm/yyyy.
#' @param aqs_code Code of AQS. See `monitor_ar_aqs`.
#' @param verbose Print query summary.
#' @param to_local Date information in local time. TRUE by default.
#' @param to_csv Create a csv file. FALSE by default.
#' @param csv_path Path to save the csv file.
Expand All @@ -25,8 +24,8 @@
#' ca_met <- monitor_ar_retrieve_met(start_date, end_date, "CA")
#' }
monitor_ar_retrieve_met <- function(start_date, end_date, aqs_code,
verbose = TRUE, to_local = TRUE,
to_csv = FALSE, csv_path = ""){
to_local = TRUE, to_csv = FALSE,
csv_path = ""){
# Check is aqs_code is valid
if (!(aqs_code %in% aqs_monitor_ar$code)){
stop("Wrong aqs_code, please check monitor_ar_aqs", call. = FALSE) # nocov
Expand All @@ -37,16 +36,8 @@ monitor_ar_retrieve_met <- function(start_date, end_date, aqs_code,
# Adding query summary
aqs_name <- aqs_monitor_ar$name[aqs_monitor_ar$code == aqs_code]

if (verbose){
message("Your query is:")
message("Parameter: ", paste(params, collapse = ", "))
message("Air quality station: ", aqs_name)
message("Period: From ", start_date, " to ", end_date)
}

all_met <- monitor_ar_retrieve_param(start_date, end_date, aqs_code,
params, to_local = to_local,
verbose = FALSE)
params, to_local = to_local)

names(all_met) <- c("date", "tc", "rh", "ws", "wd", "p", "aqs")

Expand Down
14 changes: 3 additions & 11 deletions R/monitor_ar_retrieve_met_pol.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#' @param start_date Date to start downloading dd/mm/yyyy
#' @param end_date Date to end downloading dd/mm/yyyy
#' @param aqs_code Code of AQS
#' @param verbose Print query summary
#' @param to_local Date information in local time. TRUE by default.
#' @param to_csv Creates a csv file. FALSE by default.
#' @param csv_path Path to save the csv file.
Expand All @@ -27,8 +26,8 @@
#' ca_all <- monitor_ar_retrieve_met_pol(start_date, end_date, "CA")
#' }
monitor_ar_retrieve_met_pol <- function(start_date, end_date, aqs_code,
verbose = TRUE, to_local = TRUE,
to_csv = FALSE, csv_path = ""){
to_local = TRUE, to_csv = FALSE,
csv_path = ""){
# Check is aqs_code is valid
if (!(aqs_code %in% aqs_monitor_ar$code)){
stop("Wrong aqs_code, please check monitor_ar_aqs", call. = FALSE) # nocov
Expand All @@ -40,16 +39,9 @@ monitor_ar_retrieve_met_pol <- function(start_date, end_date, aqs_code,
# Adding query summary
aqs_name <- aqs_monitor_ar$name[aqs_monitor_ar$code == aqs_code]

if (verbose){
message("Your query is: ")
message("Parameter: ", paste(params, collapse = ", "))
message("Air quality station: ", aqs_name)
message("Period: From ", start_date, " to ", end_date)
}

all_data <- monitor_ar_retrieve_param(start_date, end_date, aqs_code,
params, to_local = to_local,
verbose = FALSE)
params, to_local = to_local)

names(all_data) <- c("date", tolower(params), "aqs")
names(all_data)[2:6] <- c("tc", "rh", "ws", "wd", "p")
Expand Down
24 changes: 10 additions & 14 deletions R/monitor_ar_retrieve_param.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#' @param parameters Parameters to download.
#' It can be a vector with many parameters.
#' @param to_local Date information in local time. TRUE by default.
#' @param verbose Print query summary.
#' @param to_csv Creates a csv file. FALSE by default
#' @param csv_path Path to save the csv file.
#'
Expand All @@ -28,7 +27,7 @@
#'
#' }
monitor_ar_retrieve_param <- function(start_date, end_date, aqs_code,
parameters, to_local=TRUE, verbose = TRUE,
parameters, to_local=TRUE,
to_csv = FALSE, csv_path = ""){

# Check if params are measured
Expand All @@ -45,12 +44,8 @@ monitor_ar_retrieve_param <- function(start_date, end_date, aqs_code,
aqs_name <- aqs_monitor_ar$name[aqs_monitor_ar$code == aqs_code]

# Adding query summary
if (verbose){
message("Your query is:")
message("Parameter: ", paste(parameters, collapse = ", "))
message("Air quality station: ", aqs_name)
message("Period: From ", start_date, " to ", end_date)
}
query_summary(start_date, end_date, aqs_name, "param", parameters)


start_date_format <- as.POSIXct(strptime(start_date, format="%d/%m/%Y"),
tz = "UTC")
Expand Down Expand Up @@ -89,12 +84,13 @@ monitor_ar_retrieve_param <- function(start_date, end_date, aqs_code,
f = 'json'
))
# Checking request
if (res$status_code == 200){
message("Succesful request")
message(paste("Downloading ", paste(parameters, collapse = " ")))
} else {
stop("Unsuccesful request. Something goes wrong", call. = FALSE) # nocov
}
download_ok_monitor_msg(res, parameters)
# if (res$status_code == 200){
# message("Succesful request")
# message(paste("Downloading ", paste(parameters, collapse = " ")))
# } else {
# stop("Unsuccesful request. Something goes wrong", call. = FALSE) # nocov
# }

# Reading json
raw_data <- jsonlite::fromJSON(rawToChar(res$content))
Expand Down
15 changes: 3 additions & 12 deletions R/monitor_ar_retrieve_pol.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#' @param start_date Date to start downloading in dd/mm/yyyy.
#' @param end_date Date to end downloading in dd/mm/yyyy.
#' @param aqs_code Code of AQS.
#' @param verbose Print query summary.
#' @param to_local Date information in local time. TRUE by default.
#' @param to_csv Creates a csv file. FALSE by default.
#' @param csv_path Path to save the csv file.
Expand All @@ -25,8 +24,8 @@
#' ca_pol <- monitor_ar_retrieve_pol(start_date, end_date, "CA")
#' }
monitor_ar_retrieve_pol <- function(start_date, end_date, aqs_code,
verbose = TRUE, to_local = TRUE,
to_csv = FALSE, csv_path = ""){
to_local = TRUE, to_csv = FALSE,
csv_path = ""){
# Check is aqs_code is valid
if (!(aqs_code %in% aqs_monitor_ar$code)){
stop("Wrong aqs_code, please check monitor_ar_aqs", call. = FALSE) # nocov
Expand All @@ -37,16 +36,8 @@ monitor_ar_retrieve_pol <- function(start_date, end_date, aqs_code,
# Adding query summary
aqs_name <- aqs_monitor_ar$name[aqs_monitor_ar$code == aqs_code]

if (verbose){
message("Your query is:")
message("Parameter: ", paste(params, collapse = ", "))
message("Air quality station: ", aqs_name)
message("Period: From ", start_date, " to ", end_date)
}

all_pol <- monitor_ar_retrieve_param(start_date, end_date, aqs_code,
params, to_local = to_local,
verbose = FALSE)
params, to_local = to_local)

names(all_pol) <- c("date", tolower(params), "aqs")
names(all_pol)[8] <- "pm25"
Expand Down
Loading
Loading