{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/ringsaturn/xue/blob/main/docs/schemas/sounding-index.schema.json",
  "title": "SoundingIndex",
  "description": "sounding/<YYYY>/<MM>/<DD>/sounding.<issue>/index.json (docs/sounding.md section 4). The soundings file's name, the stations' order and uniqueness, the byte spans tiling soundings.jsonl, the times' order and latest equal to the first of them are the readers' own checks.",
  "type": "object",
  "required": ["schemaVersion", "issued", "generated", "watermark", "soundings", "stations", "sources"],
  "properties": {
    "schemaVersion": {"type": "integer", "const": 1},
    "issued": {"$ref": "#/$defs/Timestamp"},
    "generated": {"$ref": "#/$defs/Timestamp"},
    "watermark": {
      "description": "Per source id, the newest object modification time its listing held, or null when it has never been listed.",
      "type": "object",
      "additionalProperties": {"type": ["string", "null"], "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$", "format": "date-time"}
    },
    "soundings": {"$ref": "#/$defs/CrcFile"},
    "stations": {"type": "array", "items": {"$ref": "#/$defs/SoundingStationEntry"}},
    "sources": {"type": "array", "items": {"$ref": "#/$defs/SoundingSource"}}
  },
  "$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"
    },
    "SourceKey": {
      "description": "A gateway id (jp-jma-gts-to-wis2), a national WIS2 node (wis2:fr-meteofrance), the node discovery (wis2), Météo-France's open data (meteofrance) or NOAA's text bulletins (nws-tgftp), admitted on shape alone.",
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*([.:-][a-z0-9]+)*$"
    },
    "CrcFile": {
      "description": "The file beside the index: its name, length and zlib CRC32.",
      "type": "object",
      "required": ["path", "byteLength", "crc32"],
      "properties": {
        "path": {"type": "string", "minLength": 1},
        "byteLength": {"type": "integer", "minimum": 0},
        "crc32": {"type": "string", "pattern": "^[0-9a-f]{8}$"}
      }
    },
    "Height": {
      "description": "Geopotential metres, in the range of the level array z.",
      "type": ["number", "null"],
      "minimum": -1000,
      "maximum": 100000
    },
    "SoundingHeadline": {
      "description": "The newest sounding, summarised for a marker layer: 500 hPa temperature and dew point in degrees C, freezing level and precipitable water (mm) as derived, and its level count.",
      "type": "object",
      "required": ["levels"],
      "properties": {
        "t500": {"type": ["number", "null"], "minimum": -120, "maximum": 60},
        "td500": {"type": ["number", "null"], "minimum": -150, "maximum": 60},
        "freezingLevel": {"$ref": "#/$defs/Height"},
        "pw": {"type": ["number", "null"], "minimum": 0, "maximum": 300},
        "levels": {"type": "integer", "minimum": 1}
      }
    },
    "SoundingStationEntry": {
      "description": "One station: its position from the newest sounding, the byte span of its line in soundings.jsonl and its nominal times, newest first.",
      "type": "object",
      "required": ["id", "lat", "lon", "offset", "length", "latest", "times", "headline"],
      "properties": {
        "id": {"description": "A WIGOS identifier, series-issuer-issue-local.", "type": "string", "pattern": "^\\d+-\\d+-\\d+-[0-9A-Za-z_]+$"},
        "wmo": {"type": ["string", "null"], "pattern": "^\\d{5}$"},
        "name": {"type": ["string", "null"]},
        "lat": {"type": "number", "minimum": -90, "maximum": 90},
        "lon": {"type": "number", "minimum": -180, "maximum": 180},
        "elev": {"type": ["number", "null"], "minimum": -500, "maximum": 9000},
        "offset": {"type": "integer", "minimum": 0},
        "length": {"type": "integer", "minimum": 1},
        "latest": {"$ref": "#/$defs/Timestamp"},
        "times": {"type": "array", "minItems": 1, "items": {"$ref": "#/$defs/Timestamp"}},
        "headline": {"$ref": "#/$defs/SoundingHeadline"}
      }
    },
    "SoundingSource": {
      "description": "One source's outcome (docs/sounding.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": {"type": ["string", "null"], "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$", "format": "date-time"},
        "url": {"type": "string"},
        "watermark": {"type": ["string", "null"], "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$", "format": "date-time"},
        "listed": {"type": "integer", "minimum": 0},
        "objects": {"type": "integer", "minimum": 0},
        "reused": {"type": "integer", "minimum": 0},
        "failed": {"type": "integer", "minimum": 0},
        "bytes": {"type": "integer", "minimum": 0},
        "bulletins": {"type": "integer", "minimum": 0},
        "nil": {"type": "integer", "minimum": 0},
        "descents": {"type": "integer", "minimum": 0},
        "unreadable": {"type": "integer", "minimum": 0},
        "subsets": {"type": "integer", "minimum": 0},
        "dropped": {"type": "integer", "minimum": 0},
        "soundings": {"type": "integer", "minimum": 0}
      }
    }
  }
}
