-
Notifications
You must be signed in to change notification settings - Fork 2
/
cetesb_retrieve_met.R
96 lines (84 loc) · 3.3 KB
/
cetesb_retrieve_met.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
#' Download meteorological parameters from CETESB QUALAR
#'
#' This function download the main meteorological parameters for
#' model evaluation from one air quality station (AQS) of CETESB AQS network.
#' It will pad out the date with missing data with NA.
#' This function requires to have
#' \href{https://seguranca.cetesb.sp.gov.br/Home/CadastrarUsuario}{an account}
#' in \href{https://qualar.cetesb.sp.gov.br/qualar/home.do}{CETESB QUALAR}.
#'
#' @param username User name of CETESB QUALAR
#' @param password User name's password of CETESB QUALAR
#' @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.
#'
#' @return data.frame with Temperature (C), Relative Humidity (%),
#' Wind Speed (m/s) and Direction (degrees), and Pressure information.
#' @export
#'
#' @examples
#' \dontrun{
#' # Downloading meteorological data from Pinheiros AQS
#' # from January first to 7th of 2020
#' my_user_name <- "John Doe"
#' my_pass_word <- "drowssap"
#' pin_code <- 99 # Check with cetesb_aqs
#' start_date <- "01/01/2020"
#' end_date <- "07/01/2020"
#'
#' pin_pol <- cetesb_retrieve_met(my_user_name, my_pass_word, pin_code,
#' start_date, end_date)
#' }
cetesb_retrieve_met <- function(username, password,
aqs_code, start_date,
end_date, verbose = TRUE,
to_csv = FALSE, csv_path = ""){
# Check if aqs_code is valid
aqs <- cetesb
check_code <- check_cetesb_code(aqs, aqs_code)
aqs_name <- check_code[1]
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)
}
tc <- cetesb_retrieve(username, password, 25,
aqs_code, start_date,
end_date, verbose = FALSE)
rh <- cetesb_retrieve(username, password, 28,
aqs_code, start_date,
end_date, verbose = FALSE)
ws <- cetesb_retrieve(username, password, 24,
aqs_code, start_date,
end_date, verbose = FALSE)
wd <- cetesb_retrieve(username, password, 23,
aqs_code, start_date,
end_date, verbose = FALSE)
p <- cetesb_retrieve(username, password, 29,
aqs_code, start_date,
end_date, verbose = FALSE)
all_met <- data.frame(date = tc$date,
tc = tc$pol,
rh = rh$pol,
ws = ws$pol,
wd = wd$pol,
p = p$pol,
aqs = tc$aqs,
stringsAsFactors = FALSE)
cols_unchange <- -c(1, ncol(all_met))
all_met[, cols_unchange] <- sapply(all_met[, cols_unchange], as.numeric)
message(paste(
"Download complete for", unique(all_met$aqs)
))
if (to_csv){
write_csv(all_met, aqs_name, start_date, end_date, "MET", csv_path) # nocov
}
return(all_met)
}