Commit 0013da4b authored by Heraut Louis's avatar Heraut Louis
Browse files

New structure

parent 82e35e88
No related merge requests found
Showing with 145 additions and 261 deletions
+145 -261
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")
#' 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('')
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')
louis@botan.31363:1636457477
\ No newline at end of file
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'))
#!/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
#!/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=';')
#!/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')
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment