Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/stactools/modis/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ def add_hdf_or_xml_href(
xml_href = f"{href}.xml"
else:
raise ValueError(f"Invalid HDF or XML href: {href}")
self.add_xml_asset(xml_href)

# Add XML asset if it exists, otherwise extract metadata from HDF
if os.path.exists(xml_href):
self.add_xml_asset(xml_href)
else:
self.metadata = Metadata.from_hdf_href(hdf_href, self.read_href_modifier)

self.add_hdf_asset(
hdf_href, cog_directory=cog_directory, create_cogs=create_cogs
)
Expand Down
29 changes: 29 additions & 0 deletions src/stactools/modis/metadata.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import datetime
import os.path
import warnings
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Tuple

import fsspec
import numpy as np
import rasterio
from lxml import etree
from rasterio import Affine
from rasterio.crs import CRS
from rasterio.errors import NotGeoreferencedWarning
from shapely.geometry import shape
from stactools.core.io import ReadHrefModifier
from stactools.core.io.xml import XmlElement
Expand Down Expand Up @@ -252,6 +255,32 @@ def from_cog_tags(cls, cog_tags: Dict[str, str]) -> "Metadata":
collection=collection,
)

@classmethod
def from_hdf_href(
cls, href: str, read_href_modifier: Optional[ReadHrefModifier] = None
) -> "Metadata":
"""Reads metadata from an HDF file when XML is not available.

Args:
href (str): The href of the HDF file
read_href_modifier (Optional[Callable[[str], str]]): Optional
function to modify the read href

Returns:
Metadata: Information that will map to Item attributes.
"""
if read_href_modifier:
read_href = read_href_modifier(href)
else:
read_href = href

with warnings.catch_warnings():
warnings.simplefilter("ignore", category=NotGeoreferencedWarning)
with rasterio.open(read_href) as dataset:
hdf_tags = dataset.tags()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to catch this warning? Feels like we do want to propagate this up to the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the suppression. It is now visible in the output.


return cls.from_cog_tags(hdf_tags)

@property
def datetime(self) -> Optional[datetime.datetime]:
"""Returns a single nominal datetime for this metadata file.
Expand Down
Loading