Skip to main content

qualia_core_db/q42/field_spectrum/
mod.rs

1//! Candidate physical spectral axes over the full representable positive spectrum.
2//! No sensor/manufacturer, frequency-band or colour-display dependency. Schema132
3//! references field data; this adds no new .10d/.emf magic or duplicated cell store.
4//! Static/aperiodic fields have no carrier axis and must not substitute zero Hz.
5use super::field_envelope::{Block, Field};
6use super::field_semantics::{field, validate_fields, FieldRule};
7mod axis;
8mod conversion;
9pub use axis::{
10    admit_axis, schema_rules, AxisView, Coordinates, NoResponse, ResponseValidator,
11    SigmaInterpretation,
12};
13pub use conversion::*;
14pub const SCHEMA_ID: u16 = 132;
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum Error {
17    Schema,
18    Shared(super::field_semantics::Error),
19    Limit,
20    Overflow,
21    Count,
22    Axis,
23    Semantics,
24    QuantityUnit,
25    Spacing,
26    Coordinate,
27    Index,
28    Sigma,
29    UnsupportedResponse,
30    NonPositive,
31    NonFinite,
32    Unrepresentable,
33    NotDensity,
34    BinAverageNeedsIntegral,
35}
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum AxisKind {
38    FrequencyHz,
39    VacuumWavelengthMetres,
40}
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub enum SampleSemantics {
43    PointDensity,
44    BinIntegral,
45    BinAverage,
46}
47/// Exact candidate quantity spellings; aliases are not silently admitted.
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49pub enum Quantity {
50    SpectralRadiance,
51    SpectralIrradiance,
52    BinIntegratedRadiance,
53    BinIntegratedIrradiance,
54    DimensionlessReflectance,
55}
56impl Quantity {
57    pub fn name(self) -> &'static str {
58        match self {
59            Self::SpectralRadiance => "spectral_radiance",
60            Self::SpectralIrradiance => "spectral_irradiance",
61            Self::BinIntegratedRadiance => "bin_integrated_radiance",
62            Self::BinIntegratedIrradiance => "bin_integrated_irradiance",
63            Self::DimensionlessReflectance => "dimensionless_reflectance",
64        }
65    }
66    pub fn unit(self, axis: AxisKind) -> &'static str {
67        match (self, axis) {
68            (Self::SpectralRadiance, AxisKind::FrequencyHz) => "W sr^-1 m^-2 Hz^-1",
69            (Self::SpectralRadiance, AxisKind::VacuumWavelengthMetres) => "W sr^-1 m^-2 m^-1",
70            (Self::SpectralIrradiance, AxisKind::FrequencyHz) => "W m^-2 Hz^-1",
71            (Self::SpectralIrradiance, AxisKind::VacuumWavelengthMetres) => "W m^-2 m^-1",
72            (Self::BinIntegratedRadiance, _) => "W sr^-1 m^-2",
73            (Self::BinIntegratedIrradiance, _) => "W m^-2",
74            (Self::DimensionlessReflectance, _) => "1",
75        }
76    }
77    pub fn is_density(self) -> bool {
78        matches!(self, Self::SpectralRadiance | Self::SpectralIrradiance)
79    }
80}
81#[derive(Clone, Copy, Debug)]
82pub struct Limits {
83    pub samples: u64,
84    pub work: u64,
85}
86impl Default for Limits {
87    fn default() -> Self {
88        Self {
89            samples: 65536,
90            work: 4_000_000,
91        }
92    }
93}
94/// Response providers must charge their own variable work before performing it.
95pub struct WorkBudget {
96    remaining: u64,
97}
98impl WorkBudget {
99    pub fn charge(&mut self, amount: u64) -> Result<(), Error> {
100        self.remaining = self.remaining.checked_sub(amount).ok_or(Error::Limit)?;
101        Ok(())
102    }
103    pub fn remaining(&self) -> u64 {
104        self.remaining
105    }
106}
107fn required<'a>(block: Block<'a>, key: u16) -> Result<Field<'a>, Error> {
108    field(block, key).ok_or(Error::Shared(super::field_semantics::Error::MissingField))
109}
110fn string(field: Field<'_>) -> Result<&str, Error> {
111    std::str::from_utf8(field.value()).map_err(|_| Error::Coordinate)
112}
113fn f64_at(bytes: &[u8], at: usize) -> f64 {
114    f64::from_le_bytes(bytes[at..at + 8].try_into().unwrap())
115}
116#[cfg(test)]
117mod tests;