1
1
library(DBI )
2
+ # library(tidyverse)
2
3
3
- library(tictoc )
4
- library(tidyverse )
5
-
6
- # rm(list = ls())
7
- connection_sql = TRUE
8
-
9
-
10
- # connection to the data base
11
- if (connection_sql )
12
- # conn_eurodiad <- dbConnect(RPostgres::Postgres(), dbname = 'eurodiad',
13
- # host = 'citerne.bordeaux.irstea.priv',
14
- # port = 5432,
15
- # user = 'patrick.lambert',
16
- # password = rstudioapi::askForPassword("Database password"))
17
- conn_eurodiad <- connect()
4
+ pkgload :: load_all(here :: here()) # simulate installation and give access to objects in the package
5
+ # session <- shiny::MockShinySession$new() #new.env()
6
+ # connect(session)
7
+ # con <- get_con(session)
8
+ # connection to the data base
9
+ conn_eurodiad <- connect()
18
10
19
11
# data upload ----
20
12
21
13
# ---------------------------------------------------------------------- #
22
14
# # Catchment features ----
23
- if (connection_sql ) {
24
- data_catchment <- dbGetQuery(conn_eurodiad , " SELECT basin_id, basin_name, country, surface_area_drainage_basin as surface_area, ccm_area FROM diadesatlas.basin b
25
- INNER JOIN diadesatlas.basin_outlet bo USING (basin_id);" ) %> %
15
+ data_catchment <- dbGetQuery(conn_eurodiad ,
16
+ " SELECT
17
+ basin_id,
18
+ basin_name,
19
+ country,
20
+ surface_area_drainage_basin as surface_area,
21
+ ccm_area
22
+ FROM
23
+ diadesatlas.basin b
24
+ INNER JOIN
25
+ diadesatlas.basin_outlet bo
26
+ USING (basin_id);" ) %> %
26
27
tibble()
27
-
28
-
29
- # write_rds(data_catchment, './data_input/data_catchment.rds')
30
- } else {
31
- data_catchment <- read_rds(' ./data_input/data_catchment.rds' )
32
- }
28
+
33
29
# ---------------------------------------------------------------------- #
34
30
# # Distances between catchment ----
35
- if ( connection_sql ) {
36
- outlet_distance = dbGetQuery( conn_eurodiad , " SELECT
31
+ outlet_distance = dbGetQuery( conn_eurodiad ,
32
+ " SELECT
37
33
b.basin_name AS departure,
38
34
od.departure AS departure_id,
39
35
b2.basin_name AS arrival,
@@ -48,37 +44,54 @@ INNER JOIN diadesatlas.basin b2 ON
48
44
ORDER BY departure, distance ;" ) %> %
49
45
tibble()
50
46
51
- # write_rds(outlet_distance, "./data_input/outletDistance.rds")
52
- } else {
53
- outlet_distance <- read_rds( " ./data_input/outletDistance.rds" )
54
- }
55
47
56
48
# ---------------------------------------------------------------------- #
57
49
# HyDiaD parameters ----
58
- if (connection_sql ) {
59
- hydiad_parameter <- dbGetQuery(conn_eurodiad , "
60
- SELECT s.latin_name, s.local_name AS \" Lname\" , h.* FROM diadesatlas.hydiadparameter h
61
- INNER JOIN diadesatlas.species s USING (species_id);" ) %> %
50
+ hydiad_parameter <-
51
+ dbGetQuery(conn_eurodiad ,
52
+ " SELECT
53
+ s.latin_name,
54
+ s.local_name AS \" Lname\" ,
55
+ h.*
56
+ FROM
57
+ diadesatlas.hydiadparameter h
58
+ INNER JOIN
59
+ diadesatlas.species s
60
+ USING (species_id);" ) %> %
62
61
tibble()
63
-
64
- # hydiad_parameter %>% write_rds("./data_input/HyDiaDParameter.rds")
65
62
66
- } else {
67
- hydiad_parameter <- read_rds(" ./data_input/HyDiaDParameter.rds" )
68
- }
69
63
70
64
71
65
# ---------------------------------------------------------------------- #
72
66
# # HSI abd Nmax ----
73
- if (connection_sql ) {
74
- # a query to load HSI for only 8.5 scenario (which do not change between simulations)
75
- query = " SELECT s.latin_name, basin_id, basin_name, country, surface_area_drainage_basin as surface_area, year, climatic_scenario, climatic_model_code, hsi FROM diadesatlas.hybrid_model_result hmr
76
- INNER JOIN diadesatlas.species s USING (species_id)
77
- INNER JOIN diadesatlas.basin b USING (basin_id)
78
- INNER JOIN diadesatlas.climatic_model cm USING (climatic_model_id)
79
- WHERE year > 0 AND climatic_scenario = 'rcp85'"
80
-
81
- data_hsi_nmax <- dbGetQuery(conn_eurodiad , query ) %> %
67
+ # a query to load HSI for only 8.5 scenario (which do not change between simulations)
68
+ query =
69
+ " SELECT
70
+ s.latin_name,
71
+ basin_id,
72
+ basin_name,
73
+ country,
74
+ surface_area_drainage_basin as surface_area,
75
+ year,
76
+ climatic_scenario,
77
+ climatic_model_code,
78
+ hsi
79
+ FROM
80
+ diadesatlas.hybrid_model_result hmr
81
+ INNER JOIN
82
+ diadesatlas.species s
83
+ USING (species_id)
84
+ INNER JOIN
85
+ diadesatlas.basin b
86
+ USING (basin_id)
87
+ INNER JOIN
88
+ diadesatlas.climatic_model cm
89
+ USING (climatic_model_id)
90
+ WHERE
91
+ year > 0
92
+ AND climatic_scenario = 'rcp85'"
93
+
94
+ data_hsi_nmax <- dbGetQuery(conn_eurodiad , query ) %> %
82
95
tibble() %> %
83
96
# compute the maximum abundance (#) according to hsi,
84
97
# maximal density (Dmax) , catchment area (ccm_area)
@@ -88,53 +101,75 @@ WHERE year > 0 AND climatic_scenario = 'rcp85'"
88
101
mutate(Nmax = hsi * Dmax * surface_area ) %> %
89
102
select(- c(surface_area , Dmax ))
90
103
91
- # write_rds(data_hsi_nmax, './data_input/data_hsi_Nmax.rds')
92
-
93
- rm(query )
94
- } else {
95
- data_hsi_nmax <- read_rds(' ./data_input/data_hsi_Nmax.rds' )
96
- }
104
+ rm(query )
97
105
98
106
99
107
# No ccm_area for Bou_Regreg, Loukkos, Oum_er_Rbia, Sebou. use surface_area_drainage_basin
100
108
101
109
# reference results
102
- if (connection_sql ) {
103
- reference_results <- dbGetQuery(conn_eurodiad ,
104
- " SELECT s.latin_name, basin_id, basin_name, year, climatic_scenario, climatic_model_code, nit FROM diadesatlas.hybrid_model_result hmr
105
- INNER JOIN diadesatlas.species s USING (species_id)
106
- INNER JOIN diadesatlas.basin b USING (basin_id)
107
- INNER JOIN diadesatlas.climatic_model cm USING (climatic_model_id)
108
- WHERE year > 0 AND climatic_scenario = 'rcp85'
109
- ORDER BY latin_name, basin_id, climatic_model_code" ) %> %
110
+ reference_results <- dbGetQuery(conn_eurodiad ,
111
+ " SELECT
112
+ s.latin_name,
113
+ basin_id,
114
+ basin_name,
115
+ year,
116
+ climatic_scenario,
117
+ climatic_model_code,
118
+ nit
119
+ FROM
120
+ diadesatlas.hybrid_model_result hmr
121
+ INNER JOIN
122
+ diadesatlas.species s
123
+ USING (species_id)
124
+ INNER JOIN
125
+ diadesatlas.basin b
126
+ USING (basin_id)
127
+ INNER JOIN
128
+ diadesatlas.climatic_model cm
129
+ USING (climatic_model_id)
130
+ WHERE
131
+ year > 0 AND
132
+ climatic_scenario = 'rcp85'
133
+ ORDER BY
134
+ latin_name,
135
+ basin_id,
136
+ climatic_model_code" ) %> %
110
137
tibble()
111
138
112
- # write_rds(reference_results, './data_input/referenceResults.rds')
113
- } else {
114
- reference_results <- read_rds(' ./data_input/referenceResults.rds' )
115
- }
116
-
117
-
118
139
# # initial abundance in catchments ----
119
- if (connection_sql ) {
120
- data_ni0 <- dbGetQuery(conn_eurodiad , " SELECT s.latin_name, basin_id, basin_name, surface_area_drainage_basin as surface_area, year, climatic_scenario, climatic_model_code, nit, hsi FROM diadesatlas.hybrid_model_result hmr
121
- INNER JOIN diadesatlas.species s USING (species_id)
122
- INNER JOIN diadesatlas.basin b USING (basin_id)
123
- INNER JOIN diadesatlas.climatic_model cm USING (climatic_model_id)
124
- WHERE climatic_scenario = 'rcp85'
125
- AND year = 0
126
- ORDER BY latin_name, basin_id, climatic_model_code" ) %> %
140
+ data_ni0 <-
141
+ dbGetQuery(conn_eurodiad ,
142
+ " SELECT
143
+ s.latin_name,
144
+ basin_id,
145
+ basin_name,
146
+ surface_area_drainage_basin as surface_area,
147
+ year,
148
+ climatic_scenario,
149
+ climatic_model_code,
150
+ nit,
151
+ hsi
152
+ FROM
153
+ diadesatlas.hybrid_model_result hmr
154
+ INNER JOIN
155
+ diadesatlas.species s
156
+ USING (species_id)
157
+ INNER JOIN
158
+ diadesatlas.basin b
159
+ USING (basin_id)
160
+ INNER JOIN
161
+ diadesatlas.climatic_model cm
162
+ USING (climatic_model_id)
163
+ WHERE
164
+ climatic_scenario = 'rcp85'
165
+ AND year = 0
166
+ ORDER BY
167
+ latin_name,
168
+ basin_id,
169
+ climatic_model_code" ) %> %
127
170
tibble() %> %
128
171
inner_join(hydiad_parameter %> %
129
172
select(latin_name , Dmax ),
130
173
by = ' latin_name' ) %> %
131
174
mutate(Nmax = hsi * Dmax * surface_area ) %> %
132
175
select(- c(surface_area , Dmax ))
133
-
134
- # write_rds(data_ni0, './data_input/data_ni0.rds')
135
- } else {
136
- data_ni0 <- read_rds(' ./data_input/data_ni0.rds' )
137
- }
138
-
139
- #
140
-
0 commit comments