Skip to content

Commit

Permalink
mostly solves #94 and #39
Browse files Browse the repository at this point in the history
* compute_results has now parameters 'as_stars' and 'format' to open the
data as stars object
* 'output_file' can be omitted, if so a temporary file will be created
  • Loading branch information
flahn committed Feb 18, 2022
1 parent 1ba9b08 commit c312b5c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 23 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Suggests:
testthat,
knitr,
sf,
stars,
geojsonsf,
pkgdown,
rmarkdown,
Expand Down
49 changes: 36 additions & 13 deletions R/jobs.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ list_jobs = function(con=NULL) {
#' @param output_file storage location for the returned data
#' @param budget numeric, maximum spendable amount for testing
#' @param plan character, selection of a service plan
#' @param as_stars logical to indicate if the data shall be interpreted as a stars object
#' @param format character or \code{FileFormat} specifying the File format for the output
#' @param con connected and authenticated openEO client (optional) otherwise \code{\link{active_connection}}
#' is used.
#' @param ... additional parameters passed to jsonlite::toJSON() (like 'digits')
#'
#' @return A connection to a file if the parameter 'output_file' was provided otherwise the raw binary data
#' @return a local path to the downloaded file or a \code{stars} object if \code{as_stars=TRUE}
#'
#' @importFrom methods as
#' @export
compute_result = function(graph, output_file = NULL, budget=NULL, plan=NULL, con=NULL, ...) {
compute_result = function(graph, output_file = NULL, budget=NULL, plan=NULL, as_stars=FALSE, format = NULL, con=NULL, ...) {
tryCatch({
con = .assure_connection(con)

Expand Down Expand Up @@ -95,21 +97,42 @@ compute_result = function(graph, output_file = NULL, budget=NULL, plan=NULL, con
}

tag = "execute_sync"
# res = con$request(tag = tag, authorized = TRUE, data = job, encodeType = "json", raw = TRUE, ...)
res = con$request(tag = tag, authorized = TRUE, data = job, encodeType = "json", parsed=FALSE, ...)

if (!is.null(output_file)) {
tryCatch({
writeBin(resp_body_raw(res), output_file)
message("Task result was sucessfully stored into a local file.")
}, error = function(err) {
stop(err)
})

return(output_file)
if (length(format) > 0 && length(output_file) == 0) {
if (is.character(format)) {
driver = format
} else if ("FileFormat" %in% class(format)) {
driver = format$name
} else {
message("Cannot derive a file extension. Using none which might affect data interpretation.")
driver = ""
}

suffix = switch(driver, netCDF=".nc",GTiff=".tif",JSON=".json",default="")
output_file = tempfile(fileext = suffix)
} else {
return(resp_body_raw(res))
# TODO now we need to look into the process graph and search for save_result
}

tryCatch({
writeBin(resp_body_raw(res), output_file)
}, error = function(err) {
stop(err)
})

if (isTRUE(as_stars) && isNamespaceLoaded("stars")) {
obj=stars::read_stars(output_file,proxy=FALSE,quiet=TRUE)

tryCatch({
return(obj)
}, finally={
unlink(output_file)
})
} else {
return(output_file)
}

}, error = .capturedErrorToMessage)
}

Expand Down
39 changes: 32 additions & 7 deletions R/sample_data.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
#' @include jobs.R
NULL

#' Get sample data
#'
#' In order to inspect data locally a very small spatial extent will be processed, downloaded and made available in R.
#'
#' @details
#' In order to get a better understanding about the processing mechanisms and the data structures used in the openEO backend, it helps
#' to check the actual data from time to time. This function aids the user in doing to. It replaces all spatial extents of the derived
#' process graph with a new spatial extent which is calculated by the first spatial extent of the mandatory openEO process 'load_collection'.
#' We take the center of the extent and add 0.0003 degrees to it. Currently just spatial bounding boxes with the default CRS EPSG:4326 are
#' replaced.
#'
#' @param graph a ProcessGraph, a Process or the final node in a process for which the sample shall be calculated
#' @param replace_aoi a logical flag to indicate whether or not the original spatial extent shall be substituted with a different one, default TRUE
#' @param execution \code{sync} or \code{async} which indicates the processing chain, a not "async" value results in a synchronous processing
#' @param immediate flag to be considered if the retrieval shall be immediately queued on the back-end
#' @param con connected and authenticated openEO client (optional) otherwise \code{\link{active_connection}}
#' is used.
#' @param ... additional parameters that are passed to \code{\link{compute_result}} or \code{\link{create_job}}
#'
#' @export
get_sample = function(graph, replace_aoi = TRUE,execution="sync",immediate=TRUE,con=NULL, ...) {
tryCatch({
con = openeo:::.assure_connection(con)
con = .assure_connection(con)
dots = list(...)

if (isTRUE(replace_aoi)) {
Expand Down Expand Up @@ -44,7 +64,8 @@ get_sample = function(graph, replace_aoi = TRUE,execution="sync",immediate=TRUE,
center = c(lon=mean(v[["west"]],v[["east"]]),
lat=mean(v[["south"]],v[["north"]]))
dlon = 0.0003 #between 30 and 40m
dlat = (1-abs(center["lat"])/90) * dlon
dlat = 0.0003

sample_extent = list(west = center["lon"]-dlon/2,
east = center["lon"]+dlon/2,
south=center["lat"]-dlat/2,
Expand All @@ -56,22 +77,26 @@ get_sample = function(graph, replace_aoi = TRUE,execution="sync",immediate=TRUE,
}
}


if (!is.null(execution) && execution == "async") {
# create job
arg_names = names(formals(create_job))
arg_names = arg_names[-which("..." == names(arg_names))]
job_meta = dots[which(arg_names %in% names(dots))]
job = do.call(create_job, c(list(graph=graph),job_meta))
# queue job

# queue job
if (isTRUE(immediate)) {
start_job(job,con=con)
job = start_job(job,con=con)
}
# download results
return(job)
# download results on your own
} else {
# compute_results
return(compute_result(graph = graph, ...))
arg_names = names(formals(compute_result))
compute_config = dots[which(names(dots) %in% arg_names)]
res = do.call(compute_result, c(list(graph=graph,con=con),compute_config))

return(res)
}
return(graph)
})
Expand Down
8 changes: 7 additions & 1 deletion man/compute_result.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/download_results.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions man/get_sample.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c312b5c

Please sign in to comment.