From 3e1edf46e6afc5a69e9fd3e81942d6f7069484e4 Mon Sep 17 00:00:00 2001
From: "louis.heraut" <louis.heraut@inrae.fr>
Date: Tue, 30 Nov 2021 11:22:06 +0100
Subject: [PATCH] Name change and new extraction all from BH

---
 plotting/layout.R                     |   7 +-
 processing/{extractBH.R => extract.R} | 101 +++++++++++++++++------
 script.R                              | 113 +++++++++++++++-----------
 3 files changed, 147 insertions(+), 74 deletions(-)
 rename processing/{extractBH.R => extract.R} (80%)

diff --git a/plotting/layout.R b/plotting/layout.R
index f2e27d0..b3631f9 100644
--- a/plotting/layout.R
+++ b/plotting/layout.R
@@ -98,7 +98,7 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
     for (code in Code) {
         
         # Print code of the station for the current plotting
-        print(paste("Plotting for sation :", code))
+        print(paste("Plotting for station :", code))
         
         nbh = as.numeric(info_header) + as.numeric(!is.null(time_header))
         nbg = nbp + nbh
@@ -111,8 +111,9 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
         }
 
         if (!is.null(time_header)) {
-            time_header_code = time_header[time_header$code == code,]
 
+            time_header_code = time_header[time_header$code == code,]
+            
             Htime = time_panel(time_header_code, df_trend_code=NULL,
                                period=period, missRect=TRUE,
                                unit2day=365.25, type='Q')
@@ -129,7 +130,7 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
             unit2day = list_df2plot[[i]]$unit2day
             missRect = list_df2plot[[i]]$missRect
             type = list_df2plot[[i]]$type
-            
+
             df_data_code = df_data[df_data$code == code,] 
             df_trend_code = df_trend[df_trend$code == code,]
 
diff --git a/processing/extractBH.R b/processing/extract.R
similarity index 80%
rename from processing/extractBH.R
rename to processing/extract.R
index 2ce4f4b..606877d 100644
--- a/processing/extractBH.R
+++ b/processing/extract.R
@@ -83,10 +83,40 @@ iRegHydro = c('D'='Affluents du Rhin',
               '4'='RĂ©union')
 
 
+create_selection = function (computer_data_path, filedir, outname) {
+    
+    outfile = file.path(computer_data_path, outname)
+    codelist = c()
+    dir_path = file.path(computer_data_path, filedir)                  
+    filelist_tmp = list.files(dir_path)
+               
+    for (f in filelist_tmp) {
+            if (file_ext(f) == 'txt') {
+                codelist = c(codelist, gsub('.txt', '', f)) 
+            }
+        }  
+
+    df_file = tibble(code=codelist,
+                     filename=paste(codelist,
+                                    '_HYDRO_QJM.txt', sep=''),
+                     ok=TRUE)
+
+    write.table(df_file, outfile, sep=";", col.names=TRUE, quote=FALSE)
+    
+    return (NULL)
+}
+
+# Example
+# create_selection(
+#     "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
+#     "France207",
+#     "nival_selection.txt")
+
+
 # Get the selection of data from the 'Liste-station_RRSE' file and the BanqueHydro directory
-get_selection = function (computer_data_path, listdir, listname,
-                          cnames=c('code','station', 'BV_km2', 'axe_principal_concerne', 'longueur_serie', 'commentaires', 'choix'), 
-                          c_num=c('BV_km2', 'longueur_serie')) {
+get_selection_AG = function (computer_data_path, listdir, listname,
+                             cnames=c('code','station', 'BV_km2', 'axe_principal_concerne', 'longueur_serie', 'commentaires', 'choix'), 
+                             c_num=c('BV_km2', 'longueur_serie')) {
     
     # Get the file path to the data
     list_path = file.path(computer_data_path, listdir, listname)
@@ -123,22 +153,46 @@ get_selection = function (computer_data_path, listdir, listname,
 }
 
 # Example
-# df_selec = get_selection(
-#     "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
-#     "",
-#     "Liste-station_RRSE.docx",
-#     cnames=c('code','station', 
-#              'BV_km2',
-#              'axe_principal_concerne',
-#              'longueur_serie',
-#              'commentaires',
-#              'choix'), 
-#     c_num=c('BV_km2',
-#              'longueur_serie'))
+df_selec_AG = get_selection_AG(
+    "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
+    "",
+    "Liste-station_RRSE.docx",
+    cnames=c('code','station', 
+             'BV_km2',
+             'axe_principal_concerne',
+             'longueur_serie',
+             'commentaires',
+             'choix'), 
+    c_num=c('BV_km2',
+            'longueur_serie'))
+
+
+get_selection_NV = function (computer_data_path, listdir, listname) {
+    
+    # Get the file path to the data
+    list_path = file.path(computer_data_path, listdir, listname)
+    
+    # Extract the data as a data frame
+    df_selec = read.table(list_path,
+                          header=TRUE,
+                          encoding='UTF-8',
+                          sep=';',
+                          )
+    df_selec = tibble(code=as.character(df_selec$code),
+                      filename=as.character(df_selec$filename),
+                      ok=df_selec$ok)
+
+    return (df_selec)
+}
+# Example
+# df_selec_NV = get_selection_NV(
+    # "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
+    # "",
+    # "nival_selection.txt")
 
 
 # Extraction of metadata
-extractBH_meta = function (computer_data_path, filedir, filename, verbose=TRUE) {
+extract_meta = function (computer_data_path, filedir, filename, verbose=TRUE) {
     
     # Convert the filename in vector
     filename = c(filename)
@@ -177,7 +231,7 @@ extractBH_meta = function (computer_data_path, filedir, filename, verbose=TRUE)
             
             # Concatenate by raw data frames created by this function when filename correspond to only one filename
             df_meta = rbind(df_meta,
-                            extractBH_meta(computer_data_path, 
+                            extract_meta(computer_data_path, 
                                            filedir, 
                                            f))
         }
@@ -192,7 +246,7 @@ extractBH_meta = function (computer_data_path, filedir, filename, verbose=TRUE)
     
     # Print metadata if asked
     if (verbose) {
-        print(paste("extraction of BH info for file :", filename))
+        print(paste("extraction of BH meta for file :", filename))
     }
 
     # Get the file path to the data
@@ -221,7 +275,6 @@ extractBH_meta = function (computer_data_path, filedir, filename, verbose=TRUE)
                    QME=iQME[trimws(substr(metatxt[26], 74, 74))],
                    QHE=iQHE[trimws(substr(metatxt[26], 76, 76))],
                    file_path=file_path,
-                   source='BH'
                    )
 
         df_meta$region_hydro = iRegHydro[substr(df_meta$code, 1, 1)]
@@ -235,14 +288,14 @@ extractBH_meta = function (computer_data_path, filedir, filename, verbose=TRUE)
 }
 
 # Example
-# df_meta = extractBH_meta(
+# df_meta = extract_meta(
     # "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
     # '',
     # c('H5920011_HYDRO_QJM.txt', 'K4470010_HYDRO_QJM.txt'))
 
 
 # Extraction of data
-extractBH_data = function (computer_data_path, filedir, filename, verbose=TRUE) {
+extract_data = function (computer_data_path, filedir, filename, verbose=TRUE) {
     
     # Convert the filename in vector
     filename = c(filename)
@@ -280,7 +333,7 @@ extractBH_data = function (computer_data_path, filedir, filename, verbose=TRUE)
 
             # Concatenate by raw data frames created by this function when filename correspond to only one filename
             df_data = rbind(df_data,
-                            extractBH_data(computer_data_path, 
+                            extract_data(computer_data_path, 
                                            filedir, 
                                            f))
         }
@@ -311,7 +364,7 @@ extractBH_data = function (computer_data_path, filedir, filename, verbose=TRUE)
                              skip=41)   
 
         # Extract all the metadata for the station
-        df_meta = extractBH_meta(computer_data_path, filedir, filename, verbose=FALSE)
+        df_meta = extract_meta(computer_data_path, filedir, filename, verbose=FALSE)
         # Get the code of the station
         code = df_meta$code
         # Create a tibble with the date as Date class and the code of the station
@@ -330,7 +383,7 @@ extractBH_data = function (computer_data_path, filedir, filename, verbose=TRUE)
 }
 
 # Example
-# df_data = extractBH_data(
+# df_data = extract_data(
 #     "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
 #     '',
 #     c('H5920011_HYDRO_QJM.txt', 'K4470010_HYDRO_QJM.txt'))
diff --git a/script.R b/script.R
index d45154a..d7763d0 100644
--- a/script.R
+++ b/script.R
@@ -15,57 +15,53 @@ computer_work_path =
 
 ### BANQUE HYDRO ###
 # Path to the directory where BH data is stored
-BHfiledir = 
-    ""
-    # "BanqueHydro_Export2021"
+filedir = 
+    # ""
+    "BanqueHydro_Export2021"
+
+
+### MANUAL SELECTION ###
+# Name of the file that will be analysed from the AG directory
+filename =
+    # ""
 
-## Manual selection ##
-# Name of the file that will be analysed from the BH directory
-BHfilename =
-    ""
     # c(
       # "S2235610_HYDRO_QJM.txt", 
       # "P1712910_HYDRO_QJM.txt", 
       # "P0885010_HYDRO_QJM.txt",
-      # "A1000030_HYDRO_QJM.txt",
+      # "O5055010_HYDRO_QJM.txt",
       # "A2250310_HYDRO_QJM.txt"
       # )
 
-## Or list selection ##
-# Path to the list file of BH data that will be analysed
-BHlistdir = 
+    c("O3035210_HYDRO_QJM.txt",
+      "O3011010_HYDRO_QJM.txt",
+      "O1442910_HYDRO_QJM.txt")
+
+
+### AGENCE ADOUR GARONNE SELECTION ###
+# Path to the list file of AG data that will be analysed
+AGlistdir = 
     ""
 
-BHlistname = 
+AGlistname = 
     ""
     # "Liste-station_RRSE.docx" 
-    
-
-### NIVALE ###
-# Path to the directory where NV data is stored
-NVfiledir = 
-    # ""
-    "France207"
-
-# Name of the file that will be analysed from the NV directory
-NVfilename = 
-    # ""
-    "all"
 
 
+### NIVALE SELECTION ###
 # Path to the list file of metadata about station that will be analysed
 NVlistdir =
     ""
 
 NVlistname = 
-    # ""
-    "liste_bv_principaux_global.txt"
+    ""
+    # "nival_selection.txt"
 
 
 ### TREND ANALYSIS ###
 # Time period to analyse
 period_all = c("1800-01-01", "2019-12-31")
-period2 = c("1980-01-01", "2019-12-31")
+period2 = c("1968-01-01", "2019-12-31")
 
 
 ########################
@@ -76,8 +72,7 @@ period2 = c("1980-01-01", "2019-12-31")
 setwd(computer_work_path)
 
 # Sourcing R file
-source('processing/extractBH.R', encoding='latin1')
-source('processing/extractNV.R', encoding='latin1')
+source('processing/extract.R', encoding='latin1')
 source('processing/format.R', encoding='latin1')
 source('processing/analyse.R', encoding='latin1')
 source('plotting/panel.R', encoding='latin1')
@@ -101,12 +96,19 @@ if (!(file.exists(figdir))) {
 print(paste('figdir :', figdir))
 
 
-# BANQUE HYDRO #
-# Get only the selected station from a list station file
-if (BHlistname != ""){
-    df_selec = get_selection(computer_data_path, 
-                             BHlistdir,
-                             BHlistname,
+df_data_AG = NULL
+df_data_NV = NULL
+df_meta_AG = NULL
+df_meta_NV = NULL
+
+
+# AGENCE ADOUR GARONNE SELECTION #
+if (AGlistname != ""){
+    
+    # Get only the selected station from a list station file
+    df_selec_AG = get_selection_AG(computer_data_path, 
+                             AGlistdir,
+                             AGlistname,
                              cnames=c('code',
                                       'station', 
                                       'BV_km2',
@@ -117,28 +119,45 @@ if (BHlistname != ""){
                              c_num=c('BV_km2',
                                       'longueur_serie'))
 
-    BHfilename = df_selec[df_selec$ok,]$filename
+    filename = df_selec_AG[df_selec_AG$ok,]$filename
+
+    # Extract metadata about selected stations
+    df_meta_AG = extract_meta(computer_data_path, filedir, filename)
+
+    # Extract data about selected stations
+    df_data_AG = extract_data(computer_data_path, filedir, filename)
 }
 
+# NIVALE SELECTION #
+if (NVlistname != ""){
+    
+    # Get only the selected station from a list station file
+    df_selec_NV = get_selection_NV(computer_data_path, 
+                                   NVlistdir,
+                                   NVlistname)
 
-# Extract metadata about selected stations
-df_meta_BH = extractBH_meta(computer_data_path, BHfiledir, BHfilename)
+    filename = df_selec_NV[df_selec_NV$ok,]$filename
 
-# Extract data about selected stations
-df_data_BH = extractBH_data(computer_data_path, BHfiledir, BHfilename)
+    # Extract metadata about selected stations
+    df_meta_NV = extract_meta(computer_data_path, filedir, filename)
 
+    # Extract data about selected stations
+    df_data_NV = extract_data(computer_data_path, filedir, filename)
+} 
 
-# NIVALE #
-# Extract metadata about selected stations
-df_meta_NV = extractNVlist_meta(computer_data_path, NVfiledir, NVlistdir, NVlistname)
-### /!\ missing station info ###
+# MANUAL SELECTION #
+if (AGlistname == "" & NVlistname == "") {
+    
+    # Extract metadata about selected stations
+    df_meta_AG = extract_meta(computer_data_path, filedir, filename)
 
-# Extract data about selected stations
-df_data_NV = extractNV_data(computer_data_path, NVfiledir, NVfilename)
+    # Extract data about selected stations
+    df_data_AG = extract_data(computer_data_path, filedir, filename)
+}
 
 
 # JOIN #
-df_join = join(df_data_BH, df_data_NV, df_meta_BH, df_meta_NV)
+df_join = join(df_data_AG, df_data_NV, df_meta_AG, df_meta_NV)
 df_data = df_join$data
 df_meta = df_join$meta
 
-- 
GitLab