-
Notifications
You must be signed in to change notification settings - Fork 282
Add function to Optionally get site.info if not present
#3324
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
Changes from all commits
cb41971
8b82f2b
8c998fd
7a5a848
74365fd
a0ae689
18dd086
7a69113
604634b
0f154e5
fae37a8
cb42033
5ee450a
e37b93b
144ce9c
54ce644
6d76455
ca0b50e
8082194
65cdea7
acb8507
59f451a
f0ca425
e59c981
d66503f
6d0b730
f5fac44
9d328eb
45fc781
16a9b93
6a4b131
798d09f
5ba853d
e2cf4f1
18cc1ae
3ee93a5
e7be402
a314897
92b9b06
e76124d
dff7ab8
903efc9
14950aa
d392847
0ac5b39
2639e88
308380e
9edc268
a7c4b7d
59b293e
b685c58
65b976d
99e4345
a4025a8
f6390eb
5135e82
c411a6e
0009c6e
9039c23
71e293c
3c537f6
f9e8b4a
2939e52
06b62db
4f42372
25f5548
013c9eb
a3faa1c
7831dc7
b599171
81f53d3
a68c928
bd58f69
8132ae6
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 |
|---|---|---|
|
|
@@ -29,7 +29,9 @@ export(derive.trait) | |
| export(derive.traits) | ||
| export(dplyr.count) | ||
| export(fancy_scientific) | ||
| export(generate_site_id) | ||
| export(get.id) | ||
| export(get.new.site) | ||
| export(get.trait.data) | ||
| export(get.trait.data.pft) | ||
| export(get_postgres_envvars) | ||
|
|
@@ -63,6 +65,7 @@ export(try2sqlite) | |
| export(var_names_all) | ||
| export(workflow) | ||
| export(workflows) | ||
| importFrom(digest,digest) | ||
|
Member
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. Should not be needed |
||
| importFrom(magrittr,"%>%") | ||
| importFrom(rlang,"!!!") | ||
| importFrom(rlang,"!!") | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,121 @@ | ||||
| ##' Get new site info using provided site information | ||||
Sweetdevil144 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| ##' | ||||
| ##' @title Get New Site Info | ||||
| ##' @param site a dataframe with site information including id, lat, lon, and time_zone. | ||||
| ##' @param con Database connection object. | ||||
| ##' @return a dataframe with new site information on lat, lon and time_zone | ||||
| ##' @export | ||||
| ##' @author Abhinav Pandey | ||||
| ##' @importFrom digest digest | ||||
|
Member
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. What does this do? First, as far as I can tell this function is never used here. Second, where it is used I don't see why it can't be called by namespace |
||||
| ##' | ||||
| ##' @examples | ||||
| ##' get.new.site(site=data.frame(id=1,lat=40.1,lon=-88.2, time_zone = "UTC"), con = NULL) | ||||
|
|
||||
| get.new.site <- function(site, con = NULL) { | ||||
| if (is.null(con)) { | ||||
| PEcAn.logger::logger.debug("DB connection is closed. Trying to generate a new site ID or use pre-existing one.") | ||||
| # No DB connection present. Generate a new ID using one of below steps: | ||||
|
|
||||
| if (is.null(site$id) | is.na(site$id)) { | ||||
| if ((!is.null(site$lat) && !is.null(site$lon)) && | ||||
| (!is.na(site$lat) && !is.na(site$lon)) | ||||
Sweetdevil144 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| ) { | ||||
| site.id <- paste0(site$lat, "_", site$lon) | ||||
| new.site <- data.frame( | ||||
| id = as.numeric(site.id), | ||||
|
Member
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. the as.numeric here seems like it's going to always return NA given how site.id is constructed |
||||
| lat = site$lat, | ||||
| lon = site$lon | ||||
| ) | ||||
| str_ns <- paste0(new.site$lat, "_", new.site$lon) | ||||
|
Member
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. this seems redundant with site.id |
||||
| } # lat/lon present but ID is absent | ||||
| # Use Pre-existing normalised lat/lon | ||||
| else { | ||||
| PEcAn.logger::logger.warn("Site dataframe does not have an id column") | ||||
| site.id <- generate_site_id() | ||||
|
Member
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. This function has two non-optional arguments that aren't being specified, so how is this code not throwing an error when you run it? |
||||
| PEcAn.logger::logger.info(paste0("Generated siteID:", site.id)) | ||||
| # Optionally, create a new site data frame with the random ID | ||||
| new.site <- data.frame( | ||||
| id = site.id, | ||||
| lat = NULL, | ||||
| lon = NULL | ||||
| ) | ||||
| str_ns <- paste0(new.site$id %/% 1e+09, "_", new.site$id %% 1e+09) | ||||
| # We used a different str_ns as identifier here due to absence of lat/lon | ||||
| } | ||||
| # ID as well as lat/lon absent. | ||||
| # Return a WARN as we will be unable to identify such an instance due to lack of information. | ||||
| # We'll try to Generate a new ID similar to previous ones. | ||||
| } else { | ||||
| if ((!is.null(site$lat) && !is.null(site$lon)) && | ||||
| (!is.na(site$lat) && !is.na(site$lon)) | ||||
| ) { | ||||
| new.site <- data.frame( | ||||
| id = as.numeric(site$id), | ||||
| lat = site$lat, | ||||
| lon = site$lon | ||||
| ) | ||||
| str_ns <- paste0(new.site$lat, "-", new.site$lon) | ||||
| } # siteId as well as lat/lon present | ||||
| else { | ||||
| new.site <- data.frame( | ||||
| id = site$id, | ||||
| lat = NULL, | ||||
| lon = NULL | ||||
| ) | ||||
| str_ns <- paste0(new.site$id %/% 1e+09, "-", new.site$id %% 1e+09) | ||||
| } # siteId present but lat/lon absent | ||||
| } | ||||
| } else { | ||||
| # Check if site dataframe has an id column | ||||
| if (is.null(site$id) | is.na(site$id)) { | ||||
| PEcAn.logger::logger.warn("Site dataframe does not have an id column. Generating a unique ID") | ||||
| if ((!is.null(site$lat) && !is.null(site$lon)) && | ||||
| (!is.na(site$lat) && !is.na(site$lon)) | ||||
| ) { | ||||
| PEcAn.logger::logger.info(paste0("Generated siteID using lat and lon:", site.id)) | ||||
| site.id <- generate_site_id(site$lat, site$lon) | ||||
| new.site <- data.frame( | ||||
| id = as.numeric(site.id), | ||||
|
Member
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. does this id need to be numeric? If so, why? Is the algorithm generating it guaranteed to produce numbers?
Member
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. It won't be numeric if using digest as is. |
||||
| lat = site$lat, | ||||
| lon = site$lon | ||||
| ) | ||||
| str_ns <- paste0(site$lat, "-", site$lon) | ||||
| } else { | ||||
| PEcAn.logger::logger.severe("Missing site-id, site lat & site-lon. Stopping the process due to lack of information") | ||||
| } | ||||
| } else { | ||||
| # setup site database number, lat, lon and name and copy for format.vars if new input | ||||
| if ((!is.null(site$lat) && !is.null(site$lon)) | | ||||
| (!is.na(site$lat) && !is.na(site$lon)) | ||||
| ) { | ||||
| new.site <- data.frame( | ||||
| id = as.numeric(site$id), | ||||
| lat = site$lat, | ||||
| lon = site$lon | ||||
| ) | ||||
| str_ns <- paste0(site$lat, "_", site$lon) | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| site.info <- list(new.site = new.site, str_ns = str_ns) | ||||
|
Member
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. Q in live review: why this structure? A: to match what's expected at met.process line 165
Contributor
Author
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. Detailed Explanation : Current structure is followed to match data flow of what information is later on utilised in |
||||
|
|
||||
| return(site.info) | ||||
| } | ||||
|
|
||||
|
|
||||
| #' Generate a unique site ID from latitude/longitude | ||||
| #' | ||||
| #' @param lat Latitude value | ||||
| #' @param lon Longitude value | ||||
| #' @return A unique string ID generated by hashing the lat/lon values | ||||
| #' @details This function is intended for database-less runs only by generating | ||||
| #' a unique site ID by hashing the rounded lat/lon coordinates. | ||||
| #' | ||||
| #' @author Abhinav Pandey | ||||
| #' @export | ||||
| generate_site_id <- function(lat, lon) { | ||||
| latlon_str <- paste0(lat, lon) | ||||
| uid <- digest::digest(latlon_str, algo = "xxhash64") | ||||
|
Member
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. Do we really need to add an entirely new package dependency to PEcAn just for this one call in one function? Is there an easier solution, or one that relies on existing packages?
Member
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. Doesn't necessarily warrant using it, but that was my suggestion since I was using it in ccmmf workflows, and it was already a dependency b/c of the api. Line 14 in 7faa280
Member
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. There's also |
||||
| return(uid) | ||||
| } | ||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,11 +142,11 @@ met.process <- function(site, input_met, start_date, end_date, model, | |
|
|
||
|
|
||
| # setup site database number, lat, lon and name and copy for format.vars if new input | ||
| latlon <- PEcAn.DB::query.site(site$id, con = con)[c("lat", "lon")] | ||
| new.site <- data.frame(id = as.numeric(site$id), | ||
| lat = latlon$lat, | ||
| lon = latlon$lon) | ||
| str_ns <- paste0(new.site$id %/% 1e+09, "-", new.site$id %% 1e+09) | ||
| site.info <- PEcAn.DB::get.new.site(site, con=con, latlon = latlon) | ||
|
Member
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. latlon undefined |
||
|
|
||
| # extract new.site and str_ns from site.info | ||
| new.site <- site.info$new.site | ||
|
Member
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. Hypothesis to confirm before acting: No downstream object uses new.site$id, can remove it here
Contributor
Author
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.
|
||
| str_ns <- site.info$str_ns | ||
|
|
||
| if (is.null(format.vars$lat)) { | ||
| format.vars$lat <- new.site$lat | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,14 +6,15 @@ | |
| ##' @param input Taken from settings$run$inputs. This should include id, path, and source | ||
| ##' @param dir settings$database$dbfiles | ||
| ##' @param overwrite Default = FALSE. whether to force ic_process to proceed | ||
| ##' @param site Current site information | ||
|
Member
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. This line is defining a new argument to the function, but the argument has't been added to the function itself |
||
| ##' | ||
| ##' @author Istem Fer, Hamze Dokoohaki | ||
| ic_process <- function(settings, input, dir, overwrite = FALSE){ | ||
|
|
||
|
|
||
| #--------------------------------------------------------------------------------------------------# | ||
| # Extract info from settings and setup | ||
| site <- settings$run$site | ||
| site <- settings$run$site | ||
| model <- list() | ||
| model$type <- settings$model$type | ||
| model$id <- settings$model$id | ||
|
|
@@ -49,21 +50,16 @@ ic_process <- function(settings, input, dir, overwrite = FALSE){ | |
| con <- PEcAn.DB::db.open(dbparms$bety) | ||
| on.exit(PEcAn.DB::db.close(con), add = TRUE) | ||
|
|
||
| #grab site lat and lon info | ||
| latlon <- PEcAn.DB::query.site(site$id, con = con)[c("lat", "lon")] | ||
| # setup site database number, lat, lon and name and copy for format.vars if new input | ||
| new.site <- data.frame(id = as.numeric(site$id), | ||
| lat = latlon$lat, | ||
| lon = latlon$lon) | ||
| # Setup site database number, lat, lon and name and copy for format.vars if new input | ||
| # Then extract new.site and str_ns from site.info | ||
| site.info <- PEcAn.DB::get.new.site(site, con=con, latlon = latlon) | ||
|
Member
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. latlon undefined |
||
| new.site <- site.info$new.site | ||
| str_ns <- site.info$str_ns | ||
|
|
||
| new.site$name <- settings$run$site$name | ||
|
|
||
| if (isTRUE(new.site$id > 1e9)) { | ||
| # Assume this is a BETYdb id, condense for readability | ||
| str_ns <- paste0(new.site$id %/% 1e+09, "-", new.site$id %% 1e+09) | ||
| } else { | ||
| str_ns <- as.character(site$id) | ||
| } | ||
| str_ns <- paste0(new.site$id %/% 1e+09, "-", new.site$id %% 1e+09) | ||
|
|
||
|
|
||
| outfolder <- file.path(dir, paste0(input$source, "_site_", str_ns)) | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Package added as a dependency, but I can't find any places in your PR where it's actually used. Can this be removed?