使用R语言运行APSIM模型并提取结果

本代码示例演示如何使用R语言读取土壤信息、设置APSIM模型参数,并运行APSIM模型。代码还包含结果提取部分,将每个站点和深度的输出文件读取到一个数据框中,然后提取最终产量和土壤水分数据,并保存为CSV文件。

1. 读取土壤信息并存储为数据框

soil_df <- read.csv('E:/weidan/SOIL.csv')

2. 安装和加载APSIM R包,设置模型运行参数

#install.packages("APSIM")
#library(APSIM)

apsim <- readLines('E:/weidan/region1wheat/quyu1xiaomai.apsim', warn = FALSE)

# 模型运行参数
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 # 并行运行的进程数
)

3. 设置土壤参数并运行APSIM模型

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)
    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'))
   
    
    print(i)
  }
}

4. 提取模型输出并保存为CSV文件

# 创建一个空的数据表来保存结果
result_df <- data.frame(site = character(),
                        depth = numeric(),
                        yield = numeric(),
                        soil_moisture = numeric(),
                        stringsAsFactors = FALSE)

# 读取每个站点和深度的输出文件,并提取需要的数据
for (i in 1:nrow(soil_df)) {
  site <- soil_df$site[i]
  
  for (depth in c(5, 15, 30, 60, 100, 200)) {
    # 读取输出文件
    output <- read_APSIM_output(paste0("E:/weidan/region1wheat/SHIYONG/", site, "_", depth, ".out"))
    
    # 提取最终产量和土壤水分数据
    yield <- tail(output$yield, n = 1)
    soil_moisture <- tail(output$total_water, n = 1)
    
    # 将结果保存到数据表中
    result_df <- rbind(result_df, data.frame(site = site,
                                             depth = depth,
                                             yield = yield,
                                             soil_moisture = soil_moisture,
                                             stringsAsFactors = FALSE))
  }
}

# 将结果保存为CSV文件
write.csv(result_df, "E:/weidan/shiyongresult.csv", row.names = FALSE)

函数定义

read_APSIM_output <- function(file) {
  # 读取APSIM输出文件
  output <- read.table(file, header = TRUE, sep = ",")
  # 将输出文件的列名处理为合适的格式
  colnames(output) <- gsub("\\.", "_", colnames(output))
  # 返回数据框
  return(output)
}

以上代码会将每个站点和深度的输出文件读取到一个数据框中,然后提取最终产量和土壤水分数据,并将结果保存到一个数据表中。最后,将结果保存为CSV文件。

注意:

  • 代码中需要根据实际情况修改文件路径和变量名。
  • 代码假设已经安装并加载了APSIM R包。
  • 代码中的apsim_exe变量代表APSIM的可执行文件路径,需要根据实际情况进行修改。
  • 为了方便阅读,代码中使用了一些注释,可以根据需要进行修改或删除。

建议:

  • 使用更简洁的代码风格,例如使用dplyr包进行数据操作。
  • 使用更可靠的方法处理文件操作,例如使用tryCatch语句捕获异常。
  • 将代码封装成函数,方便调用和维护。
  • 使用版本控制系统管理代码,方便代码追踪和合作。
APSIM模型运行与结果提取:  R语言代码示例

原文地址: https://www.cveoy.top/t/topic/nCgI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录