specfilewrapper: Reading SpecFile (old API)

This module provides a backward compatibility layer with the legacy specfile wrapper.

If you are starting a new project, please consider using silx.io.specfile instead of this module.

If you want to use this module for an existing project that used the old wrapper through PyMca, you can try replacing:

from PyMca5.PyMcaIO import specfilewrapper


from silx.io import specfilewrapper

There might still be differences between this module and the old wrapper, due to differences in the underlying implementation. Any of these differences that break your code should be reported on https://github.com/silx-kit/silx/issues

The documentation mentions only the methods and attributes that are different from the ones in silx.io.specfile.SpecFile and silx.io.specfile.Scan. You should refer to the documentation of these base classes for more information.

class Specfile(filename)[source]

Bases: silx.io.specfile.SpecFile

This class is a subclass of silx.io.specfile.SpecFile.

It redefines following methods:

Following methods are added:


Get scan by 0-based index

Parameters:key (int) – 0-based scan index
Return type:scandata

Return a string representation of a list of scan numbers.

The scans numbers are listed in the order in which they appear in the file. Continuous ranges of scan numbers are represented as first:last.

For instance, let’s assume our specfile contains following scans: 1, 2, 3, 4, 5, 684, 685, 687, 688, 689, 700, 688, 688. This method will then return:


Get scan by n.m key

Parameters:key (str) – "s.o" (scan number, scan order)
Return type:scandata

Return the number of scans in the SpecFile

This is an alias for __len__(), for compatibility with the old specfile wrapper API.


This is an alias for motor_names(), for compatibility with the old specfile wrapper API.

Returns:Epoch, from last word on file header line #E
Return type:int
Raise:ValueError if #E line not found in header or last word on #E cannot be converted to type int
Returns:Title, from second field on #C header line (field are strings separated by two spaces)
Return type:str
Raise:ValueError if #C line not found in header or line is empty
class scandata(specfile, scan_index)[source]

Bases: silx.io.specfile.Scan

This class is a subclass of silx.io.specfile.Scan.

It redefines following methods/attributes:

  • data() becomes a method returning an array, instead of just an array
  • mca(): becomes a method returning an array, instead of a silx.io.specfile.MCA object
  • header(): becomes a method returning a list of scan header lines (or a list of a single header line, if a key is specified), instead of just a list of all header lines

Following methods are added:


Return a list of all motor names (identical to motor_names).


Return a list of all motor positions (identical to motor_positions).


Return a list of all labels (labels).


Return the number of data columns (number of detectors)


Return the command called for this scan (#S header line)


Return the data in this scan as a 2D numpy array.

The first index corresponds to the columns/detectors in the original file, and the second index is the row index from the original file. Indices are 0-based.

For instance, this is how you access the 18th data sample for the 3rd detector (assuming sc is your scan object):

>>> scdata = sc.data()
>>> data_sample = scdata[2, 17]

Return a data column (all data for one detector)

Parameters:col – column number (1-based index)

Return a data line (one sample for all detectors)

Parameters:line – line number (1-based index)

Return the date from the scan header line #D


Return a list of file header lines


Return a list of scan header lines if no key is specified. If a valid key is specified, return a list of a single header line.

Parameters:key – Header key (e.g. S, N, L, @CALIB…) If key is an empty string, return complete list of scan header lines. If key does not match any header line, return empty list.
Returns:List of scan header lines
Return type:List[str]

Return the number of data lines (number of data points per detector)


Return one MCA spectrum

Parameters:number – MCA number (1-based index)
Return type:1D numpy array

Return number of MCAs in this scan