-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathw.cwl
More file actions
386 lines (386 loc) · 15.7 KB
/
w.cwl
File metadata and controls
386 lines (386 loc) · 15.7 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
$graph:
- $namespaces:
cwltool: http://commonwl.org/cwltool#
class: Workflow
doc: Wrapped workflow with required stage-in and stage-out steps
hints:
cwltool:Secrets:
secrets: []
id: main
inputs:
aoi:
doc: area of interest as a bounding box
id: aoi
label: area of interest
type: string
aws_access_key_id:
type: string
aws_secret_access_key:
type: string
bands:
default:
- green
- nir08
doc: bands used for the NDWI
id: bands
label: bands used for the NDWI
type: string[]
endpoint_url:
type: string
epsg:
default: EPSG:4326
doc: EPSG code
id: epsg
label: EPSG code
type: string
item_1:
doc: Reference to a STAC item
id: item_1
label: STAC item reference
type: string
item_2:
doc: Optional reference to a STAC item
id: item_2
label: Optional STAC item reference
type: string?
region_name:
type: string
s3_bucket:
type: string
sub_path:
type: string
label: Wrapped workflow
outputs:
stac_catalog:
outputSource:
- node_stage_out/stac_catalog_out
type: string
requirements:
ScatterFeatureRequirement: {}
SubworkflowFeatureRequirement: {}
steps:
node_stage_in:
in:
input: item_1
out:
- item_1_out
run:
arguments:
- $( inputs.input )
baseCommand:
- python
- stage.py
class: CommandLineTool
cwlVersion: v1.0
id: main
inputs:
input:
type: string?
outputs:
item_1_out:
outputBinding:
glob: .
type: Directory
requirements:
DockerRequirement:
dockerPull: ghcr.io/eoap/mastering-app-package/stage:1.1.0
InitialWorkDirRequirement:
listing:
- entry: "import pystac\nimport stac_asset\nimport asyncio\nimport os\n\
import sys\nfrom loguru import logger\n\nconfig = stac_asset.Config(warn=True)\n\
\nasync def main(href: str):\n \n item = pystac.read_file(href)\n\
\ \n os.makedirs(item.id, exist_ok=True)\n cwd = os.getcwd()\n\
\ \n os.chdir(item.id)\n item = await stac_asset.download_item(item=item,\
\ directory=\".\", config=config)\n os.chdir(cwd)\n \n cat\
\ = pystac.Catalog(\n id=\"catalog\",\n description=f\"\
catalog with staged {item.id}\",\n title=f\"catalog with staged\
\ {item.id}\",\n )\n cat.add_item(item)\n \n cat.normalize_hrefs(\"\
./\")\n cat.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)\n\
\n return cat\n\nhref = sys.argv[1]\nlogger.info(f\"Staging {href}\"\
)\ncat = asyncio.run(main(href))\nlogger.info(f\"Staged {href} to\
\ {cat.get_self_href()}\")"
entryname: stage.py
InlineJavascriptRequirement: {}
node_stage_in_1:
in:
input: item_2
out:
- item_2_out
run:
arguments:
- $( inputs.input )
baseCommand:
- python
- stage.py
class: CommandLineTool
cwlVersion: v1.0
id: main
inputs:
input:
type: string?
outputs:
item_2_out: ''
requirements:
DockerRequirement:
dockerPull: ghcr.io/eoap/mastering-app-package/stage:1.1.0
InitialWorkDirRequirement:
listing:
- entry: "import pystac\nimport stac_asset\nimport asyncio\nimport os\n\
import sys\nfrom loguru import logger\n\nconfig = stac_asset.Config(warn=True)\n\
\nasync def main(href: str):\n \n item = pystac.read_file(href)\n\
\ \n os.makedirs(item.id, exist_ok=True)\n cwd = os.getcwd()\n\
\ \n os.chdir(item.id)\n item = await stac_asset.download_item(item=item,\
\ directory=\".\", config=config)\n os.chdir(cwd)\n \n cat\
\ = pystac.Catalog(\n id=\"catalog\",\n description=f\"\
catalog with staged {item.id}\",\n title=f\"catalog with staged\
\ {item.id}\",\n )\n cat.add_item(item)\n \n cat.normalize_hrefs(\"\
./\")\n cat.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)\n\
\n return cat\n\nhref = sys.argv[1]\nlogger.info(f\"Staging {href}\"\
)\ncat = asyncio.run(main(href))\nlogger.info(f\"Staged {href} to\
\ {cat.get_self_href()}\")"
entryname: stage.py
InlineJavascriptRequirement: {}
node_stage_out:
in:
aws_access_key_id: aws_access_key_id
aws_secret_access_key: aws_secret_access_key
endpoint_url: endpoint_url
region_name: region_name
s3_bucket: s3_bucket
sub_path: sub_path
wf_outputs: on_stage/stac_catalog
out:
- stac_catalog_out
run:
arguments:
- $( inputs.wf_outputs.path )
- $( inputs.s3_bucket )
- $( inputs.sub_path )
baseCommand:
- python
- stage.py
class: CommandLineTool
cwlVersion: v1.0
doc: Stage-out the results to S3
id: stage-out
inputs:
aws_access_key_id:
type: string
aws_secret_access_key:
type: string
endpoint_url:
type: string
region_name:
type: string
s3_bucket:
type: string
sub_path:
type: string
wf_outputs:
type: Directory
outputs:
stac_catalog_out:
outputBinding:
glob: message
loadContents: true
outputEval: $( self[0].contents )
type: string
requirements:
DockerRequirement:
dockerPull: ghcr.io/eoap/mastering-app-package/stage:1.1.0
EnvVarRequirement:
envDef:
aws_access_key_id: $( inputs.aws_access_key_id )
aws_endpoint_url: $( inputs.endpoint_url )
aws_region_name: $( inputs.region_name )
aws_secret_access_key: $( inputs.aws_secret_access_key )
InitialWorkDirRequirement:
listing:
- entry: "import os\nimport sys\nimport pystac\nimport botocore\nimport\
\ boto3\nimport shutil\nimport uuid\nfrom loguru import logger\nfrom\
\ pystac.stac_io import DefaultStacIO, StacIO\nfrom urllib.parse import\
\ urlparse\nfrom datetime import datetime\nfrom pystac.extensions.item_assets\
\ import ItemAssetsExtension, AssetDefinition\n\ncat_url = sys.argv[1]\n\
bucket = sys.argv[2]\nuid = str(uuid.uuid4()).replace(\"-\", \"\"\
)[:6]\nsubfolder = f\"{sys.argv[3]}-{uid}\"\n\naws_access_key_id =\
\ os.environ[\"aws_access_key_id\"]\naws_secret_access_key = os.environ[\"\
aws_secret_access_key\"]\nregion_name = os.environ[\"aws_region_name\"\
]\nendpoint_url = os.environ[\"aws_endpoint_url\"]\n\nlogger.info(f\"\
stage-out {cat_url} to s3://{bucket}/{subfolder}\")\n\nshutil.copytree(cat_url,\
\ \"/tmp/catalog\")\ncat = pystac.read_file(os.path.join(\"/tmp/catalog\"\
, \"catalog.json\"))\n\nlogger.info(f\"catalog {cat}\")\n\nclass CustomStacIO(DefaultStacIO):\n\
\ \"\"\"Custom STAC IO class that uses boto3 to read from S3.\"\
\"\"\n\n def __init__(self):\n self.session = botocore.session.Session()\n\
\ self.s3_client = self.session.create_client(\n \
\ service_name=\"s3\",\n use_ssl=True,\n aws_access_key_id=aws_access_key_id,\n\
\ aws_secret_access_key=aws_secret_access_key,\n \
\ endpoint_url=endpoint_url,\n region_name=region_name,\n\
\ )\n\n def write_text(self, dest, txt, *args, **kwargs):\n\
\ parsed = urlparse(dest)\n if parsed.scheme == \"s3\"\
:\n self.s3_client.put_object(\n Body=txt.encode(\"\
UTF-8\"),\n Bucket=parsed.netloc,\n \
\ Key=parsed.path[1:],\n ContentType=\"application/geo+json\"\
,\n )\n else:\n super().write_text(dest,\
\ txt, *args, **kwargs)\n\n\nclient = boto3.client(\n \"s3\",\n\
\ aws_access_key_id=aws_access_key_id,\n aws_secret_access_key=aws_secret_access_key,\n\
\ endpoint_url=endpoint_url,\n region_name=region_name,\n)\n\
\nStacIO.set_default(CustomStacIO)\n\n# create a STAC collection for\
\ the process\ncollection_id = subfolder\ndate = datetime.now().strftime(\"\
%Y-%m-%d\")\n\ndates = [\n datetime.strptime(f\"{date}T00:00:00\"\
, \"%Y-%m-%dT%H:%M:%S\"),\n datetime.strptime(f\"{date}T23:59:59\"\
, \"%Y-%m-%dT%H:%M:%S\"),\n]\n\ncollection = pystac.Collection(\n\
\ id=collection_id,\n description=\"description\",\n extent=pystac.Extent(\n\
\ spatial=pystac.SpatialExtent([[-180, -90, 180, 90]]),\n \
\ temporal=pystac.TemporalExtent(intervals=[[min(dates), max(dates)]]),\n\
\ ),\n title=\"Processing results\",\n href=f\"s3://{bucket}/{subfolder}/collection.json\"\
,\n stac_extensions=[],\n keywords=[\"keyword1\", \"keyword2\"\
],\n license=\"proprietary\",\n)\n\n\nfor item in cat.get_items():\n\
\ item.set_collection(collection)\n collection.add_item(item)\n\
\ for key, asset in item.get_assets().items():\n s3_path\
\ = os.path.normpath(\n os.path.join(os.path.join(subfolder,\
\ item.id, asset.href))\n )\n print(f\"upload {asset.href}\
\ to s3://{bucket}/{s3_path}\",file=sys.stderr)\n client.upload_file(\n\
\ asset.get_absolute_href(),\n bucket,\n \
\ s3_path,\n )\n asset.href = f\"s3://{bucket}/{s3_path}\"\
\n item.add_asset(key, asset)\ncollection.update_extent_from_items()\n\
\n# Access the item-assets extension\nitem_assets_ext = ItemAssetsExtension.ext(collection,\
\ add_if_missing=True)\nif ItemAssetsExtension.get_schema_uri() not\
\ in collection.stac_extensions:\n collection.stac_extensions.append(ItemAssetsExtension.get_schema_uri())\n\
\nitem_assets = {}\nfor item in collection.get_items():\n # Loop\
\ over the assets in the item and create AssetDefinitions for each\n\
\ for asset_key, asset in item.assets.items():\n # Create\
\ AssetDefinition from existing asset properties\n # remove\
\ the statistics and histogram from the extra fields (raster extension)\n\
\ asset.extra_fields[\"raster:bands\"][0].pop(\"statistics\"\
)\n asset.extra_fields[\"raster:bands\"][0].pop(\"histogram\"\
)\n asset_definition = AssetDefinition.create(\n \
\ title=asset.title,\n description=asset.description,\n\
\ media_type=asset.media_type,\n roles=asset.roles,\n\
\ extra_fields=asset.extra_fields,\n )\n \
\ # Add the asset definition to the collection's item assets\n \
\ item_assets[asset_key] = asset_definition\ncat.clear_items()\n\
\ncat.add_child(collection)\n\ncat.normalize_hrefs(f\"s3://{bucket}/{subfolder}\"\
)\n\nfor item in collection.get_items():\n for index, link in enumerate(item.links):\n\
\ if link.rel in [\"root\"]:\n item.links.pop(index)\n\
\ # upload item to S3\n logger.info(f\"upload {item.id} to s3://{bucket}/{subfolder}\"\
)\n pystac.write_file(item, item.get_self_href())\n\n# upload collection\
\ to S3\nlogger.info(f\"upload collection.json to s3://{bucket}/{subfolder}\"\
)\nfor index, link in enumerate(collection.links):\n if link.rel\
\ in [\"root\"]:\n collection.links.pop(index)\npystac.write_file(collection,\
\ collection.get_self_href())\n\n# upload catalog to S3\nlogger.info(f\"\
upload catalog.json to s3://{bucket}/{subfolder}\")\nfor index, link\
\ in enumerate(cat.links):\n if link.rel in [\"root\"]:\n cat.links.pop(index)\n\
pystac.write_file(cat, cat.get_self_href())\n\nshutil.rmtree(\"/tmp/catalog/\"\
)\n\nprint(f\"s3://{bucket}/{subfolder}/catalog.json\", file=sys.stdout)"
entryname: stage.py
InlineJavascriptRequirement: {}
ResourceRequirement: {}
stdout: message
on_stage:
in:
aoi: aoi
bands: bands
epsg: epsg
item_1: node_stage_in/item_1_out
item_2: node_stage_in_1/item_2_out
out:
- stac_catalog
run: '#main'
- class: Workflow
doc: Water bodies detection based on NDWI and otsu threshold applied to a single
Sentinel-2 COG STAC item
id: main
inputs:
aoi:
doc: area of interest as a bounding box
label: area of interest
type: string
bands:
default:
- green
- nir08
doc: bands used for the NDWI
label: bands used for the NDWI
type: string[]
epsg:
default: EPSG:4326
doc: EPSG code
label: EPSG code
type: string
item_1:
doc: Reference to a STAC item
label: STAC item reference
type: Directory
item_2:
doc: Optional reference to a STAC item
label: Optional STAC item reference
type: Directory?
label: Water bodies detection based on NDWI and the otsu threshold
outputs:
- id: stac_catalog
outputSource:
- step/stac-catalog
type: Directory
requirements: []
steps:
step:
in:
aoi: aoi
band: bands
epsg: epsg
item_1: item_1
item_2: item_2
out:
- stac-catalog
run: '#clt'
- arguments:
- pattern-7
baseCommand:
- vegetation-index
class: CommandLineTool
hints:
DockerRequirement:
dockerPull: docker.io/library/vegetation-indexes:latest
id: clt
inputs:
aoi:
inputBinding:
prefix: --aoi
type: string
band:
type:
- inputBinding:
prefix: --band
items: string
type: array
epsg:
inputBinding:
prefix: --epsg
type: string
item_1:
inputBinding:
prefix: --input-item-1
type: Directory
item_2:
inputBinding:
prefix: --input-item-2
type: Directory?
outputs:
stac-catalog:
outputBinding:
glob: .
type: Directory
requirements:
EnvVarRequirement:
envDef:
PATH: $PATH:/app/envs/vegetation-index/bin
InlineJavascriptRequirement: {}
ResourceRequirement:
coresMax: 1
ramMax: 512
$namespaces:
s: https://schema.org/
cwlVersion: v1.0
s:softwareVersion: 1.0.0
schemas:
- http://schema.org/version/9.0/schemaorg-current-http.rdf