forked from richaben/PRR_ONDE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_telechargement_data.R
99 lines (83 loc) · 3.16 KB
/
02_telechargement_data.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
##%####################################%##
# #
#### Téléchargement des données ONDE ####
# #
##%####################################%##
'%>%' <- dplyr::'%>%'
source("_config.R")
load("data/onde_data/to_update.rda")
# to_update <- TRUE
### Utilisation de l'API Hubeau ----
if (to_update) {
dernieres_obs <- purrr::map_df(
.x = conf_dep,
.f = function(d) {
data.frame(
code_departement = d,
date_observation = readLines(
paste0(
"https://hubeau.eaufrance.fr/api/v1/ecoulement/observations?format=json&code_departement=",
d, "&size=1&fields=date_observation&sort=desc"
)
) %>%
stringr::str_extract(pattern = "\\d{4}-\\d{2}-\\d{2}")
)
}
)
#### infos campagnes
campagnes <- purrr::map_df(.x = conf_dep,
function(x) hubeau::get_ecoulement_campagnes(
list(
code_departement = x,
date_campagne_min = "2012-01-01",
date_campagne_max = date_jour
)
)) %>%
dplyr::mutate(code_campagne = as.character(code_campagne)) %>%
dplyr::distinct()
#### infos stations
param_stations <-
hubeau::list_params(api = "ecoulement", endpoint = "stations") %>%
toString() %>%
gsub(pattern = " ",replacement = "") %>%
paste0(",etat_station")
stations <- purrr::map_df(.x = conf_dep,
function(x) hubeau::get_ecoulement_stations(
list(code_departement = x,
fields = param_stations)
)) %>%
dplyr::distinct()
#### infos observations
param_obs <-
hubeau::list_params(api = "ecoulement", endpoint = "observations") %>%
toString() %>%
gsub(pattern = " ",replacement = "")
observations <- purrr::map_df(.x = conf_dep,
function(x) hubeau::get_ecoulement_observations(
list(code_departement = x,
date_observation_min = "2012-01-01",
date_observation_max = date_jour,
fields = param_obs)
)) %>%
dplyr::mutate(code_campagne = as.character(code_campagne)) %>%
dplyr::distinct()
### Assemblage des données stations, observations, campagnes ----
onde_df <- observations %>%
dplyr::left_join(campagnes) %>%
dplyr::left_join(stations) %>%
dplyr::mutate(
date_campagne = lubridate::as_date(date_campagne, format = "%Y-%m-%d")
) %>%
dplyr::mutate(
Annee = lubridate::year(date_campagne),
libelle_ecoulement = dplyr::if_else(
condition = is.na(libelle_ecoulement),
true = "Donnée manquante",
false = libelle_ecoulement
)
) %>%
dplyr::arrange(code_station,code_departement,desc(Annee))
### Ecriture/Sauvegarde des données ----
write.csv(onde_df, "data/onde_data/onde.csv")
write.csv2(dernieres_obs, "data/onde_data/dernieres_obs.csv")
}