How-to Guide: Capturing stdout with CWL¶
This guide explains how to capture the standard output (stdout) of a CommandLineTool in CWL.
The example focuses on the stdout and outputs blocks to save the tool's output to a file and make it available for further use.
cwl_file=../cwl-workflows/return-output-file.cwl
cat $cwl_file | yq e .'$graph[1]' | yq '.. | select(has("outputs")) | {"outputs": .outputs}'
This block ensures the tool captures and returns the preview.png file generated during execution.
Objective¶
Convert a Sentinel-2 GeoTIFF file into a PNG image.
Capture the generated PNG file as the tool's output using the outputs block.
Steps¶
- Understand the outputs Block
In CWL, the outputs block specifies:
- Name:
preview– the logical name of the output. - Type:
File– indicates the output is a file. outputBinding.glob: Specifies the file's name or pattern to locate the file in the tool's working directory.
From the example:
cat $cwl_file | yq e .'$graph[1]' | yq '.. | select(has("outputs")) | {"outputs": .outputs}'
glob: preview.png: Ensures the tool captures the filepreview.pnggenerated by therio convertcommand.
- Review the return-output-file.cwl Workflow
The workflow (return-output-file.cwl) is defined as:
yq -e . $cwl_file
It's graphical representation:
cwltool --print-dot $cwl_file 2> /dev/null | dot -Tpng | display
- Provide Input Parameters
Create an input file inputs.yml with the GeoTIFF URL:
tif: "https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/10/T/FK/2021/7/S2B_10TFK_20210713_0_L2A/TCI.tif"
- Execute the Workflow
Run the workflow using cwltool:
cwltool return-output-file input.yml
cat > input.json <<'JSON'
{
"tif": {
"value": "https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/10/T/FK/2021/7/S2B_10TFK_20210713_0_L2A/TCI.tif"
}
}
JSON
cwltool --podman "$cwl_file" input.json > output.json
Expected Output¶
The rio tool processes the input GeoTIFF and generates a PNG file named preview.png.
The outputs block ensures:
- The file is located using
glob: preview.png. - It is made available as the workflow output.
Example Workflow Output:
cat output.json | yq e . -
The preview.png file is now accessible in the working directory or as defined by the CWL runner.
Key Takeaways¶
The
outputsblock defines how to capture and return files:type: File: Specifies the output type.outputBinding.glob: Matches the file name or pattern.
Ensure the file name in glob matches the file name generated by the tool (preview.png).
By focusing on the outputs block, this guide highlights how to capture and return output files in CWL workflows.
rm -f output.json
rm -f input.yml
rm -f preview.png