{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/ringsaturn/xue/blob/main/docs/schemas/tc-index.schema.json",
  "title": "TcIndex",
  "description": "tc/<YYYY>/<MM>/<DD>/tc.<issue>/index.json (docs/tc.md section 4). The storms' and paths' uniqueness, the alias keys and the windows' order, and that a crosswalk entry names two different storms are the readers' own checks.",
  "type": "object",
  "required": ["schemaVersion", "issued", "generated", "storms", "crosswalk", "sources"],
  "properties": {
    "schemaVersion": {"type": "integer", "const": 1},
    "issued": {"$ref": "#/$defs/Hour"},
    "generated": {"$ref": "#/$defs/Timestamp"},
    "storms": {"type": "array", "items": {"$ref": "#/$defs/TcIndexEntry"}},
    "crosswalk": {
      "description": "An earlier storm id → the id it merged into.",
      "type": "object",
      "additionalProperties": {"$ref": "#/$defs/StormId"}
    },
    "sources": {"type": "array", "items": {"$ref": "#/$defs/TcSource"}}
  },
  "$defs": {
    "Timestamp": {
      "description": "ISO 8601 UTC, to the second, with a Z suffix.",
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$",
      "format": "date-time"
    },
    "Hour": {
      "description": "A whole UTC hour.",
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:00:00Z$",
      "format": "date-time"
    },
    "StormId": {
      "description": "An ATCF id (EP142026) or a synthetic x-<basin>-<first seen YYYYMMDDHH>-<n> (docs/tc.md section 3).",
      "type": "string",
      "pattern": "^([A-Z]{2}\\d{6}|x-[a-z]{2}-\\d{10}-\\d+)$"
    },
    "SourceKey": {
      "description": "An agency, model or source id, admitted on shape alone.",
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*$"
    },
    "AliasWindow": {
      "description": "When an alias named the storm.",
      "type": "object",
      "required": ["from", "to"],
      "properties": {
        "from": {"$ref": "#/$defs/Timestamp"},
        "to": {"$ref": "#/$defs/Timestamp"}
      }
    },
    "TcIndexEntry": {
      "description": "One system: its file beside the index and its headline.",
      "type": "object",
      "required": ["id", "level", "basin", "path", "byteLength", "crc32", "aliases", "agencies", "models", "best"],
      "properties": {
        "id": {"$ref": "#/$defs/StormId"},
        "level": {"enum": ["A", "B", "C"]},
        "basin": {"type": "string", "pattern": "^[A-Z]{2}$"},
        "name": {"type": ["string", "null"]},
        "path": {"description": "A file name beside the index.", "type": "string", "pattern": "^[^/.][^/]*$"},
        "byteLength": {"type": "integer", "minimum": 1},
        "crc32": {"type": "string", "pattern": "^[0-9a-f]{8}$"},
        "aliases": {"type": "object", "additionalProperties": {"$ref": "#/$defs/AliasWindow"}},
        "position": {
          "description": "The headline's position, or null for a system with nothing observed.",
          "type": ["object", "null"],
          "required": ["time", "lat", "lon"],
          "properties": {
            "time": {"$ref": "#/$defs/Timestamp"},
            "lat": {"type": "number", "minimum": -90, "maximum": 90},
            "lon": {"type": "number", "minimum": -180, "maximum": 180}
          }
        },
        "vmax": {"description": "m/s.", "type": ["number", "null"], "minimum": 0},
        "pmin": {"description": "hPa.", "type": ["number", "null"], "minimum": 800, "maximum": 1100},
        "class": {"type": ["string", "null"]},
        "classAgency": {"type": ["string", "null"]},
        "agencies": {"type": "array", "items": {"$ref": "#/$defs/SourceKey"}},
        "models": {"type": "array", "items": {"$ref": "#/$defs/SourceKey"}},
        "best": {"type": "array", "items": {"$ref": "#/$defs/SourceKey"}}
      }
    },
    "TcSource": {
      "description": "One source's outcome (docs/tc.md section 6). That ok false carries error, and that ids are unique, are the readers' own checks.",
      "type": "object",
      "required": ["id", "ok"],
      "properties": {
        "id": {"$ref": "#/$defs/SourceKey"},
        "ok": {"type": "boolean"},
        "error": {"type": "string"},
        "fetched": {"$ref": "#/$defs/Timestamp"},
        "url": {"type": "string"},
        "cycle": {"description": "The model run a model source was read from.", "type": "string", "pattern": "^\\d{10}$"},
        "products": {"type": "integer", "minimum": 0},
        "storms": {"type": "integer", "minimum": 0},
        "members": {"type": "integer", "minimum": 0},
        "systems": {"type": "integer", "minimum": 0}
      }
    }
  }
}
