-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolinMarkDown.Rmd
105 lines (85 loc) · 3.15 KB
/
colinMarkDown.Rmd
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
---
title: "Tropical Storm Colin Code"
output: html_document
---
## Data retrieval function
Not sure if we want to include all this or not, but hooray it *works*!
```{r global_options, warning=FALSE, message=FALSE}
library(dataRetrieval)
library(dplyr)
retryNWIS <- function(..., retries=3){
safeWQP = function(...){
result = tryCatch({
readNWISdata(...)
}, error = function(e) {
if(e$message == 'Operation was aborted by an application callback'){
stop(e)
}
return(NULL)
})
return(result)
}
retry = 1
while (retry < retries){
result = safeWQP(...)
if (!is.null(result)){
retry = retries
} else {
message('query failed, retrying')
retry = retry+1
}
}
return(result)
}
getDischarge <- function(bbox = "-85,29,-80,33.9", minDrainage = "100", start="2016-06-4", end="2016-06-06"){
#find sites within lat/lon box, >100 km^2 drainage
#expanded output option didnt seem to provide anything extra?
sites <- whatNWISsites(bbox=bbox,siteType="ST",siteOutput="Expanded",drainAreaMin="100", parameterCd="00060")
#get the actual data, converted to numeric format
siteNos <- sites$site_no
startDate <- as.Date(start)
endDate <- as.Date(end)
#loop over sites, get data, concatenate
#leave out sites with no data or sample rates > 15 min (192 samples/2 days)
allQ <- data.frame()
for (site in siteNos)
{
siteQ <- retryNWIS(service = "iv",convertType = TRUE, sites=site,
startDate=startDate, endDate=endDate, parameterCd="00060")
#print(dim(siteQ))
siteQ <- renameNWISColumns(siteQ)
#coercing column names to be the same; might want to check if any sites besides the tallahassee one has neg #s?
#print a notification first though
if( identical(names(allQ),names(siteQ)) == FALSE && length(names(siteQ)) == 6)
{
names(siteQ) <- c("agency_cd","site_no","dateTime","Flow_Inst","Flow_Inst_cd","tz_cd")
print(paste("site",site,"had different column names; they were changed"))
}
if (nrow(siteQ) > 1) #first check that there is actually data in siteQ
{
sampRate = siteQ$dateTime[2] - siteQ$dateTime[1] #check that the sample rate is every 15 min
if ( sampRate == 15)
{
#print(site)
#print(dim(siteQ))
allQ <- rbind(allQ,siteQ)
}
}
}
allQ_info <- left_join(allQ, sites, by="site_no") %>%
select(site_no, dateTime, Flow_Inst, station_nm, dec_lat_va, dec_long_va)
return(allQ_info)
}
```
## Visualization code
```{r warning=FALSE, message=FALSE}
library(maps)
tscolin_states <- c('florida', 'georgia', 'south carolina','north carolina')
tscolin_counties <- data.frame(state = 'florida',
county = c('taylor', 'madison'))
tscolin_counties_string <- paste(tscolin_counties$state, tscolin_counties$county, sep=",")
region_map_counties <- map('county', regions = tscolin_states, col = "grey")
region_map <- map('state', regions = tscolin_states, add = TRUE, lwd = 1.5)
highlight_counties <- map('county', regions = tscolin_counties_string,
add = TRUE, fill = TRUE, col = 'cornflowerblue')
```