Spectral Band Aggregation

Plain-array wavenumber band integration, wavelength–wavenumber grid conversion, and composable leaf spectrum evaluation across UV, shortwave, and thermal bands. See the Spectral Band Aggregation guide for mathematical conventions, fallback rules, and pigment-share aggregation.

Band Averaging & Sub-Band Partition Shares

CanopyOptics.band_average — Function
band_average(values, edges, bands, weight = FlatWeight(); parent_band = nothing)

Compute the flux-weighted mean of fine-grid spectral bin means values across each band in bands by exact wavenumber overlap of the fine bins with every band:

\[\bar{v}_b = \frac{\int_{\nu_{\mathrm{lo},b}}^{\nu_{\mathrm{hi},b}} v(\nu)\,w(\nu)\,d\nu}{\int_{\nu_{\mathrm{lo},b}}^{\nu_{\mathrm{hi},b}} w(\nu)\,d\nu}.\]

  • values: N piecewise-constant bin means on [edges[i], edges[i + 1]].
  • edges: N + 1 finite, nonnegative, strictly ascending wavenumber bin edges in cm⁻¹.
  • bands: (2, nbnd) matrix of band limits [ν_lo; ν_hi] in cm⁻¹ with 0 <= ν_lo < ν_hi.
  • weight: AbstractSpectralWeight (default FlatWeight).
  • parent_band: optional parent interval ((2, 1) matrix or 2-element vector/tuple [ν_lo, ν_hi]). When bands holds sub-bands of parent_band, passing parent_band resolves the effective weight (including TabulatedWeight coverage/zero-flux fallback to FlatWeight) once for parent_band and uses it across all sub-bands so sum(subband_weights(sub_bands, parent_band, weight) .* band_average(values, edges, sub_bands, weight; parent_band = parent_band)) always recombines to only(band_average(values, edges, parent_band, weight)).

Throws an ArgumentError naming the band if any band is invalid or not fully covered by [first(edges), last(edges)] (never returning silent zeros). If weight is a TabulatedWeight whose grid does not cover a parent band (or integrates to zero over that band), that band falls back explicitly to FlatWeight as a placeholder optical mean.

See band_average! for the in-place variant (zero heap allocation when arrays and built-in weight share a matching Float32 or Float64 element type), subband_weights for normalized sub-band partition shares, and CanopyOptics.band_weight for physical band-integrated weights.

Examples

julia> edges = [1000.0, 2000.0, 3000.0];

julia> bands = [1500.0 1000.0; 2500.0 3000.0];

julia> band_average([0.2, 0.6], edges, bands, FlatWeight())
2-element Vector{Float64}:
 0.4
 0.4
source
CanopyOptics.band_average! — Function
band_average!(dest, values, edges, bands, weight = FlatWeight(); parent_band = nothing) -> dest

Overwrite dest in place with the exact-overlap band means of values over bands weighted by weight, and return dest (zero heap allocation when dest, input arrays, and built-in weight share a matching Float32 or Float64 element type; FlatWeight() matches both).

dest must not alias values, edges, bands, parent_band, or weight's storage (throws ArgumentError if aliased; disjoint views of a shared buffer are permitted). Throws DimensionMismatch when array dimensions disagree and ArgumentError naming the band if any band in bands is invalid, lies outside parent_band, or is not fully covered by edges. See band_average.

source
CanopyOptics.subband_weights — Function
subband_weights(sub_bands, parent_band, weight = FlatWeight())

Compute normalized dimensionless sub-band flux shares w_j (0 <= w_j <= 1, sum(w_j) ≈ 1) for a contiguous partition sub_bands ((2, m) matrix) of a single parent_band ((2, 1) matrix or 2-element vector/tuple [ν_lo, ν_hi]).

subband_weights resolves the effective weight once on parent_band, matching band_average(v, edges, sub_bands, weight; parent_band = parent_band):

  • If weight is a TabulatedWeight that does not cover parent_band, or whose integral across the entire parent_band is zero, all m sub-bands fall back jointly to FlatWeight (Δν_j / Δν_parent); if parent_band is covered with positive integrated weight while an individual sub-band has zero density (e.g. a dark < 280 nm UV sub-interval), that sub-band receives share 0.
  • If weight is a PlanckWeight, all sub-bands share parent_band's relative Wien-tail exponent shift so relative shares never underflow to 0/0 in Float32 or Float64.

Consequently, sum(subband_weights(sub_bands, parent_band, weight) .* band_average(v, edges, sub_bands, weight; parent_band = parent_band)) reproduces only(band_average(v, edges, parent_band, weight)) across all weights, coverage regimes, and precisions. See subband_weights! for the in-place variant (zero heap allocation when arrays and built-in weight share a matching Float32 or Float64 element type).

Examples

julia> sub = [12850.0 14285.7; 14285.7 16000.0];

julia> w = subband_weights(sub, [12850.0; 16000.0;;], FlatWeight());

julia> isapprox(sum(w), 1.0)
true
source
CanopyOptics.subband_weights! — Function
subband_weights!(dest, sub_bands, parent_band, weight = FlatWeight()) -> dest

Overwrite dest in place with the normalized sub-band shares of sub_bands within parent_band under weight, and return dest (zero heap allocation when dest, sub_bands, parent_band, and built-in weight share a matching Float32 or Float64 element type; dest must not alias sub_bands, parent_band, or weight's storage). See subband_weights.

source

Spectral Weights & Wavelength–Wavenumber Conversion

CanopyOptics.FlatWeight — Type
FlatWeight()

Uniform spectral weight $w(\nu) = 1$ per cm⁻¹, weighting fine bins purely by their wavenumber overlap $\Delta\nu$ with each band.

Examples

julia> FlatWeight() isa AbstractSpectralWeight
true
source
CanopyOptics.PlanckWeight — Type
PlanckWeight(T)

Blackbody spectral exitance weight $M_\nu(\nu, T) = \pi B_\nu(\nu, T)$ in $\mathrm{W\,m^{-2}\,(cm^{-1})^{-1}}$ at temperature T in K:

\[M_\nu(\nu, T) = \frac{2\pi \times 10^8 h c^2\,\nu^3}{\exp(100\,h c\,\nu / (k_B T)) - 1},\]

integrated in coordinates relative to each overlap's lower wavenumber edge via composite 8-point Gauss–Legendre quadrature. Temperature T must be finite and satisfy 1 <= T <= 10^8 K.

Examples

julia> PlanckWeight(300.0)
PlanckWeight{Float64}(300.0)
source
CanopyOptics.TabulatedWeight — Type
TabulatedWeight(edges, w)

Piecewise-constant spectral weight density w (per cm⁻¹) defined on ascending wavenumber bin edges edges in cm⁻¹ (length(edges) == length(w) + 1).

After conversion to element type FT, edges must contain at least two finite, nonnegative, strictly ascending points, and w must contain finite nonnegative bin densities. When band_average or subband_weights evaluates a parent band not fully covered by [first(edges), last(edges)] (or a band over which the integrated weight vanishes), evaluation falls back explicitly to FlatWeight over that band as a finite placeholder so 0/0 never occurs.

Examples

julia> tw = TabulatedWeight([10000.0, 15000.0, 20000.0], [1.2, 0.8]);

julia> length(tw.w)
2
source
CanopyOptics.ReferenceSolar — Function
ReferenceSolar(grid, irradiance; grid_unit = :nm) -> TabulatedWeight
ReferenceSolar(path = ...) -> TabulatedWeight{Float64}

Construct a TabulatedWeight (per cm⁻¹) from a clear-sky global (direct + diffuse) surface solar irradiance spectrum.

Grid and |dλ/dν| Jacobian Conventions

  • grid_unit = :nm (default): irradiance is spectral irradiance per nanometer ($\mathrm{W\,m^{-2}\,nm^{-1}}$).
    • Point samples (length(grid) == length(irradiance) >= 2): grid holds strictly ascending wavelengths $\lambda_k$ in nm. Interior wavelength bin edges sit at midpoints $\frac{1}{2}(\lambda_k + \lambda_{k+1})$ and outer edges extend by half the adjacent spacing; each bin density is converted to per cm⁻¹ via the exact bin-averaged Jacobian $|d\lambda / d\nu|$: $\bar{E}_{\nu,i} = E_{\lambda,k}\,\lambda_{\mathrm{lo}}\lambda_{\mathrm{hi}} / 10^7$. Whole-bin integrated flux $\bar{E}_{\nu,i}\,\Delta\nu_i = E_{\lambda,k}\,\Delta\lambda_k$ is conserved; a band boundary cutting through the interior of a bin integrates the piecewise-constant per-cm⁻¹ density $\bar{E}_{\nu,i}$.
    • Bin edges (length(grid) == length(irradiance) + 1): grid holds strictly ascending wavelength bin edges in nm, converted with the same per-bin Jacobian.
  • grid_unit = :cm_inv: irradiance is already per cm⁻¹, on strictly ascending wavenumber point samples (length(grid) == length(irradiance) >= 2) or bin edges (length(grid) == length(irradiance) + 1).

CSV Table Format and Zero-Irradiance UV Convention

Calling ReferenceSolar() with no arguments loads data/reference_solar_global.csv if that file exists, or throws an ArgumentError when no default table is installed; pass ReferenceSolar(path) to load a CSV file from an explicit path. Expected CSV format:

  • UTF-8 comma-separated values (,), optional # comment lines, optional single column-header row with non-numeric text in both columns (e.g. wavelength_nm,irradiance_W_m2_nm), and at least two numeric rows of (wavelength_nm, global_irradiance_W_m2_nm) with strictly ascending positive wavelengths in nm and finite nonnegative global irradiance in $\mathrm{W\,m^{-2}\,nm^{-1}}$.
  • Include an explicit zero-irradiance row below 280 nm (e.g. 195.0,0.0 paired with 280.0,0.0) so UV bands below the stratospheric ozone cutoff are covered by the table: partially illuminated bands (such as 263–345 nm) then weight strictly by their illuminated > 280 nm portion, whereas fully dark bands (such as 200–263 nm) have zero physical solar flux (CanopyOptics.band_weight == 0.0, and share 0.0 in subband_weights inside an illuminated parent band).
  • On a band whose total integrated solar weight is zero (0/0 solar-weighted mean undefined), band_average explicitly returns the FlatWeight wavenumber average as a finite placeholder optical property.

Examples

julia> tw = ReferenceSolar(400.0:100.0:700.0, [1.1, 1.5, 1.4, 1.2]);

julia> tw isa TabulatedWeight{Float64}
true
source
CanopyOptics.wavelength_to_wavenumber — Function
wavelength_to_wavenumber(wavelengths_nm) -> edges
wavelength_to_wavenumber(wavelengths_nm, values; density = false) -> (edges, bin_values)

Convert point samples on a regular ascending wavelength grid wavelengths_nm in nm (such as PROSPECT's 400:1:2500) to ascending wavenumber bin edges edges in cm⁻¹ (length(edges) == length(wavelengths_nm) + 1) and reordered wavenumber bin means bin_values.

Each sample $\lambda_k$ on a regular grid with step $\Delta\lambda$ spans the wavelength bin $[\lambda_{\mathrm{lo}}, \lambda_{\mathrm{hi}}] = [\lambda_k - \Delta\lambda/2,\, \lambda_k + \Delta\lambda/2]$, mapped to $[\nu_{\mathrm{lo}}, \nu_{\mathrm{hi}}] = [10^7/\lambda_{\mathrm{hi}},\, 10^7/\lambda_{\mathrm{lo}}]$ in reverse index order (i = N + 1 - k).

Jacobian Convention (density)

  • density = false (default): values is an intensive dimensionless or per-state quantity (such as reflectance $\rho(\lambda)$ or transmittance $\tau(\lambda)$), so bin_values[i] = values[N + 1 - i]. Apply physical spectral weights on the native fine grid before coarsening, because resampling intensive bin means in wavenumber preserves the flat-in-wavenumber (FlatWeight) integral.
  • density = true: values is a spectral density per nanometer (such as spectral irradiance $E_\lambda$ in $\mathrm{W\,m^{-2}\,nm^{-1}}$). Converting $E_\lambda\,|d\lambda| = E_\nu\,d\nu$ over $[\lambda_{\mathrm{lo}}, \lambda_{\mathrm{hi}}]$ applies the exact bin-averaged Jacobian $|d\lambda / d\nu|$ (10^7 / \nu^2):

\[\bar{E}_{\nu,i} = E_{\lambda,k}\,\frac{\lambda_{\mathrm{lo}}\,\lambda_{\mathrm{hi}}}{10^7},\]

conserving whole-bin flux $\bar{E}_{\nu,i}(\nu_{\mathrm{hi}} - \nu_{\mathrm{lo}}) = E_{\lambda,k}\Delta\lambda$ to floating-point precision. A band boundary that cuts through the interior of a bin integrates the constant per-cm⁻¹ density $\bar{E}_{\nu,i}$ (for example, a single uniform 400–800 nm bin at 1 W m⁻² nm⁻¹ yields 133.33 W m⁻² over 600–800 nm, versus 200 W m⁻² in the piecewise-constant per-nm representation).

Examples

julia> edges, rho_nu = wavelength_to_wavenumber(400.0:500.0:1400.0, [0.05, 0.45, 0.30]);

julia> length(edges) == 4 && rho_nu == [0.30, 0.45, 0.05]
true

See also wavenumber_to_wavelength, ReferenceSolar.

source
CanopyOptics.wavenumber_to_wavelength — Function
wavenumber_to_wavelength(edges) -> wavelengths_nm
wavenumber_to_wavelength(edges, bin_values; density = false) -> (wavelengths_nm, values)

Convert ascending positive wavenumber bin edges edges in cm⁻¹ (and optional bin means bin_values) back to ascending wavelength bin midpoints wavelengths_nm in nm (and reordered wavelength values values), inverting wavelength_to_wavenumber.

When density = true, per-cm⁻¹ spectral densities are converted back to per-nm densities via $E_{\lambda,k} = \bar{E}_{\nu,i}\,10^7 / (\lambda_{\mathrm{lo}}\,\lambda_{\mathrm{hi}})$.

Examples

julia> edges, e_nu = wavelength_to_wavenumber(400.0:200.0:1000.0, [1.0, 1.5, 1.2, 0.9]; density = true);

julia> λ_rt, e_rt = wavenumber_to_wavelength(edges, e_nu; density = true);

julia> λ_rt ≈ [400.0, 600.0, 800.0, 1000.0] && e_rt ≈ [1.0, 1.5, 1.2, 0.9]
true
source

Leaf Spectrum Models & Pigment Shares

CanopyOptics.leaf_spectrum — Function
leaf_spectrum(model, edges) -> (rho, tau)
leaf_spectrum(leaf::LeafProspectProProperties, edges; optics = nothing) -> (rho, tau)

Evaluate bin-mean leaf hemispherical reflectance rho and transmittance tau on ascending wavenumber bin edges edges in cm⁻¹ (length(rho) == length(tau) == length(edges) - 1).

model must fully cover every bin of edges; otherwise an ArgumentError is thrown (never returning silent zeros). Built-in methods support:

  • LeafProspectProProperties: evaluates prospect_budget on the bundled 1 nm PROSPECT-PRO optical table (399.5–2500.5 nm, 3999.20–25031.29 cm⁻¹) and averages onto edges by exact wavenumber overlap (accepting an optional optics keyword to supply a custom regular-nm LeafOpticalProperties table).
  • TabulatedLeaf: resamples measured (rho, tau) spectra onto edges by exact wavenumber overlap.
  • ConstantLeaf: fills (rho, tau) across [ν_min, ν_max].
  • CompositeLeaf: selects the first component model covering each bin.

For PROSPECT pigment absorption shares per bin, see leaf_pigment_shares.

Examples

julia> edges = wavelength_to_wavenumber(400.0:1.0:2500.0);

julia> rho, tau = leaf_spectrum(LeafProspectProProperties(), edges);

julia> length(rho) == 2101 && all(0 .< rho .+ tau .< 1)
true
source
CanopyOptics.TabulatedLeaf — Type
TabulatedLeaf(edges, rho, tau)

Measured or precomputed leaf reflectance rho and transmittance tau bin means on ascending wavenumber bin edges edges in cm⁻¹ (length(edges) == length(rho) + 1 == length(tau) + 1).

All bin values must be finite, nonnegative, and satisfy rho[i] + tau[i] <= 1 (within floating-point rounding). Evaluate on any covered wavenumber grid via leaf_spectrum.

Examples

julia> leaf = TabulatedLeaf([800.0, 2500.0, 4000.0], [0.03, 0.04], [0.0, 0.0]);

julia> leaf_spectrum(leaf, [1000.0, 2000.0, 3500.0])
([0.03, 0.03666666666666667], [0.0, 0.0])
source
CanopyOptics.ConstantLeaf — Type
ConstantLeaf(rho, tau; ν_min = ..., ν_max = ...)

Wavenumber-independent leaf reflectance rho and transmittance tau over [ν_min, ν_max] in cm⁻¹ (defaulting to [0, ∞) when ν_min and ν_max are omitted).

rho and tau must be finite, nonnegative, and satisfy rho + tau <= 1. Pass to leaf_spectrum directly or as a regional/fallback component inside CompositeLeaf.

Examples

julia> cl = ConstantLeaf(0.03, 0.0);

julia> leaf_spectrum(cl, [100.0, 500.0, 1000.0])
([0.03, 0.03], [0.0, 0.0])
source
CanopyOptics.CompositeLeaf — Type
CompositeLeaf(models...)

Ordered composition of leaf spectrum models in which the first model covering each wavenumber bin wins.

Nested CompositeLeaf instances are flattened on construction. During leaf_spectrum(comp, edges), bin i ([edges[i], edges[i + 1]]) is assigned to the earliest model in models whose domain covers the entire bin [edges[i], edges[i + 1]]; if any bin of edges remains uncovered after all models have been checked, an ArgumentError naming the uncovered bin is thrown. When splicing adjacent spectral regions, bound each regional ConstantLeaf to its physical wavenumber range and include the regional domain boundaries in edges so no bin straddles a boundary (for example, extending 400 nm PROSPECT optics onto ν >= ν_pro_hi, ConstantLeaf(0.04, 0.0; ν_min = 2500.0, ν_max = ν_pro_lo) across 2.5–4.0 µm, and ConstantLeaf(0.03, 0.0; ν_min = 0.0, ν_max = 2500.0) beyond 4.0 µm, with [2500.0, ν_pro_lo, ν_pro_hi] inserted into edges).

Examples

julia> comp = CompositeLeaf(
           TabulatedLeaf([10000.0, 20000.0], [0.45], [0.40]),
           ConstantLeaf(0.03, 0.0),
       );

julia> leaf_spectrum(comp, [2000.0, 10000.0, 20000.0, 30000.0])
([0.03, 0.45, 0.03], [0.0, 0.4, 0.0])
source
CanopyOptics.leaf_pigment_shares — Function
leaf_pigment_shares(leaf::LeafProspectProProperties, edges; optics = nothing) -> (; chlorophyll, carotenoids)

Compute per-bin chlorophyll and carotenoid shares of absorbed light on ascending wavenumber bin edges edges in cm⁻¹ for a LeafProspectProProperties leaf, returning (; chlorophyll, carotenoids) with length(edges) - 1 elements each.

edges must be fully covered by the PROSPECT-PRO domain (399.5–2500.5 nm, 3999.20–25031.29 cm⁻¹), or an ArgumentError is thrown. At each native 1 nm PROSPECT bin, directly computed material absorptance $\alpha$ from prospect_budget is partitioned across prospect_constituent_coefficients using HomogeneousAbsorption and partition_absorption, with chlorophyll, carotenoids, anthocyanins, brown material, water, bulk dry matter, protein, and carbon constituents included in the denominator $\sum_j C_j K_j(\lambda)$.

Zero-Absorption and Coarse-Band Aggregation Conventions

  • Native 1 nm bins with zero total absorption coefficient (AbsorptionNoMaterial) return 0.0 for both pigment shares (rather than NaN). Multi-bin intervals in edges with positive integrated absorptance weight native shares by $\alpha(\nu)\,d\nu$; if integrated absorptance underflows to zero while constituent coefficients remain nonzero, resampling falls back to the flat-in-wavenumber mean of $C_i K_i / \sum_j C_j K_j$.
  • For incident-spectrum-weighted coarse bands, obtain native-grid absorptance from prospect_budget(leaf, LeafOpticalProperties()).absorptance (rather than 1 - rho - tau, which can round to zero in weak-absorption bins) via wavelength_to_wavenumber, and divide band_average(α .* shares.chlorophyll, edges, bands, w) by band_average(α, edges, bands, w) (returning 0 when band-mean absorptance is zero). For photon-count shares, replace w in both calls with a photon-flux weight integrating $w(\nu) / (100\,h c\,\nu)$ across each bin.

Examples

julia> edges = wavelength_to_wavenumber(400.0:1.0:700.0);

julia> shares = leaf_pigment_shares(LeafProspectProProperties(), edges);

julia> all(0 .<= shares.chlorophyll .+ shares.carotenoids .<= 1)
true
source

Qualified Integrals & Extension Hooks

The following symbols are not exported by using CanopyOptics; access or extend them via qualified CanopyOptics.* bindings.

CanopyOptics.band_weight — Function
CanopyOptics.band_weight(bands, weight = FlatWeight())

Integrate weight (per cm⁻¹) across each band column [ν_lo, ν_hi] of the (2, nbnd) matrix bands, returning the additive physical band integrals $W_b = \int_{\nu_{\mathrm{lo},b}}^{\nu_{\mathrm{hi},b}} w(\nu)\,d\nu$ (for example $\mathrm{W\,m^{-2}}$ for PlanckWeight or solar irradiance TabulatedWeight, and $\Delta\nu$ in cm⁻¹ for FlatWeight).

For TabulatedWeight, every band in bands must be covered by [first(weight.edges), last(weight.edges)] (throwing ArgumentError otherwise), and zero-density regions integrate to 0 so W(B) = ∑ W(B_k) holds identically across contiguous sub-bands. For dimensionless sub-band flux shares w_j (∑ w_j = 1) that include band_average's FlatWeight fallback and Wien-tail scaling, use subband_weights.

This function is not exported; call CanopyOptics.band_weight (or in-place CanopyOptics.band_weight!, which runs with zero heap allocation when dest, bands, and built-in weight share a matching Float32 or Float64 element type).

Examples

julia> CanopyOptics.band_weight([1000.0 2000.0; 2000.0 3500.0], FlatWeight())
2-element Vector{Float64}:
 1000.0
 1500.0
source
CanopyOptics.band_weight! — Function
CanopyOptics.band_weight!(dest, bands, weight = FlatWeight()) -> dest

Overwrite dest in place with the band-integrated weights of bands under weight, and return dest (zero heap allocation when dest, bands, and built-in weight share a matching Float32 or Float64 element type; dest must not alias bands or weight's storage). See CanopyOptics.band_weight.

source
CanopyOptics.weight_interval_integral — Function
CanopyOptics.weight_interval_integral(weight::AbstractSpectralWeight, a, b)

Integrate spectral weight density weight (per cm⁻¹) over wavenumber interval [a, b] (0 <= a <= b in cm⁻¹), preserving exact additivity across contiguous sub-intervals.

For TabulatedWeight, [a, b] must be covered by [first(weight.edges), last(weight.edges)] (throwing ArgumentError if uncovered) and returns 0 when the tabulated density is zero across [a, b]. For normalized sub-band shares that share band_average's FlatWeight fallback and Wien-tail scaling, see subband_weights.

This qualified extension hook is not exported; custom AbstractSpectralWeight subtypes may specialize CanopyOptics.weight_interval_integral to plug into band_average!, CanopyOptics.band_weight!, and subband_weights!.

source
CanopyOptics.spectral_domain — Function
CanopyOptics.spectral_domain(model) -> (ν_min, ν_max)

Return the contiguous wavenumber coverage interval [ν_min, ν_max] in cm⁻¹ of an interval-domain leaf model (LeafProspectProProperties, TabulatedLeaf, or ConstantLeaf), or nothing when coverage must be probed per bin.

This qualified extension hook is not exported. Custom leaf models with a contiguous wavenumber domain can specialize CanopyOptics.spectral_domain(model) so CompositeLeaf evaluates them in a single vectorized slice call; models with non-contiguous domains that leave spectral_domain(model) === nothing may signal an uncovered bin in leaf_spectrum(model, bin_edges) by throwing CanopyOptics.SpectralCoverageError (or returning nothing).

source
CanopyOptics.SpectralCoverageError — Type
CanopyOptics.SpectralCoverageError(msg)

Exception thrown by custom leaf_spectrum implementations (when CanopyOptics.spectral_domain is nothing) to report that a queried bin lies outside the model's spectral coverage.

CompositeLeaf catches SpectralCoverageError (and nothing returns) during per-bin probing and delegates the uncovered bin to subsequent component models, while allowing ArgumentError and all other exceptions from invalid parameters or coefficients to propagate untouched.

source