# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "marimo",
#     "numpy",
#     "matplotlib",
#     "pystac-client",
#     "rasterio",
#     "duckdb",
#     "geopandas",
#     "zarr>=3",
#     "fsspec",
#     "aiohttp",
#     "scikit-learn",
# ]
# ///

import marimo

__generated_with = "0.25.2"
app = marimo.App(width="medium")


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    # Cloud-native geospatial

    every example reads only the few MB it needs straight from a cloud bucket.

    | | Data | Where it lives | Format | Read with |
    |---|---|---|---|---|
    | 1 | Sentinel-2 satellite image | AWS Open Data, found via the [Earth Search](https://earth-search.aws.element84.com/v1) STAC catalog | COG (Cloud-Optimized GeoTIFF) | `pystac-client` + `rasterio` |
    | 2 | Fields of the World field boundaries | [Source Cooperative](https://source.coop/ftw/global-data-2e) | GeoParquet | `duckdb` |
    | 3 | AlphaEarth embeddings | [Source Cooperative](https://source.coop/tge-labs/aef-mosaic) | Zarr | `zarr` |

    **Why it's easy:** the files sit in public buckets behind plain HTTPS, and each format is cut into tiles, row groups or chunks with an index at the front, so your code asks for byte ranges (HTTP range requests) and pulls just your area, never the whole file.
    """)
    return


@app.cell
def _():
    import warnings
    import marimo as mo
    import numpy as np
    import matplotlib.pyplot as plt
    import pystac_client
    import rasterio
    from rasterio.warp import transform_bounds
    from rasterio.windows import from_bounds
    import duckdb
    import geopandas as gpd
    import zarr
    from sklearn.decomposition import PCA

    warnings.filterwarnings("ignore", module="pystac")

    def stretch(img):
        lo, hi = np.nanpercentile(img, [2, 98], axis=(0, 1))
        return np.clip((img - lo) / (hi - lo), 0, 1)

    return (
        PCA,
        duckdb,
        from_bounds,
        gpd,
        mo,
        np,
        plt,
        pystac_client,
        rasterio,
        stretch,
        transform_bounds,
        zarr,
    )


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    ## 1 · A satellite image: the Sundarbans in false colour

    Ask the STAC catalog for the clearest Sentinel-2 scene over the Sundarbans mangroves (India–Bangladesh), January–March 2025.
    """)
    return


@app.cell
def _(mo, pystac_client):
    place = [89.08, 21.88]
    catalog = pystac_client.Client.open("https://earth-search.aws.element84.com/v1")
    items = catalog.search(
        collections=["sentinel-2-c1-l2a"],
        intersects={"type": "Point", "coordinates": place},
        datetime="2025-01-01/2025-03-31",
        query={"eo:cloud_cover": {"lt": 5}, "s2:nodata_pixel_percentage": {"lt": 5}},
    ).item_collection()
    scene = min(items, key=lambda item: item.properties["eo:cloud_cover"])
    mo.md(
        f"{len(items)} scenes match; clearest: `{scene.id}`, "
        f"{scene.datetime:%d %b %Y}, {scene.properties['eo:cloud_cover']:.3f} % cloud."
    )
    return place, scene


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    False colour = near-infrared, red, green shown as R, G, B: living plants reflect lots of near-infrared, so the mangroves glow red. Asking for a tenth of the width (1/100 of the pixels) makes GDAL read the small overview stored inside each COG, not the full-size band.
    """)
    return


@app.cell
def _(np, plt, rasterio, scene, stretch):
    def read_band(name, size=1098):
        with rasterio.open(scene.assets[name].href) as src:
            return src.read(1, out_shape=(size, size)).astype("float32")

    false_colour = np.dstack([read_band(b) for b in ("nir", "red", "green")])
    false_colour[false_colour == 0] = np.nan

    plt.figure(figsize=(7, 7))
    plt.imshow(stretch(false_colour))
    plt.title(f"Sentinel-2 false colour, {scene.datetime:%d %b %Y}, 110 × 110 km")
    plt.axis("off")
    plt.gca()
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    Zoom to full 10 m detail: a 6 × 6 km window around `place` touches at most 4 of the band's 121 tiles (1024 × 1024 pixels each).
    """)
    return


@app.cell
def _(from_bounds, np, place, plt, rasterio, scene, stretch, transform_bounds):
    lon, lat = place
    zoom_box = (lon - 0.03, lat - 0.03, lon + 0.03, lat + 0.03)

    def read_window(name):
        with rasterio.open(scene.assets[name].href) as src:
            window = from_bounds(*transform_bounds("EPSG:4326", src.crs, *zoom_box), src.transform)
            return src.read(1, window=window).astype("float32")

    zoom = np.dstack([read_window(b) for b in ("nir", "red", "green")])

    plt.figure(figsize=(7, 7))
    plt.imshow(stretch(zoom))
    plt.title(f"10 m pixels, {zoom.shape[1]} × {zoom.shape[0]}")
    plt.axis("off")
    plt.gca()
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    ## 2 · Vectors: farm fields in Iowa with DuckDB

    [Fields of the World](https://fieldsofthe.world) drew field boundaries worldwide for 2017–2025 from Sentinel-2 with an AI model, one GeoParquet file per UTM zone per year. Most of Iowa is in zone 15: a 2.3 GB file with 2,882,476 fields in 354 row groups, and DuckDB uses each row group's bounding-box min/max to fetch only the 2 that touch our 7 × 7 km box.
    """)
    return


@app.cell
def _(duckdb, gpd, mo):
    box = (-93.70, 41.95, -93.62, 42.01)
    west, south, east, north = box

    fields_url = "https://data.source.coop/ftw/global-data-2e/vector/2025/zone=15/utm15.parquet"
    duckdb.sql("INSTALL spatial; LOAD spatial;")
    fields = duckdb.sql(f"""
        SELECT id, "metrics:area" / 10000 AS hectares, ST_AsWKB(geometry) AS geometry
        FROM read_parquet('{fields_url}')
        WHERE bbox.xmin < {east} AND bbox.xmax > {west}
          AND bbox.ymin < {north} AND bbox.ymax > {south}
    """).df()
    fields = gpd.GeoDataFrame(fields, geometry=gpd.GeoSeries.from_wkb(fields.geometry.apply(bytes)), crs="EPSG:4326")
    mo.md(f"{len(fields)} fields in the box; median size {fields.hectares.median():.1f} ha.")
    return east, fields, north, south, west


@app.cell
def _(east, fields, north, south, west):
    ax = fields.plot(column="hectares", cmap="YlGn", edgecolor="#333", linewidth=0.3, figsize=(7, 7), legend=True,
                     legend_kwds={"label": "field size (ha)", "shrink": 0.6})
    ax.set_xlim(west, east)
    ax.set_ylim(south, north)
    ax.set_title("Fields of the World, 2025")
    ax
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    ## 3 · Embeddings: what AlphaEarth sees in the same box

    AlphaEarth (Google DeepMind) squeezes a year of satellite data into 64 numbers per 10 m pixel; Source Cooperative serves all of 2017–2025 as one global Zarr whose chunks hold all 64 numbers for 256 × 256 pixels.
    """)
    return


@app.cell
def _(east, mo, north, np, south, west, zarr):
    aef = zarr.open_group("https://data.source.coop/tge-labs/aef-mosaic/", mode="r")
    dx, _, x0, _, dy, y0 = aef.attrs["spatial:transform"]

    year = 2025
    rows = slice(int((north - y0) / dy), int((south - y0) / dy))
    cols = slice(int((west - x0) / dx), int((east - x0) / dx))
    q = aef["embeddings"][list(aef["time"][:]).index(year), :, rows, cols]
    embeddings = np.sign(q) * (np.abs(q) / 127.5) ** 2

    mo.md(f"Global array {aef['embeddings'].shape} (year, number, row, column); we read {q.shape}, {q.nbytes / 1e6:.0f} MB.")

    return embeddings, year


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    Squash the 64 numbers to 3 with PCA and paint them as red, green, blue: similar colours = similar land. The fields split into two clear colours, and nobody gave the model any crop labels.
    """)
    return


@app.cell
def _(
    PCA,
    east,
    embeddings,
    fields,
    north,
    np,
    plt,
    south,
    stretch,
    west,
    year,
):
    bands, height, width = embeddings.shape
    rgb = PCA(3).fit_transform(embeddings.reshape(bands, -1).T).reshape(height, width, 3)

    fig, ax3 = plt.subplots(figsize=(7, 7))
    ax3.imshow(stretch(rgb), extent=(west, east, south, north))
    fields.boundary.plot(ax=ax3, color="white", linewidth=0.4)
    ax3.set_xlim(west, east)
    ax3.set_ylim(south, north)
    ax3.set_aspect(1 / np.cos(np.radians((south + north) / 2)))
    ax3.set_title(f"AlphaEarth {year}, 64 numbers → 3 colours, with 2025 field boundaries")
    ax3
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""
    ## Try

    - Swap `place = [89.08, 21.88]` for your home town (lon, lat), or `("nir", "red", "green")` for `("red", "green", "blue")`.
    - Change `year = 2025` to `2017` and compare.
    - Move `box` anywhere between 96° W and 90° W (UTM zone 15), or change `2025` in `fields_url` to another year.

    Data: Sentinel-2 © Copernicus (ESA). Fields of the World, CC BY 4.0. "The AlphaEarth Foundations Satellite Embedding dataset is produced by Google and Google DeepMind." (CC BY 4.0).
    """)
    return


if __name__ == "__main__":
    app.run()
