femiguez / apsimx

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

Reading apsimx with many simulations #139

Closed matteolongo2 closed 4 months ago

matteolongo2 commented 7 months ago

Hello, I would like to change the Operations to apsimx files which include many simulations. Trying to open the file, I get the error: Error in manager.node[[1]] : subscript out of bounds

Here my code:

edit_apsimx("fodderbeet_quadr.apsimx", 
            node = "Manager",
            manager.child = "SowingFertiliser",
            root = "quadr_0_l_fodderbeet",
            verbose = TRUE)

Thank you

fodderbeet_quadr.zip

femiguez commented 7 months ago

@matteolongo2 The use of operations has not been very common in Next Gen, but it is a reasonable request to provide an enhancement that would handle these better. You might be able to edit it using 'edit_apsimx_replacement'. This allows you to inspect it at least

inspect_apsimx_replacement("fodderbeet_quadr.apsimx",
                          root = "quadr_0_l_fodderbeet",
                          node = "Field",
                          node.child = "Operations",
                          parm = "1993-04-22",
                          display.available = TRUE)

The previous will point to a specific date and with edit you should be able to change it, but I haven't tried. I'll let you know if I come up with a better approach

matteolongo2 commented 7 months ago

Thank you very much! So, is not possible to just input all the Operations using the same format (or changing it in batch) required by the GUI? My operations have been listed in text files like this:

1989-10-11 [SurfaceOrganicMatter].Incorporate(fraction:0.5,depth:550) 1989-10-12 [SurfaceOrganicMatter].Incorporate(fraction:0.5,depth:200) 1989-10-13 [SurfaceOrganicMatter].Incorporate(fraction:0.5,depth:200) 1989-10-19 [Wheat].Sow(population:120, cultivar:"Hartog", depth:40, rowSpacing:250) 1990-06-25 [Wheat].Harvest() 1990-06-26 [Wheat].EndCrop()

femiguez commented 7 months ago

@matteolongo2 This is something I wanted to add for sometime. I just pushed a commit that allows you to change 'Operations'. With version 2.4.8 from github you can do the following


new.action <- '[Wheat].Sow(population:100, cultivar:"Hartog", depth:40, rowSpacing:250)'
new.line <- '1991-11-03 [Wheat].Sow(population:100, cultivar:"Hartog", depth:40, rowSpacing:250)'

edit_apsimx("fodderbeet_quadr.apsimx",
            root = "quadr_0_l_fodderbeet",
            node = "Operations",
            parm = list(18, 'Action'),
            value = new.action)

inspect_apsimx("fodderbeet_quadr-edited.apsimx",
               root = "quadr_0_l_fodderbeet",
               node = "Operations",
               parm = list(18, 'Action'))

edit_apsimx("fodderbeet_quadr-edited.apsimx",
            root = "quadr_0_l_fodderbeet",
            node = "Operations",
            parm = list(18, 'Line'),
            value = new.line,
            overwrite = TRUE)

inspect_apsimx("fodderbeet_quadr-edited.apsimx",
            root = "quadr_0_l_fodderbeet",
            node = "Operations",
            parm = list(18, 'Line'))

Notice that I'm changing both 'Action' and 'Line'. These seem redundant information in the json file (they are not both displayed in the GUI).

Let me know if this works for you. I have not added any testing. Another item to add to the TODO list

matteolongo2 commented 7 months ago

Hi, Thank you! The command does add the operation but does not insert the correct date. Also, can I just overwrite the file without creating a new one? I would like to read a txt file with my list of Operations (several hundreds of rows), removing the old Operations and adding the new ones inside my original apsimx files.

femiguez commented 7 months ago

@matteolongo2 The json file has three components that need to be edited: 'Date', 'Action', and 'Line' - your code should look like what you see below. I just added some code that should allow you to edit several/all rows. Use the overwrite argument if you don't want to create new files.

edit_apsimx("fodderbeet_quadr.apsimx",
            root = "quadr_0_l_fodderbeet",
            node = "Operations",
            parm = list(18, 'Date'),
            value = new.date,
            overwrite = TRUE)

edit_apsimx("fodderbeet_quadr.apsimx",
            root = "quadr_0_l_fodderbeet",
            node = "Operations",
            parm = list(18, 'Action'),
            value = new.action,
            overwrite = TRUE)

edit_apsimx("fodderbeet_quadr.apsimx",
            root = "quadr_0_l_fodderbeet",
            node = "Operations",
            parm = list(18, 'Line'),
            value = new.line,
            overwrite = TRUE)

The character vectors 'new.date', 'new.action' and 'new.line' can have multiple elements, so length > 1. In that case, instead of just '18' you need to provide the appropriate lines to edit. For example, '10:18' will edit lines 10 through 18.

matteolongo2 commented 7 months ago

Now I got it, thank you again. However, trying with multiple dates I get this error:

new.date <- c("1981-11-03","1981-11-04")
edit_apsimx("test.apsimx",
            root = "quadr_0_l_fodderbeet",
            node = "Operations",
            parm = list(11:12, 'Date'),
            value = new.date,
            overwrite = TRUE)

Error in `*tmp*`[[parm[[1]]]] : subscript out of bounds

Also, what about adding (and not replacing) or removing lines?

femiguez commented 7 months ago

@matteolongo2 Thanks for the feedback. I'll test this a bit more and get back to you.

Edit: It is working for me at the moment, but you do need the latest version from github which is 2.4.9. Make sure this is the case.

packageVersion("apsimx")

I haven't thought about adding/deleting lines yet.

femiguez commented 7 months ago

@matteolongo2 I just tested this again and it seems to be working well. However, note that it is tricky. It is very easy to edit the wrong line and you have to remember to edit (potentially) all three: 'Date', 'Action' and 'Line'

matteolongo2 commented 7 months ago

Thank you @femiguez, I simply hadn't updated the package yet. It does work for me as well. Do you think it will be possbile to have a version able to remove/add lines in the near future?

femiguez commented 7 months ago

I consider this a reasonable feature request :)

femiguez commented 4 months ago

@matteolongo2 I have added code that can add/remove lines to 'Operations'. I have also added some instructions in the details section. I'll close this now, please open a new issue with you have problems.