From 0013da4b06d4037e1a2f42c150dfbc18788576e1 Mon Sep 17 00:00:00 2001
From: "louis.heraut" <louis.heraut@inrae.fr>
Date: Wed, 10 Nov 2021 14:49:29 +0100
Subject: [PATCH] New structure

---
 README.txt             |  14 +--
 init.R                 |  33 -------
 plotting/panel.R       |  44 ++++-----
 processing/.#extract.R |   1 -
 processing/extract.R   | 209 +++++++++++++++++++++++++----------------
 processing/read.R      |   0
 script_analyse.R       |  24 -----
 script_new_user.R      |  44 ---------
 script_plot.R          |  37 --------
 9 files changed, 145 insertions(+), 261 deletions(-)
 delete mode 100644 init.R
 delete mode 120000 processing/.#extract.R
 delete mode 100644 processing/read.R
 delete mode 100644 script_analyse.R
 delete mode 100644 script_new_user.R
 delete mode 100644 script_plot.R

diff --git a/README.txt b/README.txt
index 304328f..9a49439 100644
--- a/README.txt
+++ b/README.txt
@@ -1,15 +1,7 @@
 
-install argparser
-"""
-$ git clone https://bitbucket.org/djhshih/argparser.git
-$ cd argparser
-$ R
 
-R> library(roxygen2)
-R> roxygenize()
-R> quit()
+tools
 
-$ R CMD INSTALL .
-"""
+install_github("https://github.com/benRenard/BFunk")
 
-library('here')
+install.packages("ggplot2")
diff --git a/init.R b/init.R
deleted file mode 100644
index ef9d558..0000000
--- a/init.R
+++ /dev/null
@@ -1,33 +0,0 @@
-#' Initialization of global environnemental variable
-#' 
-#' @param x A number.
-#' @param y A number.
-#' @return The sum of \code{x} and \code{y}.
-#' @examples
-#' add(1, 1)
-#' add(10, 1)
-
-
-# Computer informations
-data_path = c(louis="/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
-              other='...')
-work_path = c(louis="/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/ASH",
-              other='.../ASH')
-
-user = Sys.getenv('USER')
-
-# Path to the data
-computer_data_path = data_path[user]
-print(paste('computer_data_path :', computer_data_path))
-
-# Work path
-computer_work_path = work_path[user]
-setwd(computer_work_path)
-print(paste('computer_work_path :', computer_work_path))
-
-# Output directory
-res_dir = file.path(computer_work_path, 'results')
-print(paste('res_dir :', res_dir))
-fig_dir = file.path(computer_work_path, 'figures')
-print(paste('fig_dir :', fig_dir))
-print('')
diff --git a/plotting/panel.R b/plotting/panel.R
index 0c84294..d614239 100644
--- a/plotting/panel.R
+++ b/plotting/panel.R
@@ -1,37 +1,25 @@
-library(here)
-
-source(file.path(dirname(here()), 'init.R'))
-source(file.path(dirname(here()), 'processing', 'extract.R'))
-
+library(ggplot2)
 
 # Time panel
-panel = function (filedir, filename, pan) {
+panel = function (df_data, df_info, figdir, filedir, span=Inf) {
+        
+    # If there is not a dedicated figure directory it creats one
+    outdir = file.path(figdir, filedir, paste('span_', as.character(span), '_years', sep=''))
+    if (!(file.exists(outdir))) {
+        dir.create(outdir)
+    }
     
-    file_path = file.path(computer_data_path, filedir, filename)
+    # Create name for the figure file
+    outfile = paste('Panel_', df_info$code, '.png', sep='')
     
-    for (p in pan) {
-        # If there is not a dedicated figure directory it creats one
-        out_dir = file.path(res_dir, filedir)
-        if (!(file.exists(out_dir))) {
-            dir.create(out_dir)
-        }
-        
-        # Get info
-        info = extract_info(filedir, filename)
-
-        # Create name for the figure file
-        out_file = paste('Panel_', info$code, '.png', sep='')
-
-        # Extract data
-        data = extract_data(filedir, filename)
-        
+    plot = 
+        ggplot(df_data, aes(x=Date, y=Qls)) +
+        geom_line()
 
 
+ 
+    return (plot)
+} 
 
 
-    }
- 
-    return (NA)
-}
 
-# panel('', 'H5920011_HYDRO_QJM.txt')
diff --git a/processing/.#extract.R b/processing/.#extract.R
deleted file mode 120000
index c0b140a..0000000
--- a/processing/.#extract.R
+++ /dev/null
@@ -1 +0,0 @@
-louis@botan.31363:1636457477
\ No newline at end of file
diff --git a/processing/extract.R b/processing/extract.R
index 067a91e..e7c593d 100644
--- a/processing/extract.R
+++ b/processing/extract.R
@@ -1,77 +1,84 @@
-library(here)
 library(tools)
-source(file.path(dirname(here()), 'init.R'))
 
+# General information on station
+iStatut = c('0'='inconnu', 
+            '1'='station avec signification hydrologique', 
+            '2'='station sans signification hydrologique', 
+            '3'="station d'essai")
+
+iFinalite = c('0'='inconnue', 
+              '1'="hydrométrie générale", 
+              '2'='alerte de crue', 
+              '3'="hydrométrie générale et alerte de crue",
+              '4'="gestion d'ouvrage", 
+              '5'='police des eaux', 
+              '6'="suivi d'étiage", 
+              '7'='bassin expérimental', 
+              '8'='drainage')
+
+iType = c('0'='inconnu',
+          '1'='une échelle',
+          '2'='deux échelles, station mère',
+          '3'='deux échelles, station fille',
+          '4'='débits mesurés',
+          '5'='virtuelle')
+
+iInfluence = c('0'='inconnue',
+               '1'='nulle ou faible',
+               '2'='en étiage seulement',
+               '3'='forte en toute saison')
+
+iDebit = c('0'='reconstitué',
+           '1'="réel (prise en compte de l'eau rajoutée ou retirée du bassin selon aménagements)",
+           '2'='naturel')
+
+iQBE = c('0'='qualité basses eaux inconnue',
+         '1'='qualité basses eaux bonne',
+         '2'='qualité basses eaux douteuse')
+
+iQME = c('0'='qualité moyennes eaux inconnue',
+         '1'='qualité moyennes eaux bonne',
+         '2'='qualité moyennes eaux douteuse')
+
+iQHE = c('0'='qualité hautes eaux inconnue',
+         '1'='qualité hautes eaux bonne',
+         '2'='qualité hautes eaux douteuse')
 
-# Extraction of data
-extract_data = function (filedir, filename) {
-    
-    data_path = file.path(computer_data_path, filedir, filename)
-
-    data = read.table(data_path,
-                      header=TRUE,
-                      na.strings=c('-99.000', '-99'),
-                      sep=';',
-                      skip=41)[,1:2] 
 
-    return (data=data)
-}
+# Extraction of information
+extract_info = function (data_path, filedir, filename) {
 
-# data = extract_data('', 'H5920011_HYDRO_QJM.txt')
+    filename = c(filename)
+    
+    if (all(filename == 'all') | length(filename) > 1) {
+        if (all(filename == 'all')) {
+            filelist = c()
+            filelist_tmp = list.files(file.path(data_path,
+                                                filedir))
+            for (f in filelist_tmp) {
+                if (file_ext(f) == 'txt') {
+                    filelist = c(filelist, f) 
+                }
+            }
+        } else if (length(filename > 1)) {
+            filelist = filename
+        } 
+        df_info = data.frame()
+        for (f in filelist) {
+            df_info = rbind(df_info,
+                            extract_info(data_path, 
+                                         filedir, 
+                                         f)
+                            )
+        }
+        rownames(df_info) = NULL
+        return (df_info)
+    }
 
+    filename = filename[1]    
 
-# Extraction of information
-extract_info = function (filedir, filename) {
-    
-    data_path = file.path(computer_data_path, filedir, filename)
-
-    # General information on station
-    iStatut = c('0'='inconnu', 
-                '1'='station avec signification hydrologique', 
-                '2'='station sans signification hydrologique', 
-                '3'="station d'essai")
-
-    iFinalite = c('0'='inconnue', 
-                  '1'="hydrométrie générale", 
-                  '2'='alerte de crue', 
-                  '3'="hydrométrie générale et alerte de crue",
-                  '4'="gestion d'ouvrage", 
-                  '5'='police des eaux', 
-                  '6'="suivi d'étiage", 
-                  '7'='bassin expérimental', 
-                  '8'='drainage')
-
-    iType = c('0'='inconnu',
-              '1'='une échelle',
-              '2'='deux échelles, station mère',
-              '3'='deux échelles, station fille',
-              '4'='débits mesurés',
-              '5'='virtuelle')
-
-    iInfluence = c('0'='inconnue',
-                   '1'='nulle ou faible',
-                   '2'='en étiage seulement',
-                   '3'='forte en toute saison')
-
-    iDebit = c('0'='reconstitué',
-               '1'="réel (prise en compte de l'eau rajoutée ou retirée du bassin selon aménagements)",
-               '2'='naturel')
-
-    iQBE = c('0'='qualité basses eaux inconnue',
-             '1'='qualité basses eaux bonne',
-             '2'='qualité basses eaux douteuse')
-
-    iQME = c('0'='qualité moyennes eaux inconnue',
-             '1'='qualité moyennes eaux bonne',
-             '2'='qualité moyennes eaux douteuse')
-
-    iQHE = c('0'='qualité hautes eaux inconnue',
-             '1'='qualité hautes eaux bonne',
-             '2'='qualité hautes eaux douteuse')
-
-    data_path = file.path(computer_data_path, filedir, filename)
-
-    infotxt = c(readLines(data_path, n=41))
+    file_path = file.path(data_path, filedir, filename)
+    infotxt = c(readLines(file_path, n=41))
     
     info = list(code=trimws(substr(infotxt[11], 38, nchar(infotxt[11]))),
                 nom=trimws(substr(infotxt[12], 39, nchar(infotxt[12]))),
@@ -88,31 +95,67 @@ extract_info = function (filedir, filename) {
                 QME=iQME[trimws(substr(infotxt[26], 74, 74))],
                 QHE=iQHE[trimws(substr(infotxt[26], 76, 76))]
                 )
- 
-    return (info=info)
+    
+    df_info = data.frame(info, file_path=file_path)
+    
+    return (df_info)
 }
 
-# info = extract_info('', 'H5920011_HYDRO_QJM.txt')
+# df_info = extract_info(
+#     "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
+#     '',
+#     c('H5920011_HYDRO_QJM.txt', 'K4470010_HYDRO_QJM.txt'))
 
 
-# Extraction and dataframing
-extract_df = function (filedir, filename) {
+# Extraction of data
+extract_data = function (data_path, filedir, filename) {
     
-    if (filename == 'all') {
-        filelist = c()
-        for (f in list.files()) {
-            if (file_ext() == 'txt') {
-               filelist = c(filelist, f) 
+    filename = c(filename)
+
+    if (all(filename == 'all') | length(filename) > 1) {
+        if (all(filename == 'all')) {
+            filelist = c()
+            filelist_tmp = list.files(file.path(data_path,
+                                                filedir))
+            for (f in filelist_tmp) {
+                if (file_ext(f) == 'txt') {
+                    filelist = c(filelist, f) 
+                }
             }
-            
+        } else if (length(filename > 1)) {
+            filelist = filename
+        } 
+        df_data = data.frame()
+        for (f in filelist) {
+            df_data = rbind(df_data,
+                            extract_data(data_path, 
+                                         filedir, 
+                                         f)
+                            )
         }
+        rownames(df_data) = NULL
+        return (df_data)
     }
+
+    filename = filename[1]
+
+    file_path = file.path(data_path, filedir, filename)
     
-    data_path = file.path(computer_data_path, filedir, filename)
-    info = extract_info(filedir, filename)
-    df = data.frame(info, data_path=data_path)
+    df_data = read.table(file_path,
+                         header=TRUE,
+                         na.strings=c('     -99'),
+                         sep=';',
+                         skip=41)[,1:2]
     
-    return (df)
+    df_info = extract_info(data_path, filedir, filename)
+    code = df_info$code
+    
+    df_data = cbind(df_data, code=code)
+
+    return (df_data)
 }
 
-# df = extract_df('', 'H5920011_HYDRO_QJM.txt')
+# df_data = extract_data(
+    # "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data",
+    # '',
+    # c('H5920011_HYDRO_QJM.txt', 'K4470010_HYDRO_QJM.txt'))
diff --git a/processing/read.R b/processing/read.R
deleted file mode 100644
index e69de29..0000000
diff --git a/script_analyse.R b/script_analyse.R
deleted file mode 100644
index c92f20e..0000000
--- a/script_analyse.R
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env Rscript
-
-library(here)
-library(argparser, quietly=TRUE)
-
-source(file.path(dirname(here()), 'init.R'))
-
-# Create a parser
-p = arg_parser("Analyse data.")
-
-# Add command line arguments
-p = add_argument(p, '--filedir',
-                 type="character",
-                 help="Directory(ies) in the data directory to search for data.")
-
-p = add_argument(p, '--filename',
-                 type="character",
-                 help="Filename(s) in the data directory to search for data. If 'all', the whole directory(ies) will used.")
-
-
-# Parse the command line arguments
-argv = parse_args(p)
-
-# Run approriate function
diff --git a/script_new_user.R b/script_new_user.R
deleted file mode 100644
index 50302d3..0000000
--- a/script_new_user.R
+++ /dev/null
@@ -1,44 +0,0 @@
-#!/usr/bin/env Rscript
-
-library(here)
-library(argparser, quietly=TRUE) 
-
-setwd(here())
-
-# Create a parser
-p = arg_parser("Add a new user to the environment with is work and data path.")
-
-# Add command line arguments
-p = add_argument(p, '--data_dir',
-                 type="character",
-                 help="Data directory.")
-
-p = add_argument(p, '--work_dir',
-                 type="character",
-                 help="Working directory.")
-
-# Parse the command line arguments
-argv = parse_args(p)
-
-# Create path file
-user = Sys.getenv('USER')
-computer_data_path = c(argv$data_dir)
-computer_work_path = c(argv$work_dir)
-
-if (file.exists('path.txt')) {
-     computer_path = read.table('path.txt', sep=';')
-     if (!any(computer_path$user == user)) {
-         computer_path = rbind(computer_path, c(user, 
-                                                computer_data_path,
-                                                computer_work_path))
-         } else {
-             stop("User already register")            
-         }
-} else {
-    computer_path = data.frame(user=user,
-                               computer_data_path=computer_data_path,
-                               computer_work_path=computer_work_path)
-    print(paste('New user:', user, sep=' '))
-}
-
-write.table(computer_path, file='path.txt', sep=';')
diff --git a/script_plot.R b/script_plot.R
deleted file mode 100644
index 46bd851..0000000
--- a/script_plot.R
+++ /dev/null
@@ -1,37 +0,0 @@
-#!/usr/bin/env Rscript
-
-library(here)
-library(argparser, quietly=TRUE)
-
-source(file.path(here(), 'init.R'))
-
-# Create a parser
-p = arg_parser("Plot data.")
-
-# Add command line arguments
-p = add_argument(p, '--filedir',
-                 type="character",
-                 help="Directory(ies) in the data directory to search for data.")
-
-p = add_argument(p, '--filename',
-                 type="character",
-                 help="Filename(s) in the data directory to search for data. If 'all', the whole directory(ies) will used.")
-
-p = add_argument(p, '--panel',
-                 type="numeric",
-                 nargs=Inf,
-                 help="Time span in day of simple time panel.")
-
-# Parse the command line arguments
-argv = parse_args(p)
-
-# Run approriate function
-
-print(argv$panel)
-
-if (any(!is.na(argv$panel))) {
-    for (pan in argv$panel) {
-        print('aaa')
-
-    }
-}
-- 
GitLab