6.1.2.1. Data related commands¶
The commands in this section are all related to the data structures within SPEX. They deal with spectral data and responses, simulation of data, etc.
6.1.2.1.1. Data¶
Loading spectral data and responses in SPEX is done with the data
command. Data can be added
by loading a .res and .spo file that contain the response and spectral information for an
instrument. The PYSPEX command is:
where resfile is the response file and spofile the spectrum file. Note that this command needs the full filename (and if necessary the path to the file) including the extension. This is the only difference with the SPEX syntax. Example:
>>> s.data('xifu.res','xifu.spo')
6.1.2.1.1.1. Data delete¶
Deleting a loaded spectrum and response is done in SPEX with the data delete instrument #i
command. In PYSPEX, this command is:
where ins
is the instrument number to be deleted. For example:
>>> s.data_del(1)
6.1.2.1.1.2. Data save¶
Simulated spectra can be saved as a .spo file. In SPEX, this is done using the
data save #i <spofile>
command. In PYSPEX, the command is:
where ins
is the instrument number of the data to be saved, spofile
is the name of
the output .spo file. Again note that you should give the full file name, including the .spo
extension. Optionally, you can tell the command that existing files may be overwritten.
For example:
>>> s.data_save(1,'sim.spo',overwrite=True)
or simply:
>>> s.data_save(1,'sim.spo')
if the file does not exist yet.
6.1.2.1.2. Binning and data selection¶
Unlike some other fitting packages, SPEX rebins the spectra within the program. The easiest binning
method is to just rebin with a certain integer factor. This is the bin
command in SPEX. In
addition, SPEX contains a binning method based on the bin statistics (vbin
) and an optimal
binning algorithm (obin
) which takes the instrument resolution into account.
Binning with factor
Binning with an integer factor is done with the bin command:
- Session.bin(inst, reg, elow, ehigh, factor, unit=None)[source]¶
Bin the spectrum using a fixed factor.
- Parameters:
inst (int) – Instrument number.
reg (int) – Region number.
elow (float) – Start point of energy/channel interval.
ehigh (float) – End point of energy/channel interval.
factor (int) – Binning factor
unit (str) – Unit of the energy/channel range, for example: ‘kev’, ‘ev’, ‘ryd’, ‘j’, ‘hz’, ‘ang’, ‘nm’
In this example, we bin the spectrum with a factor of 5 between 0.3 and 10 keV:
>>> s.bin(1,1,0.3,10.,5,'kev')
Variable binning
Variable binning is done with the vbin command:
- Session.vbin(inst, reg, elow, ehigh, factor, snr, unit=None)[source]¶
Bin the spectrum using a variable bin size, given a minimum bin factor and a minimum signal to noise ratio.
- Parameters:
inst (int) – Instrument number.
reg (int) – Region number.
elow (float) – Start point of energy/channel interval.
ehigh (float) – End point of energy/channel interval.
factor (int) – Minimal binning factor
snr (float) – Minimal signal to noise for a data point after binning.
unit (str) – Unit of the energy/channel range, for example: ‘kev’, ‘ev’, ‘ryd’, ‘j’, ‘hz’, ‘ang’, ‘nm’
In this example, we bin the spectrum with a minimum factor of 3 and a minimum signal-to-noise ratio of 25 between 0.3 and 10 keV:
>>> s.vbin(1,1,0.3,10.,3,25.,'kev')
Optimal binning
The optimal binning algorithm bins based on the instrument resolution and statistics:
In this example, we optimally bin the spectrum between 0.3 and 10 keV:
>>> s.obin(1,1,0.3,10.,'kev')
The response binning algorithm rebins the spectrum and the response matrix:
This command creates new response files that can be read into SPEX again.
6.1.2.1.2.1. Data selection¶
Selecting data is done using the use
and ignore
commands. By default, the bin selection
in the .spo
file is loaded, which should use all the good bins.
Use function
Example:
>>> s.use(1,1,0.3,10.,'kev')
Ignore function
Example:
>>> s.ignore(1,1,0.0,0.3,'kev')
>>> s.ignore(1,1,10.,100.,'kev')
6.1.2.1.2.2. Systematic errors¶
If needed, the error bars on the source and background spectrum can be enlarged by the syserr
command:
- Session.syserr(inst, reg, elow, ehigh, src, bkg, unit=None)[source]¶
Add an additional error to the source and background spectrum.
- Parameters:
inst (int) – Instrument number.
reg (int) – Region number.
elow (float) – Start point of energy/channel interval.
ehigh (float) – End point of energy/channel interval.
src (float) – Error value to be quadratically added to the current error bar of the source spectrum.
bkg (float) – Error value to be quadratically added to the current error bar of the background spectrum.
unit (str) – Unit of the energy/channel range, for example: ‘kev’, ‘ev’, ‘ryd’, ‘j’, ‘hz’, ‘ang’, ‘nm’
Warning
To use this function, you need to know what you are doing statistically. In many cases, this function would produce wrong results.
6.1.2.1.3. Simulate¶
Based on an instrument response and a model spectrum, SPEX can simulate an observed spectrum
using the simulate
commands. In SPEX, the options can be set by giving multiple commands,
but in PYSPEX this is done in a single command with optional parameters:
- Session.simulate(extime, inst=None, reg=None, ssys=None, bsys=None, noise=None, bnoise=None, seed=None)[source]¶
Simulate a spectrum using a loaded response file.
- Parameters:
extime (float) – Exposure time to simulate.
inst (str) – (Optional) Instrument range to simulate (default all)
reg (str) – (Optional) Region range to simulate (default all)
ssys (float) – (Optional) Add a systematic error to the source spectrum (Default 0).
bsys (float) – (Optional) Add a systematic error to the background spectrum (Default 0).
noise (bool) – (Optional) Add Poisson noise to the simulated source spectrum (Default True).
bnoise (bool) – (Optional) Add Poisson noise to the simulated background spectrum (Default False).
seed (int) – (Optional) Set the random seed for the simulation (Default: system clock).
The only required parameter is extime
, which is the desired exposure time (float) for the simulation.
The optional parameters are:
inst
The instrument number range to simulate (string). Just like in SPEX, the range can be provided by giving two numbers with a:
in between, like1:3
.reg
The region number range to simulate (string). This has the same behaviour asinst
.ssys
Adds a systematic error to the source spectrum (float, default is 0.).bsys
Adds a systematic error to the background spectrum (float, default is 0.).noise
Adds Poisson noise to the simulated spectrum (default is True).bnoise
Adds Poisson noise the the background spectrum (default is False).seed
Provide a random seed for the simulation (by default the seed is randomly generated using the system clock).
For example:
>>> s.simulate(1E+5, inst='1:3', bnoise=True, seed=42)