This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetabModeling_StreamPULSE.R
94 lines (82 loc) · 3.54 KB
/
MetabModeling_StreamPULSE.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
# Modeling metabolism for sites with model records not yet
# compiled on StreamPULSE
# NOTE: You will have to roll back your R version to 3.6.3 or earlier before
# proceeding, as streamMetabolizer isn't yet built for R 4.0.0
# Load libraries
library(tidyverse)
library(StreamPULSE)
library(streamMetabolizer)
# Set parallelization
options(mc.cores = parallel::detectCores())
# Choose model type and framework
model_type <- "bayes"
model_name <- "streamMetabolizer"
# Request variables
vars <- c('DO_mgL','DOsat_pct','satDO_mgL','WaterPres_kPa',
'Depth_m','WaterTemp_C','Light_PAR','AirPres_kPa',
'Discharge_m3s', "ChlorophyllA_ugL", "Nitrate_mgL")
# Define list of sites for metabolism modeling
siteList <- c("AZ_WB")
# Define dates, YYYY-MM-DD enter NULL for both if wish is to pull period of record
startDate <- "2019-05-01"
endDate <- "2019-06-01"
# This loop will iterate through the list of sites, pulling data from
# StreamPULSE and modeling metabolism for the period of record. Metabolism
# models for each site will be saved to RData files within /model_outputs/
for (i in 1:length(siteList)){
# Define site
site <- siteList[i]
print(site)
flush.console()
# Start timer for run
startTime <- proc.time()
######################## Pull data from StreamPULSE ########################
data <- request_data(sitecode = site, variables = vars,
startdate = startDate, enddate = endDate)
# If error in pulling data, throw warning and move on to next site in list
if (!exists("data")){
warning(paste0("Data could not be pulled for site ", site))
flush.console()
next
}
# Display success message
message(paste0("Data pulled for site ", site, ". \nElapsed time: ",
(proc.time()[3]-startTime[3])/60, " minutes"))
flush.console()
######################## Format data for modeling ########################
data_prep <- prep_metabolism(d = data, type = model_type, model = model_name,
rm_flagged = list("Bad Data", "Questionable"),
interval = "15 min",
estimate_areal_depth = TRUE,
retrieve_air_pres = TRUE)
# Display success message
message(paste0("Data formatted for site ", site, ". \nElapsed time: ",
(proc.time()[3]-startTime[3])/60, " minutes"))
flush.console()
############################ Model metabolism ############################
# If no discharge data, set poolK_600='none'
if (grepl("Discharge_m3s", data_prep$specs$missing_variables)){
message(paste0("Discharge data missing for site ", site,
". Will use fit_metabolism with poolK_600='none'."))
flush.console()
model_fit <- fit_metabolism(data_prep, pool_K600 = "none")
} else {
# Otherwise, execute as normal
model_fit <- fit_metabolism(data_prep)
}
# Display success message
message(paste0("Metabolism modeled for site ", site, ". \nElapsed time: ",
(proc.time()[3]-startTime[3])/60, " minutes"))
flush.console()
}
# Visualize results
library(ggplot2)
ggplot(data = model_fit$predictions) +
geom_point(aes(x = date, y = GPP)) +
geom_ribbon(aes(ymin = GPP.lower, ymax = GPP.upper, x = date))
ggplot(data = model_fit$predictions) +
geom_point(aes(x = date, y = ER)) +
geom_ribbon(aes(ymin = ER.lower, ymax = ER.upper, x = date))
ggplot(data = model_fit[["fit"]]@fit[["daily"]]) +
geom_line(aes(x = date, y = K600_daily_mean)) +
geom_ribbon(aes(ymin = K600_daily_25pct, ymax = K600_daily_75pct, x = date))