hydrus.batch <- function(setup.file, parameter.file, HP1 = FALSE, loop.time = 60){
  # setup file: json file with setup information
  # parameter file: json file with information on the parameter to be modified
  # HP1: option to enable HP1
  # loop.time: option to adjust the scrutation loop to run several model in parallel
  #--------- load necessary libraries ----------#
  library(fs)
  library(rjson)
  library(tidyverse)
  
  #---- check if the setup file exists ----#
  if (file.exists(path = setup.file) == FALSE){
    stop("invalid setup file path")
  }  
  
  #----------- import setup -------------#
  setup <- fromJSON(file = setup.file)
  
  #------------ Check setup file ------------#
  
  # check if the working folder is valid
  if (dir.exists(path = setup$working.folder) == FALSE){
    stop("invalid working folder path")
  }
  
  # check if the rhydrus folder is valid
  if (dir.exists(path = setup$rhydrus.folder) == FALSE){
    stop("invalid rhydrus folder path")
  }
  
  # check if the hydrus folder is valid
  if (dir.exists(path = setup$hydrus.folder) == FALSE){
    stop("invalid hydrus folder path")
  }
  
  # check the reference model folder is valid:
  if (dir.exists(path = setup$reference.model.folder) == FALSE){
    stop("invalid reference model path")
  }
  
  # convert simulation number to integer and check for valid output
  setup$simulation.number <- parse_integer(setup$simulation.number)
  if(is.na(setup$simulation.number) == TRUE){
    stop("invalid format for simulation.number")
    }
  
  #--------- load rhydrus functions --------#
  
  # source the file with the functions dedicated to file manipulation
  filename <- paste0(setup$rhydrus.folder,"/file_manip_fcn.R")
  source(filename)
  
  # source the file with the functions dedicated to run simulations
  filename <- paste0(setup$rhydrus.folder,"/simulation_fcn.R")
  source(filename)
  
  #-------- create file tree --------#
  
  for (i in 1:setup$simulation.number){
    # copy the reference model folder
    dir_copy(path = setup$reference.model.folder,
             new_path = paste0(setup$working.folder,"/","simulation","_",as.character(i)), overwrite = TRUE)
  
    # create level_01.dir (and path.dat)
    level_01.creator(paste0(setup$working.folder,"/","simulation","_",as.character(i)), HP1 = HP1)
    # and copy executable file(s)
    exe_copy(setup$hydrus.folder, paste0(setup$working.folder,"/","simulation","_",as.character(i)), HP1 = HP1)
    
  }
  
  #------- modify input files --------#
  
  modify.input(path = parameter.file, working.folder = setup$working.folder)
  
  #-------- start simulations --------#
  
  # start measuring computation time
  start.time <- Sys.time()
  # initialize the simulation counter
  sim.counter <- 0
  
  # computation loop
  while (sim.counter < setup$simulation.number){
    # how busy the processor is?
    temp <- system(paste0(setup$rhydrus.folder,"//processor_load.bat"),intern = TRUE)
    processor.usage <- output.bat(temp)
    if (processor.usage < 75){
      # if processor usage is less than 75%, start a new run
      sim.counter <- sim.counter + 1
      # run Hydrus
      system(paste0(setup$working.folder,"/","simulation","_",as.character(i),"//H1D_CALC.exe"), wait = FALSE)
      # print the current simulation number
      print(sim.counter)
    }
    # wait loop.time before restarting the loop
    Sys.sleep(loop.time)
  }
  # stop measuring computation time
  end.time <- Sys.time()
}

# # set the working directory to main.directory 
# setwd(main.directory)
# 
# # nb of simulation to run
# nb.runs <- 1
# 
# # generic name = the name that will be given to the model that will be run iteratively
# # hydrus.folder = the original hydrus folder generated using the GUI
# # VERY IMPORTANT = the reference folder MUST contain the HYDRUS exe file !!!
# # The option press enter at the end must have been unchecked in the GUI
# generic.name <- "PF_Model_"
# hydrus.folder <- "DPo_1pulse_modifpdt_180108"
# 
# ## van Genuchten parameters (here you can enter matrices for the different cases you want to test)
# # gravel parameters
# deposit = matrix(c(0, 0.05, 0.12, 1.8, 5, 0.5, 0.64, 0.79, 0.05),ncol=9)
# 
# # sand parameters
# gravel = matrix(c(0, 0.1, 0.572, 2.917, 300, 0.5, 0.052, 0.4, 0),ncol=9)
# 
# # create all simulation folders, copy files and modify the one containing the van Genuchten parameters
# for (i in 1:nb.runs){
#   # current simulation name
#   new.name <- paste(generic.name,as.character(i),sep = "")
#   # create a copy of the reference model in a folder named as the current simulation
#   new.hydrus.subfolder(new.name,main.directory,hydrus.folder)
#   # create the level_01 file
#   level_01.creator(new.name,main.directory)
#   # modification of the selector.in file
#   new.data <- rbind(deposit,gravel)
#   modify.hydraulic.parameters(new.name,hydrus.folder,new.data,hydrus.version="1D")
# }
# 
# # computation loop
# start.time <- Sys.time()
# sim.counter <- 0
# 
# while (sim.counter < nb.runs){
#   # how busy the processor is?
#   temp <- system('C:/Users/forquet/Documents/script_R/rhydrus/processor_load.bat',intern = TRUE)
#   processor.usage <- output.bat(temp)
#   if (processor.usage < 75){
#     # if processor usage is less than 75%, start a new run
#     sim.counter <- sim.counter + 1
#     # go to the simulation working directory
#     setwd(paste(main.directory,"/",generic.name,as.character(sim.counter),sep=""))
#     # run Hydrus
#     system("H1D_Dual.exe", wait = FALSE)
#     # print the current simulation number
#     print(sim.counter)
#     # go back to main directory
#     setwd(main.directory)
#   }
#   # wait a minute before restarting the loop
#   Sys.sleep(60)
# }
# end.time <- Sys.time()