datasheet.R 59.17 KiB
# \\\
# Copyright 2021-2022 Louis Héraut*1,
#                     Éric Sauquet*2,
#                     Valentin Mansanarez
# *1   INRAE, France
#      louis.heraut@inrae.fr
# *2   INRAE, France
#      eric.sauquet@inrae.fr
# This file is part of ash R toolbox.
# Ash R toolbox is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# Ash R toolbox is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with ash R toolbox.
# If not, see <https://www.gnu.org/licenses/>.
# ///
# Rcode/plotting/datasheet.R
# Regroups all the graphical tools to generates the datasheets. More precisely, the 'datasheet_panel' function manages all the call for each station of the different graphical functions that generates info header, time serie visualisation and trend analysis graphs for every variable. It also deals with the arranging of all the plots in a single PDF page.
# Sourcing R file
source(file.path('Rcode', 'processing', 'analyse.R'), encoding='UTF-8') # hydrograph
source(file.path('Rcode', 'plotting', 'shortcut.R'), encoding='UTF-8')
## 1. DATASHEET PANEL MANAGER ________________________________________
# Manages datasheets creations for all stations. Makes the call to
# the different headers, trend analysis graphs and realises arranging
# every plots.
datasheet_panel = function (list_df2plot, df_meta, trend_period,
                            mean_period, linetype_per, axis_xlim,
                            colorForce, info_header, time_header,
                            foot_note, layout_matrix, info_height,
                            time_ratio, var_ratio, foot_height,
                            paper_size, resources_path, df_shapefile,
                            logo_dir, PRlogo_file, AEAGlogo_file,
                            INRAElogo_file, FRlogo_file,
                            outdirTmp_pdf, outdirTmp_png,
                            df_page=NULL) {
    # The percentage of augmentation and diminution of the min
    # and max limits for y axis
    lim_pct = 10
    # Number of variable/plot
    nbVar = length(list_df2plot)
    # Get all different stations code
    Code = levels(factor(df_meta$code))
    nCode = length(Code)
    if (!is.null(trend_period)) {
        # Convert 'trend_period' to list
        trend_period = as.list(trend_period)
        # Number of trend period
        nPeriod_trend = length(trend_period)
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
# Extracts the min and the max of the mean trend for all the station res = short_trendExtremes(list_df2plot, Code, nPeriod_trend, nbVar, nCode, colorForce) minTrendValue = res$min maxTrendValue = res$max } # Blank vector to store the max number of digit of label for # each station NspaceMax = c() # For all the station for (code in Code) { # Default max digit NspaceMax_code = 0 # If the time header is given it adds one to the number of plot nbVarMod = nbVar + as.numeric(!is.null(time_header)) # For all type of graph for (i in 1:nbVarMod) { if (i > nbVar) { # Extracts the data serie corresponding to the code df_data_code = time_header[time_header$code == code,] type = 'sévérité' } else { # Extracts the data corresponding to the current variable df_data = list_df2plot[[i]]$data # Extracts the type corresponding to the current variable type = list_df2plot[[i]]$type # Extracts the data corresponding to the code df_data_code = df_data[df_data$code == code,] } # If variable type is date if (type == 'saisonnalité') { # The number of digit is 6 because months are display # with 3 characters Nspace = 6 # If it is a flow variable } else if (type == 'sévérité' | type == 'data' | type == 'pluviométrie' | type == 'température' | type == 'évapotranspiration') { # Gets the max number of digit on the label maxtmp = max(df_data_code$Value, na.rm=TRUE) # Taking into account of the augmentation of # max for the window maxtmp = maxtmp * (1 + lim_pct/100) # If the max is greater than 10 if (maxtmp >= 10) { # The number of digit is the magnitude plus # the first number times 2 Nspace = (get_power(maxtmp) + 1)*2 # Plus spaces between thousands hence every 8 digits Nspace = Nspace + as.integer(Nspace/8) # If the max is less than 10 and greater than 1 } else if (maxtmp < 10 & maxtmp >= 1) { # The number of digit is the magnitude plus # the first number times 2 plus 1 for the dot # and 2 for the first decimal Nspace = (get_power(maxtmp) + 1)*2 + 3 # If the max is less than 1 (and obviously more than 0) } else if (maxtmp < 1) { # Fixes the number of significant decimals to 3 maxtmp = signif(maxtmp, 3) # The number of digit is the number of character # of the max times 2 minus 1 for the dots that # count just 1 space Nspace = nchar(as.character(maxtmp))*2 - 1 } }