femiguez / apsimx

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

Using edit_apsim_replace_soil_profile with differing number of layers #167

Closed kac328 closed 4 weeks ago

kac328 commented 1 month ago

Hi @femiguez, thank you so much for all the work you have done creating the APSIMX package! I have a question about the edit_apsim_replace_soil_profile function. I am trying to replace the soil profile in my apsim file ("Dogacchi_Maize.apsim") with a number of different soil profiles, all with varying numbers of layers. I started by trying just one, but I get the message that the "length of value is shorter than length of soil water node." I checked to make sure this was indeed the problem, by using edit_apsim and adding one value to my values list to edit one of the soil water parameters, and indeed it does run when the length of the values vector matches the number of layers in the file I am trying to edit. Even with check.length=FALSE, I still get the same problem when the lengths do not match. Do you know of any way I could get around this? I would really like to be able to replace the whole soil profile via R, without having to create new .apsim files in the user interface. Thank you in advance for your help!

Here are my files: soil file: BAUsoil_PTFresults.txt apsim file (converted to .txt - hope this will work!): Dogacchi_Maize.txt

Here is my code: soil=read.csv("../Soil files/BAUsoil_PTFresults.txt",sep=",", header=TRUE)

Add depth upper limit to calculate layer thickness

library(tidyr) soil<- soil %>% separate(Depth, into=c("DepthLowerL", "DepthUpperL"), sep="-", remove=FALSE) soil$DepthUpperL<-as.numeric(soil$DepthUpperL) soil$DepthLowerL<-as.numeric(soil$DepthLowerL)

Add layer thickness

soil$Thickness_mm=10*soil$DepthUpperL-soil$DepthLowerL

s = soil[soil$Sr..No=="Pedon30",]

sp = apsimx_soil_profile( nlayers = nrow(s), Thickness = as.numeric(paste(s$Thickness_mm, sep=",")), BD = as.numeric(paste(s$BD, sep=",")), AirDry = as.numeric(paste(s$AirDry, sep=",")), LL15 = as.numeric(paste(s$LL15, sep=",")), DUL = as.numeric(paste(s$DUL, sep=",")), SAT = as.numeric(paste(s$SAT, sep=",")), KS = as.numeric(paste(s$KS, sep=",")), crop.LL = as.numeric(paste(s$wheat_LL, sep=",")), crop.KL = as.numeric(paste(s$wheat_KL, sep=",")), crop.XF = as.numeric(paste(s$wheat_XF, sep=",")), Carbon =as.numeric(paste(s$OC_percent, sep=",")), FBiom = c(0.040,0.020,0.020,0.020,0.010), FInert = c(0.400,0.600,0.800,0.900,1.000), PH = paste(s$pH, sep=","), ParticleSizeClay = as.numeric(paste(s$Clay, sep=",")), ParticleSizeSilt = as.numeric(paste(s$Silt, sep=",")), ParticleSizeSand = as.numeric(paste(s$Sand, sep=",")), soil.bottom = s$DepthUpperL[nrow(s)], water.table = 200, soil.type = 0, crops = c("Wheat", "Maize"), metadata = NULL, soilwat = NA, swim = NA, initialwater = NA, soilorganicmatter = NA, dist.parms = list(a = 0, b = 0.2), check = TRUE )

Maize_Pedon30 <- edit_apsim_replace_soil_profile( file = "Dogacchi_Maize.apsim", soil.profile = sp, edit.tag = "-Pedon30", src.dir = getwd(), wrt.dir=getwd() )

femiguez commented 1 month ago

@kac328 I'm glad you are finding the package useful. I believe they way I wrote this, it is possible to lengthen a soil profile, but not shorten it (it was not robust and it would lead to problems). There is no good reason in APSIM for intentionally creating soil profiles with different number of layers. The best option is to create a template simulation with say 10 layers and modify the soil properties or the entire soil profile, while keeping the number of layers constant. You can create soil profiles using 'get_isric_soil_profile', 'get_worldmodeler_soil_profile' or 'get_ssurgo_soil_profile' (US only).

femiguez commented 4 weeks ago

I'm closing this due to inactivity. Open a new one if you have something to report.