Skip to content

Stage and cog

Goal

Run the stage-and-cog.cwl CWL Workflow that that stages a Sentinel-2 Level-1C from the CDSE and converts/resamples its assets to COG.

Lab

This step has a dedicated lab available at /workspace/inference-eoap/practice-labs/Application steps/01 - Stage-and-cog.ipynb

Dependencies

This steps requires generating a CDSE access token with the Python script and your own CDSE credentials:

bin/get-bearer-token
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3

import click
import requests

def get_access_token(username: str, password: str) -> str:
    data = {
        "client_id": "cdse-public",
        "username": username,
        "password": password,
        "grant_type": "password",
    }
    try:
        r = requests.post(
            "https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token",
            data=data,
        )
        r.raise_for_status()
    except Exception as e:
        raise Exception(
            f"Access token creation failed. Response from the server was: {r.json()}"
        )
    return r.json()["access_token"]

@click.command(
    short_help="Get bearer token",
    help="Get a bearer token for the Copernicus Data Space Ecosystem ",
)
@click.option(
    "--username",
    help="Username for the Copernicus Data Space Ecosystem",
    required=True,
)
@click.option(
    "--password",
    help="Password for the Copernicus Data Space Ecosystem",
    required=True,
)
def get_bearer_token(username: str, password: str) -> str:

    return print(get_access_token(username, password))

if __name__ == "__main__":
    get_bearer_token()

Lab

This step has a dedicated lab available at /workspace/inference-eoap/practice-labs/Application steps/01 - Stage-and-cog.ipynb

Step 1 - Run the stage-and-cog.cwl_ CWL Workflow

Use cwltool to run the stage-and-cog.cwl CWL Workflow with a Sentinel-2 Level-1C acquisition reference

The CWL Workflow is shown below and the lines highlighted chain the stage-in and conversion to COG steps:

stage-and-cog.cwl
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
cwlVersion: v1.0
$namespaces:
  s: https://schema.org/
s:softwareVersion: 1.0.0
schemas:
  - http://schema.org/version/9.0/schemaorg-current-http.rdf
$graph:
  - class: Workflow
    id: main
    label: Stage-in and cog-ify a Sentinel-2 L1C acquisition
    doc: Stage-in and cog-ify a Sentinel-2 L1C acquisition
    requirements: {}
    inputs:
      item:
        doc: Reference to a STAC item
        label: STAC item reference
        type: string
      access_token:
        doc: CDSE bearer token
        label: CDSE bearer token
        type: string
    outputs:
      - id: stac_catalog
        outputSource:
          - node_sen2cog/stac_catalog
        type: Directory
    steps:
      node_stage_in:
        run: "#stage-in"
        in:
          item: item
          access_token: access_token
        out:
          - staged
      node_sen2cog:
        run: "#sen2cog"
        in:
          staged:
            source: node_stage_in/staged
        out:
          - stac_catalog

  - class: CommandLineTool
    id: stage-in
    requirements:
      InlineJavascriptRequirement: {}
      EnvVarRequirement:
        envDef:
          PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          PYTHONPATH: /app
          CDSE_ACCES_TOKEN: $(inputs.access_token)
      ResourceRequirement:
        coresMax: 1
        ramMax: 1024
    hints:
      DockerRequirement:
        dockerPull: localhost/stage-in:latest
    baseCommand: ["python", "-m", "app"]
    arguments: []
    inputs:
      item:
        type: string
        inputBinding:
          prefix: --input-item
      access_token:
        type: string
    outputs:
      staged:
        outputBinding:
          glob: .
        type: Directory
  - class: CommandLineTool
    id: sen2cog
    requirements:
      InlineJavascriptRequirement: {}
      EnvVarRequirement:
        envDef:
          PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
          PYTHONPATH: /app
      ResourceRequirement:
        coresMax: 2
        ramMax: 4096
    hints:
      DockerRequirement:
        dockerPull: docker.io/library/sen2cog:latest
    baseCommand: ["python", "-m", "app"]
    arguments: []
    inputs:
      staged:
        type: Directory
        inputBinding:
          prefix: --input-catalog
    outputs:
      stac_catalog:
        outputBinding:
          glob: .
        type: Directory

To run this CWL document run the script below to:

  • provide your CDSE username and password to generate and access token
  • Use cwltool to run stage-and-cog.cwl CWL description
terminal
version="1.0.0"

export WORKSPACE=/workspace/inference-eoap

export PATH=${WORKSPACE}/bin:$PATH

read -p "Enter the CDSE username: " username
read -sp "Enter the CDSE password: " password

BEARER_TOKEN=$( get-bearer-token ${username} ${password} )

cwltool \
    --podman \
    --outdir ${WORKSPACE}/runs \
    https://github.com/eoap/inference-eoap/releases/download/1.0.0/stage-and-cog.1.0.0.cwl \
    --access_token ${BEARER_TOKEN} \
    --item "https://catalogue.dataspace.copernicus.eu/stac/collections/SENTINEL-2/items/S2A_MSIL1C_20240125T100311_N0510_R122_T33TVM_20240125T104959.SAFE" > ${WORKSPACE}/runs/results.json

Expected outcome

The folder /workspace/inference-eoap/runs contains:

(base) jovyan@jupyter-fbrito--training:~/inference-eoap$ tree runs
runs
├── results.json
└── ydmns4od
    ├── S2A_T33TVM_20240125T100359_L1C
    │   ├── S2A_T33TVM_20240125T100359_L1C.json
    │   ├── blue.tif
    │   ├── cirrus.tif
    │   ├── coastal.tif
    │   ├── green.tif
    │   ├── nir.tif
    │   ├── nir08.tif
    │   ├── nir09.tif
    │   ├── red.tif
    │   ├── rededge1.tif
    │   ├── rededge2.tif
    │   ├── rededge3.tif
    │   ├── swir16.tif
    │   └── swir22.tif
    └── catalog.json

2 directories, 16 files