Skip to content

Commit

Permalink
finish capitalization file refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathancallahan committed Feb 27, 2024
1 parent a8eba20 commit fca4ff4
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ global_vars.R

.DS_Store

*/data/*.rda
*/data/*.log
*/*/data/*.rda
*/*/data/*.log

8 changes: 8 additions & 0 deletions 2024/State_AQC_Tables/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
################################################################################
# Makefile for publishing html content
#

publish:
- mkdir -p ../../docs/2024/state_AQC_Tables
mv Rmd/*.html ../../docs/2024/state_AQC_Tables

60 changes: 60 additions & 0 deletions 2024/State_AQC_Tables/R/02_prepare_monitoring_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Step 2) Save annual AirNow files locally
#
# Download annual files in advance

# ----- Setup ------------------------------------------------------------------

VERSION = "1.0.0"

# Check that the working directory is set properly
if ( !stringr::str_detect(getwd(), "state_AQC_Tables$") ) {
stop("WD_ERROR: Please set the working directory to 'state_AQC_Tables/'")
}

if ( packageVersion("AirMonitor") < "0.4.0" ) {
stop("VERSION_ERROR: Please upgrade to AirMonitor 0.4.0 or later.")
}

library(MazamaCoreUtils)
library(AirMonitor)

# Use MazamaCoreUtils logging functionality to save logs in the data/ directory
MazamaCoreUtils::initializeLogging("./data")
logger.setLevel(TRACE)

logger.info("Running prepare_monitoring_data.R version %s", VERSION)

# Log session info to see package versions
logger.debug(capture.output(sessionInfo()))

# ----- Process data -----------------------------------------------------------

# All processed data will live in the data/ directory.

# Configurable parameters
years <- 2014:2023

for ( year in years ) {

logger.info("\n===== %d =====", year)

# * AirNow -------------------------------------------------------------------

logger.info("Processing AirNow %d data...", year)

# Create an AirNow monitor object
monitor <-
airnow_loadAnnual(year)

# Assign a unique object name so that we can load more than one at a time
objectName <- sprintf("airnow_%d", year)
assign(objectName, monitor)

# Save to the data/ directory
fileName <- sprintf("%s.rda", objectName)
filePath <- file.path("./data", fileName)
logger.info("Saving %s", filePath)
save(list = objectName, file = filePath)

}

30 changes: 30 additions & 0 deletions 2024/State_AQC_Tables/R/03_render_AQC_tables.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Step 3) Render the daily_AQC_tables.Rmd report for months and states
#
#
# NOTE: Data archives begin in 2014

# ----- Setup ------------------------------------------------------------------

# Check that the working directory is set properly
if ( !stringr::str_detect(getwd(), "state_AQC_tables$") ) {
stop("WD_ERROR: Please set the working directory to 'state_AQC_tables/'")
}

if ( packageVersion("AirMonitor") < "0.4.0" ) {
stop("VERSION_ERROR: Please upgrade to AirMonitor 0.4.0 or later.")
}

# ----- Render daily_AQC_tables reports ----------------------------------------

for ( year in 2014:2023 ) {

params <- list(year = year)

# This path is relative to the Rmd/ directory
htmlPath <- sprintf("./html/monitoring_data_%d.html", year)

rmarkdown::render(input = 'Rmd/monitoring_data.Rmd',
params = params,
output_file = htmlPath)

}
14 changes: 14 additions & 0 deletions 2024/State_AQC_Tables/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# State Air Quality Category Tables

_Last updated on February 26, 2024_

_Disclaimer: Data presented here come from a variety of sources and do not
represent official EPA measurements. Please visit
[AirNow](https://www.airnow.gov) for official measurements._

---

## Background



183 changes: 183 additions & 0 deletions 2024/State_AQC_Tables/Rmd/daily_AQC_tables.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
---
title: "Air Quality Category Tables"
output:
html_document:
css: report_styles.css
---

Processed on `r paste(lubridate::now(),format(lubridate::now(), "%Z"))`.

----

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = FALSE, error = FALSE)
suppressPackageStartupMessages({
library(AirMonitor)
})
stateCode <- "GA"
timezone <- "America/New_York"
month <- 4
monthName <-
MazamaCoreUtils::parseDatetime(
sprintf("2024%02d15", month),
timezone = "America/New_York"
) %>%
strftime("%B", tzone = timezone)
stateName <-
MazamaSpatialUtils::US_stateCodeToName(stateCode)
```

_Disclaimer: Data presented here come from a variety of sources and do not
represent official EPA measurements. Please visit
[AirNow](https://www.airnow.gov) for official measurements._

----

```{r state_name, results = "asis"}
cat(sprintf("\n\n# %s in %s\n", monthName, stateName))
```


The following tables summarize the cumulative number of days air quality monitors spent in
each AQI category during the the month of `r monthName`. Tables include only
permanent monitors.

The 'Old' row is calculated using the 2023 PM~2.5~ NAAQS while the 'New' row is
calculated using the 2024 PM~2.5~ NAAQS.


```{r year_loop, results = "asis", fig.width = 10, fig.height = 3, fig.align = "left"}
# NOTE: No monitoring data in Georgia before 2016
for ( year in 2016:2023 ) {
startString <- sprintf("%d%02d01", year, month)
startdate <- MazamaCoreUtils::parseDatetime(startString, timezone = timezone)
enddate <- startdate + lubridate::ddays(40)
lubridate::day(enddate) <- 1
# ----- Assemble data --------------------------------------------------------
# NOTE: These paths are relative to the Rmd/ directory
airnow <- get(load(sprintf("../data/airnow_%d.rda", year)))
monitor <-
airnow %>%
monitor_filter(stateCode == "GA") %>%
monitor_filterDate(startdate, enddate) %>%
monitor_filter(deploymentType == "Permanent") %>%
monitor_dropEmpty()
#cat(sprintf("\n\n## %d\n", year))
cat(sprintf("\n\n## %s %d -- %d permanent monitors\n", monthName, year, nrow(monitor$meta)))
#cat(sprintf("\n\n**Air Quality Categories under Old (2023) and New (2024) NAAQS**\n\n"))
# ----- Create table with rownames for printing ------------------------------
oldAQCSummary <-
monitor %>%
monitor_dailyStatistic(
FUN = mean,
na.rm = TRUE,
minHours = 18,
dayBoundary = c("LST")
) %>%
monitor_dropEmpty() %>%
monitor_toAQCTable(NAAQS = "PM2.5") %>%
colSums()
newAQCSummary <-
monitor %>%
monitor_dailyStatistic(
FUN = mean,
na.rm = TRUE,
minHours = 18,
dayBoundary = c("LST")
) %>%
monitor_dropEmpty() %>%
monitor_toAQCTable(NAAQS = "PM2.5_2024") %>%
colSums()
dailyAQCTable <-
dplyr::bind_rows(oldAQCSummary, newAQCSummary) %>%
as.data.frame()
rownames(dailyAQCTable) <- c("Old", "New")
# ----- Print table ----------------------------------------------------------
print(knitr::kable(
dailyAQCTable,
format = "html", table.attr = "style='width:95%; font-size:1.5rem; margin: 20px 60px 0px 15px; line-height:2em'"
))
# ----- Create barplot -------------------------------------------------------
# Reorder so that the horizontal barplot ends up with Old on top
df_barplot <-
dplyr::bind_rows(newAQCSummary, oldAQCSummary) %>%
as.data.frame()
rownames(df_barplot) <- c("New", "Old")
par(mar = c(1,3,3,1) + 0.1)
barplot(
as.matrix(t(df_barplot)),
col = c(US_AQI$colors_EPA, "gray90"),
horiz = TRUE,
las = 1,
ylim = c(0, 1.5),
width = 0.5,
axes = FALSE
)
par(mar = c(5,4,4,2) + 0.1)
# title("Title goes here")
#
legend(
"top",
legend = c(US_AQI$names_eng, "Missing"),
fill = c(US_AQI$colors_EPA, "gray90"),
horiz = TRUE,
cex = 0.8,
bty = 'n'
)
# # ----- Create ggplot barplot ------------------------------------------------
#
# df <-
# dailyAQCTable %>%
# tibble::rownames_to_column(var = "NAAQS") %>%
# tidyr::pivot_longer(-"NAAQS", names_to = "AQC", values_to = "count")
#
# # ----- Plot table -----------------------------------------------------------
#
# gg <-
# ggplot(df, aes(x = NAAQS, y = count))+
# geom_col(aes(fill = AQC), width = 0.7)
#
#
# gg <-
# ggplot(df, aes(x = count, y = NAAQS))+
# geom_col(
# orientation = "y",
# aes(fill = AQC),
# width = 0.7
# )
}
```


0 comments on commit fca4ff4

Please sign in to comment.