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- input_file = NULL ) {
36-
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 ) {
3743 if (! is.list(pfts )) {
38- PEcAn.logger :: logger.severe(' pfts must be a list' )
44+ PEcAn.logger :: logger.severe(" pfts must be a list" )
3945 }
4046 # Check that all PFTs have associated outdir entries
41- pft_outdirs <- lapply(pfts , ' [[ ' , ' outdir' )
47+ pft_outdirs <- lapply(pfts , " [[ " , " outdir" )
4248 if (any(sapply(pft_outdirs , is.null ))) {
43- 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` " )
4450 }
45-
46- # check for flatfile path, if present use it
51+
52+ # check for flatfile path, if present use it
4753 file_path <- input_file %|| % pfts $ file_path
4854 if (! is.null(file_path )) {
4955 if (! file.exists(file_path )) {
5056 PEcAn.logger :: logger.error(" trait data file not found at specified path" , sQuote(file_path ))
5157 }
5258 PEcAn.logger :: logger.info(" Using flat file for trait data instead of database" )
53-
59+
5460 # Load flat file as data.frame
55- trait_data_flat <- read.csv(file_path , stringsAsFactors = FALSE )
56-
61+ trait_data_flat <- utils :: read.csv(file_path , stringsAsFactors = FALSE )
62+
5763 # Build trait.names from flat file if not already provided
5864 if (is.null(trait.names )) {
5965 pft_names <- vapply(pfts , " [[" , character (1 ), " name" )
@@ -65,24 +71,24 @@ get.trait.data <-
6571 trait_data_flat $ pft_id %in% pft_ids
6672 ])
6773 }
68-
74+
6975 # Call get.trait.data.pft with trait_data instead of dbcon
7076 result <- lapply(pfts , get.trait.data.pft ,
7177 modeltype = modeltype ,
7278 dbfiles = dbfiles ,
73- dbcon = NULL ,
74- trait_data = trait_data_flat ,
79+ dbcon = NULL ,
80+ trait_data = trait_data_flat ,
7581 write = write ,
7682 forceupdate = forceupdate ,
7783 trait.names = trait.names )
7884
79- return (invisible (result ))
85+ return (invisible (result ))
8086 }
8187
8288
8389 dbcon <- db.open(database )
8490 on.exit(db.close(dbcon ), add = TRUE )
85-
91+
8692 if (is.null(trait.names )) {
8793 PEcAn.logger :: logger.debug(paste0(
8894 " `trait.names` is NULL, so retrieving all traits " ,
@@ -93,15 +99,15 @@ get.trait.data <-
9399 # NOTE: Use `format` here to avoid implicit (incorrect) coercion
94100 # to double by `lapply`. This works fine if we switch to
95101 # `query_priors`, but haven't done so yet because that requires
96- # prepared statements and therefore requires the Postgres driver.
102+ # prepared statements and therefore requires the Postgres driver.
97103 all_priors_list <- lapply(format(pft_ids , scientific = FALSE ), query.priors ,
98104 con = dbcon , trstr = trait.names )
99105 trait.names <- unique(unlist(lapply(all_priors_list , rownames )))
100106 # Eventually, can replace with this:
101107 # all_priors <- query_priors(pfts, params = database)
102108 # trait.names <- unique(all_priors[["name"]])
103109 }
104-
110+
105111 # process all pfts
106112 result <- lapply(pfts , get.trait.data.pft ,
107113 modeltype = modeltype ,
@@ -110,6 +116,6 @@ get.trait.data <-
110116 write = write ,
111117 forceupdate = forceupdate ,
112118 trait.names = trait.names )
113-
119+
114120 invisible (result )
115- }
121+ }
0 commit comments