-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-Day3_web_data.html
446 lines (360 loc) · 21.1 KB
/
03-Day3_web_data.html
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Web data</title>
<script src="03-Day3_web_data_files/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="03-Day3_web_data_files/bootstrap-3.3.5/css/readable.min.css" rel="stylesheet" />
<script src="03-Day3_web_data_files/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="03-Day3_web_data_files/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="03-Day3_web_data_files/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="03-Day3_web_data_files/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="03-Day3_web_data_files/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="03-Day3_web_data_files/tocify-1.9.1/jquery.tocify.js"></script>
<script src="03-Day3_web_data_files/navigation-1.1/tabsets.js"></script>
<link href="03-Day3_web_data_files/font-awesome-5.0.13/css/fa-svg-with-js.css" rel="stylesheet" />
<script src="03-Day3_web_data_files/font-awesome-5.0.13/js/fontawesome-all.min.js"></script>
<script src="03-Day3_web_data_files/font-awesome-5.0.13/js/fa-v4-shims.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; background-color: #f8f8f8; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
pre, code { background-color: #f8f8f8; }
code > span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code > span.dt { color: #204a87; } /* DataType */
code > span.dv { color: #0000cf; } /* DecVal */
code > span.bn { color: #0000cf; } /* BaseN */
code > span.fl { color: #0000cf; } /* Float */
code > span.ch { color: #4e9a06; } /* Char */
code > span.st { color: #4e9a06; } /* String */
code > span.co { color: #8f5902; font-style: italic; } /* Comment */
code > span.ot { color: #8f5902; } /* Other */
code > span.al { color: #ef2929; } /* Alert */
code > span.fu { color: #000000; } /* Function */
code > span.er { color: #a40000; font-weight: bold; } /* Error */
code > span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #000000; } /* Constant */
code > span.sc { color: #000000; } /* SpecialChar */
code > span.vs { color: #4e9a06; } /* VerbatimString */
code > span.ss { color: #4e9a06; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #000000; } /* Variable */
code > span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code > span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code > span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code > span.ex { } /* Extension */
code > span.at { color: #c4a000; } /* Attribute */
code > span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code > span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="css\camp_style.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Web data</h1>
</div>
<style> code {color: #535353 !important;} </style>
<p><img src="images/web_data.png" width="300" style="margin-top: -25px; margin-right: 0; margin-left: 30px; float:right;"></p>
<p>This path includes examples of downloading online data from FTP sites and other data stores.</p>
<div id="epas-airnow" class="section level1">
<h1>EPA’s <em>AirNow</em></h1>
<p>Air data for the <em>entire</em> United States is at your finger tips. <em>AirNow</em> maintains a publicly accessible folder of current air monitoring data at <a href="https://files.airnowtech.org/">Today’s air quality</a>.</p>
<p>Use the following code to grab the most recent AQI results for the United States.</p>
<div id="get-air-monitoring-data" class="section level3">
<h3>Get air monitoring data</h3>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(dplyr)
<span class="kw">library</span>(readr)
<span class="co"># Connect to AirNow data site</span>
airnow_link <-<span class="st"> </span><span class="kw">paste0</span>(<span class="st">"https://s3-us-west-1.amazonaws.com//files.airnowtech.org/airnow/today/HourlyData_"</span>,
<span class="kw">format</span>(<span class="kw">Sys.time</span>() <span class="op">-</span><span class="dv">60</span><span class="op">*</span><span class="dv">75</span>, <span class="st">"%Y%m%d%H"</span>, <span class="dt">tz =</span> <span class="st">"GMT"</span>), <span class="co">#Subtracts one hour from current hour</span>
<span class="st">".dat"</span>)
<span class="co"># Read a "|" delimited file that has no column names (Thanks EPA!)</span>
aqi_now <-<span class="st"> </span><span class="kw">read_delim</span>(airnow_link, <span class="st">"|"</span>, <span class="dt">col_names =</span> F)
<span class="co"># Add column names as a list</span>
<span class="kw">names</span>(aqi_now) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"date"</span>, <span class="st">"time"</span>, <span class="st">"aqsid"</span>, <span class="st">"city"</span>, <span class="st">"local_time"</span>, <span class="st">"parameter"</span>, <span class="st">"units"</span>, <span class="st">"concentration"</span>, <span class="st">"agency"</span>)
<span class="co"># Filter to Ozone and PM2.5 results</span>
aqi_now <-<span class="st"> </span><span class="kw">filter</span>(aqi_now, parameter <span class="op">%in%</span><span class="st"> </span><span class="kw">c</span>(<span class="st">"OZONE"</span>, <span class="st">"PM2.5"</span>))
<span class="co"># Show table of top concentration results</span>
aqi_now <span class="op">%>%</span><span class="st"> </span><span class="kw">arrange</span>(<span class="kw">desc</span>(concentration)) <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(<span class="op">-</span>time, <span class="op">-</span>aqsid, <span class="op">-</span>units) <span class="op">%>%</span><span class="st"> </span><span class="kw">head</span>(<span class="dv">10</span>)</code></pre></div>
<pre><code>## # A tibble: 10 x 6
## date city local_time parameter concentration agency
## <chr> <chr> <int> <chr> <dbl> <chr>
## 1 11/14~ Chico - ~ -8 PM2.5 184 California Air Re~
## 2 11/14~ Beijing 8 PM2.5 179 U.S. Department o~
## 3 11/14~ Arden Arc~ -8 PM2.5 176 Sacramento Metro.~
## 4 11/14~ Elk Grove -8 PM2.5 115 Sacramento Metro.~
## 5 11/14~ Yuba City -8 PM2.5 114 California Air Re~
## 6 11/14~ "Downtown~ -8 PM2.5 109 California Air Re~
## 7 11/14~ Ulaanbaat~ 8 PM2.5 99 U.S. Department o~
## 8 11/14~ Modesto -~ -8 PM2.5 96 California Air Re~
## 9 11/14~ San Rafael -8 PM2.5 85 San Francisco Bay~
## 10 11/14~ Sequoia a~ -8 PM2.5 82.3 National Park Ser~</code></pre>
</div>
<div id="get-monitoring-locations" class="section level3">
<h3>Get monitoring locations</h3>
<p>The air monitoring locations can be accessed from <em>AirNow</em> as well.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Connect to AirNow data site</span>
airnow_link <-<span class="st"> "https://s3-us-west-1.amazonaws.com//files.airnowtech.org/airnow/today/monitoring_site_locations.dat"</span>
<span class="co"># Read a pipe (|) delimited file </span>
aqi_sites <-<span class="st"> </span><span class="kw">read_delim</span>(airnow_link, <span class="st">"|"</span>, <span class="dt">col_names =</span> F)
<span class="co"># Drop empty columns</span>
aqi_sites <-<span class="st"> </span>aqi_sites[ , <span class="op">-</span><span class="kw">c</span>(<span class="dv">14</span><span class="op">:</span><span class="dv">16</span>,<span class="dv">22</span><span class="op">:</span><span class="dv">23</span>)]
<span class="co"># Add column names</span>
<span class="kw">names</span>(aqi_sites) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"aqsid"</span>,
<span class="st">"parameter"</span>,
<span class="st">"local_id"</span>,
<span class="st">"name"</span>,
<span class="st">"status"</span>,
<span class="st">"state_region"</span>,
<span class="st">"agency"</span>,
<span class="st">"epa_region"</span>,
<span class="st">"lat"</span>,
<span class="st">"long"</span>,
<span class="st">"elevation"</span>,
<span class="st">"local_time"</span>,
<span class="st">"country"</span>,
<span class="st">"city"</span>,
<span class="st">"state_fips"</span>,
<span class="st">"state"</span>,
<span class="st">"county_fips"</span>,
<span class="st">"county"</span>)
<span class="co"># Drop the parameter column and take *unique* rows</span>
aqi_sites <-<span class="st"> </span>aqi_sites <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(<span class="op">-</span>parameter) <span class="op">%>%</span><span class="st"> </span><span class="kw">unique</span>()</code></pre></div>
</div>
<div id="join-tables-and-map-pm2.5-concentrations" class="section level3">
<h3>Join tables and map PM2.5 concentrations</h3>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="kw">library</span>(viridis)
<span class="co"># Add site locations to monitoring data</span>
aqi_now <-<span class="st"> </span><span class="kw">left_join</span>(aqi_now, aqi_sites, <span class="dt">by =</span> <span class="st">"aqsid"</span>)
<span class="co"># Plot map of sites for PM2.5</span>
<span class="kw">filter</span>(aqi_now, parameter <span class="op">==</span><span class="st"> "PM2.5"</span>, <span class="op">!</span><span class="kw">is.na</span>(lat)) <span class="op">%>%</span>
<span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> long, <span class="dt">y =</span> lat)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">color =</span> concentration), <span class="dt">size =</span> <span class="dv">3</span>, <span class="dt">alpha =</span> <span class="fl">0.6</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">scale_color_viridis</span>() </code></pre></div>
<p><img src="03-Day3_web_data_files/figure-html/unnamed-chunk-3-1.png" width="1152" /></p>
</div>
<div id="map-minnesota" class="section level3">
<h3>Map Minnesota</h3>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="kw">library</span>(viridis)
<span class="co"># Filter to Minnesota</span>
aqi_mn <-<span class="st"> </span><span class="kw">filter</span>(aqi_now, state <span class="op">==</span><span class="st"> "MN"</span>)
<span class="co"># Plot map of sites for PM2.5</span>
<span class="kw">filter</span>(aqi_mn, parameter <span class="op">==</span><span class="st"> "PM2.5"</span>) <span class="op">%>%</span>
<span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> long, <span class="dt">y =</span> lat)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">color =</span> concentration), <span class="dt">size =</span> <span class="dv">6</span>, <span class="dt">alpha =</span> <span class="fl">0.6</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_label</span>(<span class="kw">aes</span>(<span class="dt">label =</span> concentration), <span class="dt">hjust =</span> <span class="fl">1.4</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">scale_color_viridis</span>() </code></pre></div>
<p><img src="03-Day3_web_data_files/figure-html/unnamed-chunk-4-1.png" width="768" /></p>
</div>
</div>
<div id="weather-alerts" class="section level1">
<h1>Weather alerts</h1>
<p>The script below will make a map of the current weather alerts in the Midwest. <em>Hint: You will need to install new packages.</em></p>
<p>Install the latest version of <em>weatherAlerts</em> with:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">install.packages</span>(<span class="st">"devtools"</span>)<span class="st">`</span><span class="dt"> </span>
<span class="dt">devtools::install_github("ianmcook/weatherAlerts") </span>
<span class="dt">devtools::install_github("ianmcook/weatherAlertAreas")</span></code></pre></div>
<p><br></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(tidyverse)
<span class="kw">library</span>(weatherAlerts)</code></pre></div>
<pre><code>## Error in library(weatherAlerts): there is no package called 'weatherAlerts'</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(weatherAlertAreas)</code></pre></div>
<pre><code>## Error in library(weatherAlertAreas): there is no package called 'weatherAlertAreas'</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(leaflet)
alerts <-<span class="st"> </span><span class="kw">getAlerts</span>(<span class="dt">includeStates =</span> <span class="kw">c</span>(<span class="st">"MN"</span>, <span class="st">"WI"</span>, <span class="st">"ND"</span>, <span class="st">"SD"</span>, <span class="st">"IA"</span>, <span class="st">"MI"</span>, <span class="st">"IL"</span>, <span class="st">"IN"</span>, <span class="st">"OH"</span>, <span class="st">"MO"</span>, <span class="st">"FL"</span>)) <span class="co"># takes about 20 seconds</span></code></pre></div>
<pre><code>## Error in getAlerts(includeStates = c("MN", "WI", "ND", "SD", "IA", "MI", : could not find function "getAlerts"</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Save alert severity to a list</span>
severity <-<span class="st"> </span>alerts<span class="op">@</span>data<span class="op">$</span>severity</code></pre></div>
<pre><code>## Error in eval(expr, envir, enclos): object 'alerts' not found</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Assign alerts a color based on severity ranking</span>
colorMap <-<span class="st"> </span><span class="kw">c</span>(<span class="dt">Minor =</span> <span class="st">"green"</span>,
<span class="dt">Moderate =</span> <span class="st">"yellow"</span>,
<span class="dt">Severe =</span> <span class="st">"red"</span>,
<span class="dt">Extreme =</span> <span class="st">"magenta"</span>,
<span class="dt">Unknown =</span> <span class="st">"grey"</span>)
severityColors <-<span class="st"> </span><span class="kw">unname</span>(colorMap[severity])</code></pre></div>
<pre><code>## Error in unname(colorMap[severity]): object 'severity' not found</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">leaflet</span>() <span class="op">%>%</span>
<span class="st"> </span><span class="kw">addProviderTiles</span>(providers<span class="op">$</span>OpenStreetMap) <span class="op">%>%</span>
<span class="st"> </span><span class="kw">addPolygons</span>(<span class="dt">data =</span> alerts,
<span class="dt">fillColor =</span> severityColors,
<span class="dt">color =</span> <span class="st">"black"</span>, <span class="co"># Border color</span>
<span class="dt">weight =</span> <span class="dv">1</span>,
<span class="dt">label =</span> <span class="op">~</span>title)</code></pre></div>
<pre><code>## Error in filterNULL(list(stroke = stroke, color = color, weight = weight, : object 'severityColors' not found</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Hover for alert information.</span></code></pre></div>
<p><br></p>
<p><em>Return to the top of the page.</em></p>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>