knitr::opts_chunk$set(echo = TRUE)
# if you set echo=FALSE, then the code will be hidden from output
library(ggplot2)
library(apsimx)

Now trying to create a soil profile, following the spirit https://search.r-project.org/CRAN/refmans/apsimx/html/apsimx_soil_profile.html.

I have made various attempts, starting with the exact example in the URL, and then variations such as the following, but cannot seem to get the syntax for various arguments to be appropriate.

sp <- tryCatch( {
  apsimx_soil_profile(
  nlayers = 3,
  #  Depth = c(15,30,60),
  Depth = c(NULL, NULL, NULL),
  Thickness =  c(150.0, 150.0, 300.0),
  BD = c(1.0105, 1.0714, 1.093),
  AirDry = c(0.13, 0.19, 0.28),
  LL15 = c(0.26, 0.24, 0.28),
  DUL = c(0.52, 0.49, 0.48),
  SAT = c(0.58, 0.56, 0.55),
  KS = c(20, 20, 20),
  crop.LL = c(0.26, 0.24, 0,28),
  crop.KL = c(0.06, 0.06, 0.04),
  crop.XF = c(1.0, 1.0, 1.0),
  Carbon = c(NULL, NULL, NULL),
  SoilCNRatio = c(NULL, NULL, NULL),
  FOM = c(NULL, NULL, NULL),
  FOM.CN = c(NULL, NULL, NULL),
  FBiom = c(NULL, NULL, NULL),
  FInert = c(NULL, NULL, NULL),
  NO3N = c(NULL, NULL, NULL),
  NH4N = c(NULL, NULL, NULL),
  PH = c(NULL, NULL, NULL),
  ParticleSizeClay = c(.1, .01, .001),
  ParticleSizeSilt = c(.01, .001, .001),
  ParticleSizeSand = c(.3, .2, .1),
  soil.bottom = 150,
  water.table = 200,
  soil.type = 0,
  crops = c("Maize", "Soybean", "Wheat"),
  metadata = NULL,
  soilwat = NA,
  swim = NA,
  soilorganicmatter = NA,
  dist.parms = list(a = 0, b = 0.2)
  ) 
  }, error = function(err) {
    print(paste("There was an error, spez.: ", err))
  } )  # end tryCatch
## Warning in (function (..., deparse.level = 1) : number of rows of result is not
## a multiple of vector length (arg 1)

## Warning in (function (..., deparse.level = 1) : number of rows of result is not
## a multiple of vector length (arg 1)

## Warning in (function (..., deparse.level = 1) : number of rows of result is not
## a multiple of vector length (arg 1)
## [1] "There was an error, spez.:  Error in data.frame(Depth = Depth, Thickness = Thickness, BD = BD, AirDry = AirDry, : arguments imply differing number of rows: 0, 3\n"

Now attempting to generate a soil profile from ISRIC database. Following the example in https://search.r-project.org/CRAN/refmans/apsimx/html/get_isric_soil_profile.html.

My first attempt, based on the example in the URL, gives an error:

outmsg <- tryCatch({
get_isric_soil_profile(
  lonlat = c(147, -35),
  statistic = c("mean", "Q0.5"),
  soil.profile,     # something breaks here
  find.location.name = TRUE
)
}, error = function(err) {
    print(paste("There was an error, spez.: ", err))
  } )  # end tryCatch
## [1] "There was an error, spez.:  Error in get_isric_soil_profile(lonlat = c(147, -35), statistic = c(\"mean\", : object 'soil.profile' not found\n"

Happily, if I leave out the second through fourth arguments then I can retrieve/create the soil profile.

(This is a lon/lat for Gobbagombalin, Australia, which is north of Canberra.)

sp1 = get_isric_soil_profile(lonlat = c(147, -35))
# Soil profile at lon/lat 147/-35 is now plotted.
plot(sp1)

Now creating a new apsimx file from the Wheat.apsimx example, that uses sp1 rather than whatever is there now.

sdir <- "/Applications/APSIM2022.12.7130.0.app/Contents/Resources/Examples/"
wdir <- "/Users/rick/AG-CODE--R"
edit_apsimx_replace_soil_profile(
  file = "Wheat.apsimx",
  src.dir = sdir,
  wrt.dir = wdir,
  soil.profile = sp1,
  edit.tag = "-lonlat_147_-35",
  overwrite = FALSE,
  verbose = TRUE
)
## Created:  /Users/rick/AG-CODE--R/Wheat-lonlat_147_-35.apsimx

Now attempting to run apsimx on my modified Wheat apsimx file.

(Note: I am using a version of APSIMX from 2022 rather than a more recent one, because the recent ones do not work properly on Mac’s. This is noted in https://github.com/APSIMInitiative/ApsimX/issues/7932.)

wdir <- "/Users/rick/AG-CODE--R"
outmsg <- tryCatch({
report1 = apsimx(file = "Wheat-lonlat_147_-35.apsimx", 
                src.dir = wdir, 
                silent = FALSE, 
                value = "report",
                cleanup = FALSE,
                simplify = TRUE
                )
}, error = function(err) {
    print(paste("There was an error, spez.: ", err))
  } )  # end tryCatch
## Warning in auto_detect_apsimx(): Multiple versions of APSIM-X installed. 
## 
##                     Choosing the newest one: APSIM2022.12.7130.0.app
## Warning in system(command = run.strng, ignore.stdout = silent, intern = TRUE):
## running command
## '/Applications/APSIM2022.12.7130.0.app/Contents/Resources/bin/Models
## /Users/rick/AG-CODE--R/Wheat-lonlat_147_-35.apsimx' had status 1
## [1] "There was an error, spez.:  Error in read_apsimx(file = sub(\"apsimx\", \"db\", file), src.dir = src.dir, : No report tables found\n"

Why do we get that error for the preceding call? If I run the modified wheat apsimx file in the APSIM app, I get several errors of the form: “Saturation of 0.463 in layer 2 is above acceptable value of 0.426. You must adjust bulk density to below 1.423 OR saturation to below 0.426”.