femiguez / apsimx

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

soil.child InitialWater #130

Closed AnnaBlanke closed 1 month ago

AnnaBlanke commented 11 months ago

Hi Fernando!

I would like to modify the fraction of initialWater using inspect_apsimx and edit_apsimx. These lines of code worked fine for the APSIM classic version:

soil_esw_frac <- inspect_apsim(file = "My_simulation.apsim", node = "Soil", soil.child = "InitialWater" , parm = "FractionFull", print.path = TRUE) edit_apsim(file = "My_simulation.apsim", node = "Other", parm.path = soil_esw_frac, value =new_value, overwrite = TRUE)

But when trying the same for an APSIMX file, I get the error message: soil.child likely not present. I also noted that inspect_apsimx(file = "My_simulation.apsimx", node = "Soil", soil.child = "SoilWater") and inspect_apsimx(file = "My_simulation.apsimx", node = "Soil", soil.child = "Water") yield the same content.

Your advice is much appreciated. Thanks!

femiguez commented 11 months ago

@AnnaBlanke Can you send me the files you are trying to edit? I note that in your first example, you are editing an '.apsim' file which is for Classic and it works fine as you expect. The second set of functions '_apsimx' are for APSIM Next Gen, but the example you list here has files ending in '.apsimx'. I think the problem is that the internal way some things have been called in Next Gen have changed and this is what is causing the problem. If you send me the files I can show you how to work around that.

AnnaBlanke commented 11 months ago

Sorry, the message above had a stupid typo. It should be of course an .apsimx file in my example not .apsim (I corrected it above). Could you give me a hint how to access the InitialWater fraction in an apsimx file? I thought it would be in the soil.child “InitialWater”, but that soil child is not found. And the soil.child “Water” shows the same values as “SoilWater”.

femiguez commented 11 months ago

I do not have a problem accessing 'InitialWater' with the current version of APSIM and apsimx (from github). Try updating the 'apsimx' package using the 'remotes' method (in the README). Otherwise, send me the file you are working on (you can attach it here as a .zip) and I'll take a look at it. This works for me:

inspect_apsimx("Maize.apsimx", node = "Soil",
               soil.child = "InitialWater",
               parm = "FractionFull",
               print.path = TRUE)
Soil Type:  Clay 
Latitude:  -27.58184 
Longitude:  151.3202 
Soil children: Physical SoilWater Organic Chemical InitialWater InitialN CERESSoilTemperature Nutrient 

|   |parm         |value |
|:--|:------------|:-----|
|2  |FractionFull |1     |
Parm path: .Simulations.Simulation.Field.Soil.InitialWater.FractionFull 
AnnaBlanke commented 11 months ago

Hi Fernando! I updated the apsimx package using the library remotes as indicates in the README file. My APSIMX version is 2023.8.7287.0, I also just updated it. I renamed the soil child "Water" in the GUI into Initial Water. Running the code inspect_apsimx(file = "Peanut_Madagascar_adaptation_II.apsimx", node = "Soil", soil.child = "InitialWater" ) I get

Soil Type:  SoilType =  clay loam 
Latitude:  -18.75 
Longitude:  47.25 
Soil children: Physical SoilWater Organic Chemical InitialWater Nutrient Temperature NO3 NH4 Urea 

|parm          |value         |
|:-------------|:-------------|
|InitialPAWmm  |108.240554625 |
|RelativeTo    |LL15          |
|FilledFromTop |TRUE          |

| Thickness| InitialValues|
|---------:|-------------:|
|       150|         0.241|
|       150|         0.252|
|       300|         0.311|
|       300|         0.328|
|       300|         0.233|
|       300|         0.223|
|       300|         0.223|

I actually wanted to modify the param PercentFull and set it to 50%. But it does not appear in the param list above. I am attaching my .apsimx file. Thanks a lot! Sorry for these beginner questions, I am just starting to learn APSIMX :) Peanut_Madagascar_adaptation_II.zip

femiguez commented 11 months ago

There is an interesting discrepancy between what is shown in the GUI and what appears in the .apsimx (JSON) file. The GUI shows the PercentFull at 25%, but the .apsimx file only shows the same things that you see in R. This is the .apsimx file (raw text)

                  "InitialPAWmm": 108.240554625,
                  "RelativeTo": "LL15",
                  "FilledFromTop": true,
                  "Name": "InitialWater",
                  "ResourceName": null,
                  "Children": [],
                  "Enabled": true,
                  "ReadOnly": false

Unfortunately, if it is not part of the .apsimx (JSON) file it can't be edited from R (directly). A workaround is to edit the initial values manually. For example, here I create a vector 'inti_vals' and multiply it by 1.1. So this would be 110% of whatever values are there now. This actually gives you more flexibility as you can take (maybe the DUL values) and modify them however you prefer.

pp1 <- inspect_apsimx("Peanut_Madagascar_adaptation_II.apsimx",
                      node = "Soil",
                      soil.child = "InitialWater",
                      parm = "InitialValues",
                      print.path = TRUE)

init_vals <- c(0.241, 0.252, 0.311, 0.328, 0.233, 0.233, 0.233)

edit_apsimx("Peanut_Madagascar_adaptation_II.apsimx",
            node = "Other",
            parm.path = pp1, 
            value = init_vals * 1.1)

inspect_apsimx("Peanut_Madagascar_adaptation_II-edited.apsimx",
               node = "Soil",
               soil.child = "InitialWater",
               parm = "InitialValues")

Hope this helps!

AnnaBlanke commented 11 months ago

Thanks a lot, Fernando! I will give it a try. One follow-up question: is it possible to output the original APSIM error message in R? When running apsimx in a loop, I get an error message (replacement has 1 row, data has 0) on some of the grid cells. When I try to re-run the simulations in the APSIMX GUI, the error is most often related to soil water being below air dry values. It would be very helpful if this error message could be displayed directly in R. I tried "silent = FALSE in the apsimx function without success. Is there another way to output the APSIMX error messages?

femiguez commented 10 months ago

@AnnaBlanke It is a reasonable request to display APSIM error messages in a more friendly way through R. This is not always helpful. Now, the issue with the soil is a common one. The latest version of the package in github has a function called 'apsimx:::fix_apsimx_soil_profile' which can 'fix' soil profiles before using them in APSIM. If it doesn't work, please let me know.

femiguez commented 6 months ago

@AnnaBlanke I'm starting to look at this again. What happens is that older simulations had a child called 'InitialWater'. This was changed to 'Water' and I think sometimes simulations are not properly upgraded by APSIM Next Gen. This also means that 'inspect' and 'edit' will not find 'InitialWater' in more recent simulations because it does not exist. I'll try to make progress on that front.

Improving displaying of error messages is a different issue.

AnnaBlanke commented 6 months ago

@femiguez Thank you! Let me know if I can support this anyhow

femiguez commented 5 months ago

@AnnaBlanke I have made many changes to the package. There is a function now called 'initialwater_parms' that can create an object that can be added to a soil_profile object. I think this should solve some of the issues you were having. Notice however that there are things that are not under my control (https://github.com/APSIMInitiative/ApsimX/issues/8537).

femiguez commented 1 month ago

I'm closing this, but please open a new issue if you are having problems. With some simulations, I found out that changing the values through the 'initialwater_parms' function can help resolve the error messages.