diff --git a/plotting/layout.R b/plotting/layout.R index 9b36075641441d7f21305d7c62701e098e5c98e1..0ebbee1ab4fcd06819e0ba5029512d4e0a3089b7 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -215,6 +215,12 @@ datasheet_layout = function (df_data, df_meta, layout_matrix, var = replicate(nbp, var) }} + if (all(class(glose) != 'list')) { + glose = list(glose) + if (length(glose) == 1) { + glose = replicate(nbp, glose) + }} + if (all(class(type) != 'list')) { type = list(type) if (length(type) == 1) { @@ -238,6 +244,7 @@ datasheet_layout = function (df_data, df_meta, layout_matrix, alpha=alpha[[i]], unit2day=unit2day[[i]], var=var[[i]], type=type[[i]], + glose=glose[[i]], missRect=missRect[[i]]) # Stores it list_df2plot[[i]] = df2plot diff --git a/plotting/map.R b/plotting/map.R index 603ea24d01955f96de816e5e24323995ed441f69..1c3ae24dcfecaa7ef4083f0bf95c362af46d2092 100644 --- a/plotting/map.R +++ b/plotting/map.R @@ -315,6 +315,8 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, var = list_df2plot[[i]]$var # Extracts the type of variable of the plot type = list_df2plot[[i]]$type + # Explanations about the variable + glose = list_df2plot[[i]]$glose # Createsa name for the map if (j > 1) { @@ -404,7 +406,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, 390000) yBasin = c(6280000, 6290000, 6320000, 6385000, 6450000, 6530000, - 6360000) + 6361000) nameBasin = c('Adour', 'Garonne', 'Tarn-Aveyron', 'Lot', 'Dordogne', 'Charente', 'Fleuves-\nCôtiers') @@ -714,19 +716,59 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, plot_palette = tibble(xtick=xtick, ytick=ytick, colTick=colTick, labTick=labTick) + + nbLine = as.integer(nchar(glose)/40) + 1 + + + nbNewline = 0 + + nbLim = 40 + gloseName = glose + nbChar = nchar(gloseName) + while (nbChar > nbLim) { + nbNewline = nbNewline + 1 + posSpace = which(strsplit(gloseName, "")[[1]] == " ") + idNewline = which.min(abs(posSpace - nbLim * nbNewline)) + posNewline = posSpace[idNewline] + gloseName = paste(substring(gloseName, + c(1, posNewline + 1), + c(posNewline, + nchar(gloseName))), + collapse="\n") + Newline = substr(gloseName, + posNewline + 2, + nchar(gloseName)) + nbChar = nchar(Newline) + } + + Yline = 0.6 + 0.47*nbNewline + Ytitle = Yline + 0.15 + # New plot with void theme title = ggplot() + theme_void() + - # Plots separation line + # Plots separation lines geom_line(aes(x=c(-0.3, 3.7), y=c(0.05, 0.05)), size=0.6, color="#00A3A8") + + geom_line(aes(x=c(-0.3, 3.7), y=c(Yline, Yline)), + size=0.6, color="#00A3A8") + # Writes title - geom_shadowtext(data=tibble(x=-0.3, y=0.2, + geom_shadowtext(data=tibble(x=-0.3, y=Ytitle, label=var), aes(x=x, y=y, label=label), fontface="bold", color="#00A3A8", bg.colour="white", hjust=0, vjust=0, size=10) + + + # Writes title + geom_shadowtext(data=tibble(x=-0.3, y=0.2, + label=gloseName), + aes(x=x, y=y, label=label), + fontface="bold", + color="#00A3A8", + bg.colour="white", + hjust=0, vjust=0, size=3) + + # X axis scale_x_continuous(limits=c(-0.3, 1 + 3), expand=c(0, 0)) + diff --git a/processing/extract.R b/processing/extract.R index 07cb39fa816cec0ad3d2988d4290e59fb8a68ffe..ef5c105bee491d2ffd042272cf6990b1d99dbe98 100644 --- a/processing/extract.R +++ b/processing/extract.R @@ -130,7 +130,7 @@ iRegHydro = c('D'='Affluents du Rhin', ### 2.1. Creation of selection # Create a txt file that resume all the station data files present # in a filedir -create_selection = function (computer_data_path, filedir, outname) { +create_selection = function (computer_data_path, filedir, outname, optname='_HYDRO_QJM') { # Out file for store results outfile = file.path(computer_data_path, outname) @@ -146,18 +146,24 @@ create_selection = function (computer_data_path, filedir, outname) { for (f in filelist_tmp) { # If the filename is a 'txt' file if (file_ext(f) == 'txt') { + # Extracts the station code + code = gsub("[^[:alnum:] ].*$", '', f) # Then the station code is stored - codelist = c(codelist, gsub('.txt', '', f)) + codelist = c(codelist, code) } } # Create a tibble to store the data to write df_file = tibble(code=codelist, - filename=paste(codelist, - '_HYDRO_QJM.txt', sep=''), + filename=paste(codelist, optname, '.txt', + sep=''), ok=TRUE) # Write the data in a txt file write.table(df_file, outfile, sep=";", col.names=TRUE, quote=FALSE) - return (NULL) + # Returns that it is done with the path + print('Done') + print(paste('path : ', outfile, sep='')) + print('example of file : ') + print(head(df_file)) } # Example # create_selection( @@ -167,7 +173,7 @@ create_selection = function (computer_data_path, filedir, outname) { ### 2.2. Agence de l'eau Adour-Garonne selection # Gets the selection of station from the 'Liste-station_RRSE.docx' file -get_selection_AG = function (computer_data_path, listdir, listname, +get_selection_AEAG = function (computer_data_path, listdir, listname, cnames=c('code','station', 'BV_km2', 'axe_principal_concerne', 'longueur_serie', 'commentaires', @@ -212,7 +218,7 @@ get_selection_AG = function (computer_data_path, listdir, listname, return (df_selec) } # Example -# df_selec_AG = get_selection_AG( +# df_selec_AEAG = get_selection_AEAG( # "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data", # "", # "Liste-station_RRSE.docx", @@ -228,7 +234,7 @@ get_selection_AG = function (computer_data_path, listdir, listname, ### 2.3. INRAE selection # Gets the selection of station from the selection txt file generated # by the 'create_selection' function -get_selection_IN = function (computer_data_path, listdir, listname) { +get_selection_INRAE = function (computer_data_path, listdir, listname) { # Gets the file path to the data list_path = file.path(computer_data_path, listdir, listname) @@ -244,7 +250,7 @@ get_selection_IN = function (computer_data_path, listdir, listname) { return (df_selec) } # Example -# df_selec_IN = get_selection_IN( +# df_selec_INRAE = get_selection_INRAE( # "/home/louis/Documents/bouleau/INRAE/CDD_stationnarite/data", # "", # "INRAE_selection.txt") diff --git a/processing/format.R b/processing/format.R index 316169a0f80cf0b853d61d704e8740ae87e6c7d1..175109e3532b00ff25ab5eb3c046d535c1cca660 100644 --- a/processing/format.R +++ b/processing/format.R @@ -37,42 +37,42 @@ library(Hmisc) ## 1. BEFORE TREND ANALYSE ### 1.1. Joining selection # Joins tibbles of different selection of station as a unique one -join = function (df_data_AG, df_data_IN, df_meta_AG, df_meta_IN) { +join = function (df_data_AEAG, df_data_INRAE, df_meta_AEAG, df_meta_INRAE) { # If there is an INRAE and an Agence de l'eau Adour-Garonne selection - if (!is.null(df_data_IN) & !is.null(df_data_AG)) { + if (!is.null(df_data_INRAE) & !is.null(df_data_AEAG)) { # Gets the station in common - common = levels(factor(df_meta_IN[df_meta_IN$code %in% df_meta_AG$code,]$code)) + common = levels(factor(df_meta_INRAE[df_meta_INRAE$code %in% df_meta_AEAG$code,]$code)) # Gets the Nv station to add - INadd = levels(factor(df_meta_IN[!(df_meta_IN$code %in% df_meta_AG$code),]$code)) + INRAEadd = levels(factor(df_meta_INRAE[!(df_meta_INRAE$code %in% df_meta_AEAG$code),]$code)) - # Selects only the IN meta to add - df_meta_INadd = df_meta_IN[df_meta_IN$code %in% INadd,] + # Selects only the INRAE meta to add + df_meta_INRAEadd = df_meta_INRAE[df_meta_INRAE$code %in% INRAEadd,] # Names the source of the selection - df_meta_AG$source = 'AG' - df_meta_INadd$source = 'IN' + df_meta_AEAG$source = 'AEAG' + df_meta_INRAEadd$source = 'INRAE' - # Joins IN data to AG data - df_meta = full_join(df_meta_AG, df_meta_INadd) + # Joins INRAE data to AEAG data + df_meta = full_join(df_meta_AEAG, df_meta_INRAEadd) - # Selects only the IN data to add - df_data_INadd = df_data_IN[df_data_IN$code %in% INadd,] - # Joins IN meta to AG meta - df_data = full_join(df_data_AG, df_data_INadd) + # Selects only the INRAE data to add + df_data_INRAEadd = df_data_INRAE[df_data_INRAE$code %in% INRAEadd,] + # Joins INRAE meta to AEAG meta + df_data = full_join(df_data_AEAG, df_data_INRAEadd) # If there is just an Agence de l'eau Adour-Garonne selection - } else if (is.null(df_data_IN) & !is.null(df_data_AG)) { - df_meta_AG$source = 'AG' - df_meta = df_meta_AG - df_data = df_data_AG + } else if (is.null(df_data_INRAE) & !is.null(df_data_AEAG)) { + df_meta_AEAG$source = 'AEAG' + df_meta = df_meta_AEAG + df_data = df_data_AEAG # If there is just an INRAE selection - } else if (!is.null(df_data_IN) & is.null(df_data_AG)) { - df_meta_IN$source = 'IN' - df_meta = df_meta_IN - df_data = df_data_IN + } else if (!is.null(df_data_INRAE) & is.null(df_data_AEAG)) { + df_meta_INRAE$source = 'INRAE' + df_meta = df_meta_INRAE + df_data = df_data_INRAE # If there is no selection } else { diff --git a/script.R b/script.R index d9952242776c3ec4f44b051e5151cb5a64aebbf9..20de3817c1e23a30cfe28728e94eddcca47a3f8a 100644 --- a/script.R +++ b/script.R @@ -55,40 +55,44 @@ filedir = # Name of the file that will be analysed from the BH directory # (if 'all', all the file of the directory will be chosen) filename = - # "" - c( + "" + "all" + # c( # "S2235610_HYDRO_QJM.txt", # "P1712910_HYDRO_QJM.txt", # "P0885010_HYDRO_QJM.txt", # "O5055010_HYDRO_QJM.txt", # "O0384010_HYDRO_QJM.txt", # "S4214010_HYDRO_QJM.txt", - "Q7002910_HYDRO_QJM.txt" + # "Q7002910_HYDRO_QJM.txt" # "Q0214010_HYDRO_QJM.txt" - # "O3035210_HYDRO_QJM.txt" + # "O3035210_HYDRO_QJM.txt", # "O0554010_HYDRO_QJM.txt", # "O1584610_HYDRO_QJM.txt" - ) + # ) ## AGENCE EAU ADOUR GARONNE SELECTION # Path to the 'docx' list file of station from the Agence de l'eau # Adour-Garonne that will be analysed -AGlistdir = +AEAGlistdir = "" -AGlistname = - "" - # "Liste-station_RRSE.docx" +AEAGlistname = + # "" + "Liste-station_RRSE.docx" ## NIVALE SELECTION # Path to the 'txt' list file of station from INRAE that will be analysed -INlistdir = +# Generated with : +# create_selection(computer_data_path, 'dirname', 'example.txt') +INRAElistdir = "" -INlistname = +INRAElistname = "" + # "example.txt" # "INRAE_selection.txt" @@ -177,18 +181,18 @@ rv_shpname = 'CoursEau_FXX.shp' ## 2. SELECTION OF STATION # Initialization of null data frame if there is no data selected -df_data_AG = NULL -df_data_IN = NULL -df_meta_AG = NULL -df_meta_IN = NULL +df_data_AEAG = NULL +df_data_INRAE = NULL +df_meta_AEAG = NULL +df_meta_INRAE = NULL ### 2.1. Selection of the Agence de l'eau Adour-Garonne -if (AGlistname != ""){ +if (AEAGlistname != ""){ # Get only the selected station from a list station file - df_selec_AG = get_selection_AG(computer_data_path, - AGlistdir, - AGlistname, + df_selec_AEAG = get_selection_AEAG(computer_data_path, + AEAGlistdir, + AEAGlistname, cnames=c('code', 'station', 'BV_km2', @@ -200,39 +204,39 @@ if (AGlistname != ""){ 'longueur_serie')) # Get filenames of the selection - filename = df_selec_AG[df_selec_AG$ok,]$filename + filename = df_selec_AEAG[df_selec_AEAG$ok,]$filename # Extract metadata about selected stations - df_meta_AG = extract_meta(computer_data_path, filedir, filename) + df_meta_AEAG = extract_meta(computer_data_path, filedir, filename) # Extract data about selected stations - df_data_AG = extract_data(computer_data_path, filedir, filename) + df_data_AEAG = extract_data(computer_data_path, filedir, filename) } ### 2.2. INRAE selection -if (INlistname != ""){ +if (INRAElistname != ""){ # Get only the selected station from a list station file - df_selec_IN = get_selection_IN(computer_data_path, - INlistdir, - INlistname) + df_selec_INRAE = get_selection_INRAE(computer_data_path, + INRAElistdir, + INRAElistname) # Get filenames of the selection - filename = df_selec_IN[df_selec_IN$ok,]$filename + filename = df_selec_INRAE[df_selec_INRAE$ok,]$filename # Extract metadata about selected stations - df_meta_IN = extract_meta(computer_data_path, filedir, filename) + df_meta_INRAE = extract_meta(computer_data_path, filedir, filename) # Extract data about selected stations - df_data_IN = extract_data(computer_data_path, filedir, filename) + df_data_INRAE = extract_data(computer_data_path, filedir, filename) } ### 2.3. Manual selection -if (AGlistname == "" & INlistname == "") { +if (AEAGlistname == "" & INRAElistname == "") { # Extract metadata about selected stations - df_meta_AG = extract_meta(computer_data_path, filedir, filename) + df_meta_AEAG = extract_meta(computer_data_path, filedir, filename) # Extract data about selected stations - df_data_AG = extract_data(computer_data_path, filedir, filename) + df_data_AEAG = extract_data(computer_data_path, filedir, filename) } ### 2.4. Data join -df_join = join(df_data_AG, df_data_IN, df_meta_AG, df_meta_IN) +df_join = join(df_data_AEAG, df_data_INRAE, df_meta_AEAG, df_meta_INRAE) df_data = df_join$data df_meta = df_join$meta @@ -318,40 +322,40 @@ df_shapefile = ini_shapefile(resources_path, ### 4.2. Analysis layout datasheet_layout(toplot=c( # 'datasheet', - # 'matrix', - 'map' + 'matrix' + # 'map' ), df_meta=df_meta, df_data=list( - # res_QAtrend$data, - # res_QMNAtrend$data, - # res_VCN10trend$data, - # res_tDEBtrend$data, + res_QAtrend$data, + res_QMNAtrend$data, + res_VCN10trend$data, + res_tDEBtrend$data, res_tCENtrend$data ), df_trend=list( - # res_QAtrend$trend, - # res_QMNAtrend$trend, - # res_VCN10trend$trend, - # res_tDEBtrend$trend, + res_QAtrend$trend, + res_QMNAtrend$trend, + res_VCN10trend$trend, + res_tDEBtrend$trend, res_tCENtrend$trend ), var=list( - # 'QA', - # 'QMNA', - # 'VCN10', - # 'tDEB', + 'QA', + 'QMNA', + 'VCN10', + 'tDEB', 'tCEN' ), type=list( - # 'sévérité', - # 'sévérité', - # 'sévérité', - # 'saisonnalité', + 'sévérité', + 'sévérité', + 'sévérité', + 'saisonnalité', 'saisonnalité' ), @@ -359,9 +363,9 @@ datasheet_layout(toplot=c( "Moyenne annuelle du débit journalier", "Minimum annuel de la moyenne menusuelle du débit journalier", "Minimum annuel de la moyenne sur 10 jours du débit journalier", - "Début d'étiage (jour de l'année de la première moyenne sur 10 sous le maximum des VCN10)", + "Début d'étiage (jour de l'année de la première moyenne sur 10 jours sous le maximum des VCN10)", "Centre d'étiage (jour de l'année du VCN10)" - ) + ), layout_matrix=matrix(c(1, 2, 3, 4, 5), ncol=1),