|  | 
| 4 | 4 | from __future__ import annotations | 
| 5 | 5 | 
 | 
| 6 | 6 | import argparse | 
|  | 7 | +import json | 
| 7 | 8 | import logging | 
| 8 | 9 | import subprocess | 
| 9 | 10 | import sys | 
|  | 11 | +from urllib.parse import urlparse | 
|  | 12 | +from urllib.request import urlopen | 
| 10 | 13 | 
 | 
| 11 | 14 | from get_conversion_params import get_conversion_params | 
| 12 |  | -from utils import extract_item_id, get_zarr_url | 
| 13 | 15 | 
 | 
| 14 | 16 | logging.basicConfig( | 
| 15 | 17 |     level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" | 
| 16 | 18 | ) | 
| 17 | 19 | logger = logging.getLogger(__name__) | 
| 18 | 20 | 
 | 
| 19 | 21 | 
 | 
|  | 22 | +def get_zarr_url(stac_item_url: str) -> str: | 
|  | 23 | +    """Get Zarr asset URL from STAC item.""" | 
|  | 24 | +    with urlopen(stac_item_url) as response: | 
|  | 25 | +        item = json.loads(response.read()) | 
|  | 26 | + | 
|  | 27 | +    assets = item.get("assets", {}) | 
|  | 28 | + | 
|  | 29 | +    # Priority: product, zarr, then any .zarr asset | 
|  | 30 | +    for key in ["product", "zarr"]: | 
|  | 31 | +        if key in assets and (href := assets[key].get("href")): | 
|  | 32 | +            return str(href) | 
|  | 33 | + | 
|  | 34 | +    # Fallback: any asset with .zarr in href | 
|  | 35 | +    for asset in assets.values(): | 
|  | 36 | +        if ".zarr" in asset.get("href", ""): | 
|  | 37 | +            return str(asset["href"]) | 
|  | 38 | + | 
|  | 39 | +    raise RuntimeError("No Zarr asset found in STAC item") | 
|  | 40 | + | 
|  | 41 | + | 
| 20 | 42 | def run_conversion( | 
| 21 | 43 |     source_url: str, | 
| 22 | 44 |     collection: str, | 
| @@ -44,7 +66,7 @@ def run_conversion( | 
| 44 | 66 |     logger.info("=" * 78) | 
| 45 | 67 | 
 | 
| 46 | 68 |     # Extract item ID from URL | 
| 47 |  | -    item_id = extract_item_id(source_url) | 
|  | 69 | +    item_id = urlparse(source_url).path.rstrip("/").split("/")[-1] | 
| 48 | 70 |     logger.info(f"Item ID: {item_id}") | 
| 49 | 71 | 
 | 
| 50 | 72 |     # Resolve source: STAC item or direct Zarr URL | 
|  | 
0 commit comments