How-to Guide: Using Conditional Workflows¶
This guide explains how to create workflows with conditional execution using when and pickValue.
These features allow tasks to run conditionally based on inputs and dynamically select outputs.
Objective¶
- Process raster files using
rio_stackif the EPSG code is"native". - Use
rio_warp_stackif the EPSG code is not"native". - Dynamically choose the appropriate output using
pickValue: the_only_non_null.
Key Features¶
- Conditional Execution with
when
The when field controls whether a step is executed based on an input condition.
cwl_file="../cwl-workflows/conditional-workflows.cwl"
yq '.. | select(has("when")) | {"when": .when}' $cwl_file
- Executes the step only if the condition evaluates to true.
- Dynamic Output Selection with
pickValue
The pickValue field determines how to handle multiple output sources.
yq '.. | select(has("pickValue")) | {"pickValue": .pickValue}' $cwl_file
- Selects the only non-null value from the list of outputs.
- Ensures only one output is passed downstream.
Steps¶
- Define the Workflow
Workflow Inputs and Outputs
The workflow accepts:
stac-item: A STAC item URL.epsg_code: EPSG code for projection (default: "native").bands: An array of band names (e.g.,["red", "green", "blue"]).
Workflow Definition
yq '.' $cwl_file
Its graphical representation:
cwltool --print-dot $cwl_file 2> /dev/null | dot -Tpng | display
- Conditional Steps
Step 1: Use rio_stack for Native EPSG
yq '.. | select(has("step_stack")) | {"step_stack": .step_stack}' $cwl_file
- Executes only if epsg_code == "native".
Step 2: Use rio_warp_stack for Reprojection
yq '.. | select(has("step_warp_stack")) | {"step_warp_stack": .step_warp_stack}' $cwl_file
- Executes only if epsg_code != "native".
- Dynamic Output Selection
Stack Selection
yq '.. | select(has("stack")) | {"stack": .stack}' $cwl_file
- Dynamically selects the output from
step_stackorstep_warp_stack. - Ensures the downstream step (
step_color) receives the correct output.
- Run the Workflow
Run the workflow with the following commands based on the desired EPSG code:
Native EPSG Code:
cwltool conditional-workflows.cwl \
--stac-item https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A \
--bands red \
--bands green \
--bands blue \
--epsg_code native
cat > input.json <<'JSON'
{
"stac-item": {
"value": "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A"
},
"bands": [
"red",
"green",
"blue"
],
"epsg_code": "EPSG4326"
}
JSON
cwltool --podman "$cwl_file" input.json > output.json
Reprojected EPSG Code
cwltool conditional-workflows.cwl \
--stac-item https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs/items/S2B_53HPA_20210723_0_L2A \
--bands red \
--bands green \
--bands blue \
--epsg_code "EPSG:4326"
- Expected Output
Intermediate Outputs:
stackedfile from eitherstep_stackorstep_warp_stack.
Final Output:
rgb-tif: RGB composite file produced bystep_color.
cat output.json | yq e . -o=json
Key Takeaways¶
Conditional Steps:
- Use
whento execute steps based on input conditions. - Allows workflows to adapt to varying requirements dynamically.
Dynamic Output Handling:
- Use
pickValueto select the appropriate output among multiple sources.
Flexible Workflows:
- This approach creates workflows that handle both native and reprojected data seamlessly.
This guide demonstrates how to build adaptable workflows in CWL, leveraging conditional execution and dynamic output selection.
rm -f output.json
rm -f rgb.tif
rm -f stacked.tif