Spectral discretization#
In this guide, we give an overview of how Eradiate handles the spectral dimension during simulations.
Note
This section assumes familiarity with Experiment objects. For an introduction, see First steps with Eradiate and Molecular atmosphere basics.
Upon calling the eradiate.run() function, Eradiate runs successive simulations as part of a so-called spectral loop. Each iteration of that loop covers a subset of the spectral space, and the chunking of that space depends on three factors:
the current active mode, which determines the general spectral discretization method and the underlying Python class that represents it;
scene and user basic constraints, which define a background spectral grid;
the radiometric instrument or measure that is being simulated, which will apply filters to the background spectral grid to optimize it.
Data types#
Spectral grid types and their mode dependence#
In monochromatic modes, the spectral grid is discretized into a list of wavelengths. The corresponding type is MonoSpectralGrid and is simply initialized with the list of wavelengths:[1] [2]
[2]:
MonoSpectralGrid(wavelengths=np.arange(500, 601, 1))
[2]:
In CKD modes, the spectral grid is discretized into a set of spectral bins defined by their lower and upper bounds:[3] [4]
[3]:
CKDSpectralGrid(np.arange(495, 596, 10), np.arange(505, 606, 10))
[3]:
Both grid types can be created with a convenience SpectralGrid.arange() attached to their common SpectralGrid type. In that case, the correct type is dynamically selected depending on the currently active mode:
[4]:
eradiate.set_mode("mono")
SpectralGrid.arange(500, 601, 1) # Returns a MonoSpectralGrid
[4]:
[5]:
eradiate.set_mode("ckd")
SpectralGrid.arange(500, 601, 1) # Returns a CKDSpectralGrid
[5]:
Spectral responses functions (SRF)#
Measures are assigned a spectral response function (SRF) described by the SpectralResponseFunction type and its child classes. The three types are:
BandSRF: a piecewise-linear SRF covering an arbitrary interval that can be initialized from a NetCDF dataset holding SRF data characterizing a radiometric instrument
[6]:
BandSRF.from_id("sentinel_2a-msi-3")
[6]:
UniformSRF: a constant SRF covering an arbitrary interval
[7]:
UniformSRF(wmin=500, wmax=600, value=1.0)
[7]:
DeltaSRF: a combination of one or several Dirac delta distributions
[8]:
DeltaSRF(wavelengths=[440, 550, 660])
[8]:
The background grid#
Each Experiment instance holds a background grid that serves as the starting point to the spectral grid assembly. When the experiment defines an atmosphere, this background grid is imposed by the available atmospheric molecular absorption database (if it exists). Otherwise, it can be specified using the optional background_spectral_grid parameter.
As an example, this is the background spectral grid for an atmosphere-free experiment in monochromatic mode:
[9]:
eradiate.set_mode("mono")
exp = eradiate.experiments.AtmosphereExperiment(atmosphere=None)
display(exp.background_spectral_grid)
pprint(exp.background_spectral_grid)
MonoSpectralGrid(_wavelengths=[250. 251. 252. 253. ... 3122. 3123. 3124. 3125.] nm)
The background spectral grid for the default atmosphere setup in monochromatic mode, inherited from the komodo dataset has variable spectral density: [5]
[10]:
eradiate.set_mode("mono")
exp = eradiate.experiments.AtmosphereExperiment(atmosphere={"type": "molecular"})
display(exp.background_spectral_grid)
pprint(exp.background_spectral_grid)
MonoSpectralGrid( │ _wavelengths=[250.0021 250.00835 250.0146 250.02086 ... 3122.073 3123.0483 3124.0237 3125. ] nm )
In CKD mode, the default absorption database has constant bin size in the wavenumber space, which results in bin increasing with wavelength:
[11]:
eradiate.set_mode("ckd")
exp = eradiate.experiments.AtmosphereExperiment(atmosphere={"type": "molecular"})
display(exp.background_spectral_grid)
pprint(exp.background_spectral_grid)
CKDSpectralGrid( │ wmins=[250. 250.62656642 251.25628141 251.88916877 ... 2777.77777778 2857.14285714 2941.17647059 3030.3030303 ] nm, │ wmaxs=[250.62656642 251.25628141 251.88916877 252.52525253 ... 2857.14285714 2941.17647059 3030.3030303 3125. ] nm, │ wcenters=[250.3129 250.94102 251.57233 252.2068 ... 2816.9016 2898.5505 2985.0745 3076.923 ] nm )
Setting an SRF for your measure (CKD mode)#
When we specify a measure and its SRF, it is used to select the relevant wavelengths or spectral bins in the background spectral grid. To our previous experiment definition, we add a measure specification that uses the Sentinel-2A band 11 spectral response (this is done by simply specifying the SRF dataset ID, chosen from the SRF database):
[12]:
eradiate.set_mode("ckd")
exp = eradiate.experiments.AtmosphereExperiment(
atmosphere={"type": "molecular"},
measures={
"type": "mdistant",
"srf": "sentinel_2a-msi-11",
},
)
We can plot the background spectral grid and our measure SRF:
[13]:
fig, ax = plt.subplots(1, 1, figsize=(6, 2))
exp.background_spectral_grid.plot(ax, alpha=0.25)
exp.measures[0].srf.plot(ax, alpha=0)
ax.set_xlabel("Wavelength [nm]")
plt.show()
plt.close()
Now, if we inspect the spectral grid associated with the defined measure, we can see that the bins out of the spectral range covered by the instrument are removed:
[14]:
fig, ax = plt.subplots(1, 1, figsize=(6, 2))
exp.spectral_grids[0].plot(ax, alpha=0.25)
exp.measures[0].srf.plot(ax, alpha=0)
ax.set_xlabel("Wavelength [nm]")
plt.show()
plt.close()
We can use the other SRF types.
A UniformSRF will keep the bins that intersect the interval it covers:
[15]:
eradiate.set_mode("ckd")
exp = eradiate.experiments.AtmosphereExperiment(
atmosphere={"type": "molecular"},
measures={"type": "mdistant", "srf": {"type": "uniform", "wmin": 500, "wmax": 600}},
)
fig, ax = plt.subplots(1, 1, figsize=(6, 2))
exp.spectral_grids[0].plot(ax, alpha=0.25)
exp.measures[0].srf.plot(ax, alpha=0)
ax.set_xlabel("Wavelength [nm]")
plt.show()
plt.close()
A:class:.DeltaSRF will keep the bins that contain one of the wavelengths of its delta functions:
[16]:
eradiate.set_mode("ckd")
exp = eradiate.experiments.AtmosphereExperiment(
atmosphere={"type": "molecular"},
measures={
"type": "mdistant",
"srf": {"type": "delta", "wavelengths": [440, 550, 660]},
},
)
fig, ax = plt.subplots(1, 1, figsize=(6, 2))
exp.spectral_grids[0].plot(ax, alpha=0.25)
exp.measures[0].srf.plot(ax)
ax.set_xlabel("Wavelength [nm]")
plt.show()
plt.close()
Setting an SRF for your measure (monochromatic mode)#
Similarly, in monochromatic modes, a BandSRF will retain the wavelengths of the background spectral grid that are included in its support:
[17]:
eradiate.set_mode("mono")
exp = eradiate.experiments.AtmosphereExperiment(
measures={
"type": "mdistant",
"srf": "sentinel_2a-msi-4",
}
)
fig, ax = plt.subplots(1, 1, figsize=(6, 2))
exp.spectral_grids[0].plot(ax, alpha=0.5)
exp.measures[0].srf.plot(ax, alpha=0)
ax.set_xlabel("Wavelength [nm]")
plt.show()
plt.close()
A UniformSRF will retain also the wavelengths of the background spectral grid that are included in its support:
[18]:
eradiate.set_mode("mono")
exp = eradiate.experiments.AtmosphereExperiment(
measures={"type": "mdistant", "srf": {"type": "uniform", "wmin": 500, "wmax": 550}}
)
fig, ax = plt.subplots(1, 1, figsize=(6, 2))
exp.spectral_grids[0].plot(ax, alpha=0.25)
exp.measures[0].srf.plot(ax, alpha=0)
ax.set_xlabel("Wavelength [nm]")
plt.show()
plt.close()
On the other hand, a DeltaSRF will always override the background grid:
[19]:
eradiate.set_mode("mono")
exp = eradiate.experiments.AtmosphereExperiment(
measures={
"type": "mdistant",
"srf": {"type": "delta", "wavelengths": [440, 550, 660]},
}
)
fig, ax = plt.subplots(1, 1, figsize=(6, 2))
exp.spectral_grids[0].plot(ax, alpha=0.25)
exp.measures[0].srf.plot(ax)
ax.set_xlabel("Wavelength [nm]")
plt.show()
plt.close()
Sample allocation#
The spectral grid selected for a measure drives the spectral loop: Eradiate runs one Monte Carlo simulation per loop iteration, then the post-processing pipeline combines the per-iteration results into output values.
In monochromatic modes, the loop visits each wavelength of the grid, and each wavelength is an output value.
In CKD modes, each bin is associated a quadrature rule (stored in
Experiment.ckd_quads) and the loop visits each of its nodes, called g-points. The pipeline then computes the per-bin value as the weighted sum of the per-g-point values, using the quadrature weights. If the measure has aBandSRF, the pipeline further aggregates the per-bin values into a single band value, using the SRF weight each bin carries (seespectral_response_weights()).
The sample count Measure.spp is distributed across loop iterations
according to an allocation policy (see SampleAllocation). With the
default "weighted" policy, spp is the number of samples
per pixel spent on each output value:
in monochromatic modes, every wavelength gets
sppsamples, whatever the SRF;in CKD modes, every bin gets
sppsamples, split across its g-points proportionally to the quadrature weights;in CKD modes with a
BandSRF, the band value is the output value:sppis split across bins proportionally to their SRF weights, then across g-points as above.
Every iteration gets at least Measure.spp_min samples (default:
2), so the split is only approximately proportional: bins in the tails of
the SRF get spp_min samples per g-point, even if their SRF
weight would give them less. Raising spp_min raises the
minimum valid spp accordingly: with a BandSRF,
spp_min times the total number of g-points. Setting it to
1 allows lower sample counts, but the variance of an iteration with a
single sample is undefined (NaN), which makes the per-bin and SRF-weighted
variances including it NaN as well; a warning is emitted when this happens.
The minimum does not apply in monochromatic modes or with the "uniform"
policy (see below), where every iteration gets spp
samples. Proportional allocation minimizes the variance of the aggregated
value if all iterations have the same per-sample variance.
Warning
With a BandSRF in CKD modes, only the band value (e.g.
radiance_srf) gets spp samples. Per-bin results
(e.g. radiance) of low-weight bins are computed from few samples and
are correspondingly noisy. Use the "uniform" policy (see below) if
per-bin results matter.
The srf_spp_distribution() function computes the sample count of each
loop iteration. The following example splits 10,000 samples across the bins
selected by the Sentinel-2A/MSI band 3 SRF, then sums the counts of each bin’s
g-points:
[20]:
from eradiate.spectral import srf_spp_distribution
eradiate.set_mode("ckd")
exp = eradiate.experiments.AtmosphereExperiment(
atmosphere={"type": "molecular"},
measures={"type": "mdistant", "srf": "sentinel_2a-msi-3", "spp": 10000},
)
measure = exp.measures[0]
spectral_grid = exp.spectral_grids[0]
quads = exp.ckd_quads[0]
# One count per g-point, bins visited in increasing wavelength order
spp = srf_spp_distribution(measure.spp, measure.srf, spectral_grid, ckd_quads=quads)
# Sum the counts of each bin's g-points
ng = [len(quad.nodes) for quad in quads]
bin_spp = np.add.reduceat(spp, np.cumsum([0, *ng[:-1]]))
print(f"Total: {bin_spp.sum()}")
fig, ax = plt.subplots(1, 1, figsize=(6, 2))
wmins = spectral_grid.wmins.m_as("nm")
wmaxs = spectral_grid.wmaxs.m_as("nm")
ax.bar(wmins, bin_spp, width=wmaxs - wmins, align="edge", alpha=0.5, edgecolor="C0")
ax.set_xlabel("Wavelength [nm]")
ax.set_ylabel("Samples per bin")
ax2 = ax.twinx()
measure.srf.plot(ax2, alpha=0)
ax2.set_ylabel("SRF [—]")
plt.show()
Total: 10000
Within each bin, the count is split across g-points proportionally to the quadrature weights. With 16 g-points, the bins in the SRF tails get exactly two samples per g-point:
[21]:
print(f"First bin: {spp[: ng[0]]}")
print(f"Fifth bin: {spp[sum(ng[:4]) : sum(ng[:5])]}")
First bin: [2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
Fifth bin: [12 27 42 55 66 75 81 84 84 81 75 66 55 42 27 12]
The "uniform" policy applies spp in full to every loop
iteration, g-points included, which was the behaviour of Eradiate v1.2 and
earlier. In CKD modes, it multiplies the total sample count by the number of
g-points per bin, and in addition by the number of bins for a
BandSRF. The policy is selected per measure with the
Measure.spp_allocation parameter:
measure = {"type": "mdistant", "spp": 1000, "spp_allocation": "uniform"}
If spp_allocation is unset, the sample_allocation
setting applies (see Configuration), e.g. with an
environment variable:
export ERADIATE_SAMPLE_ALLOCATION=uniform
Results record the policy in their sample_allocation attribute. The
sample counts actually rendered for each loop iteration are stored in
Measure.mi_results: samplers other than "independent" round each
count up (e.g. to a square number for "stratified", see
kernel.effective_spp()).