generated from stactools-packages/template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstac.py
455 lines (384 loc) · 12.8 KB
/
stac.py
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from __future__ import annotations
import dataclasses
import datetime
import itertools
import logging
import operator
import pathlib
import re
from typing import Any, Optional
import fsspec
import pystac
from pystac import (
CatalogType,
Collection,
Extent,
Item,
Provider,
ProviderRole,
SpatialExtent,
TemporalExtent,
)
from . import _kerchunk_helper_functions as khf
from . import constants
logger = logging.getLogger(__name__)
xpr = re.compile(
r"(?P<reference_datetime>\d{10})0000-"
r"(?P<step>\d+[h|m])-"
r"(?P<stream>\w+)-"
r"(?P<type>\w+)."
r"(?P<format>\w+)"
)
NDJSON_MEDIA_TYPE = "application/x-ndjson"
GRIB2_MEDIA_TYPE = "application/wmo-GRIB2"
@dataclasses.dataclass
class Parts:
reference_datetime: datetime.datetime
stream: str
step: str
type: str
format: str
filename: str
split_by_step: bool = False
resolution: Optional[str] = None
@property
def datetime(self):
if self.split_by_step:
return self.forecast_datetime
else:
return self.reference_datetime
@classmethod
def from_filename(cls, filename: str, split_by_step=False, resolution: Optional[str] = None) -> "Parts":
name = pathlib.Path(filename).name
m = xpr.match(name)
if not m:
raise ValueError(name)
d = m.groupdict()
d["filename"] = filename
d["reference_datetime"] = datetime.datetime.strptime(
d["reference_datetime"], "%Y%m%d%H"
) # type: ignore
if resolution:
d["resolution"] = resolution
# error: Argument 1 to "Parts" has incompatible type
# "**Dict[str, Union[str, Any]]"; expected "datetime"
return cls(**d, split_by_step=split_by_step) # type: ignore
@property
def item_id(self) -> str:
parts = [
"ecmwf",
self.reference_datetime.isoformat(timespec="hours"),
self.stream,
self.type,
]
if self.split_by_step:
parts.append(self.step)
if self.resolution is not None:
parts.append(self.resolution)
return "-".join(parts)
@property
def asset_id(self) -> str:
if self.split_by_step:
return "data" if self.format != "index" else "index"
else:
return f"{self.step}-{self.format}"
# mypy complains about error: Name "datetime.timedelta" is not defined
# for offset and forecast_datetime
@property
def offset(self):
v, u = self.step[:-1], self.step[-1]
offset_value = int(v)
if u == "h":
offset = datetime.timedelta(hours=offset_value)
else:
# TODO: this is wrong. Need to something like a DateOffset
raise NotImplementedError()
return offset
@property
def forecast_datetime(self):
return self.reference_datetime + self.offset
@property
def prefix(self) -> str | None:
if self.filename.count("/"):
prefix = self.filename.rsplit("/", 1)[0]
return prefix + "/"
return None
@property
def name(self):
return pathlib.Path(self.filename).name
def create_collection(
thumbnail=None, extra_fields: dict[str, Any] | None = None
) -> Collection:
"""Create a STAC Collection
This function includes logic to extract all relevant metadata from
an asset describing the STAC collection and/or metadata coded into an
accompanying constants.py file.
See `Collection<https://pystac.readthedocs.io/en/latest/api.html#collection>`_.
Returns:
Collection: STAC Collection object
"""
providers = [
Provider(
name="ECMWF",
roles=[ProviderRole.PRODUCER],
url="https://www.ecmwf.int/",
)
]
links = [
pystac.Link(
rel=pystac.RelType.LICENSE,
target="https://creativecommons.org/licenses/by/4.0/",
media_type="text/html",
title="CC-BY-4.0 license",
),
pystac.Link(
rel="documentation",
target="https://confluence.ecmwf.int/display/UDOC/ECMWF+Open+Data+-+Real+Time",
media_type="text/html",
title="ECMWF Open Data (Real Time) documentation",
),
]
extent = Extent(
SpatialExtent([[-180.0, -90.0, 180.0, 90.0]]),
TemporalExtent([[None, None]]), # type: ignore
)
keywords = [
"ECMWF",
"forecast",
"weather",
]
collection = Collection(
id="ecmwf-forecast",
title="ECMWF Open Data (real-time)",
description="{{ collection.description }}",
license="CC-BY-4.0",
providers=providers,
extent=extent,
catalog_type=CatalogType.RELATIVE_PUBLISHED,
keywords=keywords,
)
collection.add_links(links)
# Summaries
collection.summaries.maxcount = 50
summaries: dict[str, list[Any]] = {
"ecmwf:reference_times": constants.REFERENCE_TIMES,
"ecmwf:streams": constants.STREAMS,
"ecmwf:steps": constants.STEPS,
"ecmwf:types": constants.TYPES,
"ecmwf:pressure_levels": constants.PRESSURE_LEVELS,
}
for k, v in summaries.items():
collection.summaries.add(k, v)
if thumbnail is not None:
# TODO: guess media type?
collection.add_asset(
"thumbnail",
pystac.Asset(
thumbnail,
title="thumbnail",
roles=[thumbnail],
media_type=pystac.MediaType.PNG,
),
)
if extra_fields:
collection.extra_fields.update(extra_fields)
item_assets = {
"data": pystac.extensions.item_assets.AssetDefinition(
{
"type": GRIB2_MEDIA_TYPE,
"roles": ["data"],
"title": "GRIB2 data file",
"description": (
"The forecast data, as a grib2 file. Subsets of the data can be loaded "
"using information from the associated index file."
),
}
),
"index": pystac.extensions.item_assets.AssetDefinition(
{
"type": NDJSON_MEDIA_TYPE,
"roles": ["index"],
"title": "Index file",
"description": (
"The index file contains information on each message within "
"the GRIB2 file."
),
}
),
}
item_assets_ext = pystac.extensions.item_assets.ItemAssetsExtension.ext(
collection, add_if_missing=True
)
item_assets_ext.item_assets = item_assets
return collection
def item_key(filename) -> tuple[datetime.datetime, str, str]:
"""
Gives tuple of attributes in a filename used to determine its item.
This uses the
* reference datetime
* stream
* type
"""
parts = Parts.from_filename(filename)
return parts.reference_datetime, parts.stream, parts.type
def item_key_split_by_parts(
filename,
) -> tuple[datetime.datetime, str, str, datetime.timedelta]:
"""
Gives tuple of attributes in a filename used to determine its item.
This uses the
* reference datetime
* stream
* type
* step
"""
parts = Parts.from_filename(filename)
return parts.reference_datetime, parts.stream, parts.type, parts.offset
def group_assets(asset_hrefs: list[str], key=item_key):
"""
Groups a list of asset HREFs according to which item they belong in.
"""
asset_hrefs = sorted(asset_hrefs, key=key)
grouped = itertools.groupby(asset_hrefs, key=key)
return grouped
def create_item(asset_hrefs: list[str], split_by_step=False, resolution: Optional[str] = None) -> Item:
"""
Create an item for the hrefs.
Parameters
----------
asset_hrefs: list[str]
The HREFs for the item's assets. These should all belong to the item, according
to `item_key`. Use `group_assets` prior on a list of assets possibly belonging
to multiple items.
Returns
-------
pystac.Item
"""
parts = [
Parts.from_filename(href, split_by_step=split_by_step, resolution=resolution) for href in asset_hrefs
]
return _create_item_from_parts(parts, split_by_step=split_by_step)
def create_item_from_representative_asset(asset_href: str) -> Item:
"""
Create an item from a "representative" asset HREF.
This method takes a single HREF, and uses the guidance from the ECMWF
on which assets are published together and belong in the same item.
Parameters
----------
asset_href:
The "representative" asset for an item. This should typically be
the first asset for the item (e.g. the "-0h" asset for most products).
Returns
-------
pystac.Item
Examples
--------
>>> href = "ecmwf/20220201/00z/0p4-beta/enfo/20220201000000-0h-enfo-ef.grib2"
>>> stac.create_item_from_representative_asset(href)
"""
siblings = list_sibling_assets(asset_href)
return _create_item_from_parts(siblings)
def _create_item_from_parts(parts: list[Parts], split_by_step=False) -> Item:
part = parts[0]
for i, other in enumerate(parts):
if part.item_id != other.item_id:
raise ValueError(
f"Asset {i} has different Item ID ({part.item_id} != {other.item_id}). "
f"URL = {part.filename}"
)
geometry = {
"type": "Polygon",
"coordinates": (
(
(180.0, -90.0),
(180.0, 90.0),
(-180.0, 90.0),
(-180.0, -90.0),
(180.0, -90.0),
),
),
}
bbox = [-180.0, -90.0, 180.0, 90.0]
item = pystac.Item(
part.item_id,
geometry=geometry,
bbox=bbox,
datetime=part.datetime,
properties={},
)
item.properties["ecmwf:stream"] = part.stream
item.properties["ecmwf:type"] = part.type
item.properties["ecmwf:reference_datetime"] = (
part.reference_datetime.isoformat() + "Z"
)
item.properties["ecmwf:resolution"] = part.resolution
item.properties["ecmwf:forecast_datetime"] = (
part.forecast_datetime.isoformat() + "Z"
)
fs = fsspec.filesystem("")
fs.clear_instance_cache()
if split_by_step:
item.properties["ecmwf:step"] = part.step
else:
offset = max(p.offset for p in parts)
item.properties["start_datetime"] = part.reference_datetime.isoformat() + "Z"
item.properties["end_datetime"] = (
part.reference_datetime + offset
).isoformat() + "Z"
for p in parts:
if p.format == "grib2":
media_type = GRIB2_MEDIA_TYPE
roles = ["data"]
if ((p.stream == "wave") & (p.type == "fc")):
kerchunk_indices = khf.get_kerchunk_indices(p)
else:
kerchunk_indices = {}
elif p.format == "index":
media_type = NDJSON_MEDIA_TYPE
roles = ["index"]
kerchunk_indices = None
elif p.format == "bufr":
media_type = None
roles = ["data"]
kerchunk_indices = None
else:
raise ValueError(f"Bad extension: {p.format}")
item.add_asset(
p.asset_id,
pystac.Asset(
p.filename,
media_type=media_type,
roles=roles,
extra_fields={"ecmwf:step": p.step, "kerchunk:indices": kerchunk_indices} if not split_by_step else {"kerchunk:indices": kerchunk_indices},
),
)
return item
def _assets_key(combination):
return combination[:4]
def list_sibling_assets(filename) -> list[Parts]:
"""
List the other files that belong in the same item as `file` (have the same item_id).
This is based purely on the filename. It doesn't list any directories to determine whether
the other files are present.
Examples
--------
>>> siblings = list_sibling_assets("20220202000000-0h-enfo-ef.grib2")
"""
p = Parts.from_filename(filename)
# mypy failing on python 3.7
combinations = constants.get_combinations() # type: ignore
d = {
k: list(v) for k, v in itertools.groupby(combinations, key=_assets_key)
} # type: ignore
combos = list(d[p.format, p.type, p.reference_datetime.strftime("%H"), p.stream])
prefix = p.prefix or ""
other_files = [
f"{prefix}{p.reference_datetime:%Y%m%d%H}0000-{combo.step}-{combo.stream}"
f"-{combo.type}.{combo.format}"
for combo in combos
]
if p.format == "grib2":
other_files.extend([file.rsplit(".", 1)[0] + ".index" for file in other_files])
parts = [Parts.from_filename(other_file) for other_file in other_files]
parts = sorted(parts, key=operator.attrgetter("step"))
return parts