-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRIDischarge.Rmd
276 lines (174 loc) · 6.91 KB
/
RIDischarge.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
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
---
title: "RIDischarge"
author: "Miguel Leon"
date: "Monday, April 13, 2015"
output: html_document
---
```{r, echo=FALSE, message=FALSE}
# Load the project
project_directory <- "C:/Users/leonmi/Google Drive/Discharge" # library(devtools)
#project_directory <- "C:/Users/aa20/Dropbox/Manuscripts/Composite Method/composite_client"
library(ProjectTemplate)
#source(paste0(project_directory,"/","lib/loadProject.R"))
setwd(project_directory)
source("C:/Users/leonmi/Google Drive/loadest/composite_client/src/scripts/reportHelpers.R")
# Redefine project_directory now that loadProjectFromCache has cleared the old variable
project_directory <- "C:/Users/leonmi/Google Drive/Discharge"
#project_directory <- "C:/Users/aa20/Dropbox/Manuscripts/Composite Method/composite_client"
# Set options for producing the html filee
library(knitr)
opts_chunk$set(echo=FALSE, message=FALSE, warn=FALSE)
options(warn=-1)
# Load libraries we'll use, suppressing messages via message=FALSE option to knitr chunk
library(MASS)
library(rloadest)
library(lubridate)
library(dplyr)
library(ggplot2)
library(data.table)
#RIDischargeFrom1999
insertRow <- function(existingDF, newrow, r) {
existingDF[seq(r+1,nrow(existingDF)+1),] <- existingDF[seq(r,nrow(existingDF)),]
existingDF[r,] <- newrow
existingDF
}
newnames <- as.vector(t(read.csv(
"RIDischarge.csv",
sep=",",header=FALSE,skip=0,nrows=1,stringsAsFactors=FALSE)[1,]))
RIFullFlow <- read.csv(
"RIDischarge.csv",
sep=",",header=FALSE,col.names=newnames,skip=1,stringsAsFactors=FALSE)
newnames <- as.vector(t(read.csv(
"RIDischargeFrom2006.csv",
sep=",",header=FALSE,skip=0,nrows=1,stringsAsFactors=FALSE)[1,]))
RiFrom2006 <- read.csv(
"RIDischargeFrom2006.csv",
sep=",",header=FALSE,col.names=newnames,skip=1,stringsAsFactors=FALSE)
#DailyDischargeUSGS.csv
newnames <- as.vector(t(read.csv(
"DailyDischargeUSGS.csv",
sep=",",header=FALSE,skip=0,nrows=1,stringsAsFactors=FALSE)[1,]))
USGSDischarge <- read.csv(
"DailyDischargeUSGS.csv",
sep=",",header=FALSE,col.names=newnames,skip=1,stringsAsFactors=FALSE)
USGSDischarge$Date <- as.POSIXct(strptime(USGSDischarge$Date ,format="%m/%d/%Y",tz="America/Puerto_Rico"))
RIFullFlow$DateTime <- as.POSIXct(strptime(RIFullFlow$DateTime ,format="%m/%d/%Y %H:%M",tz="America/Puerto_Rico"))
RiFrom2006$DateTime <- as.POSIXct(strptime(RiFrom2006$DateTime ,format="%m/%d/%Y %H:%M",tz="America/Puerto_Rico"))
RIFullFlow<- merge(RIFullFlow,RiFrom2006,all=TRUE)
#remove NA values
RIFullFlow <- RIFullFlow[!is.na(RIFullFlow$USGS_cfps),]
RIFullFlow<- RIFullFlow[order(RIFullFlow$DateTime),]
RIFullFlow$num <- 1
min15pyr <- with(RIFullFlow, tapply(num, YEAR, sum))
```
15 minute (or 10 min) records per year
```{r, echo=FALSE, message=FALSE}
min15pyr
dt <- as.POSIXlt("1986-10-01 00:15:00")
dt <- dt -900
newrow <- list(1986,1,as.POSIXct(strptime(dt, format="%Y-%m-%d",tz="America/Puerto_Rico")),NA,NA,1986,1) #%H:%M:%S
#,origin = "1900-01-01"
#"1999-10-01 00:00:00","%Y-%m-%d %H:%M:%S",tz="America/Puerto_Rico"
#USGS_Sonadora_IDA2 <- rbind(USGS_Sonadora_IDA, newrow)
RIFullFlow<- insertRow(RIFullFlow, newrow, 1)
RIFullFlow$diffsec <- c(NA, tail(RIFullFlow$DateTime, -1) - head(RIFullFlow$DateTime, -1))
RIFullFlow$diffMin <- RIFullFlow$diffsec / 60
RIFullFlow$diffsec[RIFullFlow$diffsec > 900] <- 900
RIFullFlow$diffMin[RIFullFlow$diffMin > 15] <- 15
#some of these are 5 minute not 15 minute need to make sure the cfps for each record represents the right amount of time.
totalCF15mCurr<- with(RIFullFlow, tapply(((USGS_cfps*diffsec)), YEAR, sum))
totalCF15m2Curr <- totalCF15mCurr /with(RIFullFlow, tapply(((diffsec)), YEAR, sum))
```
mean discharge per year
```{r, echo=FALSE, message=FALSE}
totalCF15m2Curr
write.csv(totalCF15m2Curr,
"./RIMeanAnnualDischarge.csv",na="NA",,row.names=TRUE)
```
plot log discharge 2010
```{r, echo=FALSE, message=FALSE}
#agggregate by day
```
calculate mean daily discharge
```{r, echo=FALSE, message=FALSE}
RIFullFlow$Day<- as.POSIXct(strptime(RIFullFlow$DateTime,format="%Y-%m-%d",tz="America/Puerto_Rico"))
#FILL IN DISCHARGE MEAN FOR ALL TIMES
RIFullFlow<- RIFullFlow %>%
group_by(Day) %>%
mutate(cfsps_day_mean=mean(USGS_cfps,na.rm=TRUE))
days<- list(RIFullFlow$Day)
RIFullFlowDaily<- aggregate(RIFullFlow,by=days, FUN=head, 1)
#write daily discharge to a file
write.csv(RIFullFlowDaily,
"./RIDailyExport.csv",na="NA",,row.names=FALSE)
#Q12014day<- transform(RIFullFlow, YR=as.numeric(format(Date, "%Y")))
years<- list(RIFullFlowDaily$YEAR)
RIFullFlowMeanDailytoYr<- aggregate(RIFullFlowDaily$cfsps_day_mean,by=years, FUN=sum)
startDate <- as.POSIXct("2013-01-01")
endDate <- as.POSIXct("2014-01-01")
RIFullFlowDaily2<- RIFullFlowDaily[RIFullFlowDaily$DateTime > startDate
& RIFullFlowDaily$DateTime < endDate,]
USGSDischarge2<- USGSDischarge[USGSDischarge$Date > startDate
& USGSDischarge$Date < endDate,]
ggplot() + ggtitle("RI log Discharge CFS 2013") +
geom_line(data= RIFullFlowDaily2, aes(x=DateTime, y=log(USGS_cfps) , color="Mean Daily Discharge"))+
geom_line(data= USGSDischarge2, aes(x=Date, y=log(cfs.1) , color="Mean Daily Discharge USGS"))
```
Daily mean CFS Stats
1st is SD
```{r, echo=FALSE, message=FALSE}
sd(RIFullFlowDaily$cfsps_day_mean)
summary(RIFullFlowDaily$cfsps_day_mean)
```
Daily mean CFS Stats from USGS data
1st is SD
```{r, echo=FALSE, message=FALSE}
sd(USGSDischarge$cfs.1)
summary(USGSDischarge$cfs.1)
```
Daily mean CFS Stats 2014
1st is SD
```{r, echo=FALSE, message=FALSE}
sd(RIFullFlowDaily2$cfsps_day_mean)
summary(RIFullFlowDaily2$cfsps_day_mean)
```
Daily mean CFS Stats from USGS data 2014
1st is SD
```{r, echo=FALSE, message=FALSE}
sd(USGSDischarge2$cfs.1)
summary(USGSDischarge2$cfs.1)
```
days per year
```{r, echo=FALSE, message=FALSE}
daypYr <- with(RIFullFlowDaily, tapply(num, YEAR, sum))
daypYr
```
calculate annual mean discharge from daily average discharge
```{r, echo=FALSE, message=FALSE}
write.csv(daypYr,
"./RIDaysPYear.csv",na="NA",row.names=TRUE)
RIFullFlowMeanDailytoYr$x <- RIFullFlowMeanDailytoYr$x /daypYr
RIFullFlowMeanDailytoYr
write.csv(RIFullFlowMeanDailytoYr,
"./meanYearlyDischargeFromMeanDaily.csv",na="NA",row.names=TRUE)
```
find days per month
```{r, echo=FALSE, message=FALSE}
RIFullFlowMonthly<- transform(RIFullFlowDaily, Month=format(DateTime, "%Y-%m"))
daypMonth <- with(RIFullFlowMonthly, tapply(num, Month, sum))
daypMonth
write.csv(daypMonth,
"./RIDaysPMonth.csv",na="NA",,row.names=TRUE)
RIFullFlowMonthly<- RIFullFlowMonthly %>%
group_by(Month) %>%
mutate(cfsps_month_mean=mean(cfsps_day_mean,na.rm=TRUE))
months<- list(RIFullFlowMonthly$Month)
RIFullFlowMonthly<- aggregate(RIFullFlowMonthly,by=months, FUN=head, 1)
write.csv(RIFullFlowMonthly,
"./RIMonthlyExport.csv",na="NA",,row.names=FALSE)
```
Monthly mean CFS Stats
1st is SD
```{r, echo=FALSE, message=FALSE}
sd(RIFullFlowMonthly$cfsps_month_mean)
summary(RIFullFlowMonthly$cfsps_month_mean)