Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some global variables in PEcAn.DB package #2971

Merged
merged 20 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions base/db/R/get.trait.data.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
##' @param trait.names Character vector of trait names to search. If
##' `NULL` (default), use all traits that have a prior for at least
##' one of the `pfts`.
##' @param update logical: Rerun the db if result files already exist?
##' @return list of PFTs with update posteriorids
##' @author David LeBauer, Shawn Serbin, Alexey Shiklomanov
##' @export
get.trait.data <- function(pfts, modeltype, dbfiles, database, forceupdate,
trait.names = NULL) {
trait.names = NULL, update = FALSE) {
nanu1605 marked this conversation as resolved.
Show resolved Hide resolved
if (!is.list(pfts)) {
PEcAn.logger::logger.severe('pfts must be a list')
}
Expand All @@ -56,7 +57,7 @@ get.trait.data <- function(pfts, modeltype, dbfiles, database, forceupdate,
# `query_priors`, but haven't done so yet because that requires
# prepared statements and therefore requires the Postgres driver.
all_priors_list <- lapply(format(pft_ids, scientific = FALSE), query.priors,
con = dbcon, trstr = trait.names)
con = dbcon, trstr = trait.names, update = update)
trait.names <- unique(unlist(lapply(all_priors_list, rownames)))
# Eventually, can replace with this:
# all_priors <- query_priors(pfts, params = database)
Expand All @@ -69,7 +70,8 @@ get.trait.data <- function(pfts, modeltype, dbfiles, database, forceupdate,
dbfiles = dbfiles,
dbcon = dbcon,
forceupdate = forceupdate,
trait.names = trait.names)
trait.names = trait.names,
update = update)

invisible(result)
}
9 changes: 7 additions & 2 deletions base/db/R/get.trait.data.pft.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
##' @param dbcon database connection
##' @param forceupdate set this to true to force an update, auto will check to see if an update is needed.
##' @param trait.names list of trait names to retrieve
##' @param update logical: Rerun the db if result files already exist?
##' @return updated pft with posteriorid
##' @author David LeBauer, Shawn Serbin, Rob Kooper
##' @export
get.trait.data.pft <- function(pft, modeltype, dbfiles, dbcon, trait.names,
forceupdate = FALSE) {
forceupdate = FALSE, update = FALSE) {
nanu1605 marked this conversation as resolved.
Show resolved Hide resolved

# Create directory if necessary
if (!file.exists(pft$outdir) && !dir.create(pft$outdir, recursive = TRUE)) {
Expand Down Expand Up @@ -68,7 +69,11 @@ get.trait.data.pft <- function(pft, modeltype, dbfiles, dbcon, trait.names,
dplyr::mutate_if(is.character, ~dplyr::na_if(., ""))

# get the priors
prior.distns <- PEcAn.DB::query.priors(pft = pftid, trstr = PEcAn.utils::vecpaste(trait.names), con = dbcon)
prior.distns <- PEcAn.DB::query.priors(
pft = pftid,
trstr = PEcAn.utils::vecpaste(trait.names),
con = dbcon,
update = update)
prior.distns <- prior.distns[which(!rownames(prior.distns) %in% names(pft$constants)),]
traits <- rownames(prior.distns)

Expand Down
16 changes: 5 additions & 11 deletions base/db/R/query.prior.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
##' @param con Database connection object.
##' @param ... Optional arguments for connecting to database (e.g.
##' password, user name, database).
##' @param update logical: Rerun the db if result files already exist?
##'
##' @return `data.frame` of priors for each trait and the given PFT.
##' @export query.priors
Expand All @@ -31,7 +32,7 @@
##' con <- db.open(...)
##' query.priors("ebifarm.pavi", c("SLA", "Vcmax", "leaf_width"), con = con)
##' }
query.priors <- function(pft, trstr = NULL, con = NULL, ...){
query.priors <- function(pft, trstr = NULL, con = NULL, update = FALSE, ...){
nanu1605 marked this conversation as resolved.
Show resolved Hide resolved

if (inherits(pft, "integer64")) {
# Convert to character with correct representation
Expand All @@ -41,16 +42,9 @@ query.priors <- function(pft, trstr = NULL, con = NULL, ...){
if (is.null(con)) {
params <- list(...)
if (!length(params)) {
PEcAn.logger::logger.warn(paste0(
"No connection (`con`) or params (`...`) specified. ",
'Trying to connect from `settings[[c("database", "bety")]]`.'
))
if (!exists("settings")) {
PEcAn.logger::logger.severe(
"`settings` object not found. Unable to connect to database."
)
}
params <- settings[[c("database", "bety")]]
PEcAn.logger::logger.severe(
"No connection (`con`) specified and no connection parameters given in `...`.",
"Unable to connect to database.")
}
con <- db.open(params)
on.exit(db.close(con), add = TRUE)
Expand Down
5 changes: 4 additions & 1 deletion base/db/man/get.trait.data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion base/db/man/get.trait.data.pft.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion base/db/man/query.priors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.