comparison aquainfra_ogc_api_processes.R @ 1:1ff618d89af4 draft

planemo upload for repository https://github.com/AquaINFRA/tools-ecology/tree/master commit 3989415663771a85b3a0be6c02308994ac4adc85
author ecology
date Wed, 23 Oct 2024 22:35:20 +0000
parents 0077885b6f1d
children af00a67d3649
comparison
equal deleted inserted replaced
0:0077885b6f1d 1:1ff618d89af4
3 library("getopt") 3 library("getopt")
4 4
5 cat("start generic wrapper service \n") 5 cat("start generic wrapper service \n")
6 6
7 remove_null_values <- function(x) { 7 remove_null_values <- function(x) {
8 # Check if the input is a list
9 if (is.list(x)) { 8 if (is.list(x)) {
10 # Remove NULL values and apply the function recursively to sublists
11 x <- lapply(x, remove_null_values) 9 x <- lapply(x, remove_null_values)
12 x <- x[!sapply(x, is.null)] 10 x <- x[!sapply(x, is.null)]
13 } 11 }
14 return(x) 12 return(x)
15 } 13 }
16 14
17 getParameters <- function() { 15 getParameters <- function() {
18 con <- file("inputs.json", "r") 16 con <- file("inputs.json", "r")
19 lines <- readLines(con) 17 lines <- readLines(con)
20 close(con) 18 close(con)
21 19
22 json_string <- paste(lines, collapse = "\n") 20 json_string <- paste(lines, collapse = "\n")
23 json_data <- fromJSON(json_string) 21 json_data <- fromJSON(json_string)
24 22
25 # Remove NULL values from json_data
26 cleaned_json_data <- remove_null_values(json_data) 23 cleaned_json_data <- remove_null_values(json_data)
27 return(cleaned_json_data$conditional_process) 24 return(cleaned_json_data$conditional_process)
28 } 25 }
29 26
30 parseResponseBody <- function(body) { 27 parseResponseBody <- function(body) {
36 return(jsonObject) 33 return(jsonObject)
37 } 34 }
38 35
39 getOutputs <- function(inputs, output, server) { 36 getOutputs <- function(inputs, output, server) {
40 url <- 37 url <-
41 paste(paste(server, "/processes/", sep = ""), 38 paste(paste(server, "processes/", sep = ""),
42 inputs$select_process, 39 inputs$select_process,
43 sep = "") 40 sep = "")
41 print(url)
44 request <- request(url) 42 request <- request(url)
45 response <- req_perform(request) 43 response <- req_perform(request)
46 responseBody <- parseResponseBody(response$body) 44 responseBody <- parseResponseBody(response$body)
47 outputs <- list() 45 outputs <- list()
48 46
79 req_body_json(body) %>% 77 req_body_json(body) %>%
80 req_perform() 78 req_perform()
81 79
82 cat("\n Process executed") 80 cat("\n Process executed")
83 cat("\n status: ", response$status_code) 81 cat("\n status: ", response$status_code)
84 #if ( process == "barplot-trend-results") {
85 # process = "batplot-trend-results"
86 #}
87 #href <- parseResponseBody(response$body)$outputs[[gsub("-", "_", process)]]$href
88 jobId <- parseResponseBody(response$body)$jobID 82 jobId <- parseResponseBody(response$body)$jobID
89 83
90 return(jobId) 84 return(jobId)
91 } 85 }
92 86
113 request(paste0(server, "jobs/", jobID, "/results?f=json")) %>% 107 request(paste0(server, "jobs/", jobID, "/results?f=json")) %>%
114 req_perform() 108 req_perform()
115 return(response) 109 return(response)
116 } 110 }
117 111
118 # Recursive function to search for href in a nested list
119 findHref <- function(obj) { 112 findHref <- function(obj) {
120 hrefs <- c() # Initialize an empty vector to store hrefs 113 hrefs <- c()
121
122 if (is.list(obj)) { 114 if (is.list(obj)) {
123 # If the object is a list, loop through its elements
124 for (name in names(obj)) { 115 for (name in names(obj)) {
125 element <- obj[[name]] 116 element <- obj[[name]]
126
127 if (is.list(element)) { 117 if (is.list(element)) {
128 # Recursively search if the element is another list
129 hrefs <- c(hrefs, findHref(element)) 118 hrefs <- c(hrefs, findHref(element))
130 } else if (name == "href") { 119 } else if (name == "href") {
131 # If the element has a name "href", capture its value
132 hrefs <- c(hrefs, element) 120 hrefs <- c(hrefs, element)
133 } 121 }
134 } 122 }
135 } 123 }
136 return(hrefs) 124 return(hrefs)
152 result <- getResult(server, process, jobID) 140 result <- getResult(server, process, jobID)
153 141
154 if (result$status_code == 200) { 142 if (result$status_code == 200) {
155 resultBody <- parseResponseBody(result$body) 143 resultBody <- parseResponseBody(result$body)
156 print(resultBody) 144 print(resultBody)
157
158 # Call the recursive function to find all hrefs
159 hrefs <- findHref(resultBody) 145 hrefs <- findHref(resultBody)
160 146
161 if (length(hrefs) > 0) { 147 if (length(hrefs) > 0) {
162 # Collapse the URLs with a newline
163 urls_with_newline <- paste(hrefs, collapse = "\n") 148 urls_with_newline <- paste(hrefs, collapse = "\n")
164 print(urls_with_newline) 149 print(urls_with_newline)
165
166 # Write the URLs to a file
167 con <- file(outputData, "w") 150 con <- file(outputData, "w")
168 writeLines(urls_with_newline, con = con) 151 writeLines(urls_with_newline, con = con)
169 close(con) 152 close(con)
170 } else { 153 } else {
171 print("No hrefs found.") 154 print("No hrefs found.")
188 } else { 171 } else {
189 print(paste("HTTP", status_code1, "Error:", resp1$status_message)) 172 print(paste("HTTP", status_code1, "Error:", resp1$status_message))
190 } 173 }
191 } 174 }
192 175
193
194
195 saveResult <- function(href, outputData) { 176 saveResult <- function(href, outputData) {
196 con <- file(outputData, "w") 177 con <- file(outputData, "w")
197 writeLines(href, con = con) 178 writeLines(href, con = con)
198 close(con) 179 close(con)
199 } 180 }
204 185
205 server <- "https://aqua.igb-berlin.de/pygeoapi-dev/" 186 server <- "https://aqua.igb-berlin.de/pygeoapi-dev/"
206 187
207 print("--> Retrieve parameters") 188 print("--> Retrieve parameters")
208 inputParameters <- getParameters() 189 inputParameters <- getParameters()
209 #print(inputParameters)
210 print("--> Parameters retrieved") 190 print("--> Parameters retrieved")
211 191
212 args <- commandArgs(trailingOnly = TRUE) 192 args <- commandArgs(trailingOnly = TRUE)
213 outputLocation <- args[2] 193 outputLocation <- args[2]
214 194
215 print("--> Retrieve outputs") 195 print("--> Retrieve outputs")
216 outputs <- getOutputs(inputParameters, outputLocation, server) 196 outputs <- getOutputs(inputParameters, outputLocation, server)
217 print("--> Outputs retrieved") 197 print("--> Outputs retrieved")
218 198
219 print("--> Parse inputs") 199 print("--> Parse inputs")
200
220 convertedKeys <- c() 201 convertedKeys <- c()
202
221 for (key in names(inputParameters)) { 203 for (key in names(inputParameters)) {
222 if (is.character(inputParameters[[key]]) && 204 if (is.character(inputParameters[[key]]) &&
223 (endsWith(inputParameters[[key]], ".dat") || 205 (endsWith(inputParameters[[key]], ".dat") ||
224 endsWith(inputParameters[[key]], ".txt"))) { 206 endsWith(inputParameters[[key]], ".txt"))) {
225 con <- file(inputParameters[[key]], "r") 207 con <- file(inputParameters[[key]], "r")
226 url_list <- list() 208 url_list <- list()
227 #while (length(line <- readLines(con, n = 1)) > 0) { 209
228 # if (is_url(line)) {
229 # url_list <- c(url_list, list(list(href = trimws(line))))
230 # }
231 #}
232 con <- file(inputParameters[[key]], "r") 210 con <- file(inputParameters[[key]], "r")
233 lines <- readLines(con) 211 lines <- readLines(con)
234 print("--------------------------------------------------------------------1")
235 print(length(lines)) 212 print(length(lines))
236 close(con) 213 close(con)
237 if (!length(lines) > 1 && endsWith(lines, ".jp2") && startsWith(lines, "https")) { 214
238 print("--------------------------------------------------------------------2") 215 json_string <- paste(lines, collapse = "\n")
239 tmp <- list() 216 inputParameters[[key]] <- json_string
240 tmp$href <- lines 217
241 tmp$type <- "image/jp2"
242 inputParameters[[key]] <- tmp
243 }
244 else if (!length(lines) > 1 && endsWith(lines, ".zip") && startsWith(lines, "https")) {
245 print("--------------------------------------------------------------------3")
246 json_string <- paste(lines, collapse = "\n")
247 inputParameters[[key]] <- json_string
248 } else if (!length(lines) > 1 && (endsWith(lines, ".xlsx") || endsWith(lines, ".csv") || grepl("f=csv", lines)) && startsWith(lines, "https")) {
249 print("--------------------------------------------------------------------4")
250 json_string <- paste(lines, collapse = "\n")
251 inputParameters[[key]] <- json_string
252 } else if (inputParameters$select_process == "plot-image" ||
253 inputParameters$select_process == "reproject-image") {
254 print("--------------------------------------------------------------------5")
255 tmp <- list()
256 tmp$href <- lines
257 tmp$type <- "image/tiff; application=geotiff"
258 if (inputParameters$select_process == "reproject-image") {
259 tmp$type <- "image/tiff; subtype=geotiff"
260 }
261 inputParameters[[key]] <- tmp
262 } else {
263 print("-----------------------------------6")
264 json_string <- paste(lines, collapse = "\n")
265 json_data <- fromJSON(json_string)
266 inputParameters[[key]] <- json_data
267 }
268 convertedKeys <- append(convertedKeys, key) 218 convertedKeys <- append(convertedKeys, key)
269 } 219 }
270 else if (grepl("_Array_", key)) { 220 else if (grepl("_Array_", key)) {
271 keyParts <- strsplit(key, split = "_")[[1]] 221 keyParts <- strsplit(key, split = "_")[[1]]
272 type <- keyParts[length(keyParts)] 222 type <- keyParts[length(keyParts)]
294 } 244 }
295 convertedKey <- substr(convertedKey, 1, nchar(convertedKey) - 1) 245 convertedKey <- substr(convertedKey, 1, nchar(convertedKey) - 1)
296 } 246 }
297 247
298 inputParameters[[key]] <- convertedValues 248 inputParameters[[key]] <- convertedValues
299 print("-------------------------")
300 print(convertedValues)
301 print("-------------------------")
302 convertedKeys <- append(convertedKeys, convertedKey) 249 convertedKeys <- append(convertedKeys, convertedKey)
303 } else { 250 } else {
304 print("-------------------------")
305 print(key)
306 print(inputParameters[[key]])
307 if (!is.null(inputParameters[[key]])) { 251 if (!is.null(inputParameters[[key]])) {
308 convertedKeys <- append(convertedKeys, key) 252 convertedKeys <- append(convertedKeys, key)
309 } 253 }
310 print("-------------------------")
311
312 } 254 }
313 } 255 }
314 print(inputParameters) 256 print(inputParameters)
315 names(inputParameters) <- convertedKeys 257 names(inputParameters) <- convertedKeys
316 #print(inputParameters)
317 print("--> Inputs parsed") 258 print("--> Inputs parsed")
318 259
319 print("--> Prepare process execution") 260 print("--> Prepare process execution")
320 jsonData <- list("inputs" = inputParameters, 261 jsonData <- list("inputs" = inputParameters,
321 "outputs" = outputs) 262 "outputs" = outputs)