From f62d4134167f6cef18206c8123e6d302a9984c34 Mon Sep 17 00:00:00 2001
From: Dorchies David <david.dorchies@inrae.fr>
Date: Fri, 6 Aug 2021 11:06:39 +0200
Subject: [PATCH] feat(piezometrie): add chroniques and chroniques_tr functions

Fix #8
---
 DESCRIPTION                             |  2 +-
 NAMESPACE                               |  2 +
 R/get_niveaux_nappes_chroniques.R       | 30 +++++++++++++++
 R/get_niveaux_nappes_chroniques_tr.R    | 26 +++++++++++++
 R/get_niveaux_nappes_stations.R         |  2 +-
 inst/config.yml                         | 51 ++++++++++---------------
 man/get_niveaux_nappes_chroniques.Rd    | 34 +++++++++++++++++
 man/get_niveaux_nappes_chroniques_tr.Rd | 30 +++++++++++++++
 man/get_niveaux_nappes_stations.Rd      |  2 +-
 9 files changed, 145 insertions(+), 34 deletions(-)
 create mode 100644 R/get_niveaux_nappes_chroniques.R
 create mode 100644 R/get_niveaux_nappes_chroniques_tr.R
 create mode 100644 man/get_niveaux_nappes_chroniques.Rd
 create mode 100644 man/get_niveaux_nappes_chroniques_tr.Rd

diff --git a/DESCRIPTION b/DESCRIPTION
index 1e57fc9..7b1d8a1 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -17,6 +17,6 @@ Imports:
     urltools
 RoxygenNote: 7.1.1
 Roxygen: list(markdown = TRUE)
-Suggests: 
+Suggests:
     testthat (>= 3.0.0)
 Config/testthat/edition: 3
diff --git a/NAMESPACE b/NAMESPACE
index 8342d3c..ea72a59 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -7,6 +7,8 @@ export(get_hydrometrie_sites)
 export(get_hydrometrie_stations)
 export(get_indicateurs_services_communes)
 export(get_indicateurs_services_indicateurs)
+export(get_niveaux_nappes_chroniques)
+export(get_niveaux_nappes_chroniques_tr)
 export(get_niveaux_nappes_stations)
 export(get_prelevements_chroniques)
 export(get_prelevements_ouvrages)
diff --git a/R/get_niveaux_nappes_chroniques.R b/R/get_niveaux_nappes_chroniques.R
new file mode 100644
index 0000000..33f07a2
--- /dev/null
+++ b/R/get_niveaux_nappes_chroniques.R
@@ -0,0 +1,30 @@
+#' Retrieve piezometric archived time series from API "Piézométrie"
+#'
+#' See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-piezometrie}
+#'
+#' @template param_get_common
+#'
+#' @return a [tibble::tibble] with all available parameters in columns and one row by time step and by station.
+#' @export
+#'
+#' @examples
+#' # Retrieve the archived observed piezometric level at station '07548X0009/F' (old BSS identifier)
+#' # for the year 2020
+#' df <- get_niveaux_nappes_chroniques(list(code_bss = "07548X0009/F",
+#'                                       date_debut_mesure = "2020-01-01",
+#'                                       date_fin_mesure = "2020-12-31"))
+#'
+#' # Plot the water elevation (NGF)
+#' plot(as.POSIXct(df$date_mesure), df$niveau_nappe_eau, type = "l")
+#'
+get_niveaux_nappes_chroniques  <- function(params,
+                                   cfg = config::get(file = system.file("config.yml",
+                                                        package = "hubeau"))) {
+  l <- doApiQuery(
+    api = "niveaux_nappes",
+    operation = "chroniques",
+    params = params,
+    cfg = cfg
+  )
+  convert_list_to_tibble(l)
+}
diff --git a/R/get_niveaux_nappes_chroniques_tr.R b/R/get_niveaux_nappes_chroniques_tr.R
new file mode 100644
index 0000000..390b701
--- /dev/null
+++ b/R/get_niveaux_nappes_chroniques_tr.R
@@ -0,0 +1,26 @@
+#' Retrieve piezometric "real time" chroniques from API "Piézométrie"
+#'
+#' See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-piezometrie}
+#'
+#' @template param_get_common
+#'
+#' @return a [tibble::tibble] with all available parameters in columns and one row by time step and by station.
+#' @export
+#'
+#' @examples
+#' # For retrieving the last real time observed piezometric level at station 'BSS001VZGZ' (new BSS identifier)
+#' df <- get_niveaux_nappes_chroniques_tr(list(bss_id = "BSS001VZGZ"))
+#'
+#' # Plot the water elevation (NGF)
+#' plot(as.POSIXct(df$date_mesure), df$niveau_eau_ngf, type = "l")
+get_niveaux_nappes_chroniques_tr  <- function(params,
+                                   cfg = config::get(file = system.file("config.yml",
+                                                        package = "hubeau"))) {
+  l <- doApiQuery(
+    api = "niveaux_nappes",
+    operation = "chroniques_tr",
+    params = params,
+    cfg = cfg
+  )
+  convert_list_to_tibble(l)
+}
diff --git a/R/get_niveaux_nappes_stations.R b/R/get_niveaux_nappes_stations.R
index e8884ee..5da5c1b 100644
--- a/R/get_niveaux_nappes_stations.R
+++ b/R/get_niveaux_nappes_stations.R
@@ -8,7 +8,7 @@
 #' @export
 #'
 #' @examples
-#' # For retrieving the hydrometric stations in the department of Aube
+#' # Retrieve the hydrometric stations in the department of Aube
 #' get_niveaux_nappes_stations(list(code_departement = "10"))
 #'
 get_niveaux_nappes_stations  <- function(params, cfg = config::get(file = system.file("config.yml",
diff --git a/inst/config.yml b/inst/config.yml
index 9d15239..a88f2f7 100644
--- a/inst/config.yml
+++ b/inst/config.yml
@@ -153,6 +153,26 @@ default:
     niveaux_nappes:
       path: v1/niveaux_nappes
       operations:
+        chroniques:
+          path: chroniques
+          fields:
+            - code_bss
+            - date_debut_mesure
+            - date_fin_mesure
+            - fields
+        chroniques_tr:
+          path: chroniques_tr
+          fields:
+            - bbox
+            - bss_id
+            - code_bss
+            - date_debut_mesure
+            - date_fin_mesure
+            - fields
+            - niveau_ngf_max
+            - niveau_ngf_min
+            - profondeur_max
+            - profondeur_min
         stations:
           path: stations
           fields:
@@ -166,34 +186,3 @@ default:
             - date_recherche
             - fields
             - srid
-        sites:
-          path: referentiel/sites
-          fields:
-            - bbox
-            - code_commune_site
-            - code_cours_eau
-            - code_departement
-            - code_region
-            - code_site
-            - code_troncon_hydro_site
-            - code_zone_hydro_site
-            - distance
-            - fields
-            - latitude
-            - libelle_cours_eau
-            - libelle_site
-            - longitude
-        observations_tr:
-          path: observations_tr
-          fields:
-            - bbox
-            - code_entite
-            - cursor
-            - date_debut_obs
-            - date_fin_obs
-            - distance
-            - fields
-            - grandeur_hydro
-            - latitude
-            - longitude
-            - timestep
diff --git a/man/get_niveaux_nappes_chroniques.Rd b/man/get_niveaux_nappes_chroniques.Rd
new file mode 100644
index 0000000..3666412
--- /dev/null
+++ b/man/get_niveaux_nappes_chroniques.Rd
@@ -0,0 +1,34 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/get_niveaux_nappes_chroniques.R
+\name{get_niveaux_nappes_chroniques}
+\alias{get_niveaux_nappes_chroniques}
+\title{Retrieve piezometric archived time series from API "Piézométrie"}
+\usage{
+get_niveaux_nappes_chroniques(
+  params,
+  cfg = config::get(file = system.file("config.yml", package = "hubeau"))
+)
+}
+\arguments{
+\item{params}{a \link{list} the list of parameters of the queries and their values in the format \code{list(ParamName = "Param value", ...)}, use the function \link{get_available_params} for a list of the available parameters for a given operation in an API and see the API documentation for the complete list of available filter parameters}
+
+\item{cfg}{a \link{config} object Configuration of the communication. Use by default the internal package
+configuration}
+}
+\value{
+a \link[tibble:tibble]{tibble::tibble} with all available parameters in columns and one row by time step and by station.
+}
+\description{
+See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-piezometrie}
+}
+\examples{
+# Retrieve the archived observed piezometric level at station '07548X0009/F' (old BSS identifier)
+# for the year 2020
+df <- get_niveaux_nappes_chroniques(list(code_bss = "07548X0009/F",
+                                      date_debut_mesure = "2020-01-01",
+                                      date_fin_mesure = "2020-12-31"))
+
+# Plot the water elevation (NGF)
+plot(as.POSIXct(df$date_mesure), df$niveau_nappe_eau, type = "l")
+
+}
diff --git a/man/get_niveaux_nappes_chroniques_tr.Rd b/man/get_niveaux_nappes_chroniques_tr.Rd
new file mode 100644
index 0000000..19627fd
--- /dev/null
+++ b/man/get_niveaux_nappes_chroniques_tr.Rd
@@ -0,0 +1,30 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/get_niveaux_nappes_chroniques_tr.R
+\name{get_niveaux_nappes_chroniques_tr}
+\alias{get_niveaux_nappes_chroniques_tr}
+\title{Retrieve piezometric "real time" chroniques from API "Piézométrie"}
+\usage{
+get_niveaux_nappes_chroniques_tr(
+  params,
+  cfg = config::get(file = system.file("config.yml", package = "hubeau"))
+)
+}
+\arguments{
+\item{params}{a \link{list} the list of parameters of the queries and their values in the format \code{list(ParamName = "Param value", ...)}, use the function \link{get_available_params} for a list of the available parameters for a given operation in an API and see the API documentation for the complete list of available filter parameters}
+
+\item{cfg}{a \link{config} object Configuration of the communication. Use by default the internal package
+configuration}
+}
+\value{
+a \link[tibble:tibble]{tibble::tibble} with all available parameters in columns and one row by time step and by station.
+}
+\description{
+See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-piezometrie}
+}
+\examples{
+# For retrieving the last real time observed piezometric level at station 'BSS001VZGZ' (new BSS identifier)
+df <- get_niveaux_nappes_chroniques_tr(list(bss_id = "BSS001VZGZ"))
+
+# Plot the water elevation (NGF)
+plot(as.POSIXct(df$date_mesure), df$niveau_eau_ngf, type = "l")
+}
diff --git a/man/get_niveaux_nappes_stations.Rd b/man/get_niveaux_nappes_stations.Rd
index 279e1a1..4efbb7d 100644
--- a/man/get_niveaux_nappes_stations.Rd
+++ b/man/get_niveaux_nappes_stations.Rd
@@ -22,7 +22,7 @@ a \link[tibble:tibble]{tibble::tibble} with all available parameters in columns
 See the API documentation for available filter parameters: \url{https://hubeau.eaufrance.fr/page/api-piezometrie}
 }
 \examples{
-# For retrieving the hydrometric stations in the department of Aube
+# Retrieve the hydrometric stations in the department of Aube
 get_niveaux_nappes_stations(list(code_departement = "10"))
 
 }
-- 
GitLab