changeset 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
files OTB_BandMath.R OTB_BandMath.xml test-data/otb_band_math_test1_output.png test-data/otb_band_math_test2_output.txt test-data/otb_band_math_test_input.txt
diffstat 5 files changed, 244 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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
--- /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 @@
+<tool id="otb_band_math" name="OTB.BandMath" version="1.0" profile="22.05">
+    <description> outputs a monoband image as a result of a mathematical operation on several multi-band images</description>
+    <requirements>
+        <requirement type="package" version="4.3.1">r-base</requirement>
+        <requirement type="package" version="1.20.4">r-getopt</requirement>
+        <requirement type="package" version="0.2.3">r-httr2</requirement>
+        <requirement type="package" version="1.8.7">r-jsonlite</requirement>
+    </requirements>
+    <command detect_errors='exit_code'>
+        <![CDATA[
+            Rscript '$__tool_directory__/OTB_BandMath.R' 
+                --file '$file' 
+                --processingMemory '$processing_memory' 
+                --mathExpression '$math_expression'
+                --outputImage '$output_image' 
+                --outputType '$output_type' 
+                --outputFormat '$output_format' 
+                --outputData '$output_data'    
+        ]]>
+    </command>
+    <inputs>
+        <param type="data" format="txt" name="file" label="List of images" help="Image-list of operands to the mathematical expression (.txt file, one per line)" />
+        <param type="integer" name="processing_memory" label="Random-Access Memory (RAM)" value="256" min="128" max="16384" optional="true" help="Available memory for processing (in MB)" />
+        <param type="text" name="math_expression" label="muParser" optional="false" help="The muParser mathematical expression to apply on input images (e.g., 'im1b3,im1b2,im1b1'). See 'Help' for documentation."/>
+        <param type="select" name="output_image" label="Output image" help="Output image which is the result of the mathematical expressions on input image-list operands">
+            <option value="float">float</option>
+            <option value="uint8">uint8</option>
+            <option value="uint16">uint16</option>
+            <option value="int16">int16</option>
+            <option value="int32">int32</option>
+            <option value="double">double</option>
+        </param>
+        <param type="select" name="output_type" label="Output image format" help="Select .png for obtaining the response as an OGC web service. Select .tiff or .jpeg for using it in a workflow.">
+            <option value="png">.png (for OGC web services)</option>
+            <option value="tiff">.tiff (for workflows)</option>
+            <option value="jpeg">.jpeg (for workflows)</option>
+        </param>
+        <param type="select" name="output_format" label="Download the result to your Galaxy history or get the URL?" help="Select 'Get URL' to use the result as input for another OGC API Process in a workflow.">
+            <option value="download">Download</option>
+            <option value="getUrl">Get URL</option>
+        </param>
+    </inputs>
+    <outputs>
+        <data name="output_data" format="png">
+            <change_format>
+                <when input="output_format" value="getUrl" format="txt" />
+                <when input="output_type" value="tiff" format="tiff" />
+                <when input="output_type" value="jpeg" format="jpeg" />
+            </change_format>
+        </data>
+    </outputs>
+    <tests>
+        <test>
+            <param name="file" value="otb_band_math_test_input.txt"/>
+            <param name="processing_memory" value="256"/>
+            <param name="math_expression" value="im1b3,im1b2,im1b1"/>
+            <param name="output_type" value="png"/>
+            <param name="output_image" value="float"/>
+            <param name="output_format" value="download"/>
+            <output name="output_data" file="otb_band_math_test1_output.png" compare="sim_size" delta_frac="0.1" />
+        </test>
+        <test>
+            <param name="file" value="otb_band_math_test_input.txt"/>
+            <param name="processing_memory" value="256"/>
+            <param name="math_expression" value="im1b3,im1b2,im1b1"/>
+            <param name="output_type" value="png"/>
+            <param name="output_image" value="float"/>
+            <param name="output_format" value="getUrl"/>
+            <output name="output_data" file="otb_band_math_test2_output.txt" compare="sim_size" delta_frac="0.1" />
+        </test>
+    </tests>
+    <help><![CDATA[
+        OGC API Process documentation: https://ospd.geolabs.fr:8300/ogc-api/processes/OTB.BandMath.html
+        
+        This application performs a mathematical operation on several multi-band images and outputs the result into a monoband image. 
+        The given expression is computed at each pixel position. Evaluation of the mathematical formula is done by the muParser libraries.
+        The formula can be written using:  * numerical values ( 2.3, -5, 3.1e4, ...)  * variables containing pixel 
+        values (e.g. : 'im2b3' is the pixel value in 2nd image, 3rd band)  * binary operators:    * '+' addition, '-' subtraction, '*' multiplication, '/' division    * '^' raise x to the power of y    * '.
+    ]]></help>
+    <citations>
+        <citation type="bibtex">
+            @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},
+            }
+        </citation>
+        <citation type="doi">10.48550/arXiv.1403.2805</citation>
+    </citations>
+</tool>
\ No newline at end of file
Binary file test-data/otb_band_math_test1_output.png has changed
--- /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
--- /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