# HG changeset patch # User ecology # Date 1710247199 0 # Node ID 0ed34e3202b984f1e6a95ddcb302c128c19ef736 planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/OtbBandMath commit d85a00bfd9f603fd25ad0fc93736d0b1e395fe25 diff -r 000000000000 -r 0ed34e3202b9 OTB_BandMath.R --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OTB_BandMath.R Tue Mar 12 12:39:59 2024 +0000 @@ -0,0 +1,150 @@ +#Run with Rscript ./OTB_BandMath.R --file otb_band_math_test_input.txt --processingMemory 256 --mathExpression im1b3,im1b2,im1b1 --outputType png --outputFormat download --outputImage float --outputData otb_band_math_test_output.png + +library("httr2") +library("jsonlite") +library("getopt") + +args <- commandArgs(trailingOnly = TRUE) +option_specification <- matrix(c( + 'file', 'i1', 1, 'character', + 'processingMemory', 'i2', 2, 'integer', + 'mathExpression', 'i3', 2, 'character', + 'outputType', 'i4', 2, 'character', + 'outputFormat', 'i5', 1, 'character', + 'outputImage', 'i6', 1, 'character', + 'outputData', 'o', 1, 'character' +), byrow = TRUE, ncol = 4) +options <- getopt(option_specification) + +file <- options$file +processingMemory <- options$processingMemory +mathExpression <-options$mathExpression +outputType <- paste0("image/", options$outputType) +outputFormat <- options$outputFormat +outputImage <- options$outputImage +outputData <- options$outputData + +cat("\n file: ", file) +cat("\n ram: ", processingMemory) +cat("\n exp: ", mathExpression) +cat("\n outputType: ", outputType) +cat("\n outputFormat: ", outputFormat) +cat("\n outputImage: ", outputImage) + +baseUrl <- "https://ospd.geolabs.fr:8300/ogc-api/" +execute <- "processes/OTB.BandMath/execution" +getStatus <- "jobs/" +getResult <- "/results" + +file_urls <- readLines(file, warn = FALSE) + +il_list <- lapply(file_urls, function(url) { + list("href" = url) +}) + +json_data <- list( + "inputs" = list( + "il" = il_list, + "out" = outputImage, + "exp" = mathExpression, + "ram" = processingMemory + ), + "outputs" = list( + "out" = list( + "format" = list( + "mediaType" = outputType + ), + "transmissionMode" = "reference" + ) + ) +) + +makeResponseBodyReadable <- function(body) { + hex <- c(body) + int_values <- as.integer(hex) + raw_vector <- as.raw(int_values) + readable_output <- rawToChar(raw_vector) + json_object <- jsonlite::fromJSON(readable_output) + return(json_object) +} + +tryCatch({ + #Request 1 + resp1 <- request(paste0(baseUrl, execute)) %>% + req_headers( + 'accept' = '/*', + 'Prefer' = 'respond-async;return=representation', + 'Content-Type' = 'application/json' + ) %>% + req_body_json(json_data) %>% + req_perform() + + response <- makeResponseBodyReadable(resp1$body) + status_code1 <- resp1$status_code + + if (status_code1 == 201) { + status <- "running" + attempt = 1 + while (status == "running") { + #Request 2 + #cat("\n",response$jobID) + resp2 <- request(paste0(baseUrl,getStatus,response$jobID)) %>% + req_headers( + 'accept' = 'application/json' + ) %>% + req_perform() + status_code2 <- resp2$status_code + #cat("\n", status_code2) + if (status_code2 == 200) { + response2 <- makeResponseBodyReadable(resp2$body) + cat("\n", response2$status) + if (response2$status=="successful") { + status <- "successful" + #Request 3 + resp3 <- request(paste0(baseUrl,getStatus, response2$jobID, getResult)) %>% + req_headers( + 'accept' = 'application/json' + ) %>% + req_perform() + status_code3 <- resp3$status_code + if (status_code3 == 200) { + response3 <- makeResponseBodyReadable(resp3$body) + if (outputFormat == "download") { + options(timeout=300) + download.file(response3$out$href, destfile = outputData, mode = "wb") + } else if (outputFormat == "getUrl") { + writeLines(response3$out$href, con = outputData) + } + } else if (status_code3 == 404) { + print("The requested URI was not found.") + } else if (status_code3 == 500) { + print("A server error occurred.") + } else { + print(paste("HTTP", status_code3, "Error:", resp3$status_message)) + } + } else if (response2$status=="failed") { + status <- "failed" + } + #else { + # attempt <- attempt +1 + # if (attempt == 200) { + # status <- "failed" + # } + #} + } else { + status <- "failed" + print(paste("HTTP", status_code2, "Error:", resp2$status_message)) + } + Sys.sleep(3) + } + print(status) + } else if (status_code1 == 400) { + print("A query parameter has an invalid value.") + } else if (status_code1 == 404) { + print("The requested URI was not found.") + } else if (status_code1 == 500) { + print("The requested URI was not found.") + } else { + print(paste("HTTP", status_code1, "Error:", resp1$status_message)) + } +}) \ No newline at end of file diff -r 000000000000 -r 0ed34e3202b9 OTB_BandMath.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OTB_BandMath.xml Tue Mar 12 12:39:59 2024 +0000 @@ -0,0 +1,92 @@ + + outputs a monoband image as a result of a mathematical operation on several multi-band images + + r-base + r-getopt + r-httr2 + r-jsonlite + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @Manual{httr2, + title = {httr2: Perform HTTP Requests and Process the Responses}, + author = {Hadley Wickham}, + year = {2023}, + note = {R package version 1.0.0, https://github.com/r-lib/httr2}, + url = {https://httr2.r-lib.org}, + } + + 10.48550/arXiv.1403.2805 + + \ No newline at end of file diff -r 000000000000 -r 0ed34e3202b9 test-data/otb_band_math_test1_output.png Binary file test-data/otb_band_math_test1_output.png has changed diff -r 000000000000 -r 0ed34e3202b9 test-data/otb_band_math_test2_output.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/otb_band_math_test2_output.txt Tue Mar 12 12:39:59 2024 +0000 @@ -0,0 +1,1 @@ +https://ospd.geolabs.fr:8300/cgi-bin/mapserv?map=/usr/com/zoo-project/out_0_5a749926-e064-11ee-ad8b-0242ac10ee0a.map&request=GetMap&service=WMS&version=1.3.0&layers=out&width=640.000&height=640.000&format=image/png&bbox=499980.000000000000000,5690220.000000000000000,609780.000000000000000,5800020.000000000000000&crs=EPSG:32631 diff -r 000000000000 -r 0ed34e3202b9 test-data/otb_band_math_test_input.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/otb_band_math_test_input.txt Tue Mar 12 12:39:59 2024 +0000 @@ -0,0 +1,1 @@ +https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/31/U/ET/2019/4/S2B_31UET_20190421_0_L2A/TCI.tif