kevinwolz / hisafer

An R toolbox for the Hi-sAFe biophysical agroforestry model
6 stars 4 forks source link

build_hisafe() function erases template folder #131

Open Guillaume-Blanchet opened 4 years ago

Guillaume-Blanchet commented 4 years ago

Hi all, I have an issue/ a bug with the use of the build_hisafe function that I found particularly annoying : in some cases, the function erases the template folder I created, without any idea of the reason. I probably make a mistake when I call the function, but in any case, I think it shouldn't allow for deleting the template folder.

Here is the description of the bug I have : 1) I want to build several simulation folders for a calibration procedure. Therefore I run the following command lines :

hip <- define_hisafe(path = getwd(),
                     exp.name = "calibration_winter_barley_test0",
                     profiles = "all",
                     template = "template_winter_barley_monocrop_A2", # Template was created before using the copy_hisafe_template
                     SimulationName = c("Barley_original",
                                        "Barley",
                                        "WinterBarley",
                                        "WinterBarley_Augusta"),
                     mainCropSpecies = c("barley.plt",
                                         "trad_proto_barley.plt",
                                         "trad_proto_winterbarley.plt",
                                         "trad_proto_winterbarley_AUGUSTA.plt"))

build_hisafe(hip = hip)

When I run those lines, no problem arises. However, if for specific trials, I want to perform a simulation with only one crop file, I run :

hip <- define_hisafe(path = getwd(),
                     exp.name = "calibration_winter_barley_test1",
                     profiles = "all",
                     template = "template_winter_barley_monocrop_A2", # Template was created before using the copy_hisafe_template
                     SimulationName = c("Barley_original"),
                     mainCropSpecies = c("barley.plt"))

build_hisafe(hip = hip)

I basically remove some inputs for the two arguments "SimulationName" and "mainCropSpecies"). When I do this, then my template folder is lost and replaced by a folder called "Barley_original" with intern simulation folders. And I got the following error message :

> build_hisafe(hip = hip)
Error: template directory does not exist

Any idea of what's wrong with this ?

PS : I also tried to remove the "c" and the brackets in the input as well, but it gives the same result.

Guillaume-Blanchet commented 4 years ago

Looking at the code function, I am wondering whether the issue come from the build_hisafe or the define_hisafe function. More specifically, I see that there are some logical conditions for checking whether there is more than one simulation folder. Could it be part of the issue I get now (as I get only 1 simulation folder in that specific case) ?

kevinwolz commented 4 years ago

@Guillaume-Blanchet, I'm so sorry that I did not see this! For some reason, I have stopped getting notifications from my own GitHub account! I just took a quick look at this. The bug only happens when the simulation folder(s) is/are created in the same directory as where the template folder is located. For example, if you just change the path argument of define_hisafe() to path = paste0(getwd(), "/test"), then you will have no issues.

I remember having to deal with this issue of creating simulation folders in the same directory as the template folder a long time ago. I thought I had fixed it, though! I will look closer and try to fix this, but hopefully the above temporary fix can help you.

kevinwolz commented 4 years ago

If you want to investigate yourself before I can get to it, the problem definitely occurs within build_structure(), which is an internal function to hisafer within the build.R file. The problem has to do with the mechanism that simulation folders are created, which is by duplicating the template folder and then renaming it to the simulation name.

kevinwolz commented 4 years ago

Actually, I found the problem and it was very simple. It turns out that there already IS a check within build_structure() to ensure that you cannot build simulation folders in the same directory as your template folder. However, that check had a small bug in it to where it didn't work 100% of the time. So it's fixed now - if you try to run your code above, you should get an error telling you that you can't do that.

mariegosme commented 3 years ago

Hello, I'm digging this issue back up again, it seems the error still occurs: ( with sessionInfo() => other attached packages: [1] hisafer_1.5.0) library(hisafer) copy_hisafe_template("agroforestry", destination=getwd(), new.name="faketemplate") list.dirs(getwd()) #the template is created hip <- define_hisafe(path = getwd(), SimulationName ="nothingchanged", profiles = "all", template = file.path(getwd(), "faketemplate")) list.dirs(getwd()) #the template is still here build_hisafe(hip=hip)

Erreur : template directory does not exist

list.dirs(getwd()) #the template has been deleted

The workaround works: library(hisafer) copy_hisafe_template("agroforestry", destination=getwd(), new.name="faketemplate") list.dirs(getwd(), recursive = FALSE) #the template is created dir.create(file.path(getwd(), "mysimulationfolder")) hip <- define_hisafe(path = file.path(getwd(), "mysimulationfolder"), SimulationName ="nothingchanged", profiles = "all", template = file.path(getwd(), "faketemplate")) list.dirs(getwd(), recursive = FALSE) #the template is still here build_hisafe(hip=hip) #no error list.dirs(getwd()) #the template is still here, and the simulation folder has been created in mysimulationfolder