-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_marbl_input_file.R
executable file
·87 lines (68 loc) · 3.19 KB
/
gen_marbl_input_file.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
#!/usr/bin/env Rscript
# --- load libraries ---
library('stringr')
library('dplyr')
library('reshape2')
`%ni%` = Negate(`%in%`)
# --- read in data ---
ss <- read.csv("data/phytoplankton_input_data.csv", as.is=TRUE)
sz <- read.csv("data/zooplankton_input_data.csv", as.is=TRUE)
sg <- read.csv("data/grazing_input_data.csv", as.is=TRUE)
# --- namelist variables ---
namelist_vars_autotrophs <- c("sname", "lname", "Nfixer", "imp_calcifier",
"exp_calcifier", "silicifier", "is_carbon_limited", "kCO2", "kFe", "kPO4",
"kDOP", "kNO3", "kNH4", "kSiO3", "Qp_fixed", "gQfe_0", "gQfe_min",
"alphaPI_per_day", "PCref_per_day", "thetaN_max", "loss_thres",
"loss_thres2", "temp_thres", "mort_per_day", "mort2_per_day",
"agg_rate_max", "agg_rate_min", "loss_poc", "temp_func_form_opt", "Ea")
namelist_vars_zooplankton <- c("sname", "lname", "z_mort_0_per_day",
"basal_respiration_rate_per_day", "loss_thres", "z_mort2_0_per_day",
"temp_func_form_opt", "Ea")
namelist_vars_grazing <- c("sname", "lname", "auto_ind_cnt", "zoo_ind_cnt",
"grazing_function", "z_umax_0_per_day", "z_grz", "graze_zoo", "graze_poc",
"graze_doc", "f_zoo_detr", "auto_ind.1.", "zoo_ind.1.")
# --- convert to long form ---
# phyto
ss <- ss[,c("type", "index", namelist_vars_autotrophs)]
ss$sname <- str_c("\'", ss$sname, "\'")
ss$lname <- str_c("\'", ss$lname, "\'")
ss$temp_func_form_opt <- str_c("\'", ss$temp_func_form_opt, "\'")
ssM <- melt(ss, id.vars=c("type", "index"), variable.name="varname")
ssM <- arrange(ssM, index, varname)
# zoo
sz <- sz[,c("type", "index", namelist_vars_zooplankton)]
sz$sname <- str_c("\'", sz$sname, "\'")
sz$lname <- str_c("\'", sz$lname, "\'")
sz$temp_func_form_opt <- str_c("\'", sz$temp_func_form_opt, "\'")
szM <- melt(sz, id.vars=c("type", "index"), variable.name="varname")
szM <- arrange(szM, index, varname)
# grazing
sg <- sg[,c("type", "index1", "index2", namelist_vars_grazing)]
sg$sname <- str_c("\'", sg$sname, "\'")
sg$lname <- str_c("\'", sg$lname, "\'")
sgM <- melt(sg, id.vars=c("type", "index1", "index2"), variable.name="varname")
sgM <- sgM[complete.cases(sgM),]
sgM <- arrange(sgM, index2, index1, varname)
sgM$varname <- str_replace(sgM$varname, ".1.", "(1)")
# --- convert to namelist input form ---
dstr1 <- str_c(ssM$type, "_settings", "(", ssM$index, ")%", ssM$varname, " = ", ssM$value)
dstr2 <- str_c(szM$type, "_settings", "(", szM$index, ")%", szM$varname, " = ", szM$value)
dstr3 <- str_c(sgM$type, "_relationship_settings", "(", sgM$index1, ",", sgM$index2, ")%", sgM$varname, " = ", sgM$value)
d <- c(dstr1, "", "", dstr2, "", "", dstr3)
# just in case the TRUE/FALSE do not get interpreted in the same way
d <- str_replace(d, "TRUE", "T")
d <- str_replace(d, "FALSE", "F")
# --- add headers ---
nP <- nrow(ss)
nZ <- nrow(sz)
max_grazer_prey_cnt <- max(sg$index1)
dhead <- c("lvariable_PtoC = .false.",
"auto_mort2_exp = 2.0",
"zoo_mort2_exp = 2.0",
"QCaCO3_max = 1.0",
"PFT_defaults = 'user-specified'",
str_c("autotroph_cnt = ", nP),
str_c("zooplankton_cnt = ", nZ),
str_c("max_grazer_prey_cnt = ", max_grazer_prey_cnt))
d <- c(dhead, "", "", d)
write.table(d, "user_nl_marbl", col.names=FALSE, row.names=FALSE, quote=FALSE)