Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. # Imports
  2. library(dplyr)
  3. library(tidyhydat)
  4. library(purrr)
  5.  
  6. calc_thresh <- function(wsc_station_number){
  7.  
  8. # Add tick to progress bar
  9. pb$tick()$print()
  10.  
  11. # Grab data for station (all parameters)
  12. station_data <- hy_daily(
  13. station_number = wsc_station_number
  14. ) %>%
  15. # Group by parameter type for stat calculations
  16. group_by(Parameter) %>%
  17. # Get rid of missing values
  18. filter(!is.na(Value)) %>%
  19. summarise(
  20. "station_number" = wsc_station_number,
  21. # Number of values considered
  22. "n" = n(),
  23. # Percentiles
  24. "25%" = quantile(Value, 0.25),
  25. "50%" = quantile(Value, 0.5),
  26. "75%" = quantile(Value, 0.75),
  27. "90%" = quantile(Value, 0.9),
  28. "100%" = quantile(Value, 1)
  29. ) %>%
  30. mutate_if(is.double, round, 3) %>%
  31. ungroup()
  32.  
  33. return(station_data)
  34. }
  35.  
  36. # All active stations
  37. active_ont_stns <- hy_stations(
  38. prov_terr_state_loc = "ON"
  39. ) %>%
  40. filter(
  41. HYD_STATUS == "ACTIVE"
  42. )
  43.  
  44. # Set progress bar
  45. pb <- progress_estimated(length(active_ont_stns$STATION_NUMBER))
  46.  
  47. # Grab thresholds for all stations
  48. all_stn_thresh <- purrr::map(
  49. active_ont_stns$STATION_NUMBER,
  50. calc_thresh
  51. ) %>%
  52. bind_rows() %>%
  53. # Only flow/level stats
  54. filter(
  55. Parameter %in% c("Flow", "Level")
  56. ) %>%
  57. # Rearrange columns
  58. select(
  59. station_number,
  60. everything()
  61. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement