Skip to content

Commit e738afe

Browse files
authored
Merge pull request #15 from bcgov/devel
0.2.3
2 parents 8482b44 + 813818d commit e738afe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1720
-442
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: tidyhydat
22
Title: Extract tidy hydrometric data
3-
Version: 0.2.2
3+
Version: 0.2.3
44
Authors@R: c(person("Sam", "Albers", email = "sam.albers@gov.bc.ca", role = c("aut", "cre"),
55
), person("Dave", "Hutchinson", email = "david.hutchinson@canada.ca", role = "ctb"))
66
Description: tidyhydat provides functions to extract river data from Water Survey of Canada sources and make it tidy.

NAMESPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ export(DATUM_LIST)
77
export(DLY_FLOWS)
88
export(DLY_LEVELS)
99
export(REGIONAL_OFFICE_LIST)
10+
export(SED_DLY_LOADS)
1011
export(SED_DLY_SUSCON)
12+
export(SED_SAMPLES)
13+
export(SED_SAMPLES_PSD)
1114
export(STATIONS)
15+
export(STN_DATA_RANGE)
16+
export(STN_DATUM_CONVERSION)
17+
export(STN_OPERATION_SCHEDULE)
1218
export(STN_REGULATION)
19+
export(STN_REMARKS)
20+
export(VERSION)
1321
export(download_realtime_dd)
1422
export(download_realtime_ws)
1523
export(get_ws_token)

NEWS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
#tidyhydat 0.2.3
2+
* Remove significant redundancy in station selecting mechanism
3+
* Added package startup message when HYDAT is out of date
4+
* Add internal allstations data
5+
* Added all the tables as functions or data from HYDAT
6+
* Made missing station ouput truncated at 10 missign stations
7+
8+
# tidyhdyat 0.2.2
9+
* Adding several new tables
10+
* removed need for both prov and stn args
11+
* reduced some repetition in code
12+
113
# tidyhydat 0.2.1
214
* added STN_REGULATION
315
* tidied ANNUAL_STATISTICS

R/ANNUAL_INSTANT_PEAKS.R

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,51 +35,21 @@
3535
#'
3636
ANNUAL_INSTANT_PEAKS <- function(hydat_path, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC = NULL,
3737
start_year = "ALL", end_year = "ALL") {
38-
39-
if(missing(hydat_path))
40-
stop("No Hydat.sqlite3 set. Download the hydat database from here: http://collaboration.cmc.ec.gc.ca/cmc/hydrometrics/www/")
41-
42-
## Read in database
43-
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), hydat_path)
44-
45-
## Only possible values for PROV_TERR_STATE_LOC
46-
stn_option = dplyr::tbl(hydat_con, "STATIONS") %>%
47-
dplyr::distinct(PROV_TERR_STATE_LOC) %>%
48-
dplyr::pull(PROV_TERR_STATE_LOC)
49-
50-
## If not STATION_NUMBER arg is supplied then this controls how to handle the PROV arg
51-
if((is.null(STATION_NUMBER) & !is.null(PROV_TERR_STATE_LOC))){
52-
STATION_NUMBER = "ALL" ## All stations
53-
prov = PROV_TERR_STATE_LOC ## Prov info
54-
55-
if(any(!prov %in% stn_option) == TRUE){
56-
stop("Invalid PROV_TERR_STATE_LOC value")
57-
DBI::dbDisconnect(hydat_con)
38+
if(is.null(hydat_path)){
39+
hydat_path = Sys.getenv("hydat")
40+
if(is.na(hydat_path)){
41+
stop("No Hydat.sqlite3 path set either in this function or in your .Renviron file. See tidyhydat for more documentation.")
5842
}
5943
}
6044

61-
## If PROV arg is supplied then simply use the STATION_NUMBER independent of PROV
62-
if(is.null(PROV_TERR_STATE_LOC)){
63-
STATION_NUMBER = STATION_NUMBER
64-
}
65-
66-
67-
## Steps to create the station vector
68-
stns = STATION_NUMBER
6945

70-
## Get all stations
71-
if(is.null(stns) == TRUE && is.null(PROV_TERR_STATE_LOC) == TRUE){
72-
stns = dplyr::tbl(hydat_con, "STATIONS") %>%
73-
dplyr::collect() %>%
74-
dplyr::pull(STATION_NUMBER)
75-
}
46+
## Read in database
47+
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), hydat_path)
7648

77-
if(stns[1] == "ALL"){
78-
stns = dplyr::tbl(hydat_con, "STATIONS") %>%
79-
dplyr::filter(PROV_TERR_STATE_LOC %in% prov) %>%
80-
dplyr::pull(STATION_NUMBER)
81-
}
49+
## Determine which stations we are querying
50+
stns = station_choice(hydat_con, STATION_NUMBER, PROV_TERR_STATE_LOC)
8251

52+
## Data manipulations
8353
aip = dplyr::tbl(hydat_con, "ANNUAL_INSTANT_PEAKS") %>%
8454
dplyr::filter(STATION_NUMBER %in% stns) %>%
8555
dplyr::collect()
@@ -110,6 +80,19 @@ ANNUAL_INSTANT_PEAKS <- function(hydat_path, STATION_NUMBER = NULL, PROV_TERR_ST
11080

11181
DBI::dbDisconnect(hydat_con)
11282

83+
## What stations were missed?
84+
differ = setdiff(unique(stns), unique(aip$STATION_NUMBER))
85+
if( length(differ) !=0 ){
86+
if( length(differ) <= 10) {
87+
message("The following station(s) were not retrieved: ", paste0(differ, sep = " "))
88+
message("Check station number typos or if it is a valid station in the network") }
89+
else {
90+
message("More than 10 stations from the initial query were not returned. Ensure realtime and active status are correctly specified.")
91+
}
92+
} else{
93+
message("All station successfully retrieved")
94+
}
95+
11396
return(aip)
11497

11598
}

R/ANNUAL_STATISTICS.R

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,55 +34,24 @@
3434
#'
3535
#' @export
3636

37-
ANNUAL_STATISTICS <- function(hydat_path, STATION_NUMBER =NULL, PROV_TERR_STATE_LOC=NULL,
37+
ANNUAL_STATISTICS <- function(hydat_path=NULL, STATION_NUMBER =NULL, PROV_TERR_STATE_LOC=NULL,
3838
start_year = "ALL", end_year = "ALL") {
3939

40-
if(missing(hydat_path))
41-
stop("No Hydat.sqlite3 set. Download the hydat database from here: http://collaboration.cmc.ec.gc.ca/cmc/hydrometrics/www/")
42-
43-
## Read in database
44-
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), hydat_path)
45-
46-
## Only possible values for PROV_TERR_STATE_LOC
47-
stn_option = dplyr::tbl(hydat_con, "STATIONS") %>%
48-
dplyr::distinct(PROV_TERR_STATE_LOC) %>%
49-
dplyr::pull(PROV_TERR_STATE_LOC)
50-
51-
## If not STATION_NUMBER arg is supplied then this controls how to handle the PROV arg
52-
if((is.null(STATION_NUMBER) & !is.null(PROV_TERR_STATE_LOC))){
53-
STATION_NUMBER = "ALL" ## All stations
54-
prov = PROV_TERR_STATE_LOC ## Prov info
55-
56-
if(any(!prov %in% stn_option) == TRUE){
57-
stop("Invalid PROV_TERR_STATE_LOC value")
58-
DBI::dbDisconnect(hydat_con)
40+
if(is.null(hydat_path)){
41+
hydat_path = Sys.getenv("hydat")
42+
if(is.na(hydat_path)){
43+
stop("No Hydat.sqlite3 path set either in this function or in your .Renviron file. See tidyhydat for more documentation.")
5944
}
6045
}
6146

62-
## If PROV arg is supplied then simply use the STATION_NUMBER independent of PROV
63-
if(is.null(PROV_TERR_STATE_LOC)){
64-
STATION_NUMBER = STATION_NUMBER
65-
}
66-
67-
68-
## Steps to create the station vector
69-
stns = STATION_NUMBER
70-
71-
## Get all stations
72-
if(is.null(stns) == TRUE && is.null(PROV_TERR_STATE_LOC) == TRUE){
73-
stns = dplyr::tbl(hydat_con, "STATIONS") %>%
74-
dplyr::collect() %>%
75-
dplyr::pull(STATION_NUMBER)
76-
}
7747

48+
## Read in database
49+
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), hydat_path)
7850

79-
if(stns[1] == "ALL"){
80-
stns = dplyr::tbl(hydat_con, "STATIONS") %>%
81-
filter(PROV_TERR_STATE_LOC %in% prov) %>%
82-
pull(STATION_NUMBER)
83-
}
51+
## Determine which stations we are querying
52+
stns = station_choice(hydat_con, STATION_NUMBER, PROV_TERR_STATE_LOC)
8453

85-
54+
## Data manipulations
8655
annual_statistics = dplyr::tbl(hydat_con, "ANNUAL_STATISTICS")
8756

8857
## If a yearis supplied...
@@ -133,8 +102,12 @@ ANNUAL_STATISTICS <- function(hydat_path, STATION_NUMBER =NULL, PROV_TERR_STATE_
133102
## What stations were missed?
134103
differ = setdiff(unique(stns), unique(annual_statistics$STATION_NUMBER))
135104
if( length(differ) !=0 ){
136-
message("The following station(s) were not retrieved: ", paste0(differ, sep = " "))
137-
message("Check station number typos or if it is a valid station in the network")
105+
if( length(differ) <= 10) {
106+
message("The following station(s) were not retrieved: ", paste0(differ, sep = " "))
107+
message("Check station number typos or if it is a valid station in the network") }
108+
else {
109+
message("More than 10 stations from the initial query were not returned. Ensure realtime and active status are correctly specified.")
110+
}
138111
} else{
139112
message("All station successfully retrieved")
140113
}

R/DLY_FLOWS.R

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636

3737

38-
DLY_FLOWS <- function(hydat_path, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC = NULL, start_date ="ALL", end_date = "ALL") {
38+
DLY_FLOWS <- function(hydat_path=NULL, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC = NULL, start_date ="ALL", end_date = "ALL") {
3939

4040
if(start_date == "ALL" & end_date == "ALL"){
4141
message("No start and end dates specified. All dates available will be returned.")
@@ -63,52 +63,22 @@ DLY_FLOWS <- function(hydat_path, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC = N
6363
}
6464
}
6565

66-
67-
if(missing(hydat_path))
68-
stop("No Hydat.sqlite3 set. Download the hydat database from here: http://collaboration.cmc.ec.gc.ca/cmc/hydrometrics/www/")
69-
70-
## Read in database
71-
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), hydat_path)
72-
73-
## Only possible values for PROV_TERR_STATE_LOC
74-
stn_option = dplyr::tbl(hydat_con, "STATIONS") %>%
75-
dplyr::distinct(PROV_TERR_STATE_LOC) %>%
76-
dplyr::pull(PROV_TERR_STATE_LOC)
77-
78-
## If not STATION_NUMBER arg is supplied then this controls how to handle the PROV arg
79-
if((is.null(STATION_NUMBER) & !is.null(PROV_TERR_STATE_LOC))){
80-
STATION_NUMBER = "ALL" ## All stations
81-
prov = PROV_TERR_STATE_LOC ## Prov info
82-
83-
if(any(!prov %in% stn_option) == TRUE){
84-
stop("Invalid PROV_TERR_STATE_LOC value")
85-
DBI::dbDisconnect(hydat_con)
66+
if(is.null(hydat_path)){
67+
hydat_path = Sys.getenv("hydat")
68+
if(is.na(hydat_path)){
69+
stop("No Hydat.sqlite3 path set either in this function or in your .Renviron file. See tidyhydat for more documentation.")
8670
}
8771
}
8872

89-
## If PROV arg is supplied then simply use the STATION_NUMBER independent of PROV
90-
if(is.null(PROV_TERR_STATE_LOC)){
91-
STATION_NUMBER = STATION_NUMBER
92-
}
93-
9473

95-
## Steps to create the station vector
96-
stns = STATION_NUMBER
97-
98-
## Get all stations
99-
if(is.null(stns) == TRUE && is.null(PROV_TERR_STATE_LOC) == TRUE){
100-
stns = dplyr::tbl(hydat_con, "STATIONS") %>%
101-
dplyr::collect() %>%
102-
dplyr::pull(STATION_NUMBER)
103-
}
104-
105-
if(stns[1] == "ALL"){
106-
stns = dplyr::tbl(hydat_con, "STATIONS") %>%
107-
dplyr::filter(PROV_TERR_STATE_LOC %in% prov) %>%
108-
dplyr::pull(STATION_NUMBER)
109-
}
74+
## Read in database
75+
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), hydat_path)
11076

77+
## Determine which stations we are querying
78+
stns = station_choice(hydat_con, STATION_NUMBER, PROV_TERR_STATE_LOC)
79+
11180

81+
## Data manipulations to make it "tidy"
11282
dly_flows = dplyr::tbl(hydat_con, "DLY_FLOWS")
11383
dly_flows = dplyr::filter(dly_flows, STATION_NUMBER %in% stns)
11484

@@ -148,8 +118,12 @@ DLY_FLOWS <- function(hydat_path, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC = N
148118
## What stations were missed?
149119
differ = setdiff(unique(stns), unique(dly_flows$STATION_NUMBER))
150120
if( length(differ) !=0 ){
151-
message("The following station(s) were not retrieved: ", paste0(differ, sep = " "))
152-
message("Check station number typos or if it is a valid station in the network")
121+
if( length(differ) <= 10) {
122+
message("The following station(s) were not retrieved: ", paste0(differ, sep = " "))
123+
message("Check station number typos or if it is a valid station in the network") }
124+
else {
125+
message("More than 10 stations from the initial query were not returned. Ensure realtime and active status are correctly specified.")
126+
}
153127
} else{
154128
message("All station successfully retrieved")
155129
}

R/DLY_LEVELS.R

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434

3535

36-
DLY_LEVELS <- function(hydat_path, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC = NULL, start_date ="ALL", end_date = "ALL") {
36+
DLY_LEVELS <- function(hydat_path=NULL, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC = NULL, start_date ="ALL", end_date = "ALL") {
3737

3838
if(start_date == "ALL" & end_date == "ALL"){
3939
message("No start and end dates specified. All dates available will be returned.")
@@ -61,57 +61,28 @@ DLY_LEVELS <- function(hydat_path, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC =
6161
}
6262
}
6363

64-
if(missing(hydat_path))
65-
stop("No Hydat.sqlite3 set. Download the hydat database from here: http://collaboration.cmc.ec.gc.ca/cmc/hydrometrics/www/")
66-
67-
## Read in database
68-
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), hydat_path)
69-
70-
## Only possible values for PROV_TERR_STATE_LOC
71-
stn_option = dplyr::tbl(hydat_con, "STATIONS") %>%
72-
dplyr::distinct(PROV_TERR_STATE_LOC) %>%
73-
dplyr::pull(PROV_TERR_STATE_LOC)
74-
75-
## If not STATION_NUMBER arg is supplied then this controls how to handle the PROV arg
76-
if((is.null(STATION_NUMBER) & !is.null(PROV_TERR_STATE_LOC))){
77-
STATION_NUMBER = "ALL" ## All stations
78-
prov = PROV_TERR_STATE_LOC ## Prov info
79-
80-
if(any(!prov %in% stn_option) == TRUE){
81-
stop("Invalid PROV_TERR_STATE_LOC value")
82-
DBI::dbDisconnect(hydat_con)
64+
if(is.null(hydat_path)){
65+
hydat_path = Sys.getenv("hydat")
66+
if(is.na(hydat_path)){
67+
stop("No Hydat.sqlite3 path set either in this function or in your .Renviron file. See tidyhydat for more documentation.")
8368
}
8469
}
8570

86-
## If PROV arg is supplied then simply use the STATION_NUMBER independent of PROV
87-
if(is.null(PROV_TERR_STATE_LOC)){
88-
STATION_NUMBER = STATION_NUMBER
89-
}
90-
91-
92-
## Steps to create the station vector
93-
stns = STATION_NUMBER
9471

95-
## Get all stations
96-
if(is.null(stns) == TRUE && is.null(PROV_TERR_STATE_LOC) == TRUE){
97-
stns = dplyr::tbl(hydat_con, "STATIONS") %>%
98-
dplyr::collect() %>%
99-
dplyr::pull(STATION_NUMBER)
100-
}
72+
## Read in database
73+
hydat_con <- DBI::dbConnect(RSQLite::SQLite(), hydat_path)
10174

102-
if(stns[1] == "ALL"){
103-
stns = dplyr::tbl(hydat_con, "STATIONS") %>%
104-
dplyr::filter(PROV_TERR_STATE_LOC %in% prov) %>%
105-
dplyr::pull(STATION_NUMBER)
106-
}
75+
## Determine which stations we are querying
76+
stns = station_choice(hydat_con, STATION_NUMBER, PROV_TERR_STATE_LOC)
10777

78+
## Data manipulations
10879
dly_levels = dplyr::tbl(hydat_con, "DLY_LEVELS")
10980
dly_levels = dplyr::filter(dly_levels, STATION_NUMBER %in% stns)
11081

11182
## Do the initial subset to take advantage of dbplyr only issuing sql query when it has too
11283
if (start_date != "ALL" | end_date != "ALL") {
11384
dly_levels = dplyr::filter(dly_levels, YEAR >= start_year &
114-
YEAR <= end_year)
85+
YEAR <= end_year)
11586
}
11687

11788
dly_levels = dplyr::select(dly_levels, STATION_NUMBER, YEAR, MONTH, NO_DAYS, dplyr::contains("LEVEL"))
@@ -144,8 +115,12 @@ DLY_LEVELS <- function(hydat_path, STATION_NUMBER = NULL, PROV_TERR_STATE_LOC =
144115
## What stations were missed?
145116
differ = setdiff(unique(stns), unique(dly_levels$STATION_NUMBER))
146117
if( length(differ) !=0 ){
147-
message("The following station(s) were not retrieved: ", paste0(differ, sep = " "))
148-
message("Check station number typos or if it is a valid station in the network")
118+
if( length(differ) <= 10) {
119+
message("The following station(s) were not retrieved: ", paste0(differ, sep = " "))
120+
message("Check station number typos or if it is a valid station in the network") }
121+
else {
122+
message("More than 10 stations from the initial query were not returned. Ensure realtime and active status are correctly specified.")
123+
}
149124
} else{
150125
message("All station successfully retrieved")
151126
}

0 commit comments

Comments
 (0)