-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreadme.Rmd
127 lines (101 loc) · 5.92 KB
/
readme.Rmd
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
---
title: "Mexico City Air Quality Data"
author: "Diego Valle-Jones"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output:
github_document:
toc: true
fig_width: 8
fig_height: 5
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
[![R build status](https://github.com/diegovalle/aire.zmvm/workflows/R-CMD-check/badge.svg)](https://github.com/diegovalle/aire.zmvm/actions) [![Coverage Status](https://img.shields.io/codecov/c/github/diegovalle/aire.zmvm/master.svg)](https://codecov.io/github/diegovalle/aire.zmvm?branch=master) ![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version-ago/aire.zmvm?color=green)
| | |
|-----------------------|-------------------------------------------------------|
| __Author:__ | Diego Valle-Jones |
| __License:__ | [BSD_3](https://opensource.org/licenses/BSD-3-Clause) |
| __Website:__ | [https://hoyodesmog.diegovalle.net/aire.zmvm/](https://hoyodesmog.diegovalle.net/aire.zmvm/) |
## What does it do?
Tools for downloading airquality data for the Mexico City metro area. This package can download real-time, daily maximum, minimum, or hourly average data for each of the pollution measuring stations or geographical zones in the Zona Metropolitana del Valle de México (greater Mexico City). It also includes the locations of all the measuring stations and a function to perform inverse distance weighting modified to work with wind direction.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Installation
You can always install the development version from GitHub:
```r
if (!require(devtools)) {
install.packages("devtools")
}
devtools::install_github('diegovalle/aire.zmvm')
```
To install the most recent package version from CRAN type:
```r
install.packages("aire.zmvm")
library(aire.zmvm)
```
Note that the version on CRAN might not reflect the most recent changes made to this package.
## Core Functions
The package core functions:
* ```get_station_data``` and ```get_station_month_data``` download pollution, wind and temperature data for each of the measuring stations in the original units (ppb, µg/m³, etc).
* ```get_station_imeca``` download pollution values for each station in IMECAs
* ```get_zone_imeca``` download pollution data in IMECAs for each of the 5 geographic zones of Mexico City
* ```get_latest_imeca``` download the latest pollution hourly maximums for each of the measuring stations.
* ```idw360``` inverse distance weighting modified to work with degrees, useful for wind data
| Function | Date range | Units | Wind, Tmp, RH | Earliest Date | Pollutants | Includes All Stations | Criterion |
|---------------------------|----------------|----------|------------------|------------|------------|-------|-------|
| get_station_data | years | Original | Yes | 1986 | SO2, CO, NO2, O3, PM10, PM25, WSP, WDR, TMP, RH | Yes | hourly, daily maximum, daily minimum |
| get_station_month_data | 1 month | Original | Yes | 2005‑01 | SO2, CO, NO2, O3, PM10, PM25, WSP, WDR, TMP, RH | Yes | hourly, daily maximum, daily minimum |
| get_station_imeca | 1 day | IMECA | No | 2009‑01‑01 | SO2, CO, NO2, O3, PM10 | No | hourly |
| get_zone_imeca | 1 or more days | IMECA | No | 2008‑01‑01 | SO2, CO, NO2, O3, PM10 | Only zones | hourly, daily maximum |
| get_latest_imeca | 1 hour | IMECA | No | Latest only| Maximum value of SO2, CO, NO2, O3, PM10 | No |latest hourly |
## Quick Example
```{r, fig.show='hold', fig.width=11, warning=FALSE, message=FALSE}
library("aire.zmvm")
library("dplyr")
library("ggplot2")
library("ggseas")
o3 <- get_station_data(criterion = "MAXIMOS", # Can be one of MAXIMOS (daily maximum),
# MINIMOS (daily minimum),
# or HORARIOS (hourly average)
pollutant = "O3", # Can be one of "SO2", "CO", "NOX", "NO2", "NO", "O3",
# "PM10", "PM25", "WSP", "WDR", "TMP", "RH"
year = 2009:2018) # A numeric vector, the earliest year allowed is 1986
knitr::kable(head(o3))
# Daily max among all base stations
o3_max <- o3 %>%
group_by(date) %>%
summarise(max = ifelse(all(is.na(value)),
NA,
base::max(value, na.rm = TRUE))) %>%
na.omit()
# ozone values at which a contingencia ambiental was declared
# and the dates during which they were valid
# source: http://www.aire.cdmx.gob.mx/descargas/ultima-hora/calidad-aire/pcaa/pcaa-modificaciones.pdf
contingencia <- data.frame(ppb = c(216, 210, 205, 199, 185, 155, 155),
start = c(2009, 2009.4973, 2010.4973, 2011.5795,
2012.6052, 2016.291, 2016.4986),
end = c(2009.4973, 2010.4945, 2011.4945,
2012.6025, 2016.2883, 2016.4959, Inf))
max_daily_df <- tsdf(ts(o3_max$max, start = c(2009,1), frequency = 365.25))
ggplot(max_daily_df,
aes(x = x, y = y)) +
geom_line(colour = "grey75", alpha = .5) +
stat_rollapplyr(width = 30, align = "right", color = "#01C5D2") +
#geom_vline(xintercept = 2015 + 183/365) +
geom_segment(data = contingencia,
aes(x=start, y=ppb, xend=end, yend=ppb), color="darkred",
linetype = 2) +
xlab("date") +
ylab("parts per billion") +
scale_x_continuous(breaks = c(2010, 2012, 2014, 2016, 2018)) +
ggtitle("Maximum daily ozone concentration and 30 day rolling average",
subtitle = paste0("Red lines indicate the values necessary to ",
"activate a phase I smog alert.",
"\nData source: SEDEMA"))
```