From 532a0e36659defda2f8a61d00bcf9e9fb3e6f3ba Mon Sep 17 00:00:00 2001
From: "louis.heraut" <louis.heraut@inrae.fr>
Date: Fri, 17 Dec 2021 18:06:06 +0100
Subject: [PATCH] Layout

---
 plotting/layout.R |   4 +-
 plotting/panel.R  | 332 ++++++++++++++++++++++------------------------
 script.R          |  12 +-
 3 files changed, 164 insertions(+), 184 deletions(-)

diff --git a/plotting/layout.R b/plotting/layout.R
index 34bf77c..08267e7 100644
--- a/plotting/layout.R
+++ b/plotting/layout.R
@@ -389,8 +389,8 @@ panels_layout = function (df_data, df_meta, layout_matrix, figdir='', filedir_op
 
     }
 
-    matrice_panel(list_df2plot, df_meta, trend_period,
-                  slice=12, outdirTmp=outdirTmp)
+    matrice_panel(list_df2plot, df_meta, trend_period, mean_period,
+                  slice=12, outdirTmp=outdirTmp, A3=TRUE)
 
     
     map_panel(list_df2plot, 
diff --git a/plotting/panel.R b/plotting/panel.R
index fb911c3..f508e29 100644
--- a/plotting/panel.R
+++ b/plotting/panel.R
@@ -686,7 +686,7 @@ time_panel = function (df_data_code, df_trend_code, type, p_threshold=0.1, missR
 }
 
 
-matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdirTmp='', outnameTmp='matrix') {
+matrice_panel = function (list_df2plot, df_meta, trend_period, mean_period, slice=NULL, outdirTmp='', outnameTmp='matrix', A3=FALSE) {
 
     nbp = length(list_df2plot)
     
@@ -721,10 +721,12 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
             mat = matrice_panel(sublist_df2plot, 
                                 subdf_meta,
                                 trend_period=trend_period,
+                                mean_period=mean_period,
                                 outdirTmp=outdirTmp,
                                 outnameTmp=paste(outnameTmp,
                                                  '_', i,
-                                                 sep=''))
+                                                 sep=''),
+                                A3=A3)
         }
         
     } else { 
@@ -837,15 +839,15 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
         minTrendMean = apply(TrendMean_code, c(1, 2), min, na.rm=TRUE)
         maxTrendMean = apply(TrendMean_code, c(1, 2), max, na.rm=TRUE)
 
-        Periods_mat = c()
-        NPeriod_mat = c()
-        Type_mat = list()
-        Code_mat = c()
-        Pthresold_mat = c()
-        TrendMean_mat = c()
-        DataMean_mat = c()
-        Fill_mat = c()
-        Color_mat = c()
+        Periods_trend = c()
+        NPeriod_trend = c()
+        Type_trend = list()
+        Code_trend = c()
+        Pthresold_trend = c()
+        TrendMean_trend = c()
+        DataMean_trend = c()
+        Fill_trend = c()
+        Color_trend = c()
 
         for (j in 1:nPeriod_max) {
 
@@ -896,22 +898,102 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
                         Pthresold = NA
                     }
 
-                    Periods_mat = append(Periods_mat, Periods)
-                    NPeriod_mat = append(NPeriod_mat, j)
-                    Type_mat = append(Type_mat, type)
-                    Code_mat = append(Code_mat, code)
-                    Pthresold_mat = append(Pthresold_mat, Pthresold)
-                    TrendMean_mat = append(TrendMean_mat, trendMean)
-                    DataMean_mat = append(DataMean_mat, dataMean)
-                    Fill_mat = append(Fill_mat, fill)
-                    Color_mat = append(Color_mat, color)
+                    Periods_trend = append(Periods_trend, Periods)
+                    NPeriod_trend = append(NPeriod_trend, j)
+                    Type_trend = append(Type_trend, type)
+                    Code_trend = append(Code_trend, code)
+                    Pthresold_trend = append(Pthresold_trend, Pthresold)
+                    TrendMean_trend = append(TrendMean_trend, trendMean)
+                    DataMean_trend = append(DataMean_trend, dataMean)
+                    Fill_trend = append(Fill_trend, fill)
+                    Color_trend = append(Color_trend, color)
+                }
+            }
+        }
+
+
+        Periods_mean = c()
+        NPeriod_mean = c()
+        Type_mean = list()
+        Code_mean = c()
+        DataMean_mean = c()
+        Break_mean = c()
+        Fill_mean = c()
+        Color_mean = c()
+
+        # If there is a 'mean_period'
+        if (!is.null(mean_period)) {
+            # Convert 'mean_period' to list
+            mean_period = as.list(mean_period)
+            # Number of mean period
+            nPeriod_mean = length(mean_period)
+
+            dataMeantmp = array(rep(0, nbp*nCode),
+                                dim=c(nbp, nCode))
+            
+            # For all mean period
+            for (j in 1:nPeriod_mean) {
+
+                for (k in 1:nCode) {
+                
+                    code = Code[k]
+                    
+                    for (i in 1:nbp) {
+                        
+                        df_data = list_df2plot[[i]]$data
+                        type = list_df2plot[[i]]$type
+                        
+                        df_data_code = df_data[df_data$code == code,] 
+                        
+                        # Get the current start and end of the sub period
+                        Start_mean = mean_period[[j]][1]
+                        End_mean = mean_period[[j]][2]
+                        
+                        # Extract the data corresponding to this sub period
+                        df_data_code_per =
+                            df_data_code[df_data_code$Date >= Start_mean 
+                                         & df_data_code$Date <= End_mean,]
+                        
+                        # Min for the sub period
+                        Datemin = min(df_data_code_per$Date)
+                        
+                        # Max for the sub period
+                        Datemax = max(df_data_code_per$Date)
+
+                        Periods = paste(Datemin, Datemax,
+                                        sep=' / ')
+
+                        fill = 'white'
+                        color = 'grey85'  
+                        
+                        # Mean of the flow over the sub period
+                        dataMean = mean(df_data_code_per$Qm3s,
+                                           na.rm=TRUE)
+                        
+                        Break = dataMeantmp[i, k] - dataMean
+                        dataMeantmp[i, k] = dataMean
+                        
+                        Periods_mean = append(Periods_mean, Periods)
+                        NPeriod_mean = append(NPeriod_mean, j)
+                        Type_mean = append(Type_mean, type)
+                        Code_mean = append(Code_mean, code)
+                        DataMean_mean = append(DataMean_mean, dataMean)
+                        Break_mean = append(Break_mean, Break)
+                        Fill_mean = append(Fill_mean, fill)
+                        Color_mean = append(Color_mean, color)
+                        
+                    }
                 }
             }
         }
 
         height = length(Code)
-        width = nbp * 2 * nPeriod_max + nPeriod_max
+        width = nbp * 2 * nPeriod_max + nPeriod_max + nPeriod_mean * nbp + nPeriod_mean + nbp
 
+        print(nPeriod_mean)
+        print(width)
+        
+        
         options(repr.plot.width=width, repr.plot.height=height)
         
         mat = ggplot() + theme_ash + 
@@ -934,24 +1016,26 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
         #              label=Title,
         #              hjust=0, vjust=0.5, 
         #              size=6, color="#00A3A8")
-        
+
+
+        ### Trend ###
         for (j in 1:nPeriod_max) {
 
-            Type_mat_per = Type_mat[NPeriod_mat == j]
-            Code_mat_per = Code_mat[NPeriod_mat == j]
-            Pthresold_mat_per = Pthresold_mat[NPeriod_mat == j]
-            TrendMean_mat_per = TrendMean_mat[NPeriod_mat == j]
-            DataMean_mat_per = DataMean_mat[NPeriod_mat == j]
-            Fill_mat_per = Fill_mat[NPeriod_mat == j]
-            Color_mat_per = Color_mat[NPeriod_mat == j]
+            Type_trend_per = Type_trend[NPeriod_trend == j]
+            Code_trend_per = Code_trend[NPeriod_trend == j]
+            Pthresold_trend_per = Pthresold_trend[NPeriod_trend == j]
+            TrendMean_trend_per = TrendMean_trend[NPeriod_trend == j]
+            DataMean_trend_per = DataMean_trend[NPeriod_trend == j]
+            Fill_trend_per = Fill_trend[NPeriod_trend == j]
+            Color_trend_per = Color_trend[NPeriod_trend == j]
 
-            Xtmp = as.integer(factor(as.character(Type_mat_per)))
+            Xtmp = as.integer(factor(as.character(Type_trend_per)))
 
             Xc = j + (j - 1)*nbp*2        
             Xm = Xtmp + (j - 1)*nbp*2 + j
             X = Xtmp + (j - 1)*nbp*2 + nbp + j
 
-            Y = as.integer(factor(Code_mat_per))
+            Y = as.integer(factor(Code_trend_per))
 
             x = Xc - 0.4
             xend = X[length(X)] + 0.25
@@ -980,7 +1064,7 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
                 mat = mat +
 
     gg_circle(r=0.45, xc=X[i], yc=Y[i],
-              fill=Fill_mat_per[i], color=Color_mat_per[i]) +
+              fill=Fill_trend_per[i], color=Color_trend_per[i]) +
 
     gg_circle(r=0.45, xc=Xm[i], yc=Y[i],
               fill='white', color='grey40') +
@@ -989,17 +1073,17 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
               fill='white', color='grey40') 
             }
 
-            for (i in 1:length(TrendMean_mat_per)) {
-                trendMean = TrendMean_mat_per[i]
+            for (i in 1:length(TrendMean_trend_per)) {
+                trendMean = TrendMean_trend_per[i]
                 trendC = round(trendMean*100, 2)
 
-                if (!is.na(Pthresold_mat_per[i])) {
+                if (!is.na(Pthresold_trend_per[i])) {
                     Tcolor = 'white'
                 } else {
                     Tcolor = 'grey85'
                 }
                 
-                dataMean = round(DataMean_mat_per[i], 2)
+                dataMean = round(DataMean_trend_per[i], 2)
 
                 mat = mat +
                     annotate('text', x=X[i], y=Y[i],
@@ -1038,7 +1122,19 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
                              size=3.5, color='grey20')  
             }
         }
+
+
+        ### Mean ###
+
+
+
+
+
+
+
+
         
+        ### Code ###
         for (i in 1:length(Code)) {
 
             code = Code[i]
@@ -1079,6 +1175,8 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
             }
         }
 
+        ### Environment ###
+
         mat = mat +
 
         coord_fixed() +
@@ -1092,10 +1190,21 @@ matrice_panel = function (list_df2plot, df_meta, trend_period, slice=NULL, outdi
                            expand=c(0, 0))
         
         # Saving matrix plot
+        if (A3) {
+            width = 42
+            height = 29.7
+            dpi = 300
+            print('aaaa')
+        } else {
+            width = 29.7
+            height = 21
+            dpi = 100
+        }
+        
         ggsave(plot=mat, 
                path=outdirTmp,
                filename=paste(outnameTmp, '.pdf', sep=''),
-               width=29.7, height=21, units='cm', dpi=100)
+               width=width, height=height, units='cm', dpi=dpi)
     }
 }
 
@@ -1408,12 +1517,12 @@ map_panel = function (list_df2plot, df_meta, computer_data_path, fr_shpdir, fr_s
                 geom_point(data=plot_map_codeNo,
                            aes(x=lon, y=lat),
                            shape=21, size=0.5, stroke=0.5,
-                           color='grey70', fill='grey97') +
+                           color='grey70', fill='grey70') +
                 
                 geom_point(data=plot_map_code,
                            aes(x=lon, y=lat),
                            shape=21, size=1.5, stroke=0.5,
-                           color='grey70', fill='grey70')
+                           color='grey40', fill='grey40')
         }
                 
         posTick = palette_res$posTick
@@ -1958,143 +2067,6 @@ cumulative = function (data_bin, df_meta, dyear=10, figdir='', filedir_opt='') {
 # }
 
 
-
-
-
-# get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE, nbTick=10) {
-    
-    # if (palette_name == 'perso') {
-        # colorList = c('#0f3b57',
-                      # '#1d7881',
-                      # '#80c4a9',
-                      # '#e2dac6', #mid
-                      # '#fadfad',
-                      # '#d08363',
-                      # '#7e392f')
-    # } else {
-        # colorList = brewer.pal(11, palette_name)
-    # }
-    
-    # nSample = length(colorList)
-    
-    # palette = colorRampPalette(colorList)(ncolor)
-    # 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)
-
-    # if (reverse) {
-        # palette = rev(palette)
-        # palette_hot = rev(palette_hot)
-        # palette_cold = rev(palette_cold)
-    # }
-
-    # if (value < 0) {
-        # idNorm = (value - min) / (0 - min)
-        # id = round(idNorm*(ncolor - 1) + 1, 0)
-        # color = palette_cold[id]
-    # } else {
-        # idNorm = (value - 0) / (max - 0)
-        # id = round(idNorm*(ncolor - 1) + 1, 0)
-        # color = palette_hot[id]
-    # }    
-
-    # if (min < 0 & max < 0) {
-        # paletteShow = palette_cold
-        # idTick = c()
-        # for (i in 1:nbTick) {
-            # id = round((ncolor-1)/(nbTick-1)*(i-1)) + 1
-            # idTick = c(idTick, id)
-        # }
-        # labTick = seq(min, max, length.out=nbTick)
-        # colTick = paletteShow[idTick]
-        
-    # } else if (min > 0 & max > 0) {
-        # paletteShow = palette_hot
-        # idTick = c()
-        # for (i in 1:nbTick) {
-            # id = round((ncolor-1)/(nbTick-1)*(i-1)) + 1
-            # idTick = c(idTick, id)
-        # }
-        # labTick = seq(min, max, length.out=nbTick)
-        # colTick = paletteShow[idTick]
-        
-    # } else {
-        # paletteShow = palette
-        
-        # nbTick_hot = round(max / (max - min) * nbTick) + 1
-        # nbTick_cold = round(-min / (max - min) * nbTick) + 1
-
-        # print(nbTick_hot)
-        # print(nbTick_cold)
-        # print("")
-
-        # idTick_hot = c()
-        # for (i in 1:nbTick_hot) {
-            # id = round((ncolor-1)/(nbTick_hot-1)*(i-1)) + 1
-            # idTick_hot = c(idTick_hot, id)
-        # }
-
-        # idTick_cold = c()
-        # for (i in 1:nbTick_cold) {
-            # id = round((ncolor-1)/(nbTick_cold-1)*(i-1)) + 1
-            # idTick_cold = c(idTick_cold, id)
-        # }
-
-        # print(idTick_hot)
-        # print(idTick_cold)
-        # print("")
-        
-        # labTick_hot = c()
-        # for (i in 1:nbTick_hot) {
-            # lab = (i-1)/(nbTick_hot-1) * max
-            # labTick_hot = c(labTick_hot, lab)
-        # }
-
-        # labTick_cold = c()
-        # for (i in 1:nbTick_cold) {
-            # lab = (i-1)/(nbTick_cold-1) * min
-            # labTick_cold = c(labTick_cold, lab)
-        # }
-
-        # print(min)
-        # print(max)
-        # print("")
-        
-        # print(labTick_hot)
-        # print(labTick_cold)
-        # print("")
-        
-        # colTick_hot = palette_hot[idTick_hot]
-        # colTick_cold = palette_cold[idTick_cold]
-
-        # print(colTick_hot)
-        # print(colTick_cold)
-        # print("")
-
-
-        
-        # idTick = c(-rev(idTick_cold), idTick_hot[-1])
-        # idTick = idTick - min(idTick)
-        # idTick = idTick / max(idTick)
-
-
-
-        # labTick = c(rev(labTick_cold), labTick_hot[-1])
-        # colTick = c(colTick_cold, colTick_hot[-1])
-
-        # print(idTick)
-        # print(labTick)
-        # print(colTick)
-        # print("")
-        # print("")
-    # }
- 
-    # return(list(color=color, palette=paletteShow,
-                # idTick=idTick, labTick=labTick, colTick=colTick))
-# }
-
 get_color = function (value, min, max, ncolor=256, palette_name='perso', reverse=FALSE) {
     
     if (palette_name == 'perso') {
@@ -2167,6 +2139,14 @@ get_palette = function (min, max, ncolor=256, palette_name='perso', reverse=FALS
         palette_cold = rev(palette_cold)
     }  
 
+    if (min < 0 & max < 0) {
+        paletteShow = palette_cold
+    } else if (min > 0 & max > 0) {
+        paletteShow = palette_hot
+    } else {
+        paletteShow = palette
+    }
+
     posTick = seq(0, 1, length.out=nbTick)
     
     labTick = c()
diff --git a/script.R b/script.R
index 323c222..9d4acc3 100644
--- a/script.R
+++ b/script.R
@@ -22,7 +22,7 @@ filedir =
 ### MANUAL SELECTION ###
 # Name of the file that will be analysed from the AG directory
 filename =
-    ""
+    # ""
 
     # c(
       # "S2235610_HYDRO_QJM.txt", 
@@ -32,9 +32,9 @@ filename =
       # "Q7002910_HYDRO_QJM.txt"
       # )
 
-    # c("S4214010_HYDRO_QJM.txt",
-      # "O0384010_HYDRO_QJM.txt",
-      # "Q7002910_HYDRO_QJM.txt")
+    c("S4214010_HYDRO_QJM.txt",
+      "O0384010_HYDRO_QJM.txt",
+      "Q7002910_HYDRO_QJM.txt")
 
 
 
@@ -44,8 +44,8 @@ AGlistdir =
     ""
 
 AGlistname = 
-    # ""
-    "Liste-station_RRSE.docx" 
+    ""
+    # "Liste-station_RRSE.docx" 
 
 
 ### NIVALE SELECTION ###
-- 
GitLab