Skip to content

Commit 50b7f2b

Browse files
author
Karthick
committed
Make XML metadata optional, extract from HDF if XML not found
1 parent e8ee4a3 commit 50b7f2b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/stactools/modis/builder.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ def add_hdf_or_xml_href(
110110
xml_href = f"{href}.xml"
111111
else:
112112
raise ValueError(f"Invalid HDF or XML href: {href}")
113-
self.add_xml_asset(xml_href)
113+
114+
# Add XML asset if it exists, otherwise extract metadata from HDF
115+
if os.path.exists(xml_href):
116+
self.add_xml_asset(xml_href)
117+
else:
118+
self.metadata = Metadata.from_hdf_href(hdf_href, self.read_href_modifier)
119+
114120
self.add_hdf_asset(
115121
hdf_href, cog_directory=cog_directory, create_cogs=create_cogs
116122
)

src/stactools/modis/metadata.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import datetime
22
import os.path
3+
import warnings
34
from dataclasses import dataclass
45
from typing import Any, Callable, Dict, List, Optional, Tuple
56

67
import fsspec
78
import numpy as np
9+
import rasterio
810
from lxml import etree
911
from rasterio import Affine
1012
from rasterio.crs import CRS
13+
from rasterio.errors import NotGeoreferencedWarning
1114
from shapely.geometry import shape
1215
from stactools.core.io import ReadHrefModifier
1316
from stactools.core.io.xml import XmlElement
@@ -252,6 +255,32 @@ def from_cog_tags(cls, cog_tags: Dict[str, str]) -> "Metadata":
252255
collection=collection,
253256
)
254257

258+
@classmethod
259+
def from_hdf_href(
260+
cls, href: str, read_href_modifier: Optional[ReadHrefModifier] = None
261+
) -> "Metadata":
262+
"""Reads metadata from an HDF file when XML is not available.
263+
264+
Args:
265+
href (str): The href of the HDF file
266+
read_href_modifier (Optional[Callable[[str], str]]): Optional
267+
function to modify the read href
268+
269+
Returns:
270+
Metadata: Information that will map to Item attributes.
271+
"""
272+
if read_href_modifier:
273+
read_href = read_href_modifier(href)
274+
else:
275+
read_href = href
276+
277+
with warnings.catch_warnings():
278+
warnings.simplefilter("ignore", category=NotGeoreferencedWarning)
279+
with rasterio.open(read_href) as dataset:
280+
hdf_tags = dataset.tags()
281+
282+
return cls.from_cog_tags(hdf_tags)
283+
255284
@property
256285
def datetime(self) -> Optional[datetime.datetime]:
257286
"""Returns a single nominal datetime for this metadata file.

0 commit comments

Comments
 (0)