Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The recent WV flooding using opendata
- ## Comparing Conditions before and after the storm
- ## You need to have dplyr v0.5.0
- library(dataRetrieval)
- library(dplyr)
- library(maps)
- start_date <- "2016-06-22"
- end_date <- "2016-06-25"
- # Read in streamflow (Q) data using dataRetrieval
- pcode <- '00060'
- possiblesites <- whatNWISsites(startDt = start_date, endDt = end_date, parameterCd = pcode,
- siteType="ST", stateCd = "WV", drainAreaMin=200)
- iv <- readNWISdata(sites=possiblesites$site_no, startDate = start_date, endDate = end_date,
- parameterCd = pcode, service = "iv")
- siteInfo <- attr(iv, 'siteInfo')
- # Calculate the percent change in flow from the day before the storm and the last day of the storm
- percentChange <- function(values){ ((values[2] - values[1])/values[1])*100 }
- col_df <- data.frame(threshold = c(0,100,200,500,Inf),
- col = c("red", "lightblue", "steelblue1", "royalblue", "mediumblue"),
- stringsAsFactors = FALSE)
- legend_labels <- c(paste('<', head(col_df$threshold, -1)), paste(">", tail(col_df$threshold, 2)[1]))
- iv_change <- iv %>% renameNWISColumns() %>%
- group_by(site_no) %>%
- filter(dateTime == min(dateTime) | dateTime == max(dateTime)) %>%
- summarize(changeQ = percentChange(Flow_Inst)) %>%
- left_join(siteInfo, by = 'site_no') %>%
- select(station_nm, site_no, changeQ, latitude = dec_lat_va, longitude = dec_lon_va) %>%
- mutate(siteColor = case_when(
- .$changeQ < col_df$threshold[1] ~ col_df$col[1],
- .$changeQ <= col_df$threshold[2] ~ col_df$col[2],
- .$changeQ <= col_df$threshold[3] ~ col_df$col[3],
- .$changeQ <= col_df$threshold[4] ~ col_df$col[4],
- TRUE ~ col_df$col[5]
- ))
- # Map the results
- par(mfrow=c(1,2), mai=c(0,0,0,0), oma = c(0,0,2,0))
- map(database = 'county', regions = 'west virginia')
- map(database = 'state', regions = 'west virginia', lwd=2, add=TRUE)
- points(x = iv_change$longitude, y = iv_change$latitude, pch=20, col=iv_change$siteColor)
- plot.new()
- legend("center", legend = legend_labels, col = col_df$col,
- title = "% Change in Streamflow", pch = 20, bty='n')
- title("Change in streamflow for West Virginia", outer=TRUE)
- mtext(side=3, line=-1, outer=TRUE, font=3, text = paste("from", start_date, "to", end_date))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement