forked from PecanProject/pecan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive-workflow.R
124 lines (95 loc) · 4.24 KB
/
interactive-workflow.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env Rscript
# install_github("blernermhc/RDataTracker")
library(RDataTracker)
#args <- commandArgs(trailingOnly = TRUE)
#settings.file = args[1]
settings.file <- "tests/ebi-forecast.igb.illinois.edu.biocro.xml"
## See README in tests/ folder for details
library(PEcAn.all)
#--------------------------------------------------------------------------------#
# functions used to write STATUS used by history
#--------------------------------------------------------------------------------#
# remove previous runs
unlink("pecan", recursive=TRUE)
# show all queries to the database
#db.showQueries(TRUE)
# check settings
settings <- read.settings(settings.file)
# get traits of pfts
settings$pfts <- get.trait.data(settings$pfts, settings$model$type, settings$database$dbfiles, settings$database$bety, settings$meta.analysis$update)
saveXML(PEcAn.settings::listToXml(settings, "pecan"), file=file.path(settings$outdir, 'pecan.xml'))
# run meta-analysis
run.meta.analysis(settings$pfts, settings$meta.analysis$iter, settings$meta.analysis$random.effects,
settings$meta.analysis$threshold, settings$database$dbfiles, settings$database$bety)
# do conversions
for(i in 1:length(settings$run$inputs)) {
input <- settings$run$inputs[[i]]
if (is.null(input)) next
if (length(input) == 1) next
# fia database
if (input['input'] == 'fia') {
fia.to.psscss(settings)
}
# met download
if (input['input'] == 'Ameriflux') {
# start/end date for weather
start_date <- settings$run$start.date
end_date <- settings$run$end.date
# site
site <- sub(".* \\((.*)\\)", "\\1", settings$run$site$name)
# download data
fcn <- paste("download", input['input'], sep=".")
do.call(fcn, list(site, file.path(settings$database$dbfiles, input['input']), start_date=start_date, end_date=end_date))
# convert to CF
met2CF.Ameriflux(file.path(settings$database$dbfiles, input['input']), site, file.path(settings$database$dbfiles, "cf"), start_date=start_date, end_date=end_date)
# gap filing
metgapfill(file.path(settings$database$dbfiles, "cf"), site, file.path(settings$database$dbfiles, "gapfill"), start_date=start_date, end_date=end_date)
# model specific
load.modelpkg(input['output'])
fcn <- paste("met2model", input['output'], sep=".")
r <- do.call(fcn, list(file.path(settings$database$dbfiles, "gapfill"), site, file.path(settings$database$dbfiles, input['output']), start_date=start_date, end_date=end_date))
settings$run$inputs[[i]] <- r[['file']]
}
# narr download
}
saveXML(PEcAn.settings::listToXml(settings, "pecan"), file=file.path(settings$outdir, 'pecan.xml'))
# write configurations
if (!file.exists(file.path(settings$rundir, "runs.txt")) | settings$meta.analysis$update == "TRUE") {
run.write.configs(settings, settings$database$bety$write)
} else {
PEcAn.logger::logger.info("Already wrote configuraiton files")
}
# run model
if (!file.exists(file.path(settings$rundir, "runs.txt"))) {
PEcAn.logger::logger.severe("No ensemble or sensitivity analysis specified in pecan.xml, work is done.")
} else {
PEcAn.workflow::start_model_runs(settings, settings$database$bety$write)
}
# get results
get.results(settings)
# ensemble analysis
if (!file.exists(file.path(settings$outdir,"ensemble.ts.pdf"))) {
run.ensemble.analysis(settings,TRUE)
} else {
PEcAn.logger::logger.info("Already executed run.ensemble.analysis()")
}
# sensitivity analysis
if (!file.exists(file.path(settings$outdir, "sensitivity.results.Rdata"))) {
run.sensitivity.analysis(settings)
} else {
PEcAn.logger::logger.info("Already executed run.sensitivity.analysis()")
}
# all done
status.start("FINISHED")
# send email if configured
if (!is.null(settings$email) && !is.null(settings$email$to) && (settings$email$to != "")) {
sendmail(settings$email$from, settings$email$to,
paste0("Workflow has finished executing at ", date()),
paste0("You can find the results on ", PEcAn.remote::fqdn(), " in ", normalizePath(settings$outdir)))
}
# write end time in database
if (settings$workflow$id != 'NA') {
db.query(paste0("UPDATE workflows SET finished_at=NOW() WHERE id=", settings$workflow$id, " AND finished_at IS NULL"), params=settings$database$bety)
}
status.end()
db.print.connections()