How-to Guide: Scattering Workflows¶
This guide demonstrates how to use scattering in CWL workflows, allowing parallel execution of tasks on multiple inputs.
The focus is on the ScatterFeatureRequirement and scatterMethod.
Objective¶
- Execute a subworkflow (rgb-composite) in parallel for multiple stac-items.
- Scatter the input stac-items to create multiple RGB composite outputs.
Key Concepts¶
ScatterFeatureRequirement
The ScatterFeatureRequirement enables scattering, allowing a step to process multiple inputs in parallel.
cwl_file="../cwl-workflows/scatter-workflows.cwl"
cat $cwl_file | yq e .'$graph[0]' | yq '.. | select(has("ScatterFeatureRequirement")) | {"requirements": {"ScatterFeatureRequirement": .ScatterFeatureRequirement}}'
This makes it possible to process an array of inputs independently, where each item is executed as a separate job.
ScatterandScatterMethod
scatter: Specifies the input field(s) to be scattered.scatterMethod: Defines how the inputs are combined when multiple fields are scattered:dotproduct: Matches corresponding elements of input arrays (e.g., the first element of each array is processed together).nested_crossproduct: Creates a Cartesian product of inputs, producing combinations of all elements.flat_crossproduct: Similar tonested_crossproductbut flattens the structure of results.
cat $cwl_file | yq e .'$graph[0].steps' -
scatter: stac-item: Thestac-itemarray is scattered, creating one subworkflow execution per item.scatterMethod: dotproduct: Matches eachstac-itemwith the same bands input for each subworkflow execution.
Steps¶
- Define the Workflow
The main workflow (scatter-workflows.cwl) processes multiple stac-items in parallel:
Workflow Definition:
cat $cwl_file | yq e .'$graph[0]' -
Its graphical representation:
cwltool --print-dot $cwl_file 2> /dev/null | dot -Tpng | display
Inputs:
stac-items: An array of URLs to STAC items.bands: Array of band names (default: ["red", "green", "blue"]).
Output:
rgb-tif: An array of RGB TIFF files (one perstac-item).
Steps:
step_rgb_composite: Calls the rgb-composite subworkflow with scattered stac-items.
- Define the Subworkflow
The subworkflow (rgb-composite) processes a single stac-item to create an RGB composite:
Subworkflow Definition
cat $cwl_file | yq e .'$graph[1]' -
Its graphical representation:
cwltool --print-dot $cwl_file#rgb-composite 2> /dev/null | dot -Tpng | display
Scatter in Subworkflow:
step_curlscatters bands (e.g.,red,green,blue).- Each band is processed independently to retrieve its corresponding asset.
- Run the Workflow
Execute the main workflow with multiple stac-items:
cwltool scatter-workflows.cwl \
--stac-item https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A \
--stac-item https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2A_53HPA_20210728_0_L2A
cat > input.json <<'JSON'
{
"stac-items": [
{
"value": "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A"
},
{
"value": "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2A_53HPA_20210728_0_L2A"
}
],
"bands": [
"red",
"green",
"blue"
]
}
JSON
cwltool --podman "$cwl_file" input.json > output.json
- Expected Output
The workflow creates an array of RGB composite TIFF files, one for each stac-item:
Output Files:
- rgb-tif[0]: RGB composite for the first stac-item.
- rgb-tif[1]: RGB composite for the second stac-item.
cat output.json | yq e . -o=json
Key Takeaways¶
ScatterFeatureRequirement:
- Enables parallel processing of array inputs.
- Scatter and ScatterMethod:
scatter: Specifies the input to scatter.scatterMethod:dotproduct: Matches corresponding elements in input arrays.nested_crossproduct: Generates all combinations of input elements.
3 .Scalable Workflows:
- Scattering simplifies large-scale processing by distributing tasks over multiple inputs, improving performance and modularity.
This guide demonstrates how scattering in CWL can streamline workflows by enabling parallel execution of steps on array inputs.
rm -f output.json
rm -f rgb.tif
rm -f rgb.tif_2