V03 - Usages data preprocessing

library(tidyverse)
library(airGRccia)

Exploring available data on hubeau

Documentation:

hubeau::list_endpoints("prelevements")
#> [1] "chroniques"         "ouvrages"           "points_prelevement"
hubeau::list_params("prelevements", "chroniques")
#>  [1] "annee"                      "bbox"                      
#>  [3] "code_commune_insee"         "code_departement"          
#>  [5] "code_mode_obtention_volume" "code_ouvrage"              
#>  [7] "code_qualification_volume"  "code_statut_instruction"   
#>  [9] "code_statut_volume"         "code_usage"                
#> [11] "distance"                   "fields"                    
#> [13] "format"                     "latitude"                  
#> [15] "libelle_departement"        "longitude"                 
#> [17] "nom_commune"                "page"                      
#> [19] "prelevement_ecrasant"       "producteur_donnee"         
#> [21] "size"                       "sort"                      
#> [23] "volume_max"                 "volume_min"

The “bbbox” parameter is convenient for retrieving data from GPS coordinates (WGS84).

From hubeau’s documentation:

Rectangle d’emprise de l’objet demandé, emprise au format : min longitude, min latitude, max longitude, max latitude avec les coordonnées en WGS84 (EPSG:4326), le point doit être utilisé comme séparateur décimal, exemple : 1.6194,47.7965,2.1910,47.9988

Search for withdrawals data in the Herault catchment area

Get the bounding box of the catchment area

sf_sb <- readRDS(getCachePath("V01", "sb", "RDS"))
sf_sb
#> Simple feature collection with 6 features and 10 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 718450 ymin: 6273325 xmax: 770000 ymax: 6336175
#> Projected CRS: RGF93 v1 / Lambert-93
#> # A tibble: 6 × 11
#>   id            area mvd_outlet_x mvd_outlet_y apriori_area   tot_area
#> * <chr>        [m^2]        <dbl>        <dbl>        <dbl>      [m^2]
#> 1 Y2230010  78768750      733388.     6285662.           NA   78768750
#> 2 Y2210010 182417500      725862.     6291762.    228000000  182417500
#> 3 Y2100020 910304375      759112.     6313212.    760000000  910304375
#> 4 Y2140020 316295625      748262.     6294638.   1200000000 1226600000
#> 5 Y2140010 180587500      743138.     6283762.   1312000000 1407188750
#> 6 Y2300020 286473750      738012.     6274712.           NA 1954851875
#> # ℹ 5 more variables: geometry <POLYGON [m]>, outlet_x <dbl>, outlet_y <dbl>,
#> #   mvd_dist <dbl>, down <chr>

Coordinate system used here is RGF93. We need to convert bbox to WGS84.

sf_sb <- sf::st_transform(sf_sb, crs = "WGS84")
bbox <- sf::st_bbox(sf_sb)
bbox
#>      xmin      ymin      xmax      ymax 
#>  3.228614 43.558131  3.870453 44.123131

Retrieving data

dfWD <- hubeau::get_prelevements_chroniques(bbox = paste(round(bbox, 4), collapse = ","))
dfWD <- dfWD |> filter(volume > 0, annee >= 2012)
str(dfWD)
#> tibble [3,950 × 28] (S3: tbl_df/tbl/data.frame)
#>  $ code_ouvrage                 : chr [1:3950] "OPR0000012886" "OPR0000012886" "OPR0000012886" "OPR0000012886" ...
#>  $ annee                        : int [1:3950] 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 ...
#>  $ volume                       : num [1:3950] 55071 54018 48891 54018 52249 ...
#>  $ code_usage                   : chr [1:3950] "AEP" "AEP" "AEP" "AEP" ...
#>  $ libelle_usage                : chr [1:3950] "EAU POTABLE" "EAU POTABLE" "EAU POTABLE" "EAU POTABLE" ...
#>  $ code_statut_volume           : chr [1:3950] "1" "1" "1" "1" ...
#>  $ libelle_statut_volume        : chr [1:3950] "Contrôlé Niveau 1" "Contrôlé Niveau 1" "Contrôlé Niveau 1" "Contrôlé Niveau 1" ...
#>  $ code_qualification_volume    : chr [1:3950] "1" "1" "1" "1" ...
#>  $ libelle_qualification_volume : chr [1:3950] "Correcte" "Correcte" "Correcte" "Correcte" ...
#>  $ code_statut_instruction      : chr [1:3950] "REA" "REA" "REA" "REA" ...
#>  $ libelle_statut_instruction   : chr [1:3950] "Prélèvement réalisé" "Prélèvement réalisé" "Prélèvement réalisé" "Prélèvement réalisé" ...
#>  $ code_mode_obtention_volume   : chr [1:3950] "MES" "MES" "MES" "MED" ...
#>  $ libelle_mode_obtention_volume: chr [1:3950] "Volume mesuré" "Volume mesuré" "Volume mesuré" "Mesure directe" ...
#>  $ prelevement_ecrasant         : logi [1:3950] FALSE FALSE FALSE FALSE FALSE FALSE ...
#>  $ producteur_donnee            : chr [1:3950] "AEAG" "AEAG" "AEAG" "AEAG" ...
#>  $ longitude                    : num [1:3950] 3.4 3.4 3.4 3.4 3.4 ...
#>  $ latitude                     : num [1:3950] 44 44 44 44 44 ...
#>  $ code_commune_insee           : chr [1:3950] "12231" "12231" "12231" "12231" ...
#>  $ nom_commune                  : chr [1:3950] "Saint-Jean-du-Bruel" "Saint-Jean-du-Bruel" "Saint-Jean-du-Bruel" "Saint-Jean-du-Bruel" ...
#>  $ code_departement             : chr [1:3950] "12" "12" "12" "12" ...
#>  $ libelle_departement          : chr [1:3950] "Aveyron" "Aveyron" "Aveyron" "Aveyron" ...
#>  $ nom_ouvrage                  : chr [1:3950] "CPT COURS D'EAU \"DOURBIE\"" "CPT COURS D'EAU \"DOURBIE\"" "CPT COURS D'EAU \"DOURBIE\"" "CPT COURS D'EAU \"DOURBIE\"" ...
#>  $ uri_ouvrage                  : chr [1:3950] "https://id.eaufrance.fr/OuvragePrel/OPR0000012886" "https://id.eaufrance.fr/OuvragePrel/OPR0000012886" "https://id.eaufrance.fr/OuvragePrel/OPR0000012886" "https://id.eaufrance.fr/OuvragePrel/OPR0000012886" ...
#>  $ geometry.type                : chr [1:3950] "Point" "Point" "Point" "Point" ...
#>  $ geometry.crs.type            : chr [1:3950] "name" "name" "name" "name" ...
#>  $ geometry.crs.properties.name : chr [1:3950] "urn:ogc:def:crs:OGC:1.3:CRS84" "urn:ogc:def:crs:OGC:1.3:CRS84" "urn:ogc:def:crs:OGC:1.3:CRS84" "urn:ogc:def:crs:OGC:1.3:CRS84" ...
#>  $ geometry.coordinates1        : num [1:3950] 3.4 3.4 3.4 3.4 3.4 ...
#>  $ geometry.coordinates2        : num [1:3950] 44 44 44 44 44 ...
#>  - attr(*, "query")= chr "https://hubeau.eaufrance.fr/api/v1/prelevements/chroniques?bbox=3.2286,43.5581,3.8705,44.1231"
sf_WD <- sf::st_as_sf(dfWD, crs = "WGS84", coords = c("geometry.coordinates1", "geometry.coordinates2"))
library(tmap)
tmap_mode("view")
#> ℹ tmap modes "plot" - "view"
plot_WD_map <- function(selected_year = 2021) {
  tm_basemap("OpenStreetMap.France") +
    tm_shape(sf_sb) + tm_borders("black", lwd = 2) +
    tm_shape(sf_WD |> filter(annee == selected_year)) +
    tm_symbols(col = "libelle_usage", size = "volume", scale = 5, popup.vars = c(
      "libelle_usage", "code_ouvrage", "nom_ouvrage", "volume"
    ))
}
plot_WD_map()
#> 
#> ── tmap v3 code detected ───────────────────────────────────────────────────────
#> [v3->v4] `symbols()`: use 'fill' for the fill color of polygons/symbols
#> (instead of 'col'), and 'col' for the outlines (instead of 'border.col').[v3->v4] `tm_symbols()`: migrate the argument(s) related to the scale of the
#> map variable `size` namely 'scale' (rename to 'values.scale') to size.scale =
#> tm_scale_continuous(<HERE>).
#> ℹ For small multiples, specify a 'tm_scale_' for each multiple, and put them in
#>   a list: 'size.scale = list(<scale1>, <scale2>, ...)'[deprecated] The arguments `popup.vars` and `popup.format` of `tm_symbols()`
#> are deprecated.
#> ℹ Use `popup` with `tm_popup()` instead, e.g. `popup = tm_popup(vars = ...,
#>   format = ...)`.

Cleaning and validation of yearly data

Lets remove Water flowing through turbines of run of river hydro facilities and only keep withdrawals located in the catchment area.

sf_WD <- sf_WD |> filter(code_usage != "BAR")
sf_WD <- sf::st_intersection(sf_WD, sf_sb |> select(id))
#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries
plot_WD_map()
#> 
#> ── tmap v3 code detected ───────────────────────────────────────────────────────
#> [v3->v4] `tm_symbols()`: migrate the argument(s) related to the scale of the
#> map variable `size` namely 'scale' (rename to 'values.scale') to size.scale =
#> tm_scale_continuous(<HERE>).
#> ℹ For small multiples, specify a 'tm_scale_' for each multiple, and put them in
#>   a list: 'size.scale = list(<scale1>, <scale2>, ...)'
lapply(setNames(nm = unique(sf_WD$libelle_usage)), function(x) {
  unique(sf_WD |> filter(libelle_usage == x) |> pull(nom_ouvrage))
})
#> $`EAU POTABLE`
#>   [1] "FORAGE MAS BAS"                                        
#>   [2] "FORAGE DANS NAPPE CEBEROU"                             
#>   [3] "FORAGE DANS NAPPE LAFOUX"                              
#>   [4] "FORAGE DANS NAPPE MAS CANET"                           
#>   [5] "FORAGE ET SOURCES D'OCTON"                             
#>   [6] "SOURCE DES MARTRES"                                    
#>   [7] "SOURCE DE LA GLORIETTE LIBERENQUE"                     
#>   [8] "FORAGE DE MONTARDIE MONT LIAUSSON"                     
#>   [9] "SOURCE PAYROL"                                         
#>  [10] "SOURCE DE LA DOUX"                                     
#>  [11] "SOURCE DE MURENE 2"                                    
#>  [12] "SOURCE LA BOULE"                                       
#>  [13] "SOURCE LA VERNEDE"                                     
#>  [14] "SOURCE DE SEVIAC"                                      
#>  [15] "SOURCE L'AMBEYRAN"                                     
#>  [16] "SOURCE SOULAGES"                                       
#>  [17] "SOURCE  BRONZINADOUIRE"                                
#>  [18] "SOURCES  CHAMP DU LAC ET BOURBOUNELLE"                 
#>  [19] "SOURCE DU JUNCAS"                                      
#>  [20] "SOURCE  DE COUTELLES"                                  
#>  [21] "FORAGE EN NAPPE ST PONS"                               
#>  [22] "SOURCE  DU THERONDEL"                                  
#>  [23] "SOURCE DU CAYLARET"                                    
#>  [24] "PRISE D'EAU DE LA SARMEJANE"                           
#>  [25] "SOURCE ADRET DE GRIMAL"                                
#>  [26] "SOURCE DES BOUSCARASSES"                               
#>  [27] "SOURCE DU MAS QUEYROL"                                 
#>  [28] "SOURCE DE LA FONTASSE"                                 
#>  [29] "SOURCE CAMPBEL - COMBE CALLE - BUFFINIERES - COURTIERE"
#>  [30] "SOURCE LAFOUX"                                         
#>  [31] "SOURCE MARAPLUIE SUD"                                  
#>  [32] "SOURCE MARAPLUIE NORD - PLEGE"                         
#>  [33] "PUITS DANS NAPPE AMONT DE NAVACELLES"                  
#>  [34] "SOURCE DE LA QUINTE"                                   
#>  [35] "SOURCE DE SALAGOSSE"                                   
#>  [36] "SOURCE PUECHAGUT"                                      
#>  [37] "FORAGE DE ROUJAL"                                      
#>  [38] "SOURCE ST MARTIN"                                      
#>  [39] "SOURCE  LIEU DIT \"LES FAYSSES\""                      
#>  [40] "SOURCE ROULON"                                         
#>  [41] "SOURCE DE L'ARBOUX"                                    
#>  [42] "SOURCE DE LASFONT"                                     
#>  [43] "FORAGE DANS NAPPE MAZEL"                               
#>  [44] "FORAGE MADIERES"                                       
#>  [45] "SOURCE  DE LA GRANDE GOURGUE"                          
#>  [46] "SOURCE FONT DE COSTE"                                  
#>  [47] "FORAGE PONT DE LA SALLE"                               
#>  [48] "SOURCE LA ROUVIERETTE"                                 
#>  [49] "SOURCE  LES SUELS"                                     
#>  [50] "SOURCE LA VIELLE -  LA COSTE"                          
#>  [51] "FORAGE COMMUNAL"                                       
#>  [52] "FORAGE DANS NAPPE PUECH GARIN BARRICES"                
#>  [53] "FORAGE DANS NAPPE TOUMEYROLLES"                        
#>  [54] "PUITS DANS NAPPE DU ROSIER"                            
#>  [55] "FORAGE DU PRE LONG"                                    
#>  [56] "SOURCE DE MALBOS"                                      
#>  [57] "SOURCE DE VIALA"                                       
#>  [58] "SOURCE DU FROMENTAL"                                   
#>  [59] "PRISE DANS HERAULT"                                    
#>  [60] "SOURCE CASTANET SANISAC"                               
#>  [61] "SOURCE DU THEROND"                                     
#>  [62] "SOURCE DE LA COMMUNE MALLET"                           
#>  [63] "SOURCE TALEYRAC"                                       
#>  [64] "SOURCE ARDAILLERS -  MAS MIQUEL"                       
#>  [65] "SOURCE ARDAILLERS -  VILLAGE"                          
#>  [66] "SOURCE DE L'ESCOUTADOU"                                
#>  [67] "PRISE DANS L'HERAULT STATION DE GANGES"                
#>  [68] "FORAGE DE SOUTAYROL"                                   
#>  [69] "SOURCE DE LA FOUX"                                     
#>  [70] "FORAGE L'ESCOUTET"                                     
#>  [71] "SOURCE AMARINIERS"                                     
#>  [72] "SOURCE D'ISIS"                                         
#>  [73] "SOURCE  VERDIER"                                       
#>  [74] "FORAGES DE BOUQUELAURE"                                
#>  [75] "FORAGE DE TARLANTIER"                                  
#>  [76] "SOURCES CAMPBEL - COMBECALLE"                          
#>  [77] "FORAGE DANS NAPPE BUFFINIERE"                          
#>  [78] "SOURCE COURTIERE"                                      
#>  [79] "SOURCE BONNELS"                                        
#>  [80] "FORAGE DANS NAPPE PUECH LOUBIER"                       
#>  [81] "SOURCE LES HORS"                                       
#>  [82] "SOURCE LA COMBE"                                       
#>  [83] "SOURCE DU MOULIN"                                      
#>  [84] "SOURCES DE LA GLACIERE - QUIERS"                       
#>  [85] "SOURCE  ROUAS"                                         
#>  [86] "SOURCE  BEAUMELLE"                                     
#>  [87] "SOURCE  DE VALCROZE"                                   
#>  [88] "SOURCE DU VIDOURLE"                                    
#>  [89] "FORAGES CHAMP CAPTANT FESQUET"                         
#>  [90] "FORAGES DE LA PLAINE"                                  
#>  [91] "FORAGES DE BOUQUELAURE NORD"                           
#>  [92] "SOURCES DU THERON"                                     
#>  [93] "PRISE EAU RIVIERE LA DAUPHINE"                         
#>  [94] "PRISE D'EAU DU CAYLARET"                               
#>  [95] "FORAGE RANC DE BANE"                                   
#>  [96] "FORAGE NAVACELLES F3 AMONT"                            
#>  [97] "FORAGE NAVACELLES F4 AVAL"                             
#>  [98] "FORAGE DANS NAPPE LIEU-DIT LA VIELLE"                  
#>  [99] "SOURCE DE LA  FOUX"                                    
#> [100] "FORAGE DANS NAPPE MAS DE BAUMES"                       
#> [101] "SOURCE DES 3 BAUMETTES"                                
#> [102] "SOURCE LE MEJANEL BUEGES"                              
#> [103] "FORAGE DU RIEUTORD 1"                                  
#> [104] "SOURCE DES ESCANAUX"                                   
#> [105] "FORAGE DEVES"                                          
#> [106] "PUITS & FORAGES DANS NAPPE DU MOULINET"                
#> [107] "SOURCE ST-ROME"                                        
#> [108] "PUITS DANS NAPPE DRAC"                                 
#> [109] "SOURCE LABIDE FONTFROIDE LIEU DIT BOIS DES FONTANILLES"
#> [110] "SOURCE  DU BOUT DU MONDE"                              
#> [111] "PUITS DANS NAPPE FAMAJOU"                              
#> [112] "FORAGES LES RIVIERES F1 ET F2"                         
#> [113] "PUITS DANS NAPPE ROUJALS & CAMBOUS"                    
#> [114] "FORAGE DANS NAPPE LIEU-DIT RABIEUX"                    
#> [115] "SOURCE FONT-CHAUDE"                                    
#> [116] "FORAGE DANS NAPPE  MAS DE MARRE"                       
#> [117] "FORAGE DANS NAPPE L'AVEYRO"                            
#> [118] "SOURCE VALLOMBREUSE"                                   
#> [119] "PUITS  DANS NAPPE LA MEUSE"                            
#> [120] "FORAGE DANS NAPPE MAS DE NAVAS"                        
#> [121] "FORAGE LAVOIR"                                         
#> [122] "PUITS DANS NAPPE MAS ROUJOU"                           
#> [123] "FORAGE DANS NAPPE LE VILLAGE"                          
#> [124] "PUITS NAPPE ALLUVIALE HERAULT L'AUMEDE"                
#> [125] "PUITS DANS NAPPE LE PONT"                              
#> [126] "SOURCE DU  PESQUIER"                                   
#> [127] "SOURCE DE BERTHOMIEU"                                  
#> [128] "SOURCE LA GLORIETTE DE LA TOUR"                        
#> [129] "SOURCE DE VAL ROUSSE 3"                                
#> [130] "FORAGE DANS NAPPE CARONS OUEST"                        
#> [131] "FORAGE DANS NAPPE CARONS EST"                          
#> [132] "FORAGE DANS NAPPE F1"                                  
#> [133] "FORAGE DANS NAPPE LES FALIADOUS"                       
#> [134] "FORAGE DANS NAPPE CLOCHER"                             
#> [135] "SOURCES  BOURG ET VALAREDES"                           
#> [136] "FORAGE DU STADE"                                       
#> [137] "SOURCE  LE PONT DE L'AMOUR"                            
#> [138] "SOURCE  DE FONTANILLE"                                 
#> [139] "SOURCE F1 (LES RIOLS)"                                 
#> [140] "SOURCE F3 (LES RIOLS)"                                 
#> [141] "SOURCE F4 (LES RIOLS)"                                 
#> [142] "FORAGE  DE ROUJALS"                                    
#> [143] "PUITS DE ROUJALS"                                      
#> [144] "FORAGE  COMBE SALINIERE OUEST 2006"                    
#> [145] "FORAGE COMBE SALINIERE MILIEU 2006"                    
#> [146] "FORAGE DE LA PLAINE F1 NORD"                           
#> [147] "FORAGE DE LA PLAINE F2 SUD"                            
#> [148] "FORAGES CAMBOU F1-F2"                                  
#> [149] "FORAGE COMBE SALINIERE MILIEU 2017"                    
#> [150] "FORAGE COMBE SALINIERE OUEST 2017"                     
#> [151] "SOURCE DE LA GLORIETTE"                                
#> 
#> $`INDUSTRIE et ACTIVITES ECONOMIQUES (hors irrigation, hors énergie)`
#>  [1] "FORAGE DANS NAPPE  MAS AUDRAN"                            
#>  [2] "SOURCE DE LA GLORIETTE LIBERENQUE"                        
#>  [3] "PRISE DANS RETENUE BARRAGE DU SALAGOU"                    
#>  [4] "SOURCE DE LA QUINTE"                                      
#>  [5] "PRISE EAU RIVIERE LA DAUPHINE"                            
#>  [6] "SOURCE D'ISIS"                                            
#>  [7] "PRISE RUISSEAU LE COULAROU - FABRIQUE TEXTILE DE COLLANTS"
#>  [8] "PRISE EAU SUR L'ARRE"                                     
#>  [9] "PRISE DANS LA VIS LIEU-DIT ROC DE MOUDARE"                
#> [10] "SOURCE LA COMBE"                                          
#> [11] "PRISE D'EAU DANS L'ARRE - FABRIQUE TEXTILE DE COLLANTS"   
#> [12] "PRISE DANS CANAL DE DERIVATION DE LA FURE - ACIERIE"      
#> [13] "SOURCE DU FROMENTAL"                                      
#> [14] "FORAGES CHAMP CAPTANT FESQUET"                            
#> [15] "PRISE DANS L'AVEZE"                                       
#> [16] "PRISE D'EAU DANS L'HERAULT"                               
#> [17] "PRISE D'EAU DANS L'HERAULT STE BERNADOU R. & FILS"        
#> [18] "PRISE DANS L'HERAULT CANAL DE GIGNAC"                     
#> [19] "PRISE D'EAU DANS LA RIVIERE"                              
#> [20] "POMPAGE DANS LA NAPPE SOLAG"                              
#> [21] "PUITS DANS NAPPE MAS ROUJOU"                              
#> [22] "PUITS DANS NAPPE PROFONDE EXHAURE DE TRAVAUX MINIERS"     
#> [23] "PRISE DANS  LA LERGUE LIEU-DIT CAPITOUL"                  
#> [24] "PRISE DANS L'HERAULT LIEU-DIT GOURDIBEAU"                 
#> [25] "PRISE DANS L'HERAULT STATION DU POUGET"                   
#> 
#> $IRRIGATION
#>  [1] "PRISE DANS RETENUE BARRAGE DU SALAGOU"             
#>  [2] "PRISE DANS RETENUE DU SALAGOU"                     
#>  [3] "PRISE DANS  RETENUE DU SALAGOU"                    
#>  [4] "PRISE DANS LE COUDOULOUS LD LA FABREGUE"           
#>  [5] "PRISE DANS LE COUDOULOUS LD GALARY"                
#>  [6] "PRISE DANS LE COUDOULOUS LD LE CAMBON"             
#>  [7] "PRISE DANS LE COUDOULOUS LD FONTAINEBLEAU"         
#>  [8] "PRISE D'EAU LES COMBES"                            
#>  [9] "PRISE D'EAU MANDAGOUT"                             
#> [10] "PRISE D'EAU  RUISSEAU DE L'ARBOUX"                 
#> [11] "PRISE D'EAU VALAT DE COURBIERE"                    
#> [12] "PRISE D'EAU VALAT DE VALDIVIN"                     
#> [13] "PRISE D'EAU RAU DU GAZEL"                          
#> [14] "PRISE D'EAU RUISSEAU DE BEDOUS"                    
#> [15] "PUITS CIMETIERE"                                   
#> [16] "PRISE D'EAU COSTUBAGUE"                            
#> [17] "PRISE D'EAU LE CAMBOURNES CANAL"                   
#> [18] "PRISE DANS L'ARRE CANAL PRAT PIALOUX"              
#> [19] "PRISE DANS  RETENUE COLLINAIRE LES SIGNETTES"      
#> [20] "PRISE D'EAU"                                       
#> [21] "PRISE D'EAU AVAL RUISSEAU L'ARBOUX"                
#> [22] "PRISE D'EAU  ARBOUX POMPAGE BORIE"                 
#> [23] "PRISE DANS L'HERAULT CANAL DE CAMBON"              
#> [24] "POMPAGE DANS L'HERAULT LD LE PRAT"                 
#> [25] "PRISE DANS L'HERAULT CANAL DU PRAT"                
#> [26] "PRISE D'EAU DE LA GINOUVESE"                       
#> [27] "PRISE D'EAU FIGARET"                               
#> [28] "PRISE D'EAU DANS L'HERAULT"                        
#> [29] "PRISE DANS LE RECODIER LIEU-DIT LE CAMBON"         
#> [30] "PRISE DANS RETENUE COLLINAIRE CANAL DE COMMESSAC"  
#> [31] "PRISE DANS LE RECODIER   LE MOULIN PRE DE LA PEYRE"
#> [32] "PRISE D'EAU DANS LE RECODIER CANAL LE VERNET"      
#> [33] "PRISE D'EAU VALAT DU ROUSSET"                      
#> [34] "PRISE D'EAU VALAT DE LA BOUISSOUNE"                
#> [35] "PRISE D'EAU LD LA BAGATELLE"                       
#> [36] "PRISE D'EAU VALAT DES FONTS"                       
#> [37] "PRISE DANS LE RENUS CANAL LE RIEUTORD"             
#> [38] "PRISE D'EAU  COMBE LONGUE"                         
#> [39] "PRISE DANS L'HERAULT CANAL ANGLIVIELS RIVE GAUCHE" 
#> [40] "PRISE DANS L'HERAULT CANAL CHAPELLE RIVE DROITE"   
#> [41] "PRISE DANS BASSINS RETENUES  LES METGES"           
#> [42] "PRISE D'EAU LES PERIES"                            
#> [43] "PRISE DANS L'ARRE LD TESSAN"                       
#> [44] "PRISE DANS L'ARRE LA MAZERANDE"                    
#> [45] "PRISE EAU SUR L'ARRE CANAL ARENES"                 
#> [46] "PRISE DANS LA VIS LIEU-DIT ROC DE MOUDARE"         
#> [47] "PRISE DANS LE CLAROU CANAL"                        
#> [48] "PRISE DANS LE CLAROU CANAL ARC EN CIEL"            
#> [49] "PRISE D'EAU VALAT DU PLAGNOL"                      
#> [50] "PRISE D'EAU VALAT DES SALLES CANAL MOULIN"         
#> [51] "PRISE  VALAT DE ROQUE ROUGE AFFLUENT"              
#> [52] "PRISE  VALAT DU RENARD - SALLES -"                 
#> [53] "VALAT DE COSTE PLANE - AFFLU"                      
#> [54] "PRISE D'EAU DANS LE CLAROU"                        
#> [55] "PRISE D'EAU DANS LE REYNUS COURNIERS"              
#> [56] "CANAL DU CAMPREDON"                                
#> [57] "PRISE D'EAU DANS LE VALAT  DE L'HORT DE DIEU"      
#> [58] "PRISE EAU LE SOULIES"                              
#> [59] "PRISE DANS L'HERAULT COMBE CHAUDE"                 
#> [60] "SOURCE D'ISIS"                                     
#> [61] "PRISE D'EAU  DANS L'HERAULT POMPAGE MERCOU"        
#> [62] "PRISE D'EAU  DANS L'HERAULT MAS NEUF"              
#> [63] "PRISE D'EAU  DANS L'HERAULT GINOUVEZE"             
#> [64] "PRISE GRAVITAIRE RUISSEAU DU PHAL"                 
#> [65] "PRISE D'EAU COURBIERES POMPAGE DU MOULIN"          
#> [66] "PRISE D'EAU COURBIERES POMPAGE ARBOUX"             
#> [67] "PRISE EAU  DANS L'HERAULT LD LES LAUNES"           
#> [68] "PRISE D'EAU RIEUTORD LD L'ILE PARCELLE C4"         
#> [69] "SOURCE  DU CASTANET B 749"                         
#> [70] "PRISE D'EAU RIEUTORD LD LES AUMEDES A1155"         
#> [71] "PRISE D'EAU COMBE BONNE A 853"                     
#> [72] "PRISE DANS LE CLAROU"                              
#> [73] "PRISE D'EAU DANS LE REYNUS CAMPREDON"              
#> [74] "PRISE DANS L'HERAULT MAS FIGUIER"                  
#> [75] "PRISE VALAT DES SALLES"                            
#> [76] "PRISE D'EAU  DANS L'ARRE"                          
#> [77] "PRISE D'EAU HERAULT"                               
#> [78] "PRISE D'EAU DANS L'HERAULT LD CAMPINGOUS"          
#> [79] "PRISE D'EAU VALAT DU CROUZET"                      
#> [80] "PRISE D'EAU DANS L'HERAULT VALLERAUGUE"            
#> [81] "PRISE D'EAU DANS LE VALATTE"                       
#> [82] "PRISE D'EAU DANS LE MAS VALAT"                     
#> [83] "PRISE D'EAU VALAT DE BRAGAL"                       
#> [84] "PRISE D'EAU DANS L'HERAULT_LD MAS DIEU"            
#> [85] "PRISE D'EAU  DANS L'HERAULT_LD GINOUVESE"          
#> [86] "PRISE DANS L'HERAULT CANAL DE GIGNAC"              
#> [87] "PRISE DANS RETENUE DU SALAGOU LD LE BOUSQUET"      
#> [88] "PRISE DANS  LA LERGUE LIEU-DIT CAPITOUL"           
#> [89] "PRISE DANS L'HERAULT STATION DU POUGET"            
#> [90] "PRISE DANS L'HERAULT LIEU-DIT GOURDIBEAU"          
#> [91] "PRISE DANS L'HERAULT L'ARCADE"                     
#> 
#> $CANAUX
#>  [1] "SOURCE D'ISIS"                                    
#>  [2] "PRISE D'EAU VALAT DE COURBIERE"                   
#>  [3] "PRISE D'EAU VALAT DE VALDIVIN"                    
#>  [4] "PRISE D'EAU RAU DU GAZEL"                         
#>  [5] "PRISE D'EAU RUISSEAU DE BEDOUS"                   
#>  [6] "PRISE D'EAU  LE CAMBOUET"                         
#>  [7] "PRISE DANS L'ARRE LIEU-DIT MAGNANS"               
#>  [8] "PRISE D'EAU AVAL RUISSEAU L'ARBOUX"               
#>  [9] "PRISE D'EAU AMONT RUISSEAU L'ARBOUX"              
#> [10] "PRISE DANS L'HERAULT CANAL DU PRAT"               
#> [11] "PRISE DANS LE RECODIER LIEU-DIT LE CAMBON"        
#> [12] "PRISE DANS RETENUE COLLINAIRE CANAL DE COMMESSAC" 
#> [13] "PRISE D'EAU DANS LE RECODIER CANAL LE VERNET"     
#> [14] "PRISE D'EAU DANS VALAT DU ROUSSET"                
#> [15] "PRISE D'EAU DANS VALAT DE LA BOUISSOUNE"          
#> [16] "PRISE D'EAU LD LA BAGATELLE"                      
#> [17] "PRISE D'EAU VALAT DES FONTS"                      
#> [18] "PRISE D'EAU DANS L'HERAULT"                       
#> [19] "PRISE D'EAU DANS LE RENUS CANAL LE RIEUTORD"      
#> [20] "PRISE DANS L'HERAULT CANAL PONT NEUF RIVE DROITE" 
#> [21] "PRISE DANS L'HERAULT CANAL CHAPELLE RIVE GAUCHE"  
#> [22] "PRISE EAU SUR L'ARRE"                             
#> [23] "PRISE DANS L'ARRE LIEU-DIT TESSAN"                
#> [24] "PRISE DANS LA VIS LIEU-DIT ROC DE MOUDARE"        
#> [25] "PRISE D'EAU LE CAMBOURNES CANAL"                  
#> [26] "PRISE DANS L'ARRE CANAL PRAT PIALOUX"             
#> [27] "PRISE DANS L'HERAULT CANAL DE CAMBON"             
#> [28] "PRISE DANS LE RENUS CANAL LE RIEUTORD"            
#> [29] "PRISE DANS L'HERAULT CANAL ANGLIVIELS RIVE GAUCHE"
#> [30] "PRISE DANS L'HERAULT CANAL CHAPELLE RIVE DROITE"  
#> [31] "PRISE D'EAU VALAT DU ROUSSET"                     
#> [32] "PRISE D'EAU VALAT DE LA BOUISSOUNE"               
#> [33] "PRISE DANS LE CLAROU CANAL"                       
#> [34] "PRISE EAU SUR L'ARRE CANAL ARENES"                
#> [35] "PRISE D'EAU VALAT DES SALLES CANAL MOULIN"        
#> [36] "CANAL DU CAMPREDON"                               
#> [37] "PRISE GRAVITAIRE RUISSEAU DU PHAL"                
#> [38] "PRISE D'EAU COURBIERES POMPAGE ARBOUX"            
#> [39] "PRISE DANS L'HERAULT LIEU-DIT BELLE BEZET"        
#> [40] "PRISE DANS L'HERAULT CANAL DE GIGNAC"

Volumes registered in “CANAUX” correspond to the withdrawn at the irrigation canal intake. Volumes registered in “IRRIGATION” correspond to the amount of water used from the canal for irrigating the fields, which means that these volumes are counted twice. The difference of volume between “CANAUX” and “IRRIGATION” correspond to the amount of water that has been released in the river all along the irrigation system.

knitr::include_graphics(
  rprojroot::find_package_root_file("vignettes/scheme_water_fluxes_irrigation_system.png")
)

As first approximation, we consider that the impact on the river correspond to only water consumption, so we are not considering “CANAUX” withdrawn.

sf_WD <- sf_WD |> filter(code_usage != "CAN")
plot_withdraw_year <- function(df) {
  ggplot(
    df |> group_by(annee, code_usage) |> summarise(total_volume = sum(volume)),
    aes(x = annee, y = total_volume, fill = code_usage)
  ) +
    geom_col() +
    theme(legend.position = "bottom")
}
plot_withdraw_year(sf_WD)
#> `summarise()` has regrouped the output.
#> ℹ Summaries were computed grouped by annee and code_usage.
#> ℹ Output is grouped by annee.
#> ℹ Use `summarise(.groups = "drop_last")` to silence this message.
#> ℹ Use `summarise(.by = c(annee, code_usage))` for per-operation grouping
#>   (`?dplyr::dplyr_by`) instead.

The calibration period begins in August 2013 and ends at the 31th December 2024. Years after 2023 are missing. By default, we complete the gap with the data of 2021.

# Reference year to replicate is the last year with data
ref_year <- max(sf_WD$annee)

# Target years to fill
target_years <- (ref_year + 1):2024

# Generate rows for missing years by replicating 2021
sf_WD <- bind_rows(
  sf_WD,
  purrr::map_dfr(target_years, ~ sf_WD %>%
            filter(annee == ref_year) %>%
            mutate(annee = .x))
)
plot_withdraw_year(sf_WD)
#> `summarise()` has regrouped the output.
#> ℹ Summaries were computed grouped by annee and code_usage.
#> ℹ Output is grouped by annee.
#> ℹ Use `summarise(.groups = "drop_last")` to silence this message.
#> ℹ Use `summarise(.by = c(annee, code_usage))` for per-operation grouping
#>   (`?dplyr::dplyr_by`) instead.

And then we apply a a water consumption ratio to all raw withdrawals in order to get the real amount of water removed from the river.

Aater consumption ratio are extracted from:

Eric Sauquet, Jean-Pierre Vergnes, Guillaume Thirel, Laurent Strohmenger. Identification de stations hydrométriques et piézomètres pour l’évaluation des modélisations hydrologiques et hydrogéologiques. INRAE; BRGM. 2022. (hal-03940233) https://professionnels.ofb.fr/sites/default/files/pdf/projets/Explore2/Rapport%20Explore2%20reseauReference%20Evaluation%20-%20VF.pdf

cons_ratio <- c(AEP = 0.2, IRR = 0.7, IND = 0.07)
sf_WD$cons_ratio <- cons_ratio[sf_WD$code_usage]
sf_WD$raw_volume <- sf_WD$volume
sf_WD$volume <- sf_WD$raw_volume * sf_WD$cons_ratio
plot_withdraw_year(sf_WD)
#> `summarise()` has regrouped the output.
#> ℹ Summaries were computed grouped by annee and code_usage.
#> ℹ Output is grouped by annee.
#> ℹ Use `summarise(.groups = "drop_last")` to silence this message.
#> ℹ Use `summarise(.by = c(annee, code_usage))` for per-operation grouping
#>   (`?dplyr::dplyr_by`) instead.

We aggregate the data at sub-basin scale:

wd_yearly <-  as.data.frame(sf_WD) |>
  group_by(id, annee, code_usage) |>
  summarise(volume = sum(volume), .groups = 'drop')
head(wd_yearly)
#> # A tibble: 6 × 4
#>   id       annee code_usage  volume
#>   <chr>    <int> <chr>        <dbl>
#> 1 Y2100020  2012 AEP        563194.
#> 2 Y2100020  2012 IND          7294.
#> 3 Y2100020  2012 IRR        450044 
#> 4 Y2100020  2013 AEP        620090.
#> 5 Y2100020  2013 IND          4832.
#> 6 Y2100020  2013 IRR        434224

Usage data deaggregation at daily time step

Sauquet et al. (2022) consider a constant withdrawal throughout the calendar year for Drinking water and industry and a concentration of the irrigation between June and September.

code_usages <- c("AEP", "IND", "IRR")
monthly_ratio <- data.frame(
  code_usage = rep(code_usages, each = 12),
  month = rep(1:12, 3),
  ratio = c(rep(1/12, 2 * 12), rep(0, 5), rep(1/4, 4), rep(0, 3))
)
monthly_ratio
#>    code_usage month      ratio
#> 1         AEP     1 0.08333333
#> 2         AEP     2 0.08333333
#> 3         AEP     3 0.08333333
#> 4         AEP     4 0.08333333
#> 5         AEP     5 0.08333333
#> 6         AEP     6 0.08333333
#> 7         AEP     7 0.08333333
#> 8         AEP     8 0.08333333
#> 9         AEP     9 0.08333333
#> 10        AEP    10 0.08333333
#> 11        AEP    11 0.08333333
#> 12        AEP    12 0.08333333
#> 13        IND     1 0.08333333
#> 14        IND     2 0.08333333
#> 15        IND     3 0.08333333
#> 16        IND     4 0.08333333
#> 17        IND     5 0.08333333
#> 18        IND     6 0.08333333
#> 19        IND     7 0.08333333
#> 20        IND     8 0.08333333
#> 21        IND     9 0.08333333
#> 22        IND    10 0.08333333
#> 23        IND    11 0.08333333
#> 24        IND    12 0.08333333
#> 25        IRR     1 0.00000000
#> 26        IRR     2 0.00000000
#> 27        IRR     3 0.00000000
#> 28        IRR     4 0.00000000
#> 29        IRR     5 0.00000000
#> 30        IRR     6 0.25000000
#> 31        IRR     7 0.25000000
#> 32        IRR     8 0.25000000
#> 33        IRR     9 0.25000000
#> 34        IRR    10 0.00000000
#> 35        IRR    11 0.00000000
#> 36        IRR    12 0.00000000

More precise monthly distribution can be taken into account given other sources, notably on https://fleuve-herault.fr website.

get_daily_wd <- function(yearly) {
  # Create vector of daily dates for the current year
  bound_dates <- as.POSIXlt(c("2000-01-01", "2000-12-31"))
  lubridate::year(bound_dates) <- yearly$annee
  DatesR <- seq(bound_dates[1], bound_dates[2], "1 day")
  # Preparation of the table
  daily <- data.frame(DatesR = DatesR,
                      month = lubridate::month(DatesR),
                      id = yearly$id,
                      code_usage = yearly$code_usage)
  # Computation of the ratio to apply by day
  day_ratio <- 1 / 365.25 * 12 # Average ratio for a monthly volume
  month_ratio <- monthly_ratio |>
    filter(code_usage == yearly$code_usage) |>
    arrange(month) |>
    pull(ratio)
  daily$day_ratio <- day_ratio * month_ratio[daily$month]
  # Computation of the net withdrawal
  daily$volume <- yearly$volume * daily$day_ratio
  return(daily)
}

l_daily <- lapply(seq(nrow(wd_yearly)), function(i) {
  get_daily_wd(yearly = wd_yearly[i, , drop = FALSE])
})
wd_daily <- do.call(rbind, l_daily)
summary(wd_daily)
#>      DatesR                        month                id       
#>  Min.   :2012-01-01 00:00:00   Min.   : 1.000   Length   :67582  
#>  1st Qu.:2015-04-21 00:00:00   1st Qu.: 4.000   N.unique :    6  
#>  Median :2018-06-08 00:00:00   Median : 7.000   N.blank  :    0  
#>  Mean   :2018-06-08 07:09:38   Mean   : 6.522   Min.nchar:    8  
#>  3rd Qu.:2021-07-08 00:00:00   3rd Qu.:10.000   Max.nchar:    8  
#>  Max.   :2024-12-31 00:00:00   Max.   :12.000                    
#>      code_usage      day_ratio            volume        
#>  Length   :67582   Min.   :0.000000   Min.   :    0.00  
#>  N.unique :    3   1st Qu.:0.002738   1st Qu.:    2.30  
#>  N.blank  :    0   Median :0.002738   Median :   90.53  
#>  Min.nchar:    3   Mean   :0.002739   Mean   : 2276.56  
#>  Max.nchar:    3   3rd Qu.:0.002738   3rd Qu.:  876.86  
#>                    Max.   :0.008214   Max.   :94817.29

We aggregate the data and format it for airGRiwrm:

wd_daily <- wd_daily |> group_by(DatesR, id) |> summarise(volume = sum(volume), .groups = "drop")
Qinf <- tidyr::pivot_wider(wd_daily, names_from = id, values_from = volume, values_fill = 0)
summary(Qinf)
#>      DatesR              Y2100020       Y2140010          Y2140020    
#>  Min.   :2012-01-01   Min.   :1139   Min.   :  187.9   Min.   :281.9  
#>  1st Qu.:2015-04-02   1st Qu.:1286   1st Qu.:  423.7   1st Qu.:320.0  
#>  Median :2018-07-02   Median :1469   Median :  765.8   Median :327.0  
#>  Mean   :2018-07-02   Mean   :2436   Mean   :22197.4   Mean   :328.1  
#>  3rd Qu.:2021-10-01   3rd Qu.:4203   3rd Qu.:57417.6   3rd Qu.:337.4  
#>  Max.   :2024-12-31   Max.   :5417   Max.   :95933.3   Max.   :377.0  
#>     Y2210010        Y2230010          Y2300020    
#>  Min.   :549.7   Min.   :  56.83   Min.   : 1396  
#>  1st Qu.:722.7   1st Qu.:  64.16   1st Qu.: 1931  
#>  Median :819.8   Median :  73.33   Median : 2051  
#>  Mean   :780.0   Mean   : 698.67   Mean   : 5957  
#>  3rd Qu.:876.9   3rd Qu.:1494.86   3rd Qu.:12949  
#>  Max.   :886.5   Max.   :2871.81   Max.   :19096
TSstudio::ts_plot(Qinf, slider = TRUE)
file_Qinf <- getCachePath("V03", "Qinf_historical", ".xlsx")
if(!file.exists(file_Qinf)) {
  openxlsx2::write_xlsx(Qinf, file_Qinf)
}