knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Both the State of São Paulo and Rio de Janeiro have one of the most extensive air quality stations network in South America. CETESB QUALAR System provide to the user the air quality data from the State of São Paulo. QUALAR System limits the download to one parameter from one air station for one year in a simple query (three parameter in advance query). The data can have missing hours (e.g. due to calibration), the decimal separator is ",", and the output is a CSV file. data.rio hosts the air quality information from Monitor Ar Program. It is not an user-friendly API and the data needs the same preprocessor as the data from QUALAR System.
qualR
surpasses these limitations and brings to your R session ready-to use
data frames with the information of air quality station from the State of
São Paulo and the city of Rio de Janeiro.
qualR
has the following functions:
cetesb_retrieve_param
: Download a list of different parameter from one air quality station (AQS) from CETESB QUALAR System.cetesb_retrieve_pol
: Download criteria pollutants from one AQS from CETESB QUALAR System.cetesb_retrieve_met
: Download meteorological parameters from one AQS from CETESB QUALAR System.cetesb_retrieve_met_pol
: Download meteorological parameters and criteria pollutants from one AQS from CETESB QUALAR System.monitor_ar_retrieve_param
: Download a list of different parameters from MonitorAr - Rio program.monitor_ar_retrieve_pol
: Download criteria pollutants from one AQS from MonitorAr - Rio program.monitor_ar_retrieve_met
: Download meteorological parameters from one AQS from MonitorAr - Rio program.monitor_ar_retrieve_met_pol
: Download meteorological parameters and criteria pollutants from one AQS from MonitorAr - Rio Program.In this example we want to download one year PM10 concentration from an air quality station located in Rio de Janeiro downtown. We need to do the following:
library(qualR)
monitor_ar_aqs
monitor_ar_param
code
is CA
(Estação Centro), and
PM10 code
is PM10
. So we use the function monitor_ar_retrieve_param()
.rj_centro <- monitor_ar_retrieve_param(start_date = "01/01/2019", end_date = "31/12/2019", aqs_code = "CA", parameters = "PM10") head(rj_centro)
to_dwld <- c("PM10", "Vel_Vento") rj_ca_params <- monitor_ar_retrieve_param(start_date = "01/01/2019", end_date = "31/12/2019", aqs_code = "CA", parameters = to_dwld) head(rj_ca_params)
plot(rj_ca_params$ws, rj_ca_params$pm10, xlab = "Wind speed (m/s)", ylab = "", xlim = c(0,4), ylim = c(0,120)) mtext(expression(PM[10]~" ("*mu*"g/m"^3*")"), side = 2, line = 2.5)
tidyverse
tidyverse
is a powerful collection of R package. Here is an example using purrr
to download data from multiple stations and ggplot2
to visualize the relation between Ozone and air temperature.
As we know Ozone is formed by photochemical reaction which means the participation of solar radiation.
library(qualR) library(purrr) # Retrieve data from all stations in Rio rj_params <- purrr::map_dfr(.x = qualR::monitor_ar_aqs$code, .f = monitor_ar_retrieve_param, start_date = "01/01/2020", end_date = "31/12/2020", parameters = c("O3", "Temp") )
Now we can visualize all the data simultaneity using ggplot2
facet:
library(magrittr) library(ggplot2) # making the graph with facet rj_params %>% ggplot() + geom_point(aes(x = tc, y = o3), size = 0.5, alpha = 0.5) + labs(x = "Air temperature (º)", y = expression(O[3]~" ("*mu*"g/m"^3*")"), caption = "Source: Data from MonitorAr - Rio, retrieved with qualR R package. " )+ theme_bw()+ facet_wrap(~aqs)
PS: Special thanks to @beatrizmilz for inspiring this example.
openair
qualR
functions returns a completed data frame (i.e. missing hours padded out
with NA
) with a date
column in POSIXct
. This ensure compatibility with the
openair
package.
Here is the code to use openair timeVariation()
function. Note that no
preprocessing is needed.
#install.package("openair") library(openair) openair::timeVariation(rj_centro, pollutant = "pm10")
To use cetesb_retrieve
you first need to create an account in
CETESB QUALAR System.
The cetesb_retrieve
functions are similar as monitor_ar_retrieve_param
functions, but
they require the username
and password
arguments.
Check this section on qualR
README to safely configure your user name and
password on your R session.
In this example, we download Ozone concentration from an air quality station
located at Universidade de São Paulo (USP-Ipen) for August, 2021.
1. Check the station code
or name
head(cetesb_aqs, 15)
code
or abbreviationhead(cetesb_param, 15)
95
and ozone code is 63
. So to retrieve the
data we should use the cetesb_retrieve_param
function like this:usp_o3 <- cetesb_retrieve_param(username = my_user, password = my_password, parameters = "O3", # or 63 aqs_code = "Cid.Universitaria-USP-Ipen", # or 95 start_date = "01/08/2021", end_date = "31/08/2021")
qualR
README for more examples and good practices.qualR
and how it works
with openair
.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.