# AGENTS.md — GridIberia Open Data Catalog

Machine-oriented guide for AI agents querying the GridIberia cloud-native GeoParquet + STAC catalog.
Catalog version: `2026-11-04` (generated `2026-11-30T08:07:43Z`).

## Catalog Endpoints & File Paths

| Purpose | Relative Path | Canonical URL |
|---|---|---|
| STAC Root Catalog | `./catalog.json` | `https://data.gridiberia.com/catalog.json` |
| Snapshots STAC Collection | `./snapshots/collection.json` | `https://data.gridiberia.com/snapshots/collection.json` |
| Snapshots GeoParquet | `./snapshots/snapshots.parquet` | `https://data.gridiberia.com/snapshots/snapshots.parquet` |
| Snapshots PMTiles (MVT, latest per node) | `./snapshots/snapshots.pmtiles` | `https://data.gridiberia.com/snapshots/snapshots.pmtiles` |
| Monthly Diffs STAC Collection | `./diffs/collection.json` | `https://data.gridiberia.com/diffs/collection.json` |
| Monthly Diffs GeoParquet | `./diffs/diffs.parquet` | `https://data.gridiberia.com/diffs/diffs.parquet` |
| Asset Manifest & SHA-256 | `./versions.json` | `https://data.gridiberia.com/versions.json` |
| Human Documentation | `./README.md` | `https://data.gridiberia.com/README.md` |

## Critical Domain Rules (Read Before Querying)

1. **Always filter to the latest snapshot before aggregating `snapshots.parquet`:**
   `snapshots.parquet` stores *multiple historical dates* per node (`85,060` total rows).
   Never `SUM(available_mw)` across the whole table without filtering to each `(operator_code, direction)`'s
   `MAX(snapshot_date)` (or a specific `snapshot_date`), or you will double-count months.
2. **Unit semantics — MW (Spain) vs. MVA (Portugal):**
   - Spain (`country = 'ES'`, operators `GRT`, `R1-299`, `R1-026`, `R1-030`, `R1-002`,
     `R1-008`, `R1-005`, `R1-003`):
     `available_mw`, `occupied_mw`, `pending_mw`, `regulatory_reserved_mw` are **MW** (active power).
   - Portugal (`country = 'PT'`, operator `E-REDES-PT`): E-REDES publishes **MVA** (apparent power),
     stored unchanged in the `*_mw` columns (no power factor conversion applied). Always group by
     `country` or note the MW vs. MVA distinction when reporting Iberian totals.
3. **Coordinate coverage and precision caveats:**
   - **Spanish DSOs (`R1-299`, `R1-026`, `R1-030`, `R1-002`):** Substation coordinates converted from
     official ETRS89 UTM X/Y to WGS84 (`EPSG:4326`).
   - **EDP España (`R1-008`, `R1-005`, `R1-003`):** WGS84 coordinates published directly by the
     operator (no conversion); one row per substation busbar voltage level.
   - **REE transmission (`GRT`):** Geocoded via a static audited table (~85% coverage, derived from
     OpenStreetMap ODbL-1.0 + DSO cross-checks). Roughly ~15% of REE rows have `lat`, `lon`, and
     `geometry` equal to `NULL`.
   - **E-REDES Portugal (`E-REDES-PT`):** Coordinates are **municipality (*concelho*) centroids**,
     not exact substation coordinates; multiple substations in the same municipality share coordinates.
4. **`status_change` values in `diffs.parquet`:**
   - `newly_opened`: previous `available_mw` was `0` (or `NULL`), new `available_mw > 0`.
   - `newly_saturated`: previous `available_mw > 0`, new `available_mw = 0`.
   - `increased`: `available_mw > prev_available_mw` (and previously `> 0`).
   - `decreased`: `available_mw < prev_available_mw` (and still `> 0`).
   - Rows with `ABS(available_mw - prev_available_mw) <= 0.001` (`unchanged`) are omitted from `diffs.parquet`.
5. **GeoParquet spatial queries in DuckDB:**
   Both Parquet files contain a WKB `geometry` column in `EPSG:4326` with `(lon, lat)` coordinates.
   In DuckDB 2.0+, run `LOAD spatial; SET geometry_always_xy = true;` before calling
   `ST_Distance_Sphere(...)` so axis order is explicitly `[longitude, latitude]`.
6. **`snapshots/snapshots.pmtiles` is for visualization, not analysis:**
   PMTiles v3 vector tiles (layer `snapshots`, z0–z10,
   15,214 points) with only the latest geolocated row per
   `(operator_code, direction, node_id)` and a subset of columns
   (`snapshot_date`, `country`, `operator_code`, `operator_name`, `grid_level`, `direction`, `node_id`, `node_name`, `voltage_kv`, `province`, `municipality`, `available_mw`, `occupied_mw`, `pending_mw`). Open it in pmtiles.io, QGIS or
   MapLibre for instant viewing; query the GeoParquet files for numbers.

## Schema: `snapshots/snapshots.parquet`

| Column | Type | Description |
|---|---|---|
| `snapshot_date` | `DATE` | Publication date parsed from the official operator file (ISO YYYY-MM-DD). |
| `country` | `VARCHAR` | ISO 3166-1 alpha-2 country code ('ES' for Spain, 'PT' for Portugal). |
| `operator_code` | `VARCHAR` | Operator identifier: 'GRT' (REE), 'R1-299' / 'R1-026' / 'R1-030' (e-distribución), 'R1-002' (UFD), 'R1-008' / 'R1-005' / 'R1-003' (EDP España: E-REDES / Viesgo / Begasa), 'E-REDES-PT' (E-REDES Portugal). |
| `operator_name` | `VARCHAR` | Human-readable grid operator name. |
| `grid_level` | `VARCHAR` | Network tier: 'transmission' or 'distribution'. |
| `direction` | `VARCHAR` | Access capacity direction: 'generation' (injection) or 'demand' (consumption). |
| `node_id` | `VARCHAR` | Stable node/substation key within operator and voltage level. |
| `node_name` | `VARCHAR` | Substation or grid node name as published by the operator. |
| `rdt_node_ref` | `VARCHAR` | Upstream transmission node reference ('Nudo Afección RdT'), nullable. |
| `voltage_kv` | `DOUBLE` | Nominal voltage level in kilovolts (kV). |
| `ccaa` | `VARCHAR` | Autonomous community (Spain) or NUTS III sub-region (Portugal). |
| `province` | `VARCHAR` | Province (Spain) or district / distrito (Portugal). |
| `municipality` | `VARCHAR` | Municipality (Spain) or concelho (Portugal). |
| `utm_x` | `DOUBLE` | Raw UTM easting coordinate as published by Spanish DSOs (ETRS89), nullable. |
| `utm_y` | `DOUBLE` | Raw UTM northing coordinate as published by Spanish DSOs (ETRS89), nullable. |
| `lat` | `DOUBLE` | WGS84 latitude in decimal degrees (EPSG:4326), nullable. |
| `lon` | `DOUBLE` | WGS84 longitude in decimal degrees (EPSG:4326), nullable. |
| `available_mw` | `DOUBLE` | Available hosting capacity: MW (active power) for Spain ('ES'), MVA (apparent power, published unchanged) for Portugal ('PT'). |
| `occupied_mw` | `DOUBLE` | Granted or occupied capacity (MW for Spain, MVA for Portugal). |
| `pending_mw` | `DOUBLE` | Capacity currently in queue or pending evaluation (MW for Spain, MVA for PT). |
| `regulatory_reserved_mw` | `DOUBLE` | Capacity reserved or committed under regulatory rules (MW). |
| `unavailable_reason` | `VARCHAR` | Official operator reason when capacity is unavailable or constrained. |
| `source_url` | `VARCHAR` | Direct official operator source URL used to build this row. |
| `geometry` | `WKB` | WGS84 (EPSG:4326) WKB Point geometry constructed from (lon, lat); NULL when coordinates are unavailable. |

## Schema: `diffs/diffs.parquet`

| Column | Type | Description |
|---|---|---|
| `snapshot_date` | `DATE` | Publication date parsed from the official operator file (ISO YYYY-MM-DD). |
| `prev_snapshot_date` | `DATE` | Previous snapshot date for the same (operator_code, direction, node_id). |
| `operator_code` | `VARCHAR` | Operator identifier: 'GRT' (REE), 'R1-299' / 'R1-026' / 'R1-030' (e-distribución), 'R1-002' (UFD), 'R1-008' / 'R1-005' / 'R1-003' (EDP España: E-REDES / Viesgo / Begasa), 'E-REDES-PT' (E-REDES Portugal). |
| `operator_name` | `VARCHAR` | Human-readable grid operator name. |
| `grid_level` | `VARCHAR` | Network tier: 'transmission' or 'distribution'. |
| `direction` | `VARCHAR` | Access capacity direction: 'generation' (injection) or 'demand' (consumption). |
| `node_id` | `VARCHAR` | Stable node/substation key within operator and voltage level. |
| `node_name` | `VARCHAR` | Substation or grid node name as published by the operator. |
| `province` | `VARCHAR` | Province (Spain) or district / distrito (Portugal). |
| `municipality` | `VARCHAR` | Municipality (Spain) or concelho (Portugal). |
| `voltage_kv` | `DOUBLE` | Nominal voltage level in kilovolts (kV). |
| `lat` | `DOUBLE` | WGS84 latitude in decimal degrees (EPSG:4326), nullable. |
| `lon` | `DOUBLE` | WGS84 longitude in decimal degrees (EPSG:4326), nullable. |
| `prev_available_mw` | `DOUBLE` | Available hosting capacity on prev_snapshot_date (MW for Spain, MVA for Portugal). |
| `available_mw` | `DOUBLE` | Available hosting capacity: MW (active power) for Spain ('ES'), MVA (apparent power, published unchanged) for Portugal ('PT'). |
| `delta_available_mw` | `DOUBLE` | Change in available capacity (available_mw - prev_available_mw, rounded). |
| `status_change` | `VARCHAR` | Capacity transition category: 'newly_opened', 'newly_saturated', 'increased', or 'decreased'. |
| `geometry` | `WKB` | WGS84 (EPSG:4326) WKB Point geometry constructed from (lon, lat); NULL when coordinates are unavailable. |

## Verified DuckDB SQL Recipes

### 1. Latest Hosting Capacity by Country, Operator, and Direction

```sql
WITH latest AS (
    SELECT
        *,
        MAX(snapshot_date) OVER (PARTITION BY operator_code, direction) AS max_dt
    FROM read_parquet('https://data.gridiberia.com/snapshots/snapshots.parquet')
)
SELECT
    country,
    operator_code,
    operator_name,
    direction,
    max_dt AS latest_snapshot_date,
    CASE WHEN country = 'PT' THEN 'MVA' ELSE 'MW' END AS unit,
    COUNT(*) AS node_count,
    COUNT(*) FILTER (WHERE geometry IS NOT NULL) AS geolocated_nodes,
    ROUND(SUM(available_mw), 2) AS total_available
FROM latest
WHERE snapshot_date = max_dt
GROUP BY 1, 2, 3, 4, 5, 6
ORDER BY 1, 2, 4;
```

### 2. Top Available Demand Nodes in Spain (Latest Snapshot)

```sql
WITH latest_es AS (
    SELECT
        *,
        MAX(snapshot_date) OVER (PARTITION BY operator_code, direction) AS max_dt
    FROM read_parquet('https://data.gridiberia.com/snapshots/snapshots.parquet')
    WHERE country = 'ES' AND direction = 'demand'
)
SELECT
    snapshot_date,
    operator_name,
    grid_level,
    node_name,
    province,
    voltage_kv,
    available_mw,
    lat,
    lon
FROM latest_es
WHERE snapshot_date = max_dt
  AND available_mw >= 100
ORDER BY available_mw DESC
LIMIT 20;
```

### 3. Latest Newly Opened and Newly Saturated Substations (`diffs.parquet`)

```sql
SELECT
    snapshot_date,
    prev_snapshot_date,
    operator_name,
    direction,
    node_name,
    province,
    voltage_kv,
    prev_available_mw,
    available_mw,
    delta_available_mw,
    status_change
FROM read_parquet('https://data.gridiberia.com/diffs/diffs.parquet')
WHERE snapshot_date = (SELECT MAX(snapshot_date) FROM read_parquet('https://data.gridiberia.com/diffs/diffs.parquet'))
  AND status_change IN ('newly_opened', 'newly_saturated')
ORDER BY ABS(delta_available_mw) DESC
LIMIT 25;
```

### 4. Spatial Radius Search Around Madrid Using GeoParquet `geometry`

```sql
LOAD spatial;
SET geometry_always_xy = true;

WITH latest AS (
    SELECT
        *,
        MAX(snapshot_date) OVER (PARTITION BY operator_code, direction) AS max_dt
    FROM read_parquet('https://data.gridiberia.com/snapshots/snapshots.parquet')
    WHERE direction = 'demand'
)
SELECT
    operator_name,
    node_name,
    voltage_kv,
    available_mw,
    ROUND(ST_Distance_Sphere(geometry, ST_Point(-3.7038, 40.4168)) / 1000.0, 2) AS dist_km
FROM latest
WHERE snapshot_date = max_dt
  AND geometry IS NOT NULL
  AND available_mw > 0
  AND ST_Distance_Sphere(geometry, ST_Point(-3.7038, 40.4168)) <= 50000
ORDER BY available_mw DESC
LIMIT 15;
```
