Code to produce the base experiment in this case with 200 simulation
points
library(jsonlite)
# Load existing APSIMX JSON file
apsim_path <- "D:/Mes Donnees/R/Biomass_Crops/Modeling/APSIM/apsimx_runs/BioCrops_Base50.apsimx"
apsim_json <- fromJSON(apsim_path, simplifyVector = FALSE)
# Collect existing simulation names
existing_names <- sapply(apsim_json$Children, function(x) x$Name)
# Use the first simulation as the base template
template <- apsim_json$Children[[which(existing_names == "Mungbean_point1")]]
# Create and append new simulations (only if names don't exist)
for (i in 51:200) {
new_name <- paste0("Mungbean_point", i)
# Skip if this name already exists
if (new_name %in% existing_names) next
new_sim <- template
new_sim$Name <- new_name
# Update weather file path
for (child in new_sim$Children) {
if (!is.null(child$`$type`) && child$`$type` == "Models.Climate.Weather, Models") {
child$FileName <- paste0("D:/Mes Donnees/R/Biomass_Crops/Modeling/APSIM/weather/point", i, ".met")
}
}
# Append to APSIM JSON structure
apsim_json$Children <- append(apsim_json$Children, list(new_sim))
}
# Write to new .apsimx file
output_file <- "D:/Mes Donnees/R/Biomass_Crops/Modeling/APSIM/apsimx_runs/BioCrops_Base200.apsimx"
write(toJSON(apsim_json, auto_unbox = TRUE, pretty = TRUE, null = "null"), output_file)