{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/ringsaturn/xue/blob/main/docs/schemas/bundle-metadata.schema.json",
  "title": "BundleMetadataV3",
  "description": "A bundle's metadata JSON, schemaVersion 3, or 4 on a projected grid (docs/bundle.md, \"Metadata JSON\"): the document a .xue container embeds, a grid store's group carries as attributes.xue or attributes.xue_bundles[<bundle id>], and a manifest poster's metadataJson holds as a string. Versions 1 and 2 are read forever and published by nothing; they are the readers' own checks. Also the readers': that the declared version is the lowest the metadata needs, that exactly one of frameStep and frameOffsets is present, that frameOffsets has frameCount strictly increasing elements beginning with firstFrameOffset and does not hold one step throughout, that the last offset of a uniform axis is at most 65534, that unitSeconds divides 3600 and is the coarsest unit that fits, that the numericIds are unique, that a fixed surface value or an aerosol limit is wholly present or wholly null, that an aerosol interval of type 255 carries null limits and any other type integer ones, that a projection's latitudes are strictly between the poles, its two standard parallels do not sum to zero and its dy is not zero, that no field of the other grid kind is present even as null, and that a gaussian block's rows firstRow + row x rowStride stay below 2n.",
  "type": "object",
  "required": ["schemaVersion", "model", "runTime", "time", "grid", "variables"],
  "anyOf": [
    {"type": "object", "properties": {"schemaVersion": {"const": 3}, "grid": {"type": "object", "properties": {"projection": {"type": "null"}}}}},
    {"type": "object", "properties": {"schemaVersion": {"const": 4}, "grid": {"type": "object", "required": ["projection"]}}}
  ],
  "properties": {
    "schemaVersion": {"description": "4 exactly when the grid carries a projection, 3 otherwise: the lowest version that fits.", "type": "integer", "enum": [3, 4]},
    "model": {"description": "The source dataset, with product (docs/bundle.md, \"Projected Sources\").", "type": "string", "minLength": 1},
    "product": {"description": "Every writer writes it; no reader needs it.", "type": "string", "minLength": 1},
    "runTime": {"$ref": "#/$defs/Timestamp"},
    "profile": {"description": "The quantization profile the codebooks come from. Every writer writes it; no reader needs it.", "type": "string", "minLength": 1},
    "time": {"$ref": "#/$defs/Time"},
    "grid": {"$ref": "#/$defs/Grid"},
    "variables": {"type": "array", "minItems": 1, "maxItems": 255, "items": {"$ref": "#/$defs/Variable"}}
  },
  "$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"
    },
    "Time": {
      "description": "The schemaVersion 3 time axis (docs/bundle.md, \"Time Axis\"): integer offsets from runTime, each unitSeconds long, uniform with frameStep or listed with frameOffsets. No other field may appear.",
      "type": "object",
      "required": ["unitSeconds", "firstFrameOffset", "frameCount"],
      "properties": {
        "unitSeconds": {"description": "A whole divisor of 3600.", "type": "integer", "minimum": 1, "maximum": 3600},
        "firstFrameOffset": {"type": "integer", "minimum": 0, "maximum": 65534},
        "frameCount": {"type": "integer", "minimum": 1, "maximum": 65535},
        "frameStep": {"type": "integer", "minimum": 1, "maximum": 65535},
        "frameOffsets": {"type": "array", "minItems": 3, "items": {"type": "integer", "minimum": 0, "maximum": 65534}}
      },
      "additionalProperties": false,
      "anyOf": [
        {"type": "object", "required": ["frameStep"]},
        {"type": "object", "required": ["frameOffsets"]}
      ]
    },
    "Grid": {
      "description": "The grid the planes are on (docs/bundle.md, \"Regional Grids\"): row-major, north to south, west to east. Regular, with the four latitude/longitude fields (and a gaussian block when its rows are really a Gaussian grid's), or projected, with a projection instead of them. A field of the other kind is refused: each branch below requires its own fields and demands null of the other's, which the property's own schema here refuses. Every writer writes layout, rowOrder and columnOrder, which have one value each; no reader needs them.",
      "type": "object",
      "required": ["width", "height", "wrapLongitude"],
      "anyOf": [
        {
          "type": "object",
          "required": ["firstLongitude", "firstLatitude", "longitudeStep", "latitudeStep"],
          "properties": {"projection": {"type": "null"}}
        },
        {
          "type": "object",
          "required": ["projection"],
          "properties": {
            "firstLongitude": {"type": "null"},
            "firstLatitude": {"type": "null"},
            "longitudeStep": {"type": "null"},
            "latitudeStep": {"type": "null"},
            "gaussian": {"type": "null"},
            "wrapLongitude": {"const": false}
          }
        }
      ],
      "properties": {
        "width": {"type": "integer", "minimum": 1},
        "height": {"type": "integer", "minimum": 1},
        "layout": {"type": "string", "const": "row-major"},
        "rowOrder": {"type": "string", "const": "north-to-south"},
        "columnOrder": {"type": "string", "const": "west-to-east"},
        "firstLongitude": {"description": "The first column's centre, degrees east.", "type": "number"},
        "firstLatitude": {"description": "The first row's centre, degrees north.", "type": "number"},
        "longitudeStep": {"type": "number", "exclusiveMinimum": 0},
        "latitudeStep": {"description": "Negative: rows run north to south.", "type": "number"},
        "wrapLongitude": {"description": "True exactly when width x longitudeStep is 360 degrees; false on a projected grid.", "type": "boolean"},
        "gaussian": {"$ref": "#/$defs/Gaussian"},
        "projection": {"$ref": "#/$defs/Projection"}
      }
    },
    "Gaussian": {
      "description": "The rows are rows firstRow + row x rowStride of the regular Gaussian grid with n latitudes per hemisphere, counted from the north. The regular fields stay the equidistant approximation a renderer samples with; a reader that places a cell computes the true latitude (docs/zarr-profile.md, \"Coordinates\").",
      "type": "object",
      "required": ["n", "firstRow", "rowStride"],
      "additionalProperties": false,
      "properties": {
        "n": {"type": "integer", "minimum": 1, "maximum": 32768},
        "firstRow": {"type": "integer", "minimum": 0},
        "rowStride": {"type": "integer", "minimum": 1}
      }
    },
    "Projection": {
      "description": "The grid's map projection and where its cells lie on it, on a sphere. Cell (row, column) has its centre at x0 + column x dx, y0 + row x dy, in projected metres.",
      "type": "object",
      "required": ["kind", "radius", "standardParallel1", "standardParallel2", "latitudeOfOrigin", "centralMeridian", "x0", "y0", "dx", "dy"],
      "additionalProperties": false,
      "properties": {
        "kind": {"description": "A closed set: a reader refuses a kind it does not implement.", "type": "string", "enum": ["lambert-conformal"]},
        "radius": {"description": "The sphere's radius, metres.", "type": "number", "exclusiveMinimum": 0},
        "standardParallel1": {"type": "number", "minimum": -90, "maximum": 90},
        "standardParallel2": {"type": "number", "minimum": -90, "maximum": 90},
        "latitudeOfOrigin": {"type": "number", "minimum": -90, "maximum": 90},
        "centralMeridian": {"type": "number", "minimum": -180, "maximum": 180},
        "x0": {"description": "The first column's centre, projected metres.", "type": "number"},
        "y0": {"description": "The first (northern) row's centre, projected metres.", "type": "number"},
        "dx": {"type": "number", "exclusiveMinimum": 0},
        "dy": {"description": "Negative: rows run north to south.", "type": "number", "maximum": 0}
      }
    },
    "Variable": {
      "description": "One variable of the bundle: its file-local numericId (1..n in list order, the index's variableId), its GRIB2 identity and its codebook.",
      "type": "object",
      "required": ["numericId", "id", "label", "unit", "parameter", "quantization"],
      "properties": {
        "numericId": {"type": "integer", "minimum": 1, "maximum": 255},
        "id": {"type": "string", "minLength": 1},
        "label": {"type": "string"},
        "unit": {"type": "string"},
        "parameter": {"$ref": "#/$defs/Parameter"},
        "band": {"$ref": "#/$defs/Band"},
        "producer": {"$ref": "#/$defs/Producer"},
        "aerosol": {"$ref": "#/$defs/Aerosol"},
        "ensemble": {"$ref": "#/$defs/Ensemble"},
        "quantization": {"$ref": "#/$defs/Quantization"}
      }
    },
    "Parameter": {
      "description": "The variable's identity in GRIB2's terms (docs/bundle.md, \"Variable Identity\"). No other key is allowed.",
      "type": "object",
      "required": ["discipline", "parameterCategory", "parameterNumber", "typeOfFirstFixedSurface", "scaleFactorOfFirstFixedSurface", "scaledValueOfFirstFixedSurface"],
      "properties": {
        "discipline": {"type": "integer", "minimum": 0, "maximum": 255},
        "parameterCategory": {"type": "integer", "minimum": 0, "maximum": 255},
        "parameterNumber": {"type": "integer", "minimum": 0, "maximum": 255},
        "typeOfFirstFixedSurface": {"type": "integer", "minimum": 0, "maximum": 255},
        "scaleFactorOfFirstFixedSurface": {"$ref": "#/$defs/ScaleFactor"},
        "scaledValueOfFirstFixedSurface": {"$ref": "#/$defs/ScaledValue"},
        "typeOfStatisticalProcessing": {"description": "Code table 4.10; absent for an instantaneous field.", "type": "integer", "minimum": 0, "maximum": 255}
      },
      "additionalProperties": false
    },
    "Band": {
      "description": "The spectral band a satellite image was measured in, in the fields of GRIB2 template 4.31 (docs/bundle.md, \"Band, Producer and Aerosol\"). All five keys, no other.",
      "type": "object",
      "required": ["satelliteSeries", "satelliteNumber", "instrumentType", "scaleFactorOfCentralWaveNumber", "scaledValueOfCentralWaveNumber"],
      "properties": {
        "satelliteSeries": {"type": "integer", "minimum": 0, "maximum": 65535},
        "satelliteNumber": {"type": "integer", "minimum": 0, "maximum": 65535},
        "instrumentType": {"type": "integer", "minimum": 0, "maximum": 65535},
        "scaleFactorOfCentralWaveNumber": {"type": "integer", "minimum": -127, "maximum": 127},
        "scaledValueOfCentralWaveNumber": {"type": "integer", "minimum": 0, "maximum": 4294967294}
      },
      "additionalProperties": false
    },
    "Producer": {
      "description": "The algorithm that derived a composite field. Both keys, no other.",
      "type": "object",
      "required": ["id", "version"],
      "properties": {
        "id": {"type": "string", "pattern": "^[a-z][a-z0-9]*$"},
        "version": {"type": "string", "minLength": 1}
      },
      "additionalProperties": false
    },
    "Aerosol": {
      "description": "An aerosol product's species and intervals, in the fields of GRIB2 template 4.48. All eleven keys, no other; a limit is scaledValue x 10^-scaleFactor metres.",
      "type": "object",
      "required": [
        "aerosolType", "typeOfSizeInterval",
        "scaleFactorOfFirstSize", "scaledValueOfFirstSize", "scaleFactorOfSecondSize", "scaledValueOfSecondSize",
        "typeOfWavelengthInterval",
        "scaleFactorOfFirstWavelength", "scaledValueOfFirstWavelength", "scaleFactorOfSecondWavelength", "scaledValueOfSecondWavelength"
      ],
      "properties": {
        "aerosolType": {"description": "Code table 4.233.", "type": "integer", "minimum": 0, "maximum": 65535},
        "typeOfSizeInterval": {"description": "Code table 4.91; 255 is missing.", "type": "integer", "minimum": 0, "maximum": 255},
        "scaleFactorOfFirstSize": {"$ref": "#/$defs/ScaleFactor"},
        "scaledValueOfFirstSize": {"$ref": "#/$defs/ScaledValue"},
        "scaleFactorOfSecondSize": {"$ref": "#/$defs/ScaleFactor"},
        "scaledValueOfSecondSize": {"$ref": "#/$defs/ScaledValue"},
        "typeOfWavelengthInterval": {"type": "integer", "minimum": 0, "maximum": 255},
        "scaleFactorOfFirstWavelength": {"$ref": "#/$defs/ScaleFactor"},
        "scaledValueOfFirstWavelength": {"$ref": "#/$defs/ScaledValue"},
        "scaleFactorOfSecondWavelength": {"$ref": "#/$defs/ScaleFactor"},
        "scaledValueOfSecondWavelength": {"$ref": "#/$defs/ScaledValue"}
      },
      "additionalProperties": false
    },
    "Ensemble": {
      "description": "Which statistic of an ensemble the field is (docs/bundle.md, \"Ensemble\"): exactly one of three shapes, told apart by their keys. A probability's limits are integer exactly where its type uses them (types 0, 3 and 5 the lower, 1 and 4 the upper, 2 both) and null elsewhere; a limit is scaledValue x 10^-scaleFactor in the GRIB2 unit of the parameter.",
      "anyOf": [
        {"$ref": "#/$defs/EnsembleDerived"},
        {"$ref": "#/$defs/EnsemblePercentile"},
        {"$ref": "#/$defs/EnsembleProbabilityLower"},
        {"$ref": "#/$defs/EnsembleProbabilityUpper"},
        {"$ref": "#/$defs/EnsembleProbabilityBetween"}
      ]
    },
    "EnsembleDerived": {
      "description": "A derived forecast of every member, templates 4.2 / 4.12: code table 4.7 (0 the unweighted mean, 4 the spread).",
      "type": "object",
      "required": ["derivedForecast"],
      "properties": {"derivedForecast": {"type": "integer", "minimum": 0, "maximum": 255}},
      "additionalProperties": false
    },
    "EnsemblePercentile": {
      "description": "A percentile of the members' distribution, templates 4.6 / 4.10.",
      "type": "object",
      "required": ["percentileValue"],
      "properties": {"percentileValue": {"type": "integer", "minimum": 0, "maximum": 100}},
      "additionalProperties": false
    },
    "EnsembleProbabilityLower": {
      "description": "A probability against the lower limit, templates 4.5 / 4.9: below (0), above (3) or equal to (5) it.",
      "type": "object",
      "required": ["probabilityType", "scaleFactorOfLowerLimit", "scaledValueOfLowerLimit", "scaleFactorOfUpperLimit", "scaledValueOfUpperLimit"],
      "properties": {
        "probabilityType": {"enum": [0, 3, 5]},
        "scaleFactorOfLowerLimit": {"$ref": "#/$defs/LimitScaleFactor"},
        "scaledValueOfLowerLimit": {"$ref": "#/$defs/LimitScaledValue"},
        "scaleFactorOfUpperLimit": {"type": "null"},
        "scaledValueOfUpperLimit": {"type": "null"}
      },
      "additionalProperties": false
    },
    "EnsembleProbabilityUpper": {
      "description": "A probability against the upper limit, templates 4.5 / 4.9: above (1) or below (4) it.",
      "type": "object",
      "required": ["probabilityType", "scaleFactorOfLowerLimit", "scaledValueOfLowerLimit", "scaleFactorOfUpperLimit", "scaledValueOfUpperLimit"],
      "properties": {
        "probabilityType": {"enum": [1, 4]},
        "scaleFactorOfLowerLimit": {"type": "null"},
        "scaledValueOfLowerLimit": {"type": "null"},
        "scaleFactorOfUpperLimit": {"$ref": "#/$defs/LimitScaleFactor"},
        "scaledValueOfUpperLimit": {"$ref": "#/$defs/LimitScaledValue"}
      },
      "additionalProperties": false
    },
    "EnsembleProbabilityBetween": {
      "description": "A probability between the two limits (type 2), templates 4.5 / 4.9.",
      "type": "object",
      "required": ["probabilityType", "scaleFactorOfLowerLimit", "scaledValueOfLowerLimit", "scaleFactorOfUpperLimit", "scaledValueOfUpperLimit"],
      "properties": {
        "probabilityType": {"const": 2},
        "scaleFactorOfLowerLimit": {"$ref": "#/$defs/LimitScaleFactor"},
        "scaledValueOfLowerLimit": {"$ref": "#/$defs/LimitScaledValue"},
        "scaleFactorOfUpperLimit": {"$ref": "#/$defs/LimitScaleFactor"},
        "scaledValueOfUpperLimit": {"$ref": "#/$defs/LimitScaledValue"}
      },
      "additionalProperties": false
    },
    "LimitScaleFactor": {"description": "A probability limit's GRIB2 scale factor.", "type": "integer", "minimum": -127, "maximum": 127},
    "LimitScaledValue": {"description": "A probability limit's GRIB2 scaled value.", "type": "integer", "minimum": 0, "maximum": 4294967294},
    "ScaleFactor": {"description": "A GRIB2 scale factor, or null where the value is missing.", "type": ["integer", "null"], "minimum": -127, "maximum": 127},
    "ScaledValue": {"description": "A GRIB2 scaled value, or null where the value is missing.", "type": ["integer", "null"], "minimum": 0, "maximum": 4294967294},
    "Quantization": {
      "description": "One of the codebooks of docs/bundle.md, \"Quantization Codebooks\".",
      "anyOf": [{"$ref": "#/$defs/LinearQuantization"}, {"$ref": "#/$defs/LogQuantization"}]
    },
    "LinearQuantization": {
      "description": "value = offset + code x scale for codes 0..maximumCode; nodataCode marks missing data.",
      "type": "object",
      "required": ["type", "offset", "scale", "minimumCode", "maximumCode", "nodataCode"],
      "properties": {
        "type": {"type": "string", "const": "linear"},
        "offset": {"type": "number"},
        "scale": {"type": "number", "exclusiveMinimum": 0},
        "minimumCode": {"$ref": "#/$defs/Code"},
        "maximumCode": {"$ref": "#/$defs/Code"},
        "nodataCode": {"$ref": "#/$defs/Code"}
      }
    },
    "LogQuantization": {
      "description": "The logarithmic codebook: zeroCode below trace, minimumCode..maximumCode on the log1p grid up to maximum, overflowCode above it, nodataCode missing.",
      "type": "object",
      "required": ["type", "trace", "scale", "maximum", "minimumCode", "maximumCode", "zeroCode", "overflowCode", "nodataCode"],
      "properties": {
        "type": {"type": "string", "const": "log1p"},
        "trace": {"type": "number", "exclusiveMinimum": 0},
        "scale": {"type": "number", "exclusiveMinimum": 0},
        "maximum": {"type": "number", "exclusiveMinimum": 0},
        "minimumCode": {"$ref": "#/$defs/Code"},
        "maximumCode": {"$ref": "#/$defs/Code"},
        "zeroCode": {"$ref": "#/$defs/Code"},
        "overflowCode": {"$ref": "#/$defs/Code"},
        "nodataCode": {"$ref": "#/$defs/Code"}
      }
    },
    "Code": {"description": "A code. At most 65535, which a series store's uint16 array holds (docs/zarr-profile.md, \"Series store\"); the readers' own check is that it fits where it sits: at most 255 in a container and in every uint8 array.", "type": "integer", "minimum": 0, "maximum": 65535}
  }
}
