apsimx icon indicating copy to clipboard operation
apsimx copied to clipboard

We need to go deeper - inspect_apsimx cannot access sowing date

Open LenLon opened this issue 7 months ago • 4 comments

I currently am trying to edit the sowing date in my simulation using edit_apsimx. Unfortunately, it seems that edit_apsimx or rather inspect_apsimx are not able to dig "deep" enough in the .apsimx JSON file to get the attribute I want..

What I want to edit:

image

Code I tried:


# Using JSON queries for the parm argument
>     inspect_apsimx(file = paste0(name_sim, ".apsimx"),
+                    src.dir = in_path_sim, 
+                    node = "Other",
+                    parm = "$.Simulations.Simulation.Field")
Simulation Field Names level 3:[1] "SoilType =  loam"                   "Fertiliser"                        
[3] "SurfaceOrganicMatter"               "C4Maize"                           
[5] "CropStatus"                         "Reset on date"                     
[7] "Sow using a variable rule_extended" "Harvest"                           
[9] "Report"

# See the manager scripts, but can't access them individually :(
>     inspect_apsimx(file = paste0(name_sim, ".apsimx"),
+                    src.dir = in_path_sim, 
+                    node = "Other",
+                    parm = "$.Simulations.Simulation.Field.Sow using a variable rule_extended")
Error: Not implemented yet

# Using integer lists for parm: getting closer
>     inspect_apsimx(file = paste0(name_sim, ".apsimx"),, 
+                    src.dir = in_path_sim, 
+                    node = "Other",
+                    parm = list(1,2,6,7,3),
+                    print.path = T)
Parm path: .Simulations.Simulation.Field.Sow using a variable rule_extended.Parameters 

# But cannot access the parameters in the manager scripts
>     inspect_apsimx(file = paste0(name_sim, ".apsimx"),
+                    src.dir = in_path_sim, 
+                    node = "Other",
+                    parm = list(1,2,6,7,3,2),
+                    print.path = T)
Error in inspect_apsimx(file = paste0(name_sim, ".apsimx"), src.dir = in_path_sim,  : 
  object 'wlevel4' not found

# Hail mary: using jsonlite to try and edit the .apsimx file itself
# This does not throw any errors, but APSIMX itself gives you one when you try to open the edited file
>     apsimx_json <- jsonlite::read_json(paste0(in_path_sim, "\\", name_sim, ".apsimx"))
>     apsimx_json$Children[[2]]$Children[[6]]$Children[[7]]$Parameters[[2]]$Value <- sow_date
>     apsimx_json_mod <- jsonlite::toJSON(apsimx_json)
>     write(apsimx_json_mod, paste0(in_path_sim, "\\", name_sim, ".apsimx"))

Should I try to add just one more level in the inspect_apsimx source code, is there another way, am I doing something stupid altogether? Thank you for the great package, I am learning a lot currently :)

LenLon avatar Jul 22 '24 16:07 LenLon