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 } }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
# If it is the temporary max number if (Nspace > NspaceMax_code) { # Stores it NspaceMax_code = Nspace } } # Stores the max digit number for labels of a station NspaceMax = c(NspaceMax, NspaceMax_code) } # For all the station for (k in 1:nCode) { # Gets the code code = Code[k] # Print code of the station for the current plotting print(paste("Datasheet for station : ", code, " (", round(k/nCode*100, 0), " %)", sep='')) # Number of header (is info and time serie are needed) nbh = as.numeric(!is.null(info_header)) + as.numeric(!is.null(time_header)) nbp = max(layout_matrix, na.rm=TRUE) # Actualises the number of plot nbg = nbp + nbh + as.numeric(foot_note) # Opens a blank list to store plot P = vector(mode='list', length=nbg) for (id in 1:nbg) { P[[id]] = void } # If the info header is needed if (!is.null(info_header)) { if ("data.frame" %in% class(info_header)) { # Extracts the data serie corresponding to the code info_header_code = info_header[info_header$code == code,] to_do = 'all' } else { info_header_code = NULL to_do = info_header } # Gets the info plot Hinfo = info_panel(list_df2plot, df_meta, trend_period=trend_period, mean_period=mean_period, periodHyd=mean_period[[1]], df_shapefile=df_shapefile, codeLight=code, df_data_code=info_header_code, to_do=to_do) # Stores it P[[1]] = Hinfo } # If the time header is given if (!is.null(time_header)) { # Extracts the data serie corresponding to the code time_header_code = time_header[time_header$code == code,] if (is.null(axis_xlim)) { # Gets the limits of the time serie axis_xlim_code = c(min(time_header_code$Date), max(time_header_code$Date)) } else { axis_xlim_code = axis_xlim
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
} # Gets the time serie plot Htime = time_panel(time_header_code, df_trend_code=NULL, trend_period=trend_period, axis_xlim=axis_xlim_code, missRect=TRUE, unit2day=365.25, var='Q', type='sévérité', grid=TRUE, ymin_lim=0, NspaceMax=NspaceMax[k], first=TRUE, lim_pct=lim_pct) # Stores it P[[2]] = Htime } else { axis_xlim_code = axis_xlim } # Computes the number of column of plot asked on the datasheet nbcol = ncol(as.matrix(layout_matrix)) # For all variable for (i in 1:nbVar) { # Extracts the data corresponding to the current variable df_data = list_df2plot[[i]]$data # Extracts the trend corresponding to the # current variable df_trend = list_df2plot[[i]]$trend unit2day = list_df2plot[[i]]$unit2day missRect = list_df2plot[[i]]$missRect # Extract the variable of the plot var = list_df2plot[[i]]$var type = list_df2plot[[i]]$type # Extracts the data corresponding to the code df_data_code = df_data[df_data$code == code,] # Extracts the trend corresponding to the code df_trend_code = df_trend[df_trend$code == code,] # Blank vector to store color color = c() if (!is.null(trend_period)) { # For all the period for (j in 1:nPeriod_trend) { # If the trend is significant # if (df_trend_code$p[j] <= alpha | colorForce){ if (df_trend_code$p[j] <= alpha){ # Extract start and end of trend periods Start = df_trend_code$period_start[j] End = df_trend_code$period_end[j] # Extracts the corresponding data for the period df_data_code_per = df_data_code[df_data_code$Date >= Start & df_data_code$Date <= End,] # Same for trend df_trend_code_per = df_trend_code[df_trend_code$period_start == Start & df_trend_code$period_end == End,] # Computes the number of trend analysis selected Ntrend = nrow(df_trend_code_per) # If there is more than one trend on the same period if (Ntrend > 1) { # Takes only the first because they are similar df_trend_code_per = df_trend_code_per[1,] } # If it is a flow variable if (type == 'sévérité') { # Computes the mean of the data on the period