diff --git a/plotting/datasheet.R b/plotting/datasheet.R index 46e42223a8315c3d9ef3ecd06bc79c9b117f33ec..106a21dfd6e8a0382f28bf981aabe51aa982a5fc 100644 --- a/plotting/datasheet.R +++ b/plotting/datasheet.R @@ -27,7 +27,8 @@ # Sourcing R file -source('processing/analyse.R', encoding='UTF-8') +source('processing/analyse.R', encoding='UTF-8') # hydrograph +source('plotting/shortcut.R', encoding='UTF-8') ## 1. DATASHEET PANEL ________________________________________________ @@ -47,161 +48,20 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti Code = levels(factor(df_meta$code)) nCode = length(Code) - # Gets a trend example - df_trend = list_df2plot[[1]]$trend - - # Convert 'trend_period' to list - trend_period = as.list(trend_period) - # Number of trend period - nPeriod_trend = length(trend_period) - - nPeriod_max = 0 - for (code in Code) { - # Extracts the trend corresponding to the code - df_trend_code = df_trend[df_trend$code == code,] - - # Extract start and end of trend periods - Start = df_trend_code$period_start - End = df_trend_code$period_end - # Get the name of the different period - UStart = levels(factor(Start)) - UEnd = levels(factor(End)) - - # Compute the max of different start and end - # so the number of different period - nPeriod = max(length(UStart), length(UEnd)) - - # If the number of period for the trend is greater - # than the current max period, stocks it - if (nPeriod > nPeriod_max) { - nPeriod_max = nPeriod - } - } - - # Blank array to store time info - tab_Start = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_End = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_Code = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_Periods = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - - # For all code - for (k in 1:nCode) { - # Gets the code - code = Code[k] - # For all the variable - for (i in 1:nbp) { - df_trend = list_df2plot[[i]]$trend - # Extracts the trend corresponding to the code - df_trend_code = df_trend[df_trend$code == code,] - - # Extract start and end of trend periods - Start = df_trend_code$period_start - End = df_trend_code$period_end - # Get the name of the different period - UStart = levels(factor(Start)) - UEnd = levels(factor(End)) - - # Compute the max of different start and end - # so the number of different period - nPeriod = max(length(UStart), length(UEnd)) - - # For all the period - for (j in 1:nPeriod_max) { - # Stocks period - Periods = paste(Start[j], - End[j], - sep=' / ') - - # Saves the time info - tab_Start[k, i, j] = as.character(Start[j]) - tab_End[k, i, j] = as.character(End[j]) - tab_Code[k, i, j] = code - tab_Periods[k, i, j] = Periods - - } - } - } - - # Blank array to store mean of the trend for each - # station, perdiod and variable - TrendValue_code = array(rep(1, nPeriod_trend*nbp*nCode), - dim=c(nPeriod_trend, nbp, nCode)) - - # For all the period - for (j in 1:nPeriod_max) { - # For all the code - for (k in 1:nCode) { - # Gets the code - code = Code[k] - - for (i in 1:nbp) { - # 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 - # Extracts the type of the variable - type = list_df2plot[[i]]$type - alpha = list_df2plot[[i]]$alpha - # Extracts the data corresponding to the code - df_data_code = df_data[df_data$code == code,] - df_trend_code = df_trend[df_trend$code == code,] + # Extracts number of period of trend + res = short_nPeriodMax(list_df2plot, Code) + nPeriod_trend = res$npt + nPeriodMax = res$npM - # Gets the associated time info - Start = tab_Start[k, i, j] - End = tab_End[k, i, j] - Periods = tab_Periods[k, i, 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,] - } + # Extracts time info for each period of every station + res = short_tab(list_df2plot, Code, nbp, nCode, nPeriodMax) + tab_Start = res$start + tab_End = res$end - # If it is a flow variable - if (type == 'sévérité') { - # Computes the mean of the data on the period - dataMean = mean(df_data_code_per$Value, na.rm=TRUE) - # Normalises the trend value by the mean of the data - trendValue = df_trend_code_per$trend / dataMean - # If it is a date variable - } else if (type == 'saisonnalité') { - trendValue = df_trend_code_per$trend - } - - # If the p value is under the threshold - if (df_trend_code_per$p <= alpha) { - # Stores the mean trend - TrendValue_code[j, i, k] = trendValue - # Otherwise - } else { - # Do not stocks it - TrendValue_code[j, i, k] = NA - } - } - } - } - - # Compute the min and the max of the mean trend for all the station - minTrendValue = apply(TrendValue_code, c(1, 2), min, na.rm=TRUE) - maxTrendValue = apply(TrendValue_code, c(1, 2), max, na.rm=TRUE) + # Extracts the min and the max of the mean trend for all the station + res = short_trendExtremes(list_df2plot, tab_Start, tab_End, Code, nPeriod_trend, nbp, nCode, nPeriodMax) + minTrendValue = res$min + maxTrendValue = res$max # Blank vector to store the max number of digit of label for # each station @@ -303,7 +163,9 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti # Gets the info plot Hinfo = info_panel(list_df2plot, df_meta, - period=mean_period[[1]], + trend_period=trend_period, + mean_period=mean_period, + periodHyd=mean_period[[1]], df_shapefile=df_shapefile, codeLight=code, df_data_code=time_header_code) @@ -356,14 +218,14 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti # grey = 85 # For all the period - for (j in 1:nPeriod_max) { + for (j in 1:nPeriodMax) { # If the trend is significant if (df_trend_code$p[j] <= alpha){ # Gets the associated time info Start = tab_Start[k, i, j] End = tab_End[k, i, j] - Periods = tab_Periods[k, i, j] + # Periods = tab_Periods[k, i, j] # Extracts the corresponding data for the period df_data_code_per = @@ -405,12 +267,6 @@ datasheet_panel = function (list_df2plot, df_meta, trend_period, info_header, ti colortmp = color_res # Otherwise } else { - # # Stores the default grey color - # colortmp = paste('grey', grey, sep='') - # # And gets a new shade of grey if there is - # # an other not significant trend - # grey = grey - 10 - # Stores the default grey color colortmp = paste('grey85', sep='') @@ -1409,15 +1265,14 @@ time_panel = function (df_data_code, df_trend_code, var, type, alpha=0.1, missRe return(p) } - ### 2.2. Info panel __________________________________________________ # Plots the header that regroups all the info on the station -info_panel = function(list_df2plot, df_meta, period, df_shapefile, codeLight, df_data_code=NULL) { +info_panel = function(list_df2plot, df_meta, trend_period, mean_period, periodHyd, df_shapefile, codeLight, df_data_code=NULL) { # If there is a data serie for the given code if (!is.null(df_data_code)) { # Computes the hydrograph - hyd = hydrograph_panel(df_data_code, period=period, + hyd = hydrograph_panel(df_data_code, period=periodHyd, margin=margin(t=0, r=0, b=0, l=5, unit="mm")) # Otherwise @@ -1429,6 +1284,8 @@ info_panel = function(list_df2plot, df_meta, period, df_shapefile, codeLight, df # Computes the map associated to the station map = map_panel(list_df2plot, df_meta, + trend_period=trend_period, + mean_period=mean_period, df_shapefile=df_shapefile, codeLight=codeLight, margin=margin(t=0, r=-12, b=0, l=0, unit="mm"), diff --git a/plotting/layout.R b/plotting/layout.R index 2cc723dd44ea99ab1089a50db3cf308b08976789..1f4c725e7560755f4b387ca386582316819f13b3 100644 --- a/plotting/layout.R +++ b/plotting/layout.R @@ -49,6 +49,8 @@ source('plotting/datasheet.R', encoding='UTF-8') source('plotting/map.R', encoding='UTF-8') source('plotting/matrix.R', encoding='UTF-8') source('plotting/break.R', encoding='UTF-8') +source('plotting/color_manager.R', encoding='UTF-8') +source('plotting/tools.R', encoding='UTF-8') ## 1. PERSONALISATION ________________________________________________ @@ -115,21 +117,12 @@ void = ggplot() + geom_blank(aes(1,1)) + axis.line = element_blank() ) +### 2.2. Contour void plot ___________________________________________ # A plot completly blank with a contour contour = void + theme(plot.background=element_rect(fill=NA, color="#EC4899"), plot.margin=margin(t=0, r=0, b=0, l=0, unit="mm")) -### 2.2. Circle ______________________________________________________ -# Allow to draw circle in ggplot2 with a radius and a center position -gg_circle = function(r, xc, yc, color="black", fill=NA, ...) { - x = xc + r*cos(seq(0, pi, length.out=100)) - ymax = yc + r*sin(seq(0, pi, length.out=100)) - ymin = yc + r*sin(seq(0, -pi, length.out=100)) - annotate("ribbon", x=x, ymin=ymin, ymax=ymax, color=color, - fill=fill, ...) -} - ## 3. LAYOUT _________________________________________________________ # Generates a PDF that gather datasheets, map and summarize matrix about the trend analyses realised on selected stations @@ -598,231 +591,3 @@ foot_panel = function (name, n_page, resources_path, logo_dir, AEAGlogo_file, IN return (plot) } - -## 5. COLOR MANAGEMENT _______________________________________________ -### 5.1. Color on colorbar ___________________________________________ -# Returns a color of a palette corresponding to a value included -# between the min and the max of the variable -get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE) { - - # If the value is a NA return NA color - if (is.na(value)) { - return (NA) - } - - # If the palette chosen is the personal ones - if (palette_name == 'perso') { - colorList = palette_perso - # Else takes the palette corresponding to the name given - } else { - colorList = brewer.pal(11, palette_name) - } - - # Gets the number of discrete colors in the palette - nSample = length(colorList) - # Recreates a continuous color palette - palette = colorRampPalette(colorList)(ncolor) - # Separates it in the middle to have a cold and a hot palette - Sample_hot = 1:(as.integer(nSample/2)+1) - Sample_cold = (as.integer(nSample/2)+1):nSample - palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor) - palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor) - - # Reverses the palette if it needs to be - if (reverse) { - palette = rev(palette) - palette_hot = rev(palette_hot) - palette_cold = rev(palette_cold) - } - - # Computes the absolute max - maxAbs = max(abs(max), abs(min)) - - # If the value is negative - if (value < 0) { - # Gets the relative position of the value in respect - # to its span - idNorm = (value + maxAbs) / maxAbs - # The index corresponding - id = round(idNorm*(ncolor - 1) + 1, 0) - # The associated color - color = palette_cold[id] - # Same if it is a positive value - } else { - idNorm = value / maxAbs - id = round(idNorm*(ncolor - 1) + 1, 0) - color = palette_hot[id] - } - return(color) -} - -### 5.2. Colorbar ____________________________________________________ -# Returns the colorbar but also positions, labels and colors of some -# ticks along it -get_palette = function (min, max, ncolor=256, palette_name='perso', reverse=FALSE, nbTick=10) { - - # If the palette chosen is the personal ones - if (palette_name == 'perso') { - colorList = palette_perso - # Else takes the palette corresponding to the name given - } else { - colorList = brewer.pal(11, palette_name) - } - - # Gets the number of discrete colors in the palette - nSample = length(colorList) - # Recreates a continuous color palette - palette = colorRampPalette(colorList)(ncolor) - # Separates it in the middle to have a cold and a hot palette - Sample_hot = 1:(as.integer(nSample/2)+1) - Sample_cold = (as.integer(nSample/2)+1):nSample - palette_hot = colorRampPalette(colorList[Sample_hot])(ncolor) - palette_cold = colorRampPalette(colorList[Sample_cold])(ncolor) - - # Reverses the palette if it needs to be - if (reverse) { - palette = rev(palette) - palette_hot = rev(palette_hot) - palette_cold = rev(palette_cold) - } - - # If the min and the max are below zero - if (min < 0 & max < 0) { - # The palette show is only the cold one - paletteShow = palette_cold - # If the min and the max are above zero - } else if (min > 0 & max > 0) { - # The palette show is only the hot one - paletteShow = palette_hot - # Else it is the entire palette that is shown - } else { - paletteShow = palette - } - - # The position of ticks is between 0 and 1 - posTick = seq(0, 1, length.out=nbTick) - # Blank vector to store corresponding labels and colors - labTick = c() - colTick = c() - # For each tick - for (i in 1:nbTick) { - # Computes the graduation between the min and max - lab = (i-1)/(nbTick-1) * (max - min) + min - # Gets the associated color - col = get_color(lab, min=min, max=max, - ncolor=ncolor, - palette_name=palette_name, - reverse=reverse) - # Stores them - labTick = c(labTick, lab) - colTick = c(colTick, col) - } - # List of results - res = list(palette=paletteShow, posTick=posTick, - labTick=labTick, colTick=colTick) - return(res) -} - -### 5.3. Palette tester ______________________________________________ -# Allows to display the current personal palette -palette_tester = function (n=256) { - - # An arbitrary x vector - X = 1:n - # All the same arbitrary y position to create a colorbar - Y = rep(0, times=n) - - # Recreates a continuous color palette - palette = colorRampPalette(palette_perso)(n) - - # Open a plot - p = ggplot() + - # Make the theme blank - theme( - plot.background = element_blank(), - panel.grid.major = element_blank(), - panel.grid.minor = element_blank(), - panel.border = element_blank(), - panel.background = element_blank(), - axis.title.x = element_blank(), - axis.title.y = element_blank(), - axis.text.x = element_blank(), - axis.text.y = element_blank(), - axis.ticks = element_blank(), - axis.line = element_blank() - ) + - # Plot the palette - geom_line(aes(x=X, y=Y), color=palette[X], size=60) + - scale_y_continuous(expand=c(0, 0)) - - # Saves the plot - ggsave(plot=p, - filename=paste('palette_test', '.pdf', sep=''), - width=10, height=10, units='cm', dpi=100) -} - -## 6. OTHER TOOLS ____________________________________________________ -### 6.1. Number formatting ___________________________________________ -# Returns the power of ten of the scientific expression of a value -get_power = function (value) { - - # Do not care about the sign - value = abs(value) - - # If the value is greater than one - if (value >= 1) { - # The magnitude is the number of character of integer part - # of the value minus one - power = nchar(as.character(as.integer(value))) - 1 - # If value is zero - } else if (value == 0) { - # The power is zero - power = 0 - # If the value is less than one - } else { - # Extract the decimal part - dec = gsub('0.', '', as.character(value), fixed=TRUE) - # Number of decimal with zero - ndec = nchar(dec) - # Number of decimal without zero - nnum = nchar(as.character(as.numeric(dec))) - # Compute the power of ten associated - power = -(ndec - nnum + 1) - } - return(power) -} - -### 6.2. Pourcentage of variable _____________________________________ -# Returns the value corresponding of a certain percentage of a -# data serie -gpct = function (pct, L, min_lim=NULL, shift=FALSE) { - - # If no reference for the serie is given - if (is.null(min_lim)) { - # The minimum of the serie is computed - minL = min(L, na.rm=TRUE) - # If a reference is specified - } else { - # The reference is the minimum - minL = min_lim - } - - # Gets the max - maxL = max(L, na.rm=TRUE) - # And the span - spanL = maxL - minL - # Computes the value corresponding to the percentage - xL = pct/100 * as.numeric(spanL) - - # If the value needs to be shift by its reference - if (shift) { - xL = xL + minL - } - return (xL) -} - -### 6.3. Add months __________________________________________________ -add_months = function (date, n) { - new_date = seq(date, by = paste (n, "months"), length = 2)[2] - return (new_date) -} diff --git a/plotting/map.R b/plotting/map.R index ed432ace52c2381daa556150d57095cf83d8f6f4..48b311cca422221ad89311b99a769f5b2719db7d 100644 --- a/plotting/map.R +++ b/plotting/map.R @@ -29,8 +29,8 @@ ## 1. MAP PANEL ______________________________________________________ # Generates a map plot of the tendancy of a hydrological variable map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, - trend_period=NULL, - mean_period=NULL, outdirTmp='', codeLight=NULL, + trend_period, + mean_period, outdirTmp='', codeLight=NULL, margin=NULL, showSea=TRUE, foot_note=FALSE, foot_height=0, resources_path=NULL, @@ -52,155 +52,20 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, Code = levels(factor(df_meta$code)) nCode = length(Code) - # Gets a trend example - df_trend = list_df2plot[[1]]$trend - - nPeriod_max = 0 - for (code in Code) { - # Extracts the trend corresponding to the code - df_trend_code = df_trend[df_trend$code == code,] - - # Extract start and end of trend periods - Start = df_trend_code$period_start - End = df_trend_code$period_end - # Get the name of the different period - UStart = levels(factor(Start)) - UEnd = levels(factor(End)) - - # Compute the max of different start and end - # so the number of different period - nPeriod = max(length(UStart), length(UEnd)) + # Extracts number of period of trend + res = short_nPeriodMax(list_df2plot, Code) + nPeriod_trend = res$npt + nPeriodMax = res$npM - # If the number of period for the trend is greater - # than the current max period, stocks it - if (nPeriod > nPeriod_max) { - nPeriod_max = nPeriod - } - } - - # Blank array to store time info - tab_Start = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_End = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_Code = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_Periods = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - - # For all code - for (k in 1:nCode) { - # Gets the code - code = Code[k] - # For all the variable - for (i in 1:nbp) { - df_trend = list_df2plot[[i]]$trend - # Extracts the trend corresponding to the code - df_trend_code = df_trend[df_trend$code == code,] - - # Extract start and end of trend periods - Start = df_trend_code$period_start - End = df_trend_code$period_end - # Get the name of the different period - UStart = levels(factor(Start)) - UEnd = levels(factor(End)) - - # Compute the max of different start and end - # so the number of different period - nPeriod = max(length(UStart), length(UEnd)) - - # For all the period - for (j in 1:nPeriod_max) { - # Stocks period - Periods = paste(Start[j], - End[j], - sep=' / ') - - # Saves the time info - tab_Start[k, i, j] = as.character(Start[j]) - tab_End[k, i, j] = as.character(End[j]) - tab_Code[k, i, j] = code - tab_Periods[k, i, j] = Periods - - } - } - } - + # Extracts time info for each period of every station + res = short_tab(list_df2plot, Code, nbp, nCode, nPeriodMax) + tab_Start = res$start + tab_End = res$end - # Blank array to store mean of the trend for each - # station, perdiod and variable - TrendValue_code = array(rep(1, nPeriod_max*nbp*nCode), - dim=c(nPeriod_max, nbp, nCode)) - # For all the period - for (j in 1:nPeriod_max) { - # For all the code - for (k in 1:nCode) { - # Gets the code - code = Code[k] - # For all variable - for (i in 1:nbp) { - # 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 - # Extracts the type of the variable - type = list_df2plot[[i]]$type - alpha = list_df2plot[[i]]$alpha - # 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,] - - # Gets the associated time info - Start = tab_Start[k, i, j] - End = tab_End[k, i, j] - Periods = tab_Periods[k, i, 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 - dataMean = mean(df_data_code_per$Value, na.rm=TRUE) - # Normalises the trend value by the mean of the data - trendValue = df_trend_code_per$trend / dataMean - # If it is a date variable - } else if (type == 'saisonnalité') { - trendValue = df_trend_code_per$trend - } - - # If the p value is under the threshold - if (df_trend_code_per$p <= alpha){ - # Stores the mean trend - TrendValue_code[j, i, k] = trendValue - # Otherwise - } else { - # Do not stocks it - TrendValue_code[j, i, k] = NA - } - } - } - } - # Compute the min and the max of the mean trend for all the station - minTrendValue = apply(TrendValue_code, c(1, 2), min, na.rm=TRUE) - maxTrendValue = apply(TrendValue_code, c(1, 2), max, na.rm=TRUE) + # Extracts the min and the max of the mean trend for all the station + res = short_trendExtremes(list_df2plot, tab_Start, tab_End, Code, nPeriod_trend, nbp, nCode, nPeriodMax) + minTrendValue = res$min + maxTrendValue = res$max # If there is a 'mean_period' if (!is.null(mean_period)) { @@ -537,7 +402,7 @@ map_panel = function (list_df2plot, df_meta, df_shapefile, idPer_trend=1, # Gets the associated time info Start = tab_Start[k, i, idPer_trend] End = tab_End[k, i, idPer_trend] - Periods = tab_Periods[k, i, idPer_trend] + # Periods = tab_Periods[k, i, idPer_trend] # Extracts the corresponding data for the period df_data_code_per = diff --git a/plotting/matrix.R b/plotting/matrix.R index 4816c12d9c86891fe8c6babb1ed7df6bad37da20..5edca9303e944af76f07ad647a07f2fe1865c8fc 100644 --- a/plotting/matrix.R +++ b/plotting/matrix.R @@ -42,165 +42,20 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice Code = levels(factor(df_meta$code)) nCode = length(Code) - # Gets a trend example - df_trend = list_df2plot[[1]]$trend - - # Convert 'trend_period' to list - trend_period = as.list(trend_period) - # Number of trend period - nPeriod_trend = length(trend_period) - - # Fix the maximal number of period to the minimal possible - nPeriod_max = 0 - # For all code - for (code in Code) { - # Extracts the trend corresponding to the code - df_trend_code = df_trend[df_trend$code == code,] - - # Extract start and end of trend periods - Start = df_trend_code$period_start - End = df_trend_code$period_end - # Get the name of the different period - UStart = levels(factor(Start)) - UEnd = levels(factor(End)) - - # Compute the max of different start and end - # so the number of different period - nPeriod = max(length(UStart), length(UEnd)) - - # If the number of period for the trend is greater - # than the current max period, stocks it - if (nPeriod > nPeriod_max) { - nPeriod_max = nPeriod - } - } - - # Blank array to store time info - tab_Start = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_End = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_Code = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - tab_Periods = array(rep('', nCode*nbp*nPeriod_max), - dim=c(nCode, nbp, nPeriod_max)) - - # For all code - for (k in 1:nCode) { - # Gets the code - code = Code[k] - # For all the variable - for (i in 1:nbp) { - df_trend = list_df2plot[[i]]$trend - # Extracts the trend corresponding to the code - df_trend_code = df_trend[df_trend$code == code,] - - # Extract start and end of trend periods - Start = df_trend_code$period_start - End = df_trend_code$period_end - # Get the name of the different period - UStart = levels(factor(Start)) - UEnd = levels(factor(End)) - - # Compute the max of different start and end - # so the number of different period - nPeriod = max(length(UStart), length(UEnd)) - - # For all the period - for (j in 1:nPeriod_max) { - # Stocks period - Periods = paste(Start[j], - End[j], - sep=' / ') - - # Saves the time info - tab_Start[k, i, j] = as.character(Start[j]) - tab_End[k, i, j] = as.character(End[j]) - tab_Code[k, i, j] = code - tab_Periods[k, i, j] = Periods - - } - } - } - - + # Extracts number of period of trend + res = short_nPeriodMax(list_df2plot, Code) + nPeriod_trend = res$npt + nPeriodMax = res$npM + + # Extracts time info for each period of every station + res = short_tab(list_df2plot, Code, nbp, nCode, nPeriodMax) + tab_Start = res$start + tab_End = res$end - # Blank array to store mean of the trend for each - # station, perdiod and variable - TrendValue_code = array(rep(1, nPeriod_trend*nbp*nCode), - dim=c(nPeriod_trend, nbp, nCode)) - # For all the trend period - for (j in 1:nPeriod_trend) { - # For all the code - for (k in 1:nCode) { - # Gets the code - code = Code[k] - # For all variable - for (i in 1:nbp) { - # 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 - # Extracts the type of the variable - type = list_df2plot[[i]]$type - alpha = list_df2plot[[i]]$alpha - # 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,] - - # Gets the associated time info - Start = tab_Start[k, i, j] - End = tab_End[k, i, j] - Periods = tab_Periods[k, i, 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,] - } - # Computes the mean of the data on the period - dataMean = mean(df_data_code_per$Value, na.rm=TRUE) - - # If it is a flow variable - if (type == 'sévérité') { - # Normalises the trend value by the mean of the data - trendValue = df_trend_code_per$trend / dataMean - # If it is a date variable - } else if (type == 'saisonnalité') { - # Just stocks the trend value - trendValue = df_trend_code_per$trend - } - - # If the p value is under the threshold - if (df_trend_code_per$p <= alpha){ - # Stores the averaged trend - TrendValue_code[j, i, k] = trendValue - # Otherwise - } else { - # Do not stocks it - TrendValue_code[j, i, k] = NA - } - } - } - } - # Computes the min and the max of the mean trend for - # all the station - minTrendValue = apply(TrendValue_code, c(1, 2), min, na.rm=TRUE) - maxTrendValue = apply(TrendValue_code, c(1, 2), max, na.rm=TRUE) + # Extracts the min and the max of the mean trend for all the station + res = short_trendExtremes(list_df2plot, tab_Start, tab_End, Code, nPeriod_trend, nbp, nCode, nPeriodMax) + minTrendValue = res$min + maxTrendValue = res$max # Blank vectors to store info about trend analyses Periods_trend = c() @@ -239,7 +94,10 @@ matrix_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice # Gets the associated time info Start = tab_Start[k, i, j] End = tab_End[k, i, j] - Periods = tab_Periods[k, i, j] + + # Creates a period name + Periods = paste(Start, End, + sep=' / ') # Extracts the corresponding data for the period df_data_code_per = diff --git a/processing/format.R b/processing/format.R index b994dce4e14866b72afb14f1c4b23f905cc1c03b..11ca6fe6a1cfecd436fa954ce4d49350c8567194 100644 --- a/processing/format.R +++ b/processing/format.R @@ -340,19 +340,20 @@ prepare_date = function(df_XEx, df_Xlist, per.start="01-01") { ShiftHydro = df_dateStart$DateHydro_julian[Ok_dateStart] df_XEx$values[OkXEx_code] = df_XEx$values[OkXEx_code] + ShiftHydro - - XEx_code = df_XEx$values[OkXEx_code] - meanXEx_code = mean(XEx_code, na.rm=TRUE) - dXEx_code = meanXEx_code - XEx_code - stdXEx_code = sd(XEx_code, na.rm=TRUE) - OkOverStd = dXEx_code >= stdXEx_code*3 - OkOverStd[is.na(OkOverStd)] = FALSE - XEx_code[OkOverStd] = XEx_code[OkOverStd] + 365 + + ## Add 365 when the point is too remote + # XEx_code = df_XEx$values[OkXEx_code] + # meanXEx_code = mean(XEx_code, na.rm=TRUE) + # dXEx_code = meanXEx_code - XEx_code + # stdXEx_code = sd(XEx_code, na.rm=TRUE) + # OkOverStd = dXEx_code >= stdXEx_code*3 + # OkOverStd[is.na(OkOverStd)] = FALSE + # XEx_code[OkOverStd] = XEx_code[OkOverStd] + 365 + # df_XEx$values[OkXEx_code] = XEx_code # print(group) # print(df_XEx$datetime[df_XEx$group1 == group][dXEx_code >= stdXEx_code*3]) - df_XEx$values[OkXEx_code] = XEx_code } df_XEx$datetime = as.double(df_XEx$datetime) diff --git a/script.R b/script.R index 3fe43893c024c8b17a2e4e4e6bfed46f7fd87050..020a1763ae8a7065b4375a0f39104349d154511b 100644 --- a/script.R +++ b/script.R @@ -321,17 +321,17 @@ res_tCENtrend = res$analyse ## 4. SAVING _________________________________________________________ -for (v in var) { - df_datatmp = get(paste('df_', v, 'data', sep='')) - df_modtmp = get(paste('df_', v, 'mod', sep='')) - res_trendtmp = get(paste('res_', v, 'trend', sep='')) - # Modified data saving - write_dfdata(df_datatmp, df_modtmp, resdir, optdir='modified_data', - filedir=v) - # Trend analysis saving - write_listofdf(res_trendtmp, resdir, optdir='trend_analyse', - filedir=v) -} +# for (v in var) { +# df_datatmp = get(paste('df_', v, 'data', sep='')) +# df_modtmp = get(paste('df_', v, 'mod', sep='')) +# res_trendtmp = get(paste('res_', v, 'trend', sep='')) +# # Modified data saving +# write_dfdata(df_datatmp, df_modtmp, resdir, optdir='modified_data', +# filedir=v) +# # Trend analysis saving +# write_listofdf(res_trendtmp, resdir, optdir='trend_analyse', +# filedir=v) +# } # res_tDEBtrend = read_listofdf(resdir, 'res_tDEBtrend') @@ -359,7 +359,7 @@ df_shapefile = ini_shapefile(resources_path, ### 5.2. Analysis layout _____________________________________________ datasheet_layout(toplot=c( 'datasheet' - # 'matrix' + # 'matrix', # 'map' ), df_meta=df_meta,