diff --git a/plotting/panel.R b/plotting/panel.R index ba163204dbdc2e337b6aa46402188d5132f23941..fdd8723559f5f407d87535b78623c683677c6800 100644 --- a/plotting/panel.R +++ b/plotting/panel.R @@ -35,6 +35,7 @@ panel = function (df_data, df_info, figdir, filedir, is_sqrt=FALSE) { print(paste("Plotting for sation :", code)) df_data_code = df_data[df_data$code==code,] + # df_data_code_NoNA = df_data_code[!is.na(df_data_code$Qm3s),] # Plot p = ggplot(df_data_code, aes(x=Date, y=Qm3s)) + diff --git a/processing/analyse.R b/processing/analyse.R index b7441a6443d7a9a3c3687a8c264a4de90094e428..d5fcbe7feec437b3eeeb9bf5c832a1976726ac31 100644 --- a/processing/analyse.R +++ b/processing/analyse.R @@ -1,5 +1,5 @@ # Usefull library -# library(StatsAnalysisTrend) +library(StatsAnalysisTrend) library(dplyr) @@ -57,3 +57,11 @@ get_lacune = function (df_data, df_info) { return (df_lac) } + + + + + + + + diff --git a/processing/extractBH.R b/processing/extractBH.R index 2c841d4c5f4f5ad87858825c7662819d3540d477..09ea390447fabbe5d2efc24fd513c338ce601981 100644 --- a/processing/extractBH.R +++ b/processing/extractBH.R @@ -164,7 +164,7 @@ extractBH_info = function (computer_data_path, filedir, filename, verbose=TRUE) file_path = file.path(computer_data_path, filedir, filename) - if (file.exists(file_path)) { + if (file.exists(file_path) & substr(file_path, nchar(file_path), nchar(file_path)) != '/') { # Extract all the header infotxt = c(readLines(file_path, n=41)) @@ -185,7 +185,8 @@ extractBH_info = function (computer_data_path, filedir, filename, verbose=TRUE) QBE=iQBE[trimws(substr(infotxt[26], 72, 72))], QME=iQME[trimws(substr(infotxt[26], 74, 74))], QHE=iQHE[trimws(substr(infotxt[26], 76, 76))], - file_path=file_path + file_path=file_path, + source='BH' ) return (df_info) @@ -263,7 +264,7 @@ extractBH_data = function (computer_data_path, filedir, filename, verbose=TRUE) # Get the file path to the data file_path = file.path(computer_data_path, filedir, filename) - if (file.exists(file_path)) { + if (file.exists(file_path) & substr(file_path, nchar(file_path), nchar(file_path)) != '/') { # Extract the data as a data frame df_data = read.table(file_path, diff --git a/processing/extractNV.R b/processing/extractNV.R index 72abcd0d479bcca31dba1215bcdbd8837d603e1b..8d1f94ee3b110d281e17f1d97a87639e8ee89ae5 100644 --- a/processing/extractNV.R +++ b/processing/extractNV.R @@ -11,54 +11,62 @@ extractNVlist_info = function (computer_data_path, filedir, listdir, listname, v print(paste("extraction of NV info for file :", 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_info = read.table(list_path, - header=TRUE) - - # Create a filelist to store all the filename - codelist = c() - - dir_path = file.path(computer_data_path, filedir) - - # Get all the filename in the data directory selected - filelist_tmp = list.files(dir_path) + # Get the file path to the list of station + list_path = file.path(computer_data_path, listdir, listname) - # For all the filename in the directory selected - for (f in filelist_tmp) { - # If the filename extention is 'txt' - if (file_ext(f) == 'txt') { - # Store the filename in the filelist - codelist = c(codelist, gsub('.txt', '', f)) - } - } + if (file.exists(list_path) & substr(list_path, nchar(list_path), nchar(list_path)) != '/') { - exist = df_info$CODE %in% codelist - missing = codelist[!(codelist %in% df_info$CODE)] - print(paste('missing station info for :', missing)) - - df_info = df_info[exist,] - - # Create a tibble with all the information needed - df_info = - tibble(code=as.character(df_info$CODE), - nom=as.character(df_info$NOM), - L93X=df_info$X_L2E, - L93Y=df_info$Y_L2E, - surface_km2=df_info$S_km2, - altitude_m=df_info$Alt, - file_path=file.path(dir_path, - paste(df_info$CODE, '.txt', sep='')) - ) - - df_info = bind_rows(df_info, - data.frame(code=missing, - file_path=file.path(dir_path, - paste(missing, - '.txt', - sep='')))) + # Extract the data as a data frame + df_info = read.table(list_path, + header=TRUE) + + # Create a filelist to store all the filename + codelist = c() + + dir_path = file.path(computer_data_path, filedir) + + # Get all the filename in the data directory selected + filelist_tmp = list.files(dir_path) + + # For all the filename in the directory selected + for (f in filelist_tmp) { + # If the filename extention is 'txt' + if (file_ext(f) == 'txt') { + # Store the filename in the filelist + codelist = c(codelist, gsub('.txt', '', f)) + } + } + + exist = df_info$CODE %in% codelist + missing = codelist[!(codelist %in% df_info$CODE)] + print(paste('missing station info for :', missing)) + + df_info = df_info[exist,] + + # Create a tibble with all the information needed + df_info = + tibble(code=as.character(df_info$CODE), + nom=as.character(df_info$NOM), + L93X=df_info$X_L2E, + L93Y=df_info$Y_L2E, + surface_km2=df_info$S_km2, + altitude_m=df_info$Alt, + file_path=file.path(dir_path, + paste(df_info$CODE, '.txt', sep='')), + source='NV' + ) + + df_info = bind_rows(df_info, + data.frame(code=missing, + file_path=file.path(dir_path, + paste(missing, + '.txt', + sep='')))) + + } else { + print(paste('filename', list_path, 'do not exist')) + return (NULL) + } return (df_info) } @@ -131,7 +139,7 @@ extractNV_data = function (computer_data_path, filedir, filename, verbose=TRUE) # Get the file path to the data file_path = file.path(computer_data_path, filedir, filename) - if (file.exists(file_path)) { + if (file.exists(file_path) & substr(file_path, nchar(file_path), nchar(file_path)) != '/') { # Extract the data as a data frame df_data = read.table(file_path, diff --git a/processing/prepare.R b/processing/prepare.R new file mode 100644 index 0000000000000000000000000000000000000000..d61ddedd0a50048cdb1db855b93481d226b18b1a --- /dev/null +++ b/processing/prepare.R @@ -0,0 +1,13 @@ + + +prepare = function(df_data, df_info) { + + df_data$Gcode = as.integer(factor(df_data$code)) + df_data = tibble(Date=df_data$Date, + Qm3s=df_data$Qm3s, + Gcode=df_data$code) + + df_info$Gcode = as.integer(factor(df_info$code)) + + return (list(data=df_data, info=df_info)) +} diff --git a/script.R b/script.R index 960d7266b9a576f7777575a9e93a1ab8fea1af07..8ea2ea69bea6dddfad8d5a0c091235bbaaa9ffbc 100644 --- a/script.R +++ b/script.R @@ -17,41 +17,45 @@ computer_work_path = # Path to the directory where BH data is stored BHfiledir = # "test" + # "" "BanqueHydro_Export2021" ## Manual selection ## # Name of the file that will be analysed from the BH directory BHfilename = - FALSE + "" # c("H5920011_HYDRO_QJM.txt", "K4470010_HYDRO_QJM.txt") # "all" ## Or list selection ## # Path to the list file of BH data that will be analysed BHlistdir = - # FALSE "" BHlistname = - # FALSE + # "" "Liste-station_RRSE.docx" ### NIVALE ### # Path to the directory where NV data is stored NVfiledir = - "France207" + "" + # "France207" # Name of the file that will be analysed from the NV directory NVfilename = - "all" + "" + # "all" + # Path to the list file of information about station that will be analysed NVlistdir = "" NVlistname = - "liste_bv_principaux_global.txt" + "" + # "liste_bv_principaux_global.txt" ######################## @@ -63,6 +67,7 @@ setwd(computer_work_path) # Sourcing R file source('processing/extractBH.R') source('processing/extractNV.R') +source('processing/prepare.R') source('processing/analyse.R') source('plotting/panel.R') @@ -86,7 +91,7 @@ print(paste('figdir :', figdir)) # BANQUE HYDRO # # Get only the selected station from a list station file -if (is.character(BHlistdir) & is.character(BHlistname) & is.character(BHfiledir)){ +if (BHlistname != ""){ df_selec = get_selection(computer_data_path, BHlistdir, BHlistname, @@ -113,25 +118,52 @@ df_data_BH = extractBH_data(computer_data_path, BHfiledir, BHfilename) # NIVALE # # Extract information about selected stations df_info_NV = extractNVlist_info(computer_data_path, NVfiledir, NVlistdir, NVlistname) -### /!\ missing station info +### /!\ missing station info ### # Extract data about selected stations df_data_NV = extractNV_data(computer_data_path, NVfiledir, NVfilename) - -common = levels(factor(df_info_NV[df_info_NV$code %in% df_info_BH$code,]$code)) -NVadd = levels(factor(df_info_NV[!(df_info_NV$code %in% df_info_BH$code),]$code)) - -df_info_NVadd = df_info_NV[df_info_NV$code %in% NVadd,] -df_info = full_join(df_info_BH, df_info_NVadd, by=c("code", "nom", "L93X", "L93Y", "surface_km2", "file_path")) - -df_data_NVadd = df_data_NV[df_data_NV$code %in% NVadd,] -df_data = full_join(df_data_BH, df_data_NVadd, by=c("Date", "Qm3s", "code")) +# JOIN # +if (!is.null(df_data_NV) & !is.null(df_data_BH)) { + + # Get the station in common + common = levels(factor(df_info_NV[df_info_NV$code %in% df_info_BH$code,]$code)) + # Get the Nv station to add + NVadd = levels(factor(df_info_NV[!(df_info_NV$code %in% df_info_BH$code),]$code)) + + # Select only the NV info to add + df_info_NVadd = df_info_NV[df_info_NV$code %in% NVadd,] + # Join NV data to BH data + df_info = full_join(df_info_BH, df_info_NVadd, by=c("code", "nom", "L93X", "L93Y", "surface_km2", "file_path")) + + # Select only the NV data to add + df_data_NVadd = df_data_NV[df_data_NV$code %in% NVadd,] + # Join NV info to BH info + df_data = full_join(df_data_BH, df_data_NVadd, by=c("Date", "Qm3s", "code")) + +} else if (is.null(df_data_NV) & !is.null(df_data_BH)) { + df_info = df_info_BH + df_data = df_data_BH +} else if (!is.null(df_data_NV) & is.null(df_data_BH)) { + df_info = df_info_NV + df_data = df_data_NV +} else { + stop('No data') +} +# TIME PANEL # # Plot time panel of debit by stations -# panel(df_data, df_info, figdir, filedir) -# panel(df_data, df_info, figdir, filedir, is_sqrt=TRUE) +# panel(df_data, df_info, figdir, "") +# panel(df_data, df_info, figdir, "", is_sqrt=TRUE) +### /!\ Removed 185 row(s) containing missing values (geom_path) -> remove NA ### + # Compute gap parameters for stations # df_lac = get_lacune(df_data, df_info) + +df_list = prepare(df_data, df_info) + + + +