diff redelacComputeAclim.R @ 0:80842c5ecb58 draft default tip

"planemo upload for repository https://forgemia.inra.fr/redelac/redelac-toolbox/-/tree/68f4e9db390070700af804f77468145abfc26bc0/tools/REDELACcomputeAclim commit 68f4e9db390070700af804f77468145abfc26bc0-dirty"
author siwaa
date Tue, 24 Feb 2026 08:53:06 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/redelacComputeAclim.R	Tue Feb 24 08:53:06 2026 +0000
@@ -0,0 +1,116 @@
+library(data.table) #version  1.14.2
+library(dplyr) #version  1.0.10
+library(ggplot2)
+library(rlang)
+library(stringr)
+sessionInfo()
+
+args = commandArgs(trailingOnly = TRUE)
+
+climatPath = args[1]
+horizons = args[2]
+rdsOut = args[3]
+csvOut = args[4]
+png1Out = args[5]
+png2Out = args[6]
+
+liste_mailles_SAFRAN <- list.dirs(climatPath,full.names = F,recursive = F)
+
+#agregation des fichiers climatiques dans un unique fichier
+fichier_climat_tot<- NULL
+
+
+for(maille_DRIAS in liste_mailles_SAFRAN){
+
+  setwd(paste0(climatPath,"/",maille_DRIAS))
+  
+  filenames <- list.files()
+  
+  
+  fichier_climat_maille_tot <- NULL
+  f=1
+  for (filename in filenames)
+  {
+    file <- as.data.frame(fread(filename,sep="\t", dec=".", header=FALSE)) 
+    if (f==1) {fichier_climat_maille_tot <-file} else {fichier_climat_maille_tot <- bind_rows(fichier_climat_maille_tot,file)}
+    f=f+1
+  }
+  fichier_climat_tot<- bind_rows(fichier_climat_tot,fichier_climat_maille_tot)
+  setwd("../..")
+}
+
+#rm(file,fichier_climat_maille_tot)
+
+#entete des colonnes
+# column 1: name of weather file
+# column 2: year
+# column 3: month
+# column 4: day in month
+# column 5: Julian day
+# column 6: minimum temperature (degrees C)
+# column 7: maximum temperature (degrees C)
+# column 8: global radiation (MJ m-2 j-1)
+# column 9: Penman PET (mm j-1)
+# column 10: rainfall (mm j-1)
+# column 11: wind (m s-1)
+# column 12: vapour pressure (mbars)
+# column 13: CO2 content(ppm)
+
+names(fichier_climat_tot)
+
+names(fichier_climat_tot)<- c("maille_DRIAS","annee","mois","jcal","jjulien","TN","TX","RG","ETP","P","V","PV","CO2")
+
+
+# sanityzing
+
+trimedHorizon <-str_trim(horizons)
+purgedHorizon <-str_sub(trimedHorizon, 1, nchar(trimedHorizon)-1)
+
+listHory <- lapply(unlist(strsplit(purgedHorizon, ",")), function(x) {tmpdata <- str_split(str_trim(x), " ")[[1]]; paste0("(annee<", tmpdata[3], ")&(annee>",tmpdata[2], ")~\"", tmpdata[1], "\"" )})
+
+listHory
+
+paste(listHory, collapse = ",", sep = "")
+
+horizonsFilter <- parse_expr(paste(listHory, collapse = ",", sep = ""))
+
+listHory
+
+fichier_climat_tot <- fichier_climat_tot %>% 
+  mutate(horizon=case_when(!!horizonsFilter))
+  
+#fichier_climat_tot <- fichier_climat_tot %>% 
+#  mutate(horizon=case_when((annee>1990)&(annee<2025)~"ref"))
+
+#ecriture du fichier 
+saveRDS(fichier_climat_tot, rdsOut)
+
+fichier_aclim_an <- fichier_climat_tot %>% 
+  select(maille_DRIAS,horizon,annee,V)%>% 
+  group_by(maille_DRIAS,horizon,annee)%>% 
+  summarise(Vmed = median(V,na.rm=T))%>%
+  mutate(aclim=-6.2*log(Vmed)+17.5)
+
+fichier_aclim_hor <- fichier_aclim_an %>% 
+  select(maille_DRIAS,horizon,annee,aclim)%>% 
+  group_by(maille_DRIAS,horizon)%>% 
+  summarise(aclim = median(aclim,na.rm=T))%>%
+  filter(is.na(horizon)==F)
+
+
+#ecriture des valeurs de aclim par maille SAFRAN et horizon
+write.table(fichier_aclim_hor,csvOut,sep=";",dec=",",row.names = F)
+
+
+#tracage des valeurs calculees de aclim
+# ggplot()+geom_boxplot(data=fichier_aclim_an,aes(y=aclim))
+# ggplot()+geom_boxplot(data=fichier_aclim_an,aes(y=aclim,x=horizon))
+# ggplot()+geom_boxplot(data=fichier_aclim_an,aes(y=aclim,x=as.factor(maille_DRIAS)))
+
+ggplot()+geom_line(data=fichier_aclim_an ,aes(x=annee,y=aclim,group=maille_DRIAS,color=as.factor(maille_DRIAS)))
+ggsave(png1Out)
+ggplot()+
+  geom_boxplot(data=fichier_aclim_an %>% filter(is.na(horizon)==F),aes(x=as.factor(maille_DRIAS),y=aclim,fill=horizon))+
+  xlab("Maille DRIAS")+
+theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
+ggsave(png2Out)