APSIM 模型运行参数及结果保存
这段代码中出现了两个错误:
-
在第 23 行中,尝试将一个未定义的变量
output保存为 CSV 文件,因此出现object 'output' not found的错误。需要注释掉这一行代码,或者定义output变量并将模型输出保存到其中。 -
在第 44 行中,调用了一个未定义的函数
read_APSIM_output,因此出现could not find function "read_APSIM_output"的错误。需要先定义这个函数,或者使用其他可用的函数来读取模型输出文件。
以下是修改后的代码:
# 模型运行参数
sim_parameters <- list(
simulation_name = 'my_simulation',
override_met = TRUE, # 是否使用默认气象数据
datapath = '.', # 数据文件夹路径
soilpath = '.', # 土壤文件夹路径
version = '7413.3', # APSIM 版本号
start_date = as.Date('1990-01-01'),
end_date = as.Date('2000-12-31'),
run_type = 'auto', # 运行类型(auto 或 batch)
num_workers = 1 # 并行运行的进程数
)
soil_df <- read.csv('E:/weidan/SOIL.csv')
for (i in 1:nrow(soil_df)) {
site <- soil_df$site[i]
lat <- soil_df$lat[i]
lon <- soil_df$lon[i]
for (depth in c(5, 15, 30, 60, 100, 200)) {
# 设置土壤参数
soil_parameters <- list(
Site = site,
Latitude = lat,
Longitude = lon,
Depth = depth,
LL15 = soil_df$LL15[i],
DUL = soil_df$DUL[i],
SAT = soil_df$SAT[i],
BD = soil_df$BD[i]
)
# 运行 APSIM 模型
#output <- run_APSIM(sim_parameters, soil_parameters) # 这里需要定义 run_APSIM 函数
#system(paste(apsim_exe, file_apsim),wait = TRUE,ignore.stdout=TRUE,show.output.on.console=FALSE)
# 将结果保存为 CSV 文件
# 需要定义一个新的 apsim 文件!
file_apsim <- paste0('E:/weidan/region1wheat/SHIYONG.apsim')
writeLines(apsim, file_apsim)
system(paste(apsim_exe, file_apsim),wait = TRUE,ignore.stdout=TRUE,show.output.on.console=FALSE)
file.copy('E:/weidan/region1wheat/SHIYONG.out',paste0('E:/weidan/region1wheat/SHIYONG/',df$name[i],'.out'))
#write.csv(output, output_file) # 这里需要定义 output 变量
}
}
# 定义保存结果的文件夹路径
result_folder <- 'E:/weidan/region1wheat/SHIYONG'
# 创建文件夹
dir.create(result_folder, showWarnings = FALSE)
# 针对每个站点和深度运行模型,并保存结果
for (i in 1:nrow(soil_df)) {
site <- soil_df$site[i]
lat <- soil_df$lat[i]
lon <- soil_df$lon[i]
for (depth in c(5, 15, 30, 60, 100, 200)) {
# 设置土壤参数
soil_parameters <- list(
Site = site,
Latitude = lat,
Longitude = lon,
Depth = depth,
LL15 = soil_df$LL15[i],
DUL = soil_df$DUL[i],
SAT = soil_df$SAT[i],
BD = soil_df$BD[i]
)
# 运行 APSIM 模型
file_apsim <- 'E:/weidan/region1wheat/SHIYONG.apsim'
writeLines(apsim, file_apsim)
system(paste(apsim_exe, file_apsim), wait = TRUE, ignore.stdout = TRUE, show.output.on.console = FALSE)
# 读取输出结果,并保存为 CSV 文件
output_file <- paste0(result_folder, '/', site, '_depth', depth, '.csv')
#output_df <- read_APSIM_output(output_file) # 这里需要定义 read_APSIM_output 函数
#write.csv(output_df, output_file, row.names = FALSE)
}
}
请根据实际情况补充代码,例如定义 run_APSIM 函数、output 变量和 read_APSIM_output 函数。
注意:
apsim变量需要定义为包含 APSIM 模型文件的字符串。apsim_exe变量需要定义为 APSIM 可执行文件的路径。df变量需要定义为包含站点名称的 DataFrame。output_file变量需要定义为保存模型输出结果的 CSV 文件路径。
希望这段代码能帮助你解决问题。
原文地址: https://www.cveoy.top/t/topic/nCdn 著作权归作者所有。请勿转载和采集!