66# #' - `settings$database$bety`
77# #' - `settings$database$dbfiles`
88# #' - `settings$meta.analysis$update`
9- # #'
9+ # #'
10+ # #' If either `input_file` or `settings$pfts$file_path` is provided,
11+ # #' it should be a valid path to a CSV (with at least columns
12+ # #' `name`, `distn`, `parama`, `paramb`, `n`) and will be used instead of
13+ # #' `database` for trait lookup.
14+ # #'
1015# #' @param pfts the list of pfts to get traits for
1116# #' @param modeltype type of model that is used, this is is used to distinguish
1217# #' between different PFTs with the same name.
2126# #' @param trait.names Character vector of trait names to search. If
2227# #' `NULL` (default), use all traits that have a prior for at least
2328# #' one of the `pfts`.
29+ # #' @param input_file Path to a CSV file containing prior information.
30+ # #' If specified, `database` is not used.
2431# #' @return list of PFTs with update posteriorids
2532# #' @author David LeBauer, Shawn Serbin, Alexey Shiklomanov
33+ # #' @importFrom rlang %||%
2634# #' @export
27- get.trait.data <-
28- function (pfts ,
29- modeltype ,
30- dbfiles ,
31- database ,
32- forceupdate ,
33- write = FALSE ,
34- trait.names = NULL ) {
35-
35+ get.trait.data <- function (pfts ,
36+ modeltype ,
37+ dbfiles ,
38+ database ,
39+ forceupdate ,
40+ write = FALSE ,
41+ trait.names = NULL ,
42+ input_file = NULL ) {
3643 if (! is.list(pfts )) {
37- PEcAn.logger :: logger.severe(' pfts must be a list' )
44+ PEcAn.logger :: logger.severe(" pfts must be a list" )
3845 }
3946 # Check that all PFTs have associated outdir entries
40- pft_outdirs <- lapply(pfts , ' [[ ' , ' outdir' )
47+ pft_outdirs <- lapply(pfts , " [[ " , " outdir" )
4148 if (any(sapply(pft_outdirs , is.null ))) {
42- PEcAn.logger :: logger.severe(' At least one pft in settings is missing its "outdir"' )
49+ PEcAn.logger :: logger.severe(" At least one pft in settings is missing its `outdir`" )
50+ }
51+
52+ # check for flatfile path, if present use it
53+ file_path <- input_file %|| % pfts $ file_path
54+ if (! is.null(file_path )) {
55+ if (! file.exists(file_path )) {
56+ PEcAn.logger :: logger.error(" trait data file not found at specified path" , sQuote(file_path ))
57+ }
58+ PEcAn.logger :: logger.info(" Using flat file for trait data instead of database" )
59+
60+ # Load flat file as data.frame
61+ trait_data_flat <- utils :: read.csv(file_path , stringsAsFactors = FALSE )
62+
63+ # Build trait.names from flat file if not already provided
64+ if (is.null(trait.names )) {
65+ pft_names <- vapply(pfts , " [[" , character (1 ), " name" )
66+ pft_ids <- unique(trait_data_flat $ pft_id [
67+ trait_data_flat $ pft_name %in% pft_names &
68+ trait_data_flat $ pft_type == modeltype
69+ ])
70+ trait.names <- unique(trait_data_flat $ trait_name [
71+ trait_data_flat $ pft_id %in% pft_ids
72+ ])
73+ }
74+
75+ # Call get.trait.data.pft with trait_data instead of dbcon
76+ result <- lapply(pfts , get.trait.data.pft ,
77+ modeltype = modeltype ,
78+ dbfiles = dbfiles ,
79+ dbcon = NULL ,
80+ trait_data = trait_data_flat ,
81+ write = write ,
82+ forceupdate = forceupdate ,
83+ trait.names = trait.names )
84+
85+ return (invisible (result ))
4386 }
44-
87+
88+
4589 dbcon <- db.open(database )
4690 on.exit(db.close(dbcon ), add = TRUE )
47-
91+
4892 if (is.null(trait.names )) {
4993 PEcAn.logger :: logger.debug(paste0(
5094 " `trait.names` is NULL, so retrieving all traits " ,
@@ -55,15 +99,15 @@ get.trait.data <-
5599 # NOTE: Use `format` here to avoid implicit (incorrect) coercion
56100 # to double by `lapply`. This works fine if we switch to
57101 # `query_priors`, but haven't done so yet because that requires
58- # prepared statements and therefore requires the Postgres driver.
102+ # prepared statements and therefore requires the Postgres driver.
59103 all_priors_list <- lapply(format(pft_ids , scientific = FALSE ), query.priors ,
60104 con = dbcon , trstr = trait.names )
61105 trait.names <- unique(unlist(lapply(all_priors_list , rownames )))
62106 # Eventually, can replace with this:
63107 # all_priors <- query_priors(pfts, params = database)
64108 # trait.names <- unique(all_priors[["name"]])
65109 }
66-
110+
67111 # process all pfts
68112 result <- lapply(pfts , get.trait.data.pft ,
69113 modeltype = modeltype ,
@@ -72,6 +116,6 @@ get.trait.data <-
72116 write = write ,
73117 forceupdate = forceupdate ,
74118 trait.names = trait.names )
75-
119+
76120 invisible (result )
77- }
121+ }
0 commit comments