-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrsimRmarkdown.Rmd
199 lines (166 loc) · 8.03 KB
/
PrsimRmarkdown.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
---
title: "Visualisation"
author: "TIto-Leslie"
date: "2/25/2021"
output:
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
```{r}
load('H:/Projets_communs/2020/Outaouais PRsim/01_Intrants/Example Rdata pour Leslie/stoch_sim_10_outaouais_Kappa_1_9997_LD.Rdata')
dirsim<-"H:/Projets_communs/2020/Outaouais PRsim/02_Calculs/Resultats/results/prsim_stats_rdata/"
dir_analysis<-"H:/Projets_communs/2020/Outaouais PRsim/02_Calculs/Resultats/results/"
```
```{r,message=FALSE,echo=FALSE,warning=FALSE}
library(plotly)
library(readr)
library(tidyverse)
library(xlsx)
library(kableExtra)
i=0
Bassins=c(
"Bark Lake", "Baskatong", "Cabonga",
"Carillon et Hull" , "Chelsea", "Chenaux et Noire",
"Chute-des-Chats" , "Des Joachims", "Dozois",
"High Falls" , "Kamaniskeg", "Kiamika" ,
"Kipawa" , "Lac des Quinze", "Lac du poisson blanc",
"Lac Temiscamingue a Angliers", "Lac Victoria et lac Granet", "Lady Evelyn" ,
"Lower Notch et Indian Chute", "Madawaska-Arnprior", "Maniwaki" ,
"Masson" , "Mistinikon" , "Mitchinamecus" ,
"Mont-Laurier", "Mountain Chute", "Otto Holden",
"Paugan" , "Rabbit Lake", "Rapide-2",
"Rapide-7" , "Riviere Blanche" , "Riviere Bonnechere",
"Riviere Coulonge", "Riviere Dumoine" , "Riviere Kinojevis" ,
"Riviere Mattawa" , "Riviere Mississippi", "Riviere Petawawa" ,
"Riviere Petite Nation" , "Riviere Rideau" , "Riviere Rouge" ,
"Riviere South Nation")
#LOOP ON ALL The BASSIN
#for(bassin in Bassins){
print_fig<-function(i) {
#cat('## bassin no', i, '\n')
#i=i+1
bassin<- Bassins[i]
cmd<-paste0("stoch_sim[", i, "]$`", bassin, "`$simulation")
#print(bassin)
bsk<-eval(parse(text=cmd))
#observation
df_obs_max<-bsk%>% group_by(MM,DD)%>%summarise(MaxQ=max(Qobs,na.rm = TRUE))
df_obs_min<-bsk%>% group_by(MM,DD)%>%summarise(MinQ=min(Qobs,na.rm = TRUE))
df_obs_mean<-bsk%>% group_by(MM,DD)%>%summarise(AvgQ=mean(Qobs,na.rm = TRUE))
# Simulation
load(paste0(dirsim, bassin, '_stats.Rdata'))
simdata<-final_prsim_angliers
#Tracer le graphique
x <- seq(as.Date("2001/1/1"), as.Date("2001/12/31"), by = "day")
random_y <- simdata$MaxQ[1:365]
data <- data.frame(x, random_y)
trace1<-simdata$MaxQ[1:365]
trace2<-simdata$AvgQ[1:365]
trace3<-df_obs_mean$AvgQ
trace4<-df_obs_max$MaxQ
trace5<-df_obs_min$MinQ
fig <- plot_ly(data, x = ~x, y = ~random_y, name = 'QSimMax',type = 'scatter', mode = 'lines',line = list(color = 'transparent'),showlegend = TRUE)
fig <- fig %>% add_trace(y = ~trace2, name = 'QSimAvg', mode = 'lines', type = 'scatter', fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'transparent'),showlegend = FALSE)
fig <- fig %>% add_trace(y = ~trace1, name = 'QSimMax', mode = 'lines',line = list(color='rgb(0,100,80)'),type = 'scatter', showlegend = FALSE)
fig <- fig %>% add_trace(y = ~trace3, name = 'Qavg_obs', mode = 'lines',line = list(color='rgb(255,0,0)'),type = 'scatter')
fig <- fig %>% add_trace(y = ~trace4, name = 'Qmax_obs', mode = 'lines',line = list(color='rgb(255,0,0)'),type = 'scatter')
fig <- fig %>% add_trace(y = ~trace5, name = 'Qmin_obs', mode = 'lines',line = list(color='rgb(255,0,0)'),type = 'scatter')
titre = paste0("Average, High and Low PRSIM at ", bassin)
fig <- fig %>% layout(title = titre,
paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',
xaxis = list(title = "Days",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE),
yaxis = list(title = "Streamflow (cms)",
gridcolor = 'rgb(255,255,255)',
showgrid = TRUE,
showline = FALSE,
showticklabels = TRUE,
tickcolor = 'rgb(127,127,127)',
ticks = 'outside',
zeroline = FALSE))
#cat('\n')
#print("<P style='page-break-before: always'>")
fig
}
print_ggplot<-function(i) {
#cat('## bassin no', i, '\n')
#i=i+1
bassin<- Bassins[i]
cmd<-paste0("stoch_sim[", i, "]$`", bassin, "`$simulation")
#print(bassin)
bsk<-eval(parse(text=cmd))
#observation
df_obs_max<-bsk%>% group_by(MM,DD)%>%summarise(MaxQ=max(Qobs,na.rm = TRUE))
df_obs_min<-bsk%>% group_by(MM,DD)%>%summarise(MinQ=min(Qobs,na.rm = TRUE))
df_obs_mean<-bsk%>% group_by(MM,DD)%>%summarise(AvgQ=mean(Qobs,na.rm = TRUE))
# Simulation
load(paste0(dirsim, bassin, '_stats.Rdata'))
simdata<-final_prsim_angliers
#Tracer le graphique
x <- seq(as.Date("2001/1/1"), as.Date("2001/12/31"), by = "day")
random_y <- simdata$MaxQ[1:365]
datasim <- data.frame(x=x, y=random_y, simMax=simdata$MaxQ[1:365], simAvg=simdata$AvgQ[1:365], obsAvg=df_obs_mean$AvgQ,
obsMax=df_obs_max$MaxQ, obsMin=df_obs_min$MinQ)
#Drawing plot
fig1<-ggplot(datasim,aes(x=x,y=y, colour="yellow")) +
geom_ribbon(aes(ymin = simAvg, ymax = simMax, fill = "SimMax"), alpha = 0.30) +
scale_fill_manual(values = c("orange", "red")) +
geom_line(aes(x=x, y=obsMax, colour="red"), size = 1) +
geom_line(aes(x=x, y=obsMin, colour="blue"), size = 1) +
geom_line(aes(x=x, y=obsAvg, colour="green"), size = 1) +
scale_color_discrete(name = "Apports Observés", labels = c("Max", "Min", "Moy", "SimMax")) +
ggtitle(paste0("Comparaison de PRSIM et observation min max et moy \n pour le bassin ", bassin)) +
xlab("Date (mois/année)") + ylab(expression("Debit (m"^3*"/s") )
fig1
}
```
```{r,message=FALSE,echo=FALSE,warning=FALSE,results='asis'}
# library(htmltools)
#
# l <- htmltools::tagList()
# for (i in 1:3) {
#
# tags$div(
# l[[i]] <- print_fig(i),
# tags$br(),
# "I want a page break"
# )
# tags$div(
# "Some text followed by a break",
# tags$br(),
# "Some text following a break"
# )
#
# }
#
# l
```
```{r,message=FALSE,echo=FALSE,warning=FALSE,results='asis'}
#PRINT GRAPHS IN PDF DOCUMENT
l <- list()
for (i in 1:43) {
l[[i]] <- print_ggplot(i)
}
setwd(dir_analysis)
library(gridExtra)
pdf("plotsprim.pdf", onefile = TRUE)
for (i in seq(length(l))) {
do.call("grid.arrange", l[i])
}
dev.off()
print(l)
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.