Skip to content
Open
Changes from all 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
42 changes: 41 additions & 1 deletion base/db/R/get.trait.data.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ get.trait.data <-
database,
forceupdate,
write = FALSE,
trait.names = NULL) {
trait.names = NULL,
input_file = NULL) {

if (!is.list(pfts)) {
PEcAn.logger::logger.severe('pfts must be a list')
Expand All @@ -42,6 +43,45 @@ get.trait.data <-
PEcAn.logger::logger.severe('At least one pft in settings is missing its "outdir"')
}

#check for flatfile path if present use it
file_path <- input_file
if(is.null(file_path)){
file.path <- pfts$file_path
}
use_flatfile <- !is.null(file_path) && file.exists(file_path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest throwing an error if file_path is specified but the file doesn't exist, rather than revert to DB mode (which would be surprising to the user).


if (use_flatfile) {
Comment on lines +46 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible implementation of the error-if-not-exist pattern (If you like this, need to also add @importFrom rlang %||% in the Roxygen block above):

Suggested change
#check for flatfile path if present use it
file_path <- input_file
if(is.null(file_path)){
file.path <- pfts$file_path
}
use_flatfile <- !is.null(file_path) && file.exists(file_path)
if (use_flatfile) {
#check for flatfile path, if present use it
file_path <- input_file %||% pfts$file_path
if (!is.null(file_path) {
if (!file.exists(file_path) {
PEcAn.logger::logger.error("trait data file not found at specified path", sQuote(file_path))
}

PEcAn.logger::logger.info("Using flat file for trait data instead of database")

# Load flat file as data.frame
trait_data_flat <- read.csv(pfts$file_path, stringsAsFactors = FALSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
trait_data_flat <- read.csv(pfts$file_path, stringsAsFactors = FALSE)
trait_data_flat <- read.csv(file_path, stringsAsFactors = FALSE)


# Build trait.names from flat file if not already provided
if (is.null(trait.names)) {
pft_names <- vapply(pfts, "[[", character(1), "name")
pft_ids <- unique(trait_data_flat$pft_id[
trait_data_flat$pft_name %in% pft_names &
trait_data_flat$pft_type == modeltype
])
trait.names <- unique(trait_data_flat$trait_name[
trait_data_flat$pft_id %in% pft_ids
])
}

# Call get.trait.data.pft with trait_data instead of dbcon
result <- lapply(pfts, get.trait.data.pft,
modeltype = modeltype,
dbfiles = dbfiles,
dbcon = NULL,
trait_data = trait_data_flat,
write = write,
forceupdate = forceupdate,
trait.names = trait.names)

return(invisible(result))
}


dbcon <- db.open(database)
on.exit(db.close(dbcon), add = TRUE)

Expand Down
Loading