comparison OTB_BandMath.R @ 0:0ed34e3202b9 draft

planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/OtbBandMath commit d85a00bfd9f603fd25ad0fc93736d0b1e395fe25
author ecology
date Tue, 12 Mar 2024 12:39:59 +0000
parents
children d266345e510d
comparison
equal deleted inserted replaced
-1:000000000000 0:0ed34e3202b9
1 #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
2
3 library("httr2")
4 library("jsonlite")
5 library("getopt")
6
7 args <- commandArgs(trailingOnly = TRUE)
8 option_specification <- matrix(c(
9 'file', 'i1', 1, 'character',
10 'processingMemory', 'i2', 2, 'integer',
11 'mathExpression', 'i3', 2, 'character',
12 'outputType', 'i4', 2, 'character',
13 'outputFormat', 'i5', 1, 'character',
14 'outputImage', 'i6', 1, 'character',
15 'outputData', 'o', 1, 'character'
16 ), byrow = TRUE, ncol = 4)
17 options <- getopt(option_specification)
18
19 file <- options$file
20 processingMemory <- options$processingMemory
21 mathExpression <-options$mathExpression
22 outputType <- paste0("image/", options$outputType)
23 outputFormat <- options$outputFormat
24 outputImage <- options$outputImage
25 outputData <- options$outputData
26
27 cat("\n file: ", file)
28 cat("\n ram: ", processingMemory)
29 cat("\n exp: ", mathExpression)
30 cat("\n outputType: ", outputType)
31 cat("\n outputFormat: ", outputFormat)
32 cat("\n outputImage: ", outputImage)
33
34 baseUrl <- "https://ospd.geolabs.fr:8300/ogc-api/"
35 execute <- "processes/OTB.BandMath/execution"
36 getStatus <- "jobs/"
37 getResult <- "/results"
38
39 file_urls <- readLines(file, warn = FALSE)
40
41 il_list <- lapply(file_urls, function(url) {
42 list("href" = url)
43 })
44
45 json_data <- list(
46 "inputs" = list(
47 "il" = il_list,
48 "out" = outputImage,
49 "exp" = mathExpression,
50 "ram" = processingMemory
51 ),
52 "outputs" = list(
53 "out" = list(
54 "format" = list(
55 "mediaType" = outputType
56 ),
57 "transmissionMode" = "reference"
58 )
59 )
60 )
61
62 makeResponseBodyReadable <- function(body) {
63 hex <- c(body)
64 int_values <- as.integer(hex)
65 raw_vector <- as.raw(int_values)
66 readable_output <- rawToChar(raw_vector)
67 json_object <- jsonlite::fromJSON(readable_output)
68 return(json_object)
69 }
70
71 tryCatch({
72 #Request 1
73 resp1 <- request(paste0(baseUrl, execute)) %>%
74 req_headers(
75 'accept' = '/*',
76 'Prefer' = 'respond-async;return=representation',
77 'Content-Type' = 'application/json'
78 ) %>%
79 req_body_json(json_data) %>%
80 req_perform()
81
82 response <- makeResponseBodyReadable(resp1$body)
83 status_code1 <- resp1$status_code
84
85 if (status_code1 == 201) {
86 status <- "running"
87 attempt = 1
88 while (status == "running") {
89 #Request 2
90 #cat("\n",response$jobID)
91 resp2 <- request(paste0(baseUrl,getStatus,response$jobID)) %>%
92 req_headers(
93 'accept' = 'application/json'
94 ) %>%
95 req_perform()
96 status_code2 <- resp2$status_code
97 #cat("\n", status_code2)
98 if (status_code2 == 200) {
99 response2 <- makeResponseBodyReadable(resp2$body)
100 cat("\n", response2$status)
101 if (response2$status=="successful") {
102 status <- "successful"
103 #Request 3
104 resp3 <- request(paste0(baseUrl,getStatus, response2$jobID, getResult)) %>%
105 req_headers(
106 'accept' = 'application/json'
107 ) %>%
108 req_perform()
109 status_code3 <- resp3$status_code
110 if (status_code3 == 200) {
111 response3 <- makeResponseBodyReadable(resp3$body)
112 if (outputFormat == "download") {
113 options(timeout=300)
114 download.file(response3$out$href, destfile = outputData, mode = "wb")
115 } else if (outputFormat == "getUrl") {
116 writeLines(response3$out$href, con = outputData)
117 }
118 } else if (status_code3 == 404) {
119 print("The requested URI was not found.")
120 } else if (status_code3 == 500) {
121 print("A server error occurred.")
122 } else {
123 print(paste("HTTP", status_code3, "Error:", resp3$status_message))
124 }
125 } else if (response2$status=="failed") {
126 status <- "failed"
127 }
128 #else {
129 # attempt <- attempt +1
130 # if (attempt == 200) {
131 # status <- "failed"
132 # }
133 #}
134 } else {
135 status <- "failed"
136 print(paste("HTTP", status_code2, "Error:", resp2$status_message))
137 }
138 Sys.sleep(3)
139 }
140 print(status)
141 } else if (status_code1 == 400) {
142 print("A query parameter has an invalid value.")
143 } else if (status_code1 == 404) {
144 print("The requested URI was not found.")
145 } else if (status_code1 == 500) {
146 print("The requested URI was not found.")
147 } else {
148 print(paste("HTTP", status_code1, "Error:", resp1$status_message))
149 }
150 })