From 3a8576c9c468a460a5074a0713aa859b9fb5d703 Mon Sep 17 00:00:00 2001 From: "nicolas.forquet" <nicolas.forquet@irstea.fr> Date: Mon, 26 Oct 2020 09:31:51 +0100 Subject: [PATCH] creation de la fonction modify.input. --- hydrus/file_manip_fcn.R | 38 ++++++++++++++++++++++++++++++++++++++ hydrus/main_script.R | 7 +++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/hydrus/file_manip_fcn.R b/hydrus/file_manip_fcn.R index 882994b..810ad6b 100644 --- a/hydrus/file_manip_fcn.R +++ b/hydrus/file_manip_fcn.R @@ -221,7 +221,45 @@ modify.inputs <- function(input.file.path, line.number, new.file.path, new.line) return(file.after) } +modify.input <- function(path, working.folder){ + # path: the path to the parameter json file (see parameter.file.creator) + + # load necessary librairies + library(rjson) + # read the json parameter file + parameter <- fromJSON(file = path) + # the number of simulations is the length of the parameter list + nb.simul <- length(parameter) + # iterate on the number of simulations + for (i in 1:nb.simul){ + # path to the file to be modified + input.file.path <- paste0(working.folder,"//","simulation","_",as.character(i),"//",parameter[[i]]$file.name) + # open the connection + con <- file(input.file.path) + # read all file lines + file.content <- readLines(con, n=-1) + # close the connection + close(con) + # lines that remained unchanged before the line to be modified + file.before <- file.content[1:parameter[[i]]$line.number-1] + # lines that remained unchanged after the line to be modified + file.after <- file.content[parameter[[i]]$line.number+1:length(file.content)] + # open connection again + con <- file(input.file.path, "w") + # write lines that remained unchanged before + writeLines(file.before, con) + # write the line that has been modified + writeLines(parameter[[i]]$new.line, con) + # write lines that remained unchanged after + writeLines(file.after, con) + # close connection + close(con) + } +} + parameter.file.creator <- function(path, reference.model.folder){ + # path: the path to the json parameter file to be created + # reference.model.folder: the reference.model.folder containing the orginal input files # load necesary libraries library(rjson) library(tidyverse) diff --git a/hydrus/main_script.R b/hydrus/main_script.R index 5b53f69..ad329f3 100644 --- a/hydrus/main_script.R +++ b/hydrus/main_script.R @@ -1,4 +1,4 @@ -hydrus.batch <- function(setup.file, HP1 = FALSE){ +hydrus.batch <- function(setup.file, parameter.file HP1 = FALSE){ #--------- load necessary libraries ----------# library(fs) library(rjson) @@ -58,9 +58,8 @@ hydrus.batch <- function(setup.file, HP1 = FALSE){ dir_copy(path = setup$reference.model.folder, new_path = paste0(setup$working.folder,"/","simulation","_",as.character(i)), overwrite = TRUE) - # modify input files (cette fonction est à construire. Elle doit être précédée par une fonction permettant de créer le fichier json des paramètres) - modify.input(parameter.file, path = setup$reference.model.folder, - new_path = paste0(setup$working.folder,"/","simulation","_",as.character(i))) + # modify input files + modify.input(path = parameter.file, working.foler = setup$working.folder) # create level_01.dir (and path.dat) level_01.creator(paste0(setup$working.folder,"/","simulation","_",as.character(i)), HP1 = HP1) -- GitLab