This module is a cython binding to wrap the C SpecFile library, to access SpecFile data within a python program.
Documentation for the original C library SpecFile can be found on the ESRF website: The manual for the SpecFile Library
Start by importing SpecFile and instantiate it:
from silx.io.specfile import SpecFile
sf = SpecFile("test.dat")
A SpecFile instance can be accessed like a dictionary to obtain a Scan instance.
If the key is a string representing two values separated by a dot (e.g. "1.2"), they will be treated as the scan number (#S header line) and the scan order:
# get second occurrence of scan "#S 1"
myscan = sf["1.2"]
# access scan data as a numpy array
nlines, ncolumns = myscan.data.shape
If the key is an integer, it will be treated as a 0-based index:
first_scan = sf[0]
second_scan = sf[1]
It is also possible to browse through all scans using SpecFile as an iterator:
for scan in sf:
print(scan.scan_header_dict['S'])
MCA spectra can be selectively loaded using an instance of MCA provided by Scan:
# Only one MCA spectrum is loaded in memory
second_mca = first_scan.mca[1]
# Iterating trough all MCA spectra in a scan:
for mca_data in first_scan.mca:
print(sum(mca_data))
SpecFile(filename)
Parameters: | filename – Path of the SpecFile to read |
---|
This class wraps the main data and header access functions of the C SpecFile library.
Return the number of scans in the SpecFile
Return the next Scan in a SpecFile each time this method is called.
This usually happens when the python built-in function next() is called with a SpecFile instance as a parameter, or when a SpecFile instance is used as an iterator (e.g. in a for loop).
Return a Scan object.
This special method is called when a SpecFile instance is accessed as a dictionary (e.g. sf[key]).
Parameters: | key (int or str) – 0-based scan index or "n.m" key, where n is the scan number defined on the #S header line and m is the order |
---|---|
Returns: | Scan defined by its 0-based index or its "n.m" key |
Return type: | Scan |
Return number of columns in a scan from the #N header line (without #N and scan number)
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | Number of columns in scan from #N line |
Return type: | int |
Return #S line (without #S and scan number)
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | S line |
Return type: | str |
Returns data for the specified scan index.
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | Complete scan data as a 2D array of doubles |
Return type: | numpy.ndarray |
Returns data column for the specified scan index and column label.
Parameters: |
|
---|---|
Returns: | Data column as a 1D array of doubles |
Return type: | numpy.ndarray |
Return date from #D line
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | Date from #D line |
Return type: | str |
Return list of file header lines.
A file header contains all lines between a #F header line and a #S header line (start of scan). We need to specify a scan number because there can be more than one file header in a given file. A file header applies to all subsequent scans, until a new file header is defined.
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | List of raw file header lines |
Return type: | list of str |
Return one MCA spectrum
Parameters: |
|
---|---|
Returns: | MCA spectrum |
Return type: | 1D numpy array |
Returns scan index from scan number and order.
Parameters: |
|
---|---|
Returns: | Unique scan index |
Return type: | int |
Scan indices are increasing from 0 to len(self)-1 in the order in which they appear in the file. Scan numbers are defined by users and are not necessarily unique. The scan order for a given scan number increments each time the scan number appers in a given file.
Returns list of scan keys (eg ['1.1', '2.1',...]).
Returns: | list of scan keys |
---|---|
Return type: | list of strings |
Return all labels from #L line
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | All labels from #L line |
Return type: | list of strings |
Returns list (1D numpy array) of scan numbers in SpecFile.
Returns: | list of scan numbers (from `` #S`` lines) in the same order as in the original SpecFile (e.g [1, 1, 2, 3, …]). |
---|---|
Return type: | numpy array |
Return MCA calibration in the form \(a + b x + c x²\)
Raise a KeyError if there is no @CALIB line in the scan header.
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | MCA calibration as a list of 3 values \((a, b, c)\) |
Return type: | list of floats |
Return all motor names from #O lines
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1.If not specified, defaults to 0 (meaning the function returns motors names associated with the first scan). This parameter makes a difference only if there are more than on file header in the file, in which case the file header applies to all following scans until a new file header appears. |
---|---|
Returns: | All motor names |
Return type: | list of strings |
Return motor position
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | Specified motor position |
Return type: | double |
Return all motor positions
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | All motor positions |
Return type: | list of double |
Returns scan number from scan index.
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | User defined scan number. |
Return type: | int |
Return number of mca spectra in a scan.
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | Number of mca spectra. |
Return type: | int |
Returns scan order from scan index.
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | Scan order (sequential number incrementing each time a non-unique occurrence of a scan number is encountered). |
Return type: | int |
Return list of scan header lines.
Parameters: | scan_index (int) – Unique scan index between 0 and len(self)-1. |
---|---|
Returns: | List of raw scan header lines |
Return type: | list of str |
Parameters: |
|
---|
Interface to access a SpecFile scan
A scan is a block of descriptive header lines followed by a 2D data array.
Following three ways of accessing a scan are equivalent:
sf = SpecFile("/path/to/specfile.dat")
# Explicit class instantiation
scan2 = Scan(sf, scan_index=2)
# 0-based index on a SpecFile object
scan2 = sf[2]
# Using a "n.m" key (scan number starting with 1, scan order)
scan2 = sf["3.1"]
Scan data as a 2D numpy.ndarray with the usual attributes (e.g. data.shape).
The first index is the detector, the second index is the sample index.
Returns a data column
Parameters: | label (str) – Label of data column to retrieve, as defined on the #L line of the scan header. |
---|---|
Returns: | Line data as a 1D array of doubles |
Return type: | numpy.ndarray |
Returns data for a given line of this scan.
Note
A data line returned by this method, corresponds to a data line in the original specfile (a series of data points, one per detector). In the data array, this line index corresponds to the index in the second dimension (~ column) of the array.
Parameters: | line_index (int) – Index of data line to retrieve (starting with 0) |
---|---|
Returns: | Line data as a 1D array of doubles |
Return type: | numpy.ndarray |
List of raw file header lines (as a list of strings).
Dictionary of file header strings, keys without the leading # (e.g. file_header_dict["F"]).
List of raw header lines (as a list of strings).
This includes the file header, the scan header and possibly a MCA header.
Unique scan index 0 - len(specfile)-1
This attribute is implemented as a read-only property as changing its value may cause nasty side-effects (such as loading data from a different scan without updating the header accordingly.
List of data column headers from #L scan header
MCA data in this scan.
Each multichannel analysis is a 1D numpy array. Metadata about MCA data is to be found in mca_header.
Return type: | MCA |
---|
Dictionary of MCA header strings, keys without the leading #@ (e.g. mca_header_dict["CALIB"]).
List of motor names from the #O file header line.
Returns the position for a given motor
Parameters: | name (str) – Name of motor, as defined on the #O line of the file header. |
---|---|
Returns: | Motor position |
Return type: | float |
List of motor positions as floats from the #P scan header line.
First value on #S line (as int)
Order can be > 1 if the same number is repeated in a specfile
Check whether a scan header line exists.
This should be used before attempting to retrieve header information using a C function that may crash with a segmentation fault if the header isn’t defined in the SpecFile.
Parameters: | record (str) – single upper case letter corresponding to the header you want to test (e.g. L for labels) |
---|---|
Returns: | True or False |
Return type: | boolean |
List of raw scan header lines (as a list of strings).
Dictionary of scan header strings, keys without the leading``#`` (e.g. scan_header_dict["S"]). Note: this does not include MCA header lines starting with #@.
Parameters: | scan (Scan) – Parent Scan instance |
---|---|
Variables: |
|
This class provides access to Multi-Channel Analysis data. A MCA instance can be indexed to access 1D numpy arrays representing single MCA spectra.
To create a MCA instance, you must provide a parent Scan instance, which in turn will provide a reference to the original SpecFile instance:
sf = SpecFile("/path/to/specfile.dat")
scan2 = Scan(sf, scan_index=2)
mcas_in_scan2 = MCA(scan2)
for i in len(mcas_in_scan2):
mca_data = mcas_in_scan2[i]
... # do some something with mca_data (1D numpy array)
A more pythonic way to do the same work, without having to explicitly instantiate scan and mcas_in_scan, would be:
sf = SpecFile("specfilename.dat")
# scan2 from previous example can be referred to as sf[2]
# mcas_in_scan2 from previous example can be referred to as scan2.mca
for mca_data in sf[2].mca:
... # do some something with mca_data (1D numpy array)
Returns: | Number of mca in Scan |
---|---|
Return type: | int |
Return the next MCA data line each time this method is called.
Returns: | Single MCA |
---|---|
Return type: | 1D numpy array |
Return a single MCA data line
Parameters: | key (int) – 0-based index of MCA within Scan |
---|---|
Returns: | Single MCA |
Return type: | 1D numpy array |
Base exception inherited by all exceptions raised when a C function from the legacy SpecFile library returns an error code.
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Bases: specfile.SfError
Custom exception raised when SfNoMca() returns -1