Skip to main content
`Ctrl`+`K`

# `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

with:

    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 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)

  Bases: `SpecFile`

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

  It redefines following methods:

  > - `__getitem__()`: returns a `scandata` object instead of a `silx.io.specfile.Scan` object

  > - `list()`: returns a string representation of a list instead of a list of integers

  Following methods are added:

  > - `select()`

  > - `scanno()`

  > - `allmotors()`

  > - `epoch()`

  > - `title()`

  __getitem__(key)

    Get scan by 0-based index

    Parameters:

      key (int) – 0-based scan index

    Returns:

      Scan

    Return type:

      `scandata`

  list()

    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:

        "1:5,684:685,687:689,700,688,688"

  select(key)

    Get scan by `n.m` key

    Parameters:

      key (str) – `"s.o"` (scan number, scan order)

    Returns:

      Scan

    Return type:

      `scandata`

  scanno()

    Return the number of scans in the SpecFile

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

  allmotors(scan_index=0)

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

  epoch()
    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

  title()
    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)

  Bases: `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:

  > - `allmotors()`

  > - `allmotorpos()`

  > - `alllabels()`

  > - `cols()`

  > - `lines()`

  > - `command()`

  > - `date()`

  > - `datacol()`

  > - `dataline()`

  > - `fileheader()`

  > - `nbmca()`

  allmotors()

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

  allmotorpos()

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

  alllabels()

    Return a list of all labels (`labels`).

  cols()

    Return the number of data columns (number of detectors)

  command()

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

  data()

    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]

  datacol(col)

    Return a data column (all data for one detector)

    Parameters:

      col – column number (1-based index)

  dataline(line)

    Return a data line (one sample for all detectors)

    Parameters:

      line – line number (1-based index)

  date()

    Return the date from the scan header line `#D`

  fileheader(key='')

    Return a list of file header lines

  header(key='')

    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]

  lines()

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

  mca(number)

    Return one MCA spectrum

    Parameters:

      number – MCA number (1-based index)

    Return type:

      1D numpy array

  nbmca()

    Return number of MCAs in this scan