-
Couldn't load subscription status.
- Fork 271
Add flat-file bypass for get.trait.data() #3601
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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') | ||||||||||||||||||||||||||||||
|
|
@@ -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) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (use_flatfile) { | ||||||||||||||||||||||||||||||
|
Comment on lines
+46
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||||||
| 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) | ||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # 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) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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).