femiguez / apsimx

R package for APSIM-X
https://femiguez.github.io/apsimx-docs/
51 stars 20 forks source link

Some issues with running APSIM simulations in apsimx #113

Closed ElsaLagerquist closed 8 months ago

ElsaLagerquist commented 1 year ago

Hi Fernando,

I'm using your R package apsimx to run some APSIM NG simulations. I really like the package and it has worked really well! However, lately I have had some problems which I cannot really figure out, maybe you can help me to solve them.

I'm using the apsimx package both to run my simulations so I get the outputs directly in R, but also to shuffle about 40 years of weather data to create 10 different simulations where the different weather years come in different order. I managed to get it to work at some point, but now when I wanted to redo it it doesn't. It seems only one of the weather years is used in all simulations. See the code I'm using below. When I inspect the 'scenarioassessment_glytt.apsimx' file it has the correct weather file after each edit_apsimx_replacement() have been run, but still it seems it becomes the same weather years in the final simulation. Can you see where I'm coding wrong, or can you find another explanation to that this is not working?

Another thing that is not working, which might be related, is that when I have run edit_apsim_replacement for the first time (do not have and -edited.apsimx file in the folder), I get the following error when I run the apsimx() function: Error in read_apsimx(file = sub("apsimx", "db", file), src.dir = src.dir,: No report tables found. In addition: Warning message: In system(cmd, intern = intern, wait = wait | intern, show.output.on.console = wait, : running command 'C:\WINDOWS\system32\cmd.exe /c:\APSIM2022.12.7128.0\bin\Models.exe .\scenarioassessment_glytt-edited.apsimx' had status 1. I figured out that to avoid the error have to run the edited files in APSIM before I can run the apsimx() function. Is this how it should be? To me it seems a bit strange... If this is not how it should be, do you know how to change it? The warning message I do not know what it means and if that can also have an effect...

Edit the apsimx file with the new weather dataset editFile <- edit_apsimx_replacement('scenarioassessment_glytt.apsimx', src.dir='.', root = c("Scenarios"), node = "Experiment", node.child = "Weather", parm='FileName', value=glyttinge2) Run the simulation exp1_2 <- apsimx('scenarioassessment_glytt-edited.apsimx')

Edit the apsimx file with the new weather dataset editFile <- edit_apsimx_replacement('scenarioassessment_glytt.apsimx', src.dir='.', root = c("Scenarios"), node = "Experiment", node.child = "Weather", parm='FileName', value=glyttinge3) Run the simulation exp1_3<- apsimx('scenarioassessment_glytt-edited.apsimx')

Thank you in advance! Best, Elsa

femiguez commented 1 year ago

Elsa, it might be helpful for you to attach the apsimx file you are trying to run. The first thing I see that might be problematic is that 'glyttinge2' should be a string with the name of the weather file and the weather file should be in the current directory for APSIM to be able to find it. If you send me the weather file and the apsimx file (attached them here zipped) I can show you how I would do it.

ElsaLagerquist commented 1 year ago

Hi Fernando, Below you find the original and the edited simulations and weather files. The original runs without problems, and the edited one too, if I have run it in APSIM first... Seems I need to run it in APSIM every time I have changed the weather file if I want to update the outputs according to the latest weather. I'm also thinking it could have something to do with regional settings. I'm working in Sweden, but change between Australian and Swedish region since APSIM works best with Australian region while I have sometimes had the issue of running code in R with those settings.

scenarioassessment_glytt.zip scenarioassessment_glytt-edited.zip glyttinge8320.zip glyttinge8320_2.zip

femiguez commented 1 year ago

@ElsaLagerquist let me know if this helps. I didn't look at your '-edited' file. I recreated it using the package. It works for me.

Scenario-analysis.r.zip

ElsaLagerquist commented 1 year ago

Hi Fernando, Thanks for this, and sorry for my late response!

Now it works, but it also works with my old code again... However, I could not use the edit_apsim() function without an error, but need to use edit_apsim_replacement() function. The error I get with edit_apsim() is Error: Weather node not found

I also saw you set apsim_options differently than I was told to before: apsimx_options(warn.versions = FALSE) compared to apsimx_options(exe='C:/APSIM2022.12.7128.0/bin/Models.exe', examples.path='C:/APSIM2022.12.7128.0/Examples/')

Running the apsim() with those two gives two different warning messages Warning message: In auto_detect_apsimx() : Searching the Windows registry for APSIM-X -with your way and Warning message: In system(cmd, intern = intern, wait = wait | intern, show.output.on.console = wait, : running command 'C:\WINDOWS\system32\cmd.exe /c C:\APSIM2022.12.7128.0\bin\Models.exe .\scenarioassessment_glytt-edited.apsimx' had status 1 - with my old way

I'm not sure what any of them mean, do you? And is there a preferable way of setting apsim_options() according to you?

Best regards, Elsa

femiguez commented 1 year ago

It is perfectly fine for the options to be different depending on the set up. I'm running these examples on my laptop (Mac) and I have several versions of APSIM Next Gen installed. This is why I set that flag to 'warn.versions = FALSE'. You could set your options to 'apsimx_options(warn.find.apsimx = FALSE)' it looks like the package can find APSIM in the Windows registry without the need to set up the path manually as you were trying. Your second error suggests to me that there might be something wrong with your path. The first one is a 'warning' and it is not a problem. The second is an error and it indicates that something went wrong. Let me know if something is not working as expected.

femiguez commented 8 months ago

Should I close this issue? Can you submit a new one if you still have issues?