Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. # The recent WV flooding using opendata
  2. ## Comparing Conditions before and after the storm
  3. ## You need to have dplyr v0.5.0
  4.  
  5. library(dataRetrieval)
  6. library(dplyr)
  7. library(maps)
  8.  
  9. start_date <- "2016-06-22"
  10. end_date <- "2016-06-25"
  11.  
  12. # Read in streamflow (Q) data using dataRetrieval
  13. pcode <- '00060'
  14. possiblesites <- whatNWISsites(startDt = start_date, endDt = end_date, parameterCd = pcode,
  15. siteType="ST", stateCd = "WV", drainAreaMin=200)
  16. iv <- readNWISdata(sites=possiblesites$site_no, startDate = start_date, endDate = end_date,
  17. parameterCd = pcode, service = "iv")
  18. siteInfo <- attr(iv, 'siteInfo')
  19.  
  20. # Calculate the percent change in flow from the day before the storm and the last day of the storm
  21. percentChange <- function(values){ ((values[2] - values[1])/values[1])*100 }
  22. col_df <- data.frame(threshold = c(0,100,200,500,Inf),
  23. col = c("red", "lightblue", "steelblue1", "royalblue", "mediumblue"),
  24. stringsAsFactors = FALSE)
  25. legend_labels <- c(paste('<', head(col_df$threshold, -1)), paste(">", tail(col_df$threshold, 2)[1]))
  26.  
  27. iv_change <- iv %>% renameNWISColumns() %>%
  28. group_by(site_no) %>%
  29. filter(dateTime == min(dateTime) | dateTime == max(dateTime)) %>%
  30. summarize(changeQ = percentChange(Flow_Inst)) %>%
  31. left_join(siteInfo, by = 'site_no') %>%
  32. select(station_nm, site_no, changeQ, latitude = dec_lat_va, longitude = dec_lon_va) %>%
  33. mutate(siteColor = case_when(
  34. .$changeQ < col_df$threshold[1] ~ col_df$col[1],
  35. .$changeQ <= col_df$threshold[2] ~ col_df$col[2],
  36. .$changeQ <= col_df$threshold[3] ~ col_df$col[3],
  37. .$changeQ <= col_df$threshold[4] ~ col_df$col[4],
  38. TRUE ~ col_df$col[5]
  39. ))
  40.  
  41. # Map the results
  42. par(mfrow=c(1,2), mai=c(0,0,0,0), oma = c(0,0,2,0))
  43.  
  44. map(database = 'county', regions = 'west virginia')
  45. map(database = 'state', regions = 'west virginia', lwd=2, add=TRUE)
  46. points(x = iv_change$longitude, y = iv_change$latitude, pch=20, col=iv_change$siteColor)
  47.  
  48. plot.new()
  49. legend("center", legend = legend_labels, col = col_df$col,
  50. title = "% Change in Streamflow", pch = 20, bty='n')
  51.  
  52. title("Change in streamflow for West Virginia", outer=TRUE)
  53. 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