generated from IMMM-SFA/metarepo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilities.R
421 lines (337 loc) · 14.2 KB
/
utilities.R
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#===========================================================
# Name: utilities.R
# Author: D. Broman, PNNL
# Last Modified: 2024-04-24
# Description: [B1-data] utilities
#===========================================================
require(tidyverse)
require(cder)
require(dataRetrieval)
#' get_usgs
#'
#' @description downloads data from USGS
#' @param site_no string USGS site number
#' @param date_start string (or date) start date in 'YYYY-MM-DD' format
#' @param date_end string (or date) end date in 'YYYY-MM-DD' format
#' @importFrom tidyverse dataRetrieval
#' @return dat_fmt tibble with columns date (date in 'YYYY-MM-DD') and value (float)
#' @export none
#'
get_usgs = function(site_no, date_start, date_end){
# TODO error handling
# TODO par_cd and stat_cd hard-coded
# https://help.waterdata.usgs.gov/codes-and-parameters/parameters
par_cd = '00060' # daily discharge in cfs
stat_cd = '00003' # daily mean
#- format dates
date_start = as.Date(date_start, format = '%Y-%m-%d')
date_end = as.Date(date_end, format = '%Y-%m-%d')
dat_raw = readNWISdv(siteNumbers = site_no,
parameterCd = par_cd,
startDate = date_start,
endDate = date_end,
statCd = stat_cd)
# find column with data - NOT USED
# grepl(paste0(par_cd, '_', stat_cd), names(dat_raw))
dat_fmt = tibble(date = seq(from = date_start, to = date_end, by = 'day'))
dat_proc = dat_raw %>%
dplyr::rename(value = X_00060_00003) %>%
mutate(date = as.Date(Date)) %>%
dplyr::select(date, value)
dat_fmt = dat_fmt %>%
left_join(dat_proc, by = 'date')
return(dat_fmt)
}
#' get_pnh
#'
#' @description downloads data from Reclamation Columbia-Pacific Northwest Region Hydromet
#' @param sta_code string hydromet station id
#' @param par_code string hydromet parameter code
#' @param date_start string (or date) start date in 'YYYY-MM-DD' format
#' @param date_end string (or date) end date in 'YYYY-MM-DD' format
#' @importFrom tidyverse
#' @return
#' @export none
get_pnh = function(sta_code, par_code, date_start, date_end){
#- daily data
url_head = 'https://www.usbr.gov/pn-bin/daily.pl?'
#- format dates
date_start = as.Date(date_start, format = '%Y-%m-%d')
date_end = as.Date(date_end, format = '%Y-%m-%d')
year_start = year(date_start)
month_start = month(date_start)
day_start = day(date_start)
year_end = year(date_end)
month_end = month(date_end)
day_end = day(date_end)
url = paste0(url_head,
'station=', sta_code,
'&pcode=', par_code,
'&year=', year_start,
'&month=', month_start,
'&day=', day_start,
'&year=', year_end,
'&month=', month_end,
'&day=', day_end,
'&format=csv')
# TODO error handling
dat_proc = read_csv(url, show_col_types = FALSE)
# TODO check timestep and adjust as needed; check units and convert as needed
#- template tibble with complete timesteps
# TODO modify to alter desired timestep; hard-coded to daily
dat_fmt = tibble(date = seq(from = date_start, to = date_end, by = 'day'))
dat_proc = dat_proc %>%
setNames(c('date', 'value'))
dat_fmt = dat_fmt %>%
left_join(dat_proc, by = 'date')
return(dat_fmt)
}
#' get_mbh
#'
#' @description downloads data from Reclamation Missouri Basin Region Hydromet
#' @param sta_code string hydromet station id
#' @param par_code string hydromet parameter code
#' @param date_start string (or date) start date in 'YYYY-MM-DD' format
#' @param date_end string (or date) end date in 'YYYY-MM-DD' format
#' @importFrom tidyverse
#' @return dat_fmt tibble with columns date (date in 'YYYY-MM-DD') and value (float)
#' @export none
get_mbh = function(sta_code, par_code, date_start, date_end){
# https://www.usbr.gov/gp/hydromet/automated_retrieval.pdf
#- daily data
url_head = 'https://www.usbr.gov/gp-bin/webarccsv.pl?parameter='
#- instantaneous data
# url_head = 'http://www.usbr.gov/gp-bin/webdaycsv.pl?parameter='
#- format dates
date_start = as.Date(date_start, format = '%Y-%m-%d')
date_end = as.Date(date_end, format = '%Y-%m-%d')
year_start = year(date_start)
month_start = month(date_start)
day_start = day(date_start)
year_end = year(date_end)
month_end = month(date_end)
day_end = day(date_end)
url = paste0(url_head, sta_code, '%20', par_code, '&syer=', year_start,
'&smnth=', month_start,
'&sdy=', day_start,
'&eyer=', year_end,
'&emnth=', month_end,
'&edy=', day_end,
'&format=2')
# TODO error handling
# download.file(url, 'temp.txt')
# dat_raw = read_lines('temp.txt')
dat_raw = read_lines(url)
hdr_line_max = which(grepl('BEGIN DATA', dat_raw))
dat_line_max = which(grepl('END DATA', dat_raw))
dat_proc = read_csv(url, skip = hdr_line_max, n_max = dat_line_max - hdr_line_max - 2, show_col_types = FALSE)
# TODO check timestep and adjust as needed; check units and convert as needed
#- template tibble with complete timesteps
# TODO modify to alter desired timestep; hard-coded to daily
dat_fmt = tibble(date = seq(from = date_start, to = date_end, by = 'day'))
dat_proc = dat_proc %>%
setNames(c('date', 'value')) %>%
mutate(date = as.Date(date, format = '%m/%d/%Y'),
value = as.numeric(value))
dat_fmt = dat_fmt %>%
left_join(dat_proc, by = 'date')
return(dat_fmt)
}
#' get_rise
#'
#' @description downloads data from Reclamation Information Sharing Environment (RISE)
#' @param item_id integer RISE catalog id
#' @param date_start string (or date) start date in 'YYYY-MM-DD' format
#' @param date_end string (or date) end date in 'YYYY-MM-DD' format
#' @importFrom tidyverse
#' @return dat_fmt tibble with columns date (date in 'YYYY-MM-DD') and value (float)
#' @export none
get_rise = function(item_id, date_start, date_end){
# type (format) currenly hard-coded to 'csv'
url_head = 'https://data.usbr.gov/rise/api/result/download?type=csv&itemId='
#- format dates
date_start = as.Date(date_start, format = '%Y-%m-%d')
date_end = as.Date(date_end, format = '%Y-%m-%d')
url = paste0(url_head, item_id, '&after=', date_start, '&before=', date_end)
# &order=ASC
# TODO error handling
dat_raw = read_lines(url)
hdr_line_max = which(grepl('"#SERIES DATA#', dat_raw))
dat_proc = read_csv(url, skip = hdr_line_max, show_col_types = FALSE)
# TODO check timestep and adjust as needed; check units and convert as needed
#- template tibble with complete timesteps
# TODO modify to alter desired timestep; hard-coded to daily
dat_fmt = tibble(date = seq(from = date_start, to = date_end, by = 'day'))
dat_proc = dat_proc %>%
dplyr::rename(value = Result) %>%
mutate(date = as.Date(`Datetime (UTC)`)) %>%
dplyr::select(date, value)
dat_fmt = dat_fmt %>%
left_join(dat_proc, by = 'date')
return(dat_fmt)
}
#' get_cdec
#'
#' @description downloads data from California Data Exchange (CDEC)
#' @param sta_code character CDEC station id
#' @param sens_code integer CDEC sensor code
#' @param dur_code character CDEC duration code
#' @param date_start string (or date) start date in 'YYYY-MM-DD' format
#' @param date_end string (or date) end date in 'YYYY-MM-DD' format
#' @importFrom tidyverse cder
#' @return dat_fmt tibble with columns date (date in 'YYYY-MM-DD') and value (float)
#' @export none
get_cdec = function(sta_code, sens_code, dur_code = 'D', date_start, date_end){
#- format dates
date_start = as.Date(date_start, format = '%Y-%m-%d')
date_end = as.Date(date_end, format = '%Y-%m-%d')
dat_raw = cdec_query(sta_code, sens_code, dur_code, date_start, date_end)
dat_fmt = tibble(date = seq(from = date_start, to = date_end, by = 'day'))
dat_proc = dat_raw %>%
dplyr::rename(value = Value) %>%
mutate(date = as.Date(DateTime)) %>%
dplyr::select(date, value)
if(dur_code == 'E'){
dat_proc = dat_proc %>%
group_by(date) %>%
dplyr::summarise(value = mean(value, na.rm = T))
}
dat_fmt = dat_fmt %>%
left_join(dat_proc, by = 'date')
return(dat_fmt)
}
#' get_cdss
#'
#' @description downloads data from Colorado's Decision Support Systems (CDSS)
#' @param sta_abb character CDSS station abbreviation
#' @param date_start string (or date) start date in 'YYYY-MM-DD' format
#' @param date_end string (or date) end date in 'YYYY-MM-DD' format
#' @importFrom tidyverse
#' @return dat_fmt tibble with columns date (date in 'YYYY-MM-DD') and value (float)
#' @export none
get_cdss = function(sta_abb, date_start, date_end){
url_head = 'https://dwr.state.co.us/Rest/GET/api/v2/surfacewater/surfacewatertsday/?format=csvforced'
#- format dates
date_start = as.Date(date_start, format = '%Y-%m-%d')
date_end = as.Date(date_end, format = '%Y-%m-%d')
year_start = year(date_start)
month_start = month(date_start)
day_start = day(date_start)
year_end = year(date_end)
month_end = month(date_end)
day_end = day(date_end)
url = paste0(url_head,
'&dateFormat=dateOnly',
'&fields=stationNum%2Cabbrev%2CmeasType%2CmeasDate%2Cvalue%2CflagA%2CflagC%2CflagD%2CdataSource%2Cmodified%2CmeasUnit',
'&encoding=deflate',
'&abbrev=', sta_abb,
'&min-measDate=', month_start, '%2F', day_start, '%2F', year_start,
'&max-measDate=', month_end, '%2F', day_end, '%2F', year_end)
dat_raw = read_lines(url)
hdr_line_max = which(grepl('abbrev', dat_raw))
dat_proc = read_csv(url, skip = hdr_line_max - 1, show_col_types = FALSE)
# TODO check timestep and adjust as needed; check units and convert as needed
#- template tibble with complete timesteps
# TODO modify to alter desired timestep; hard-coded to daily
dat_fmt = tibble(date = seq(from = date_start, to = date_end, by = 'day'))
dat_proc = dat_proc %>%
dplyr::rename(date = measDate) %>%
dplyr::select(date, value)
dat_fmt = dat_fmt %>%
left_join(dat_proc, by = 'date')
return(dat_fmt)
}
#' get_nwd
#' @description downloads data from NWD Dataquery
#' @param item_id character NWD item string
#' @param units character [cfs, kcfs]
#' @param dur_code character [I (sub-daily), D (daily)]
#' @param date_start string (or date) start date in 'YYYY-MM-DD' format
#' @param date_end string (or date) end date in 'YYYY-MM-DD' format
#' @importFrom tidyverse
#' @return dat_fmt tibble with columns date (date in 'YYYY-MM-DD') and value (float)
#' @export none
get_nwd = function(item_id, units, dur_code, date_start, date_end){
url_head = 'https://www.nwd-wc.usace.army.mil/dd/common/web_service/webexec/ecsv?'
#- format dates
date_start = as.Date(date_start, format = '%Y-%m-%d')
date_end = as.Date(date_end, format = '%Y-%m-%d')
year_start = year(date_start)
month_start = month(date_start)
day_start = day(date_start)
year_end = year(date_end)
month_end = month(date_end)
day_end = day(date_end)
# - template tibble with complete timesteps
# TODO modify to alter desired timestep; hard-coded to daily
dat_fmt = tibble(date = seq(from = date_start, to = date_end, by = 'day'))
#- calculate number of years requested
yr_ct = round(as.numeric(difftime(dat_fmt$date[nrow(dat_fmt)], dat_fmt$date[1], units = 'days')) / 365)
if(dur_code == 'I' & yr_ct > 10){
date_end_seq = unique(c(seq(from = date_start + years(10) - days(1), to = date_end, by = '10 years'), date_end))
date_start_seq = c(date_start, date_end_seq[1:(length(date_end_seq) - 1)] + days(1))
dat_raw = tibble()
for(b in 1:length(date_end_seq)){
date_start_sel = date_start_seq[b]
date_end_sel = date_end_seq[b]
year_start = year(date_start_sel)
month_start = month(date_start_sel)
day_start = day(date_start_sel)
year_end = year(date_end_sel)
month_end = month(date_end_sel)
day_end = day(date_end_sel)
url = paste0(url_head,
'id=', item_id, '%3Aunits%3D', units,
'&headers=true',
'&timezone=PST',
'&startdate=', month_start, '%2F', day_start, '%2F', year_start, '+06%3A00',
'&enddate=', month_end, '%2F', day_end, '%2F', year_end, '+06%3A00')
# TODO error handling
dat_raw_temp = read_csv(url, show_col_types = FALSE)
dat_raw = bind_rows(dat_raw, dat_raw_temp)
} #- end data retrieval loop
dat_proc = dat_raw %>%
setNames(c('date_time', 'value_raw')) %>%
mutate(date = as.Date(date_time, format = '%d-%b-%Y %H:%M')) %>%
group_by(date) %>%
dplyr::summarise(value_agg = mean(value_raw, na.rm = T)) %>%
group_by(date) %>%
mutate(value = ifelse(units == 'kcfs', value_agg * 1000, value_agg)) %>%
dplyr::select(date, value)
} else if(dur_code == 'I' & yr_ct <= 10){
url = paste0(url_head,
'id=', item_id, '%3Aunits%3D', units,
'&headers=true',
'&timezone=PST',
'&startdate=', month_start, '%2F', day_start, '%2F', year_start, '+06%3A00',
'&enddate=', month_end, '%2F', day_end, '%2F', year_end, '+06%3A00')
# TODO error handling
dat_raw = read_csv(url, show_col_types = FALSE)
dat_proc = dat_raw %>%
setNames(c('date_time', 'value_raw')) %>%
mutate(date = as.Date(date_time, format = '%d-%b-%Y %H:%M')) %>%
group_by(date) %>%
dplyr::summarise(value_agg = mean(value_raw, na.rm = T)) %>%
group_by(date) %>%
mutate(value = ifelse(units == 'kcfs', value_agg * 1000, value_agg)) %>%
dplyr::select(date, value)
} else if(dur_code == 'D'){
url = paste0(url_head,
'id=', item_id, '%3Aunits%3D', units,
'&headers=true',
'&timezone=PST',
'&startdate=', month_start, '%2F', day_start, '%2F', year_start, '+06%3A00',
'&enddate=', month_end, '%2F', day_end, '%2F', year_end, '+06%3A00')
# TODO error handling
dat_raw = read_csv(url, show_col_types = FALSE)
dat_proc = dat_raw %>%
setNames(c('date_time', 'value_raw')) %>%
mutate(date = as.Date(date_time, format = '%d-%b-%Y %H:%M')) %>%
group_by(date) %>%
mutate(value = ifelse(units == 'kcfs', value_raw * 1000, value_raw)) %>%
dplyr::select(date, value)
} # end if block
dat_fmt = dat_fmt %>%
left_join(dat_proc, by = 'date')
return(dat_fmt)
}