class: title-slide, nologo ### Steffi LaZerte & Sam Albers # `weathercan` ## An R package for accessing<br>Environment and Climate Change Canada weather data .align-bottom-left[<img src = "./Figures/twitter_black.jpg" style = "height: 40px; vertical-align:middle"> @steffilazerte <img src = "./Figures/github.png" style = "height: 40px; vertical-align:middle; margin-left: 25px"> steffilazerte <img src = "./Figures/web.png" style = "height: 40px; vertical-align:middle; margin-left: 25px"> steffilazerte.ca] .align-bottom-right[PCAG 2017] --- # Historical weather data - Environment and Climate Change Canada - 1840 to Present - Hourly, daily, monthly intervals - \> 26,000 stations (past and present) -- > **Lots of Data!** --- background-image: url("./Figures/ECCC_historical.png") background-size: 85% background-position: 50% 100% # Accessing data from ECCC website --- background-image: url("./Figures/ECCC_historical2.png") background-size: 90% background-position: 50% 50% --- background-image: url("./Figures/ECCC_historical3.png") background-size: 60% background-position: 50% 50% -- <!-- <div class = "arrow-left" style = "position:absolute; right: 370px; bottom: 300px;border: 5px solid red;width:100px; height:50px;border-radius: 10px;"></div> --> <div class = "highlightbox" style = "right: 23%; bottom: 36%; width: 200px; height: 90px;"></div> --- # Data good but not ready .center[<img src = "./Figures/ECCC_historical_data.png" style = "width:100%">] --- background-image: url("./Figures/weathercan_logo.png") background-size: 450px background-position: 50% 70% class: center, nologo # .red[`weathercan`]: An R package --- background-image: url("./Figures/R_sm.png") background-size: 200px background-position: 50% 80% class: center, nologo .spacer[ ] # .center.Rblue[What's R?] .spacer[ ] -- ### An open source, programming language, and software environment --- class: center, nologo .spacer[ ] # .center.Rblue[What's R?] .spacer[ ] ### An open source, programming language, and software environment Often used with RStudio IDE <img src = "./Figures/RStudio-Ball.png" style = "height:75px;vertical-align:middle;"> --- background-image: url("./Figures/Rexample.png") background-size: cover class: nologo --- # Why use `weathercan`? -- <div style = "height: 10px;"></div> #### Free - Free *and* open-source software (FOSS) -- #### Fast and Easy - One line of code to download data from many stations, over many years - Instantly usable -- #### Customizable - Data is trimmed to start and end times - You can specify stations, time intervals, timezones, etc. --- # Why use `weathercan`? .spacer[ ] .spacer[ ] #### **Reproducible!** > - Scripts provide a record of actions > - Just note the `weathercan` version (`packageVersion(weathercan)`) > - Hard to document mouse clicks or website searches --- # Getting started with .red[`weathercan`] .spacer[ ] #### Installing `devtools` ```r install.packages("devtools") ``` #### Installing `weathercan` with `devtools` ```r devtools::install_github("steffilazerte/weathercan", build_vignettes = TRUE) ``` --- # Basic usage #### Code ```r library(weathercan) w <- weather(station_ids = c(50821, 51097), start = "2017-09-01") ``` -- #### Output ``` ## # A tibble: 1,344 x 28 ## station_name station_id prov lat lon time hmdx hmdx_flag pressure ## * <chr> <dbl> <fctr> <dbl> <dbl> <dttm> <dbl> <chr> <dbl> ## 1 BRANDON A 50821 MB 49.91 -99.95 2017-09-01 00:00:00 26 96.21 ## 2 BRANDON A 50821 MB 49.91 -99.95 2017-09-01 01:00:00 26 96.15 ## 3 BRANDON A 50821 MB 49.91 -99.95 2017-09-01 02:00:00 25 96.09 ## 4 BRANDON A 50821 MB 49.91 -99.95 2017-09-01 03:00:00 NA 96.07 ## 5 BRANDON A 50821 MB 49.91 -99.95 2017-09-01 04:00:00 NA 96.08 ## # ... with 1,339 more rows, and 19 more variables ``` --- # Plotting ```r ggplot(data = w, aes(x = time, y = temp, colour = station_name)) + theme_bw() + geom_line() + labs(x = "Date", y = "Temperature C", colour = "Station") ``` <img src="LaZerte_PCAG_2017_weathercan_extra_files/figure-html/unnamed-chunk-5-1.png" width="100%" /> --- exclude: false .corner-ribbon[ ] # Again with Pressure ```r ggplot(data = w, aes(x = time, y = pressure, colour = station_name)) + theme_bw() + geom_line() + labs(x = "Date", y = "Pressure (kPa)", colour = "Station") ``` <img src="LaZerte_PCAG_2017_weathercan_extra_files/figure-html/unnamed-chunk-6-1.png" width="100%" /> --- # And done! ```r library(weathercan) w <- weather(station_ids = c(50821, 51097), start = "2017-09-01") ggplot(data = w, aes(x = time, y = temp, colour = station_name)) + theme_bw() + geom_line() + labs(x = "Date", y = "Temperature C", colour = "Station") ``` --- # Hmmm... ```r library(weathercan) w <- weather(station_ids = c(50821, 51097), start = "2017-09-01") ggplot(data = w, aes(x = time, y = temp, colour = station_name)) + theme_bw() + geom_line() + labs(x = "Date", y = "Temperature C", colour = "Station") ``` <div class = "highlightbox" style = "top: 23%; right: 57%; width: 170px; height: 30px;"></div> <div style = "position: absolute; top: 13%; right: 63%;"><h2><strong>?<strong></h3></div> -- .spacer[ ] > How do we get station ids? --- exclude: false .corner-ribbon[ ] # Searching by station name ```r stations_search(name = "Brandon") ``` -- exclude: false ``` ## # A tibble: 15 x 10 ## prov station_name station_id climate_id lat lon elev interval start end ## <fctr> <chr> <fctr> <fctr> <dbl> <dbl> <dbl> <chr> <int> <int> ## 1 MB BRANDON #1 WINTER BAY 3474 5010498 49.83 -99.98 396.0 day 1987 2002 ## 2 MB BRANDON #1 WINTER BAY 3474 5010498 49.83 -99.98 396.0 month 1987 2002 ## 3 MB BRANDON A 3471 5010480 49.91 -99.95 409.4 day 1941 2012 ## 4 MB BRANDON A 3471 5010480 49.91 -99.95 409.4 hour 1958 2012 ## 5 MB BRANDON A 3471 5010480 49.91 -99.95 409.4 month 1941 2012 ## 6 MB BRANDON A 50821 5010481 49.91 -99.95 409.3 day 2012 2017 ## 7 MB BRANDON A 50821 5010481 49.91 -99.95 409.3 hour 2012 2017 ## 8 MB BRANDON CDA 3472 5010485 49.87 -99.98 362.7 day 1890 2010 ## 9 MB BRANDON CDA 3472 5010485 49.87 -99.98 362.7 month 1890 2007 ## 10 MB BRANDON RCS 49909 5010490 49.90 -99.95 409.4 day 2012 2017 ## # ... with 5 more rows ``` -- exclude: false <div class = "highlightbox" style = "width: 90px; height: 380px; right: 20.5%; top: 32.5%"></div> --- # Searching by station name ```r stations_search(name = "Brandon", interval = "hour") ``` -- ``` ## # A tibble: 3 x 10 ## prov station_name station_id climate_id lat lon elev interval start end ## <fctr> <chr> <fctr> <fctr> <dbl> <dbl> <dbl> <chr> <int> <int> ## 1 MB BRANDON A 3471 5010480 49.91 -99.95 409.4 hour 1958 2012 ## 2 MB BRANDON A 50821 5010481 49.91 -99.95 409.3 hour 2012 2017 ## 3 MB BRANDON RCS 49909 5010490 49.90 -99.95 409.4 hour 2012 2017 ``` --- exclude: false .corner-ribbon[ ] # Searching by station name ```r stations_search(name = "(Brandon)|(Winnipeg)", interval = "hour") ``` -- exclude: false ``` ## # A tibble: 8 x 10 ## prov station_name station_id climate_id lat lon elev interval start end ## <fctr> <chr> <fctr> <fctr> <dbl> <dbl> <dbl> <chr> <int> <int> ## 1 MB BRANDON A 3471 5010480 49.91 -99.95 409.4 hour 1958 2012 ## 2 MB BRANDON A 50821 5010481 49.91 -99.95 409.3 hour 2012 2017 ## 3 MB BRANDON RCS 49909 5010490 49.90 -99.95 409.4 hour 2012 2017 ## 4 MB WINNIPEG A CS 27174 502S001 49.92 -97.25 238.7 hour 2013 2017 ## 5 MB WINNIPEG INTL A 51097 5023227 49.91 -97.24 238.7 hour 2013 2017 ## 6 MB WINNIPEG RICHARDSON AWOS 47407 5023226 49.92 -97.23 238.7 hour 2008 2013 ## 7 MB WINNIPEG RICHARDSON INT'L A 3698 5023222 49.92 -97.23 238.7 hour 1953 2013 ## 8 MB WINNIPEG THE FORKS 28051 5023262 49.89 -97.13 230.0 hour 1999 2017 ``` -- exclude: false <div class = "highlightbox" style = "top: 46.5%; right: 50%; height: 35px; width: 350px;"></div> <div class = "highlightbox" style = "top: 60.5%; right: 50%; height: 35px; width: 350px;"></div> > Make a note: 50821 (Brandon) and 51097 (Winnipeg) --- # Alternative: Searching by coordinates - Alternatively search according to location: `c(latitude, longitude)` - Search within 10km of this location: `dist = 10` ```r stations_search(coords = c(49.84847, -99.95009), dist = 10, interval = "hour") ``` -- ``` ## # A tibble: 3 x 11 ## prov station_name station_id climate_id lat lon elev interval start end distance ## <fctr> <chr> <fctr> <fctr> <dbl> <dbl> <dbl> <chr> <int> <int> <dbl> ## 1 MB BRANDON RCS 49909 5010490 49.90 -99.95 409.4 hour 2012 2017 5.731565 ## 2 MB BRANDON A 3471 5010480 49.91 -99.95 409.4 hour 1958 2012 6.843848 ## 3 MB BRANDON A 50821 5010481 49.91 -99.95 409.3 hour 2012 2017 6.843848 ``` --- # Understanding the data #### Flags ``` ## # A tibble: 7 x 6 ## station_id date mean_min_temp mean_min_temp_flag mean_temp mean_temp_flag ## * <dbl> <date> <dbl> <chr> <dbl> <chr> ## 1 5401 2017-01-01 -7.9 -4.4 ## 2 5401 2017-02-01 -8.7 -4.3 ## 3 5401 2017-03-01 -9.6 -5.2 ## 4 5401 2017-04-01 3.3 7.9 ## 5 5401 2017-05-01 6.7 E 11.8 E ## 6 5401 2017-06-01 12.3 17.5 ## 7 5401 2017-07-01 14.3 19.3 ``` <div class = "highlightbox" style = "bottom: 40.5%; right: 41%; height: 35px; width: 65px;"></div> <div class = "highlightbox" style = "bottom: 40.5%; right: 18.75%; height: 35px; width: 65px;"></div> --- background-image: url("./Figures/flags_vignette.png") background-position: 50% 90% background-size: 72.5% # Understanding the data ```r vignette("flags", package = "weathercan") ``` --- # Understanding the data #### Units and measurements ``` ## # A tibble: 1,344 x 6 ## station_id time temp temp_dew rel_hum wind_dir ## * <dbl> <dttm> <dbl> <dbl> <dbl> <dbl> ## 1 50821 2017-09-01 00:00:00 20.8 17.3 80 18 ## 2 50821 2017-09-01 01:00:00 20.8 17.2 80 17 ## 3 50821 2017-09-01 02:00:00 20.0 16.9 83 17 ## 4 50821 2017-09-01 03:00:00 19.4 16.9 85 16 ## 5 50821 2017-09-01 04:00:00 19.2 17.2 88 19 ## 6 50821 2017-09-01 05:00:00 18.8 17.8 93 17 ## 7 50821 2017-09-01 06:00:00 18.9 17.9 94 16 ## 8 50821 2017-09-01 07:00:00 18.3 17.7 96 18 ## 9 50821 2017-09-01 08:00:00 19.9 17.8 88 20 ## 10 50821 2017-09-01 09:00:00 20.6 18.2 86 23 ## # ... with 1,334 more rows ``` --- background-image: url("./Figures/glossary_vignette.png") background-position: 50% 90% background-size: 65% # Understanding the data ```r vignette("glossary", package = "weathercan") ``` --- # Combining with other data - Adding weather data to other data sets - Times don't always line up -- .pull-left[ #### Sediment data ``` ## # A tibble: 1,392 x 2 ## time amount ## <dttm> <dbl> ## 1 2017-09-01 00:05:34 168.3133 ## 2 2017-09-01 00:35:34 156.9122 ## 3 2017-09-01 01:05:34 175.6169 ## 4 2017-09-01 01:35:34 184.5908 ## 5 2017-09-01 02:05:34 163.2017 ## 6 2017-09-01 02:35:34 169.2177 ## 7 2017-09-01 03:05:34 167.8620 ## # ... with 1,385 more rows ``` ] .pull-right[ #### Brandon Weather data ``` ## # A tibble: 672 x 3 ## time temp pressure ## <dttm> <dbl> <dbl> ## 1 2017-09-01 00:00:00 20.8 96.21 ## 2 2017-09-01 01:00:00 20.8 96.15 ## 3 2017-09-01 02:00:00 20.0 96.09 ## 4 2017-09-01 03:00:00 19.4 96.07 ## 5 2017-09-01 04:00:00 19.2 96.08 ## 6 2017-09-01 05:00:00 18.8 96.05 ## 7 2017-09-01 06:00:00 18.9 96.04 ## # ... with 665 more rows ``` ] <div class = "highlightbox" style = "left: 18%; top: 55%; height:32%; width:8%"></div> <div class = "highlightbox" style = "right: 24.5%; top: 55%; height:32%; width:8%"></div> --- # Interpolating - Linear interpolation where possible - Only a single weather station at a time ```r w <- weather(station_ids = 50821, start = "2017-09-01") ``` ```r sediment <- add_weather(data = sediment, weather = w, col = c("temp", "pressure")) ``` --- # Interpolating .pull-left-55[ #### Sediment data ``` ## # A tibble: 1,392 x 4 ## time amount temp pressure ## <dttm> <dbl> <dbl> <dbl> ## 1 2017-09-01 00:05:34 168.3133 20.80000 96.20443 ## 2 2017-09-01 00:35:34 156.9122 20.80000 96.17443 ## 3 2017-09-01 01:05:34 175.6169 20.72578 96.14443 ## 4 2017-09-01 01:35:34 184.5908 20.32578 96.11443 ## 5 2017-09-01 02:05:34 163.2017 19.94433 96.08814 ## 6 2017-09-01 02:35:34 169.2177 19.64433 96.07814 ## 7 2017-09-01 03:05:34 167.8620 19.38144 96.07093 ## # ... with 1,385 more rows ``` ] .pull-right-45[ #### Weather data ``` ## # A tibble: 672 x 3 ## time temp pressure ## * <dttm> <dbl> <dbl> ## 1 2017-09-01 00:00:00 20.8 96.21 ## 2 2017-09-01 01:00:00 20.8 96.15 ## 3 2017-09-01 02:00:00 20.0 96.09 ## 4 2017-09-01 03:00:00 19.4 96.07 ## 5 2017-09-01 04:00:00 19.2 96.08 ## 6 2017-09-01 05:00:00 18.8 96.05 ## 7 2017-09-01 06:00:00 18.9 96.04 ## # ... with 665 more rows ``` ] <div class = "highlightbox" style = "left: 34%; top: 40.5%; height:32%; width:8%"></div> <div class = "highlightbox" style = "right: 14.25%; top: 40.5%; height:32%; width:5%"></div> --- # Recap! -- #### 1. Load `weathercan` package ```r library(weathercan) ``` -- #### 2. Find a station ```r stations_search("Brandon") ``` -- #### 3. Download weather ```r w <- weather(station_ids = 50821, start = "2017-09-01") ``` -- #### 4. Add weather data to an existing data set ```r sediment <- add_weather(data = sediment, weather = w, cols = "temp") ``` --- # We invite contributions! .spacer[ ] #### Openly developed on GitHub <img src = "./Figures/github.png" style = "width: 35px; vertical-align: middle; margin-bottom: 5px;"> .spacer[ ] Contribute what you can (**You don't have to be an R programmer!**): - Ideas / Feature-requests - Bugs - Bug-fixes - Development .spacer[ ] <img src = "./Figures/github.png" style = "width: 35px; vertical-align: middle;">: <http://github.com/steffilazerte/weathercan> --- class: final-slide, nologo # Help with .red[`weathercan`] **Tutorials and Reference:** <http://steffilazerte.github.io/weathercan> **This presentation:** <https://steffilazerte.github.io/Presentations/> .spacer[ ] **Contact Steffi:** <img src = "./Figures/twitter_black.jpg" style = "height: 40px; vertical-align:middle; margin-left: 25px"> @steffilazerte <img src = "./Figures/github.png" style = "height: 40px; vertical-align:middle; margin-left: 25px"> steffilazerte <img src = "./Figures/web.png" style = "height: 40px; vertical-align:middle; margin-left: 25px"> steffilazerte.ca -- <div style = "position:absolute; bottom: 32%"> <h2>Thanks!</h2></div> <div style = "position:absolute; bottom: 20%; left: 4%">Dr. David J. Hill <br><img src = "./Figures/TRU2_bw.png" style = "height: 50px; vertical-align: middle; margin-top: 10px"></div> .small[.align-bottom-left[Slides created via the R package [xaringan](https://github.com/yihui/xaringan), using [remark.js](https://remarkjs.com), [knitr](http://yihui.name/knitr), and [R Markdown](https://rmarkdown.rstudio.com)<br> <code class = "remark-inline-code">weathercan v0.2.3</code>]]