<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/pwfslsmoke-1-0-visualizing-wildfire-smoke-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://mazamascience.com/WorkingWithData/?p=1770"> R – Working With Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Mazama Science has released the first official version (1.0) of the PWFSLSmoke R package for working with PM2.5 monitoring data. A <em>beta</em> version was released last year, along with an accompanying <a rel="nofollow" href="http://mazamascience.com/WorkingWithData/?p=1755#more-1755" target="_blank">blog post</a>. In this post, we discuss the purpose and uses of the PWFSLSmoke package and demonstrate some of the core functionality.<span></span></p>
<p>The PWFSLSmoke package provides tools to see how smoke affects communities across the country through analyzing and visualizing PM2.5 data. Its capabilities include:</p>
<ul>
<li>providing a versatile data model for dealing with PM2.5 data with the <em>ws_monitor</em> object</li>
<li>loading real-time and archival raw or pre-processed PM2.5 data from permanent and temporary monitors</li>
<li>quality-control options for vetting raw data</li>
<li>mapping and plotting functions for visualizing extended and short-term data series</li>
<li>algorithms for calculating NowCast values, rolling means, daily statistics, <em>etc</em>.</li>
<li>aggregation functionality for manipulating and analyzing PM2.5 data</li>
</ul>
<h1>Why study PM2.5 Data?</h1>
<p>Mazama Science created the PWFSLSmoke package for the <a rel="nofollow" href="https://www.airfire.org/" target="_blank">AirFire</a> team at the USFS Pacific Wildland Fire Sciences Lab (PWFSL) as part of their suite of <a rel="nofollow" href="https://tools.airfire.org/" target="_blank">tools</a> to analyze and visualize data from PM2.5 monitoring stations all over the country. PM2.5 refers to particulate matter under 2.5 micrometers in diameter that can come from many different sources: car exhaust, power plants, agricultural burning, <em>etc</em>. Breathing air with high PM2.5 levels is linked to all sorts of cardiovascular diseases and can worsen or trigger conditions like asthma and other chronic respiratory problems. For many communities outside of large metro areas, the main source of PM2.5 is wildfire smoke, and PM2.5 levels regularly reach hazardous levels during wildfire season. 2017 was a particularly bad smoke year across the Pacific Northwest, and even large cities like Seattle, Portland, and San Fransisco felt the effects of wildfire smoke during the height of the fire season.</p>
<p>There are three main organizations that aggregate PM2.5 monitoring data in the United States: <a rel="nofollow" href="https://www.airnow.gov/" target="_blank">AirNow</a>, <a rel="nofollow" href="https://wrcc.dri.edu/" target="_blank">WRCC</a>, and <a rel="nofollow" href="https://app.airsis.com/USFS" target="_blank">AIRSIS</a>. The PWFSLSmoke R package includes capabilities for ingesting, parsing, and quality-controlling raw data from these sites or loading RData files of pre-processed, real-time and archival data.</p>
<h1>Napa Valley Fires</h1>
<p>The 2017 wildfire season in the US was one of the most destructive ever. California was hit particularly hard with uncontrolled wildfires tearing through Napa wine country in October. They were incredibly destructive, destroying thousands of homes, businesses, wineries and vineyards, and taking close to 30 lives. Clouds of smoke choked communities miles away from the direct path of the fires, with concentrations climbing to unhealthy and hazardous levels.</p>
<p>We can see how smoke affected communities near the fires by using the PWFSLSmoke package to explore PM2.5 data from October of 2017. (For readability, we have omitted R code used to generate graphics and only mention relevant functions. However, this blog post was written as an R notebook and the complete source can be found on <a rel="nofollow" href="https://github.com/MazamaScience/PWFSLSmoke/blob/master/localNotebooks/NapaValley2017.Rmd" target="_blank">GitHub</a>.)</p>
<h2>Loading the Data</h2>
<p>Pre-processed archival data can easily be loaded using <code>airnow_load()</code>. We can select monitors that are within 100 kilometers of the Tubbs fire, the largest of the Napa Valley fires, with <code>monitor_subset()</code> and <code>monitor_subsetByDistance()</code>.</p>
<p>Since we are looking at smoke from specific fires, let’s load some fire data. <a rel="nofollow" href="http://cdfdata.fire.ca.gov/incidents/incidents_archived" target="_blank">Cal Fire</a> has an open archive of fire data that we can use. There were three large fires in the Napa Valley region which all started on October 9th. Let’s take a look at these fires in particular and data from monitors within 100km of the largest of the three.</p>
<pre>suppressPackageStartupMessages(library(PWFSLSmoke))

# Fires started on Oct 9
fireDF &lt;- data.frame(name = c(&quot;Tubbs&quot;, &quot;Atlas&quot;, &quot;Sulpher&quot;), 
 startdate = parseDatetime(c(20171009, 20171009, 20171009)),
 enddate = parseDatetime(c(20171031, 20171028, 20171026)),
 longitude = c(-122.63, -122.24, -122.65),
 latitude = c(38.61, 38.39, 39.01),
 stringsAsFactors = FALSE)


CAFires &lt;- airnow_load(year = 2017, month = 10) %>%
 monitor_subset(stateCode = &quot;CA&quot;) %>%
 monitor_subsetByDistance(fireDF$longitude[1], fireDF$latitude[1],
                          radius = 100)</pre>
<p>Now that we’ve got the data loaded into the environment, let’s delve into PWFSLSmoke’s plotting capabilities to see what it can tell us.</p>
<h2>Mapping</h2>
<p>A good place to start is by mapping monitor and fire locations. There are several different functions for mapping monitors. <code>monitorLeaflet()</code> will generate an interactive leaflet map that will be displayed in RStudio’s ‘Viewer’ tab. There are three functions for creating static maps: <code>monitorMap()</code> will plot monitors over the outlines of states and counties, <code>monitorEsriMap()</code> will plot monitors over a base image from ESRI, and <code>monitorGoogleMap()</code> will plot monitor locations over a base image, sourced, unsurprisingly, from GoogleMaps.</p>
<p>Particulate concentrations can be classified by the <a rel="nofollow" href="https://airnow.gov/index.cfm?action=aqibasics.aqi" target="_blank">Air Quality Index</a> (AQI) category that they fall into. AQI levels are defined by the EPA for regulating and warning people about air quality issues. The AQI cutoffs and the official colors associated with them are built into the package and used in several different plotting functions. All of the various monitor mapping functions color the monitor locations based on the AQI level of the maximum hourly PM2.5 value by default. There are some other built-in images which can be used in mapping, and added with the <code>addIcon()</code> function. For example, you could add the location of fires to a monitor map with little pictures of a fire as in the map below.</p>
<p><a rel="nofollow" href="https://i0.wp.com/mazamascience.com/WorkingWithData/wp-content/uploads/2018/01/blog_map.png" target="_blank"><img alt="" src="https://i0.wp.com/mazamascience.com/WorkingWithData/wp-content/uploads/2018/01/blog_map.png?w=450" class="wp-image-1773" data-recalc-dims="1"></a></p>
<p>This map tells us that smoke reached hazardous levels in those communities closest to the fires. As far away as San Fransisco, smoke levels were very unhealthy. Of course, this does not tell us anything about when the smoke affected these communities or for how long.</p>
<h2>Timeseries Plots</h2>
<p><code>monitorPlot_timeseries()</code> is designed to plot timeseries data for visualizing data over time. It has a ‘style’ argument, with a couple of built-in plotting styles for telling different kinds of stories. The plot below uses the ‘gnats’ style which quickly plots many points. Red bars under the plot represent the duration of different fires.</p>
<p><a rel="nofollow" href="https://i0.wp.com/mazamascience.com/WorkingWithData/wp-content/uploads/2018/01/gnats.png" target="_blank"><img alt="" src="https://i0.wp.com/mazamascience.com/WorkingWithData/wp-content/uploads/2018/01/gnats.png?w=450" class="wp-image-1779" data-recalc-dims="1"></a></p>
<p>This plot shows that PM2.5 levels were relatively constant around a baseline until shortly after the three fires started on October 9. The fires ignited and quickly grew to sizes large enough to send thick smoke to all neighboring communities. The first couple of days were the smokiest. Some monitors recoreded normal levels again around October 12 while many were still engulfed in smoke. After a brief respite, the baseline started creeping up again around October 16. All monitors returned to baseline levels around October 18, and stayed there for the rest of the month.</p>
<p>This gives us an idea of what air quality was like in the general region surrounding the fires. However, a curious observer of wildfire smoke might like to know how smoke affected a particular community. The city of Napa was directly hit by the Atlas fire, so let’s take a look at smoke levels in Napa to see if it gives us any insight into the effects of the fires there.</p>
<p>During this period, violent winds and flames meant that smoke levels could be jump wildly from hour to hour. One way to smooth out those changes a bit is by using <a rel="nofollow" href="https://en.wikipedia.org/wiki/NowCast_(air_quality_index)" target="_blank">NowCast</a> values. NowCast is used for smoothing data and can be used to estimate values for missing data. The <code>monitor_nowcast()</code> function will calculate hourly NowCast values for a <code>ws_monitor</code> object. Using <code>monitorPlot_timeseries()</code> to plot hourly values, and <code>monitor_nowcast()</code> to calculate and plot NowCast values on top of them, we can get a pretty good idea about what happened in Napa while the wildfires raged nearby.</p>
<p><a rel="nofollow" href="https://i2.wp.com/mazamascience.com/WorkingWithData/wp-content/uploads/2018/01/napa.png" target="_blank"><img alt="" src="https://i2.wp.com/mazamascience.com/WorkingWithData/wp-content/uploads/2018/01/napa.png?w=450" class="wp-image-1778" data-recalc-dims="1"></a></p>
<p>According to the data, PM2.5 levels were healthy up until close to midnight the night of October 8, when the Atlas Fire ignited and quickly exploded into a raging blaze only kilometers away. Smoke levels swung between moderate and very unhealthy throughout October 9, perhaps corresponding to changes in wind and weather. There is over a full day of missing values between October 10th and 11th, suggesting that the fire became so intense that the monitor was unable to record smoke values. This monitor comes back online during the 11th, recording dangerously high PM2.5 values for several days, indicating that clouds of smoke kept billowing into the town before finally easing off around October 19th.</p>
<h2>Plotting Aggregated Data</h2>
<p>This gives us a pretty good idea about how smoke affected a community very close to a fire on a pretty detailed level. While examining a specific subset of the data like this might give us some important insights, viewing aggregated data can tell different stories. Let’s say we want to understand how far smoke from these fires traveled and how communities farther from the fires experienced it. One option is to look at how smoke levels differed in locations at varying distances from the fires. The plot below does this, using <code>monitorPlot_dailyBarplot()</code> to plot the mean PM2.5 value for each day at Napa, Vallejo, and San Fransisco, with the distance from the Atlas Fire calculated using <code>monitor_distance()</code>.</p>
<p><a rel="nofollow" href="https://i1.wp.com/mazamascience.com/WorkingWithData/wp-content/uploads/2018/01/dailybarplots.png" target="_blank"><img alt="" src="https://i1.wp.com/mazamascience.com/WorkingWithData/wp-content/uploads/2018/01/dailybarplots.png?w=450" class="wp-image-1774" data-recalc-dims="1"></a></p>
<p>The shape of the data for the three different locations is pretty similar, with overall PM2.5 levels decreasing as the distance increases. Unfortunately, we are missing a day’s worth of data at Napa. However, the information from other monitors might help us guess what it was. At both Vallejo and San Fransisco, there was an increase in smoke from October 9 to 10, and a decrease from October 10 to 11. If smoke in Napa followed the same pattern, which would make sense if the smoke is coming from the same source in all three cities, we could speculate that smoke levels on October 10 would probably have been between the values for October 11 and October 13.</p>
<p>We hope this small foray into smoke analysis inspires you to look at data with the <strong>PWFSLSmoke</strong> package the next time wildfires erupt in North America.</p>
<p><em>Best Hopes for Healthy Air in 2018!</em></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://mazamascience.com/WorkingWithData/?p=1770"> R – Working With Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/L7j82qIwJVo" height="1"></a></div>