forked from Intro-Data-Sci-2022/3_snow_functions_iteration
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnow_data_assignment.Rmd
More file actions
315 lines (208 loc) · 8.31 KB
/
snow_data_assignment.Rmd
File metadata and controls
315 lines (208 loc) · 8.31 KB
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
---
title: "Snow Data Assignment: Web Scraping, Functions, and Iteration"
author: "Nathan Mueller, Devin Hunt"
date: "2-7-2022"
output:
html_document:
theme: cerulean
toc: TRUE
editor_options:
chunk_output_type: inline
---
```{r setup, include=FALSE}
library(rvest)
library(tidyverse)
library(lubridate)
library(readxl)
```
# Simple web scraping
R can read html using either rvest, xml, or xml2 packages. Here we are going to navigate to the Center for Snow and Avalance Studies [Website](https://snowstudies.org/archived-data/) and read a table in. This table contains links to data we want to programatically download for three sites. We don't know much about these sites, but they contain incredibly rich snow, temperature, and precip data.
## Reading an html
### Extract CSV links from webpage
```{r, message=F, warning=F}
site_url <- 'https://snowstudies.org/archived-data/'
#Read the web url
webpage <- read_html(site_url)
#See if we can extract tables and get the data that way
tables <- webpage %>%
html_nodes('table') %>%
magrittr::extract2(3) %>%
html_table(fill = TRUE)
#That didn't work, so let's try a different approach
#Extract only weblinks and then the URLs!
links <- webpage %>%
html_nodes('a') %>%
.[grepl('24hr',.)] %>%
html_attr('href')
```
## Data Download
### Download data in a for loop
```{r, message=F, warning=F}
#Grab only the name of the file by splitting out on forward slashes
splits <- str_split_fixed(links,'/',8)
#Keep only the 8th column
dataset <- splits[,8]
#generate a file list for where the data goes
file_names <- paste0('data/',dataset)
for(i in 1:3){
download.file(links[i],destfile=file_names[i])
}
downloaded <- file.exists(file_names)
evaluate <- !all(downloaded)
```
### Download data in a map
```{r, message=F, warning=F}
#Map version of the same for loop (downloading 3 files)
if(evaluate == T){
map2(links[1:3],file_names[1:3],download.file)
}else{print('data already downloaded')}
```
## Data read-in
### Read in just the snow data as a loop
```{r, message=F, warning=F}
#Pattern matching to only keep certain files
snow_files <- file_names %>%
.[!grepl('SG_24',.)] %>%
.[!grepl('PTSP',.)]
empty_data <- list()
snow_data <- for(i in 1:length(snow_files)){
empty_data[[i]] <- read_csv(snow_files[i]) %>%
select(Year,DOY,Sno_Height_M)
}
snow_data_full <- do.call('rbind',empty_data)
summary(snow_data_full)
```
### Read in the data as a map function
```{r, message=F, warning=F}
our_snow_reader <- function(file){
name = str_split_fixed(file,'/',2)[,2] %>%
gsub('_24hr.csv','',.)
df <- read_csv(file) %>%
select(Year,DOY,Sno_Height_M) %>%
mutate(site = name)
}
snow_data_full <- map_dfr(snow_files,our_snow_reader)
summary(snow_data_full)
```
### Plot snow data
```{r, message=F, warning=F}
snow_yearly <- snow_data_full %>%
group_by(Year,site) %>%
summarize(mean_height = mean(Sno_Height_M,na.rm=T))
ggplot(snow_yearly,aes(x=Year,y=mean_height,color=site)) +
geom_point() +
ggthemes::theme_few() +
ggthemes::scale_color_few()
```
# Assignment:
1. Extract the meteorological data URLs. Here we want you to use the `rvest` package to get the URLs for the `SASP forcing` and `SBSP_forcing` meteorological datasets.
```{r, message=F, warning=F}
links_forcing <- webpage %>%
html_nodes('a') %>%
.[grepl('forcing',.)] %>%
html_attr('href')
```
2. Download the meteorological data. Use the `download_file` and `str_split_fixed` commands to download the data and save it in your data folder. You can use a for loop or a map function.
```{r, message=F, warning=F}
#Grab only the name of the file by splitting out on forward slashes
splits2 <- str_split_fixed(links_forcing,'/',8)
#Keep only the 8th column
dataset2 <- splits2[,8]
#generate a file list for where the data goes
file_names2 <- paste0('data/', dataset2)
for(i in 1:2){
download.file(links_forcing[i], destfile=file_names2[i])
}
```
3. Write a custom function to read in the data and append a site column to the data.
```{r, message=F, warning=F}
# this code grabs the variable names from the metadata pdf file
library(pdftools)
headers <- pdf_text('https://snowstudies.org/wp-content/uploads/2022/02/Serially-Complete-Metadata-text08.pdf') %>%
readr::read_lines(.) %>%
trimws(.) %>%
str_split_fixed(.,'\\.',2) %>%
.[,2] %>%
.[1:26] %>%
str_trim(side = "left")
forcing_snow_reader <- function(dfile){
name = str_split_fixed(dfile,'/',2)[,2] %>%
gsub('_Forcing_Data.txt','',.) %>%
gsub('SBB_','',.)
df <- read_table(dfile, col_names = headers, na = "NA")
# When using mutate
# df <- mutate(site = name)
# Continued to recieve the error:
# Error in UseMethod("mutate") : no applicable method for 'mutate' applied to an object of class "character"
# Solution:
df %>% add_column(site = name)
}
```
4. Use the `map` function to read in both meteorological files. Display a summary of your tibble.
```{r warning = FALSE}
# Attempted renaming strategies:
# names(df) <- headers
#
# select(Year,DOY,Sno_Height_M)
# rename_with(headers) %>%
forcing_data_full <- map_dfr(file_names2, forcing_snow_reader)
summary(forcing_data_full)
```
5. Make a line plot of mean temp by year by site (using the `air temp [K]` variable). Is there anything suspicious in the plot? Adjust your filtering if needed.
```{r, message=F, warning=F}
mean_temp_year <- forcing_data_full %>%
group_by(year, site) %>%
summarize(mean_temp = mean(`air temp [K]`, na.rm = TRUE))
ggplot(mean_temp_year, aes(year, mean_temp, color = site)) +
geom_line() + labs(title = "Mean Temperature [K] By Year", x = "Year", y = "Mean Temp [Kelvin]")
```
The year 2003 produced abnormally low mean temperatures for both sites.
6. Write a function that makes line plots of monthly average temperature at each site for a given year. Use a for loop to make these plots for 2005 to 2010. Are monthly average temperatures at the Senator Beck Study Plot ever warmer than the Snow Angel Study Plot?
Hint: https://ggplot2.tidyverse.org/reference/print.ggplot.html
```{r, warning = FALSE, message = FALSE}
monthly_temp <- function(data, year_value){
dataf <- data %>% filter(year == year_value)
month_values <- dataf %>% group_by(month, site) %>%
summarize(mean_temp = mean(`air temp [K]`, na.rm = TRUE))
plot_title <- paste("Mean Temperature [K] By Month in", year_value)
p1 <- ggplot(month_values, aes(month, mean_temp, color = site)) +
geom_line() + labs(title = plot_title, x = "Month", y = "Mean Temp [Kelvin]")
print(p1)
}
year_input <- c(2005, 2006, 2007, 2008, 2009, 2010)
for (i in 1:length(year_input)){
monthly_temp_plots <- monthly_temp(forcing_data_full, year_input[i])
}
```
From the 6 graphs created, we cannot find a monthly temperature observation where SBSP exceeds the SASP site.
Bonus: Make a plot of average daily precipitation by day of year (averaged across all available years). Color each site.
```{r, message = FALSE}
# Create a date from given days
forcing_dates <- forcing_data_full %>% mutate(Date = make_date(year, month, day)) %>%
filter(!is.na(Date)) %>%
mutate(day_val = yday(Date))
daily_values <- forcing_dates %>% group_by(day_val, site) %>%
summarize(mean_precip = mean(`precip [kg m-2 s-1]`, na.rm = TRUE))
p2 <- ggplot(daily_values, aes(day_val, mean_precip, color = site)) +
geom_line() + labs(title = "Average Daily Precipitation by Day of Year", x = "Day", y = "Precip [kg m-2 s-1]")
print(p2)
```
Bonus #2: Use a function and for loop to create yearly plots of precipitation by day of year. Color each site.
```{r, message = FALSE}
daily_precip <- function(precip_data, year_value){
precip_dataf <- precip_data %>% filter(year == year_value)
day_values <- precip_dataf %>% group_by(day_val, site) %>%
summarize(mean_year_precip = mean(`precip [kg m-2 s-1]`, na.rm = TRUE))
plot_title <- paste("Mean Precipitation [kg m-2 s-1] By Date in", year_value)
p3 <- ggplot(day_values, aes(day_val, mean_year_precip, color = site)) +
geom_line() + labs(title = plot_title, x = "Date", y = "Mean Temp [Kelvin]")
print(p3)
}
year_input <- c(2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011)
for (i in 1:length(year_input)){
daily_precip_plots <- daily_precip(forcing_dates, year_input[i])
}
```
<script>
<div class="tocify-extend-page" data-unique="tocify-extend-page" style="height: 0;"></div>
</script>