You are on page 1of 7

#Two example R scripts for the batch processing of DayCent, and batch extraction of output

# R scripts for DayCent Part One:

# R script to batch run DayCent using different soil, weather and management files

# --------------------------------------------------------------------------------------------------

# Dr Anita Shepherd, Rothamsted Research, UK and Dr Melannie Hartman, NREL, UColorado,

# 31st March,2017

# These R Scripts: Part One and Two are distributed with the aim to give R beginners ideas for what

# you can do with a model and spark your creativity.

# Variations of part one and two R scripts used together could involve:

# gathering data from different output files,

# for a specific daily or monthly period,

# This script gives each result in a different file, all results could be collated into one file

# This file is set to iterate DayCent in batch mode 10 times

# using different soil, weather and site.100 files

# This R script is similar to MS-DOS commands commonly provided for DayCent

# but commands from the R Script Part Two can be built on

#====================================================================

# R SCRIPT TO BATCH RUN DAYCENT FOR MULTIPLE INPUT

# FOR SOIL, WEATHER, ANDSOILMANAGEMENT

# CREATES MULTIPLE LIS FILES IN THE SAME FOLDER

# In the outvars.txt file list all required outputs to be placed in the .lis file

# Create several .100 files varying the soil

# Create several .wth files varying climate


# Create several .sch files optionally varying management and

# calling in the different soil and/or climate files

# in this case .sch files (and their respective .bin and .lis files) are named

# DAIRYN3_2016_1, DAIRYN3_2016_2, etc. to DAIRYN3_2016_10

# (since DAIRYN3 was simply the name of the field being simulated)

#====================================================================

# Settings for the simulation runs:

#====================================================================

# path of the DayCent model

modelpath<-"D://Daycent_directory_filename//"

daycent<-"./DailyDayCent.exe"

list100<-"./DailyDayCent_list100.exe"

#====================================================================

for (r in 1:10)

count<-paste("loop: ",r,sep="")

print(count)

setwd(modelpath)

print(modelpath)

cmdexe <- paste(Sys.getenv("COMSPEC"))

bin_file <- paste("DairyN3_2016_",r,sep="")

bin_file_ext <- paste(bin_file,".bin",sep="")


# Use DOS "del" command to remove the existing .bin file

# creates: del DairyN3_2016_r.bin

delcmdline <- paste("del",bin_file_ext,sep=" ")

cmdline <- paste(cmdexe,delcmdline,sep=" ")

print(cmdline)

system(cmdline, wait=TRUE)

# run model

# creates: DailyDayCent.exe -s DairyN3_2016_r -n DairyN3_2016_r

cmdline <- paste("-s",bin_file,"-n",bin_file,sep=" ")

cmdline <- paste(daycent,cmdline,sep=" ")

print(cmdline)

system(cmdline, wait=TRUE)

# an alternate command to delete any previous ascii lis file of the same name

# creates: unlink DairyN3_2016_r.lis

lis_file_ext <- paste(bin_file,".lis",sep="")

unlink(lis_file_ext)

# create ascii lis file from binary file using DayCent list100.exe

# creates: DailyDayCent_list100.exe bin_file bin_file Outvars_N_system.txt

cmdline <- paste(bin_file,bin_file,sep=" ")

cmdline <- paste(cmdline,"Outvars_N_system.txt",sep=" ")

cmdline <- paste(list100,cmdline,sep=" ")

print(cmdline)

system(cmdline, wait=TRUE)

}
# R scripts for DayCent Part Two:

# R script to collate annual data from .lis file for 2011-2015, example ryegrass total biomass

# --------------------------------------------------------------------------------------------------

# Dr Anita Shepherd, Rothamsted Research, 31st March,2017

# This file is set to iterate 10 times to match the earlier R program Part One

# which ran Daycent 10 times using different soils (in site.100 files) and weather files

for (r in 1:10)

count<-paste("loop: ",r,sep="")

print(count)

setwd("d://Daycent_directory_filename//")

# DayCent's 'outvars.txt' file is set to output accumulated variables

# Script Part One has run DayCent 10 times

# The script now reads in 10 .lis files

tableset<-paste("DairyN3_2016_",r,".lis",sep="")

lis.df<-read.table(tableset,header=TRUE)

head(lis.df)

dim(lis.df)

# .lis files have an unusual date format, this is converted into year and month

# create years

lis.df$year <- floor(lis.df$time)


# create months

lis.df$month_frac <- lis.df$time - lis.df$year

lis.df$month <- factor(lis.df$month_frac)

levels(lis.df$month) <- 1:12

lis.df$monthYear <- paste(lis.df$month, lis.df$year)

# tidy up and check dataframes

head(lis.df)

dim(lis.df)

names(lis.df)

lis.df$month_frac <- NULL

# reorder columns

lis.df <- lis.df[,c(1,25,24,26,2:23)]

names(lis.df)

head(lis.df)

# delete unwanted columns

lis.df$aglivc <- NULL

lis.df$aglcn <- NULL

lis.df$fertot.2. <- NULL

lis.df$fertac.1 <- NULL

lis.df$fertac.2. <- NULL

lis.df$tminrl.1. <- NULL

lis.df$bglivej.1. <- NULL


lis.df$bglivem.1. <- NULL

lis.df$somse.1. <- NULL

head(lis.df)

#create vectors of annual accumulated amounts

year<-2011:2015

agcacc<-aggregate(lis.df$agcacc, by=list(lis.df$year), FUN=max)

shrema<-aggregate(lis.df$shrema, by=list(lis.df$year), FUN=max)

stdedc<-aggregate(lis.df$stdedc, by=list(lis.df$year), FUN=max)

#shorten to required years - delete rows (vary your row numbers for your output files)

agcacc<-agcacc[c(111,112,113,114,115),]

shrema<-shrema[c(111,112,113,114,115),]

stdedc<-stdedc[c(111,112,113,114,115),]

# create total annual crop biomass produced from total of accumulated aboveground crop carbon plus

# accumulated grazed carbon plus accumulated standing dead crop carbon

# ryegrass biomass is 44% carbon (empirical calculation at study site),

# therefore biomass is carbon divided by 0.44 (vary if value differs)

BiomassTOT=10*(agcacc+shrema+stdedc)/0.44 # converted into kg biomass /ha

# 28th March, 2017: checked the above calculations of output parameters by Excel and they were correct

tableset2.df<-data.frame(year,agcacc,shrema,stdedc,BiomassTOT)

tableset2.df$Group.1 <- NULL

tableset2.df$Group.1.1 <- NULL

tableset2.df$Group.1.2 <- NULL


tableset2.df$Group.1.3 <- NULL

names(tableset2.df)[2]<-"agcacc"

names(tableset2.df)[3]<-"shrema"

names(tableset2.df)[4]<-"stdedc"

names(tableset2.df)[5]<-"BiomassTOT_kg/ha"

write.table(tableset2.df,file=paste("lis5yr_",r,".df",sep=""))

You might also like