|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Query STAC API for new items to process. |
| 4 | +
|
| 5 | +This script searches for items in a source collection within a specified time window |
| 6 | +and checks if they already exist in the target collection to avoid reprocessing. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import logging |
| 11 | +import os |
| 12 | +import sys |
| 13 | +from datetime import UTC, datetime, timedelta |
| 14 | + |
| 15 | +from pystac_client import Client |
| 16 | + |
| 17 | +# Configure logging |
| 18 | +logging.basicConfig( |
| 19 | + level=os.getenv("LOG_LEVEL", "INFO"), |
| 20 | + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| 21 | +) |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +def main() -> None: |
| 26 | + """Main entry point for STAC query script.""" |
| 27 | + # Configuration from Argo workflow parameters |
| 28 | + STAC_API_URL = sys.argv[1] |
| 29 | + SOURCE_COLLECTION = sys.argv[2] |
| 30 | + TARGET_COLLECTION = sys.argv[3] |
| 31 | + END_TIME_OFFSET_HOURS = int(sys.argv[4]) |
| 32 | + LOOKBACK_HOURS = int(sys.argv[5]) |
| 33 | + AOI_BBOX = json.loads(sys.argv[6]) |
| 34 | + |
| 35 | + # Calculate time window |
| 36 | + end_time = datetime.now(UTC) - timedelta(hours=END_TIME_OFFSET_HOURS) |
| 37 | + start_time = end_time - timedelta(hours=LOOKBACK_HOURS) |
| 38 | + |
| 39 | + # Format datetime for STAC API (replace +00:00 with Z) |
| 40 | + start_time_str = start_time.isoformat().replace("+00:00", "Z") |
| 41 | + end_time_str = end_time.isoformat().replace("+00:00", "Z") |
| 42 | + |
| 43 | + logger.info(f"Querying STAC API: {STAC_API_URL}") |
| 44 | + logger.info(f"Collection: {SOURCE_COLLECTION}") |
| 45 | + logger.info(f"Time range: {start_time_str} to {end_time_str}") |
| 46 | + |
| 47 | + # Connect to STAC catalog |
| 48 | + catalog = Client.open(STAC_API_URL) |
| 49 | + |
| 50 | + # Search for items |
| 51 | + search = catalog.search( |
| 52 | + collections=[SOURCE_COLLECTION], |
| 53 | + datetime=f"{start_time_str}/{end_time_str}", |
| 54 | + bbox=AOI_BBOX, |
| 55 | + ) |
| 56 | + |
| 57 | + # Collect items to process |
| 58 | + items_to_process = [] |
| 59 | + checked_count = 0 |
| 60 | + |
| 61 | + for page in search.pages(): |
| 62 | + for item in page.items: |
| 63 | + checked_count += 1 |
| 64 | + |
| 65 | + # Get item URL |
| 66 | + item_url = next( |
| 67 | + (link.href for link in item.links if link.rel == "self"), |
| 68 | + None, |
| 69 | + ) |
| 70 | + |
| 71 | + if not item_url: |
| 72 | + logger.warning(f"Skipping {item.id}: No self link") |
| 73 | + continue |
| 74 | + |
| 75 | + # Check if already converted (prevent wasteful reprocessing) |
| 76 | + try: |
| 77 | + target_search = catalog.search( |
| 78 | + collections=[TARGET_COLLECTION], |
| 79 | + ids=[item.id], |
| 80 | + ) |
| 81 | + existing_items = list(target_search.items()) |
| 82 | + |
| 83 | + if existing_items: |
| 84 | + logger.info(f"Skipping {item.id}: Already converted") |
| 85 | + continue |
| 86 | + except Exception as e: |
| 87 | + logger.warning(f"Could not check {item.id}: {e}") |
| 88 | + # On error, process it to be safe |
| 89 | + |
| 90 | + # Add to processing queue |
| 91 | + items_to_process.append( |
| 92 | + { |
| 93 | + "source_url": item_url, |
| 94 | + "collection": TARGET_COLLECTION, |
| 95 | + "item_id": item.id, |
| 96 | + } |
| 97 | + ) |
| 98 | + |
| 99 | + logger.info(f"📊 Summary: Checked {checked_count} items, {len(items_to_process)} to process") |
| 100 | + |
| 101 | + # Output ONLY JSON to stdout (for Argo withParam) |
| 102 | + sys.stdout.write(json.dumps(items_to_process)) |
| 103 | + sys.stdout.flush() |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments