Avantes USB spectrometer ROH file 6.0: Python (read-write) parsing library

Avantes USB spectrometers are supplied with a Windows binary which generates one ROH and one RCM file when the user clicks "Save experiment". In the version of 6.0, the ROH file contains a header of 22 four-byte floats, then the spectrum as a float array and a footer of 3 floats. The first and last pixel numbers are specified in the header and determine the (length+1) of the spectral data. In the tested files, the length is (2032-211-1)=1820 pixels, but Kaitai determines this automatically anyway.

The wavelength calibration is stored as a polynomial with coefficients of 'wlintercept', 'wlx1', ... 'wlx4', the argument of which is the (pixel number + 1), as found out by comparing with the original Avantes converted data files. There is no intensity calibration saved, but it is recommended to do it in your program - the CCD in the spectrometer is so uneven that one should prepare exact pixel-to-pixel calibration curves to get reasonable spectral results.

The rest of the header floats is not known to the author. Note that the newer version of Avantes software has a different format, see also https://www.mathworks.com/matlabcentral/fileexchange/37103-avantes-to-matlab

The RCM file contains the user-specified comment, so it may be useful for automatic conversion of data. You may wish to divide the spectra by the integration time before comparing them.

Written and tested by Filip Dominec, 2017-2018

File extension

roh

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of Avantes USB spectrometer ROH file 6.0 using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Python (read-write) source code to parse Avantes USB spectrometer ROH file 6.0

avantes_roh60.py

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
# type: ignore

import kaitaistruct
from kaitaistruct import ReadWriteKaitaiStruct, KaitaiStream, BytesIO


if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 11):
    raise Exception("Incompatible Kaitai Struct Python API: 0.11 or later is required, but you have %s" % (kaitaistruct.__version__))

class AvantesRoh60(ReadWriteKaitaiStruct):
    """Avantes USB spectrometers are supplied with a Windows binary which
    generates one ROH and one RCM file when the user clicks "Save
    experiment". In the version of 6.0, the ROH file contains a header
    of 22 four-byte floats, then the spectrum as a float array and a
    footer of 3 floats. The first and last pixel numbers are specified in the
    header and determine the (length+1) of the spectral data. In the tested
    files, the length is (2032-211-1)=1820 pixels, but Kaitai determines this
    automatically anyway.
    
    The wavelength calibration is stored as a polynomial with coefficients
    of 'wlintercept', 'wlx1', ... 'wlx4', the argument of which is the
    (pixel number + 1), as found out by comparing with the original
    Avantes converted data files. There is no intensity calibration saved,
    but it is recommended to do it in your program - the CCD in the spectrometer
    is so uneven that one should prepare exact pixel-to-pixel calibration curves
    to get reasonable spectral results.
    
    The rest of the header floats is not known to the author. Note that the
    newer version of Avantes software has a different format, see also
    <https://www.mathworks.com/matlabcentral/fileexchange/37103-avantes-to-matlab>
    
    The RCM file contains the user-specified comment, so it may be useful
    for automatic conversion of data. You may wish to divide the spectra by
    the integration time before comparing them.
    
    Written and tested by Filip Dominec, 2017-2018
    """
    def __init__(self, _io=None, _parent=None, _root=None):
        super(AvantesRoh60, self).__init__(_io)
        self._parent = _parent
        self._root = _root or self

    def _read(self):
        self.unknown1 = self._io.read_f4le()
        self.wlintercept = self._io.read_f4le()
        self.wlx1 = self._io.read_f4le()
        self.wlx2 = self._io.read_f4le()
        self.wlx3 = self._io.read_f4le()
        self.wlx4 = self._io.read_f4le()
        self.unknown2 = []
        for i in range(9):
            self.unknown2.append(self._io.read_f4le())

        self.ipixfirst = self._io.read_f4le()
        self.ipixlast = self._io.read_f4le()
        self.unknown3 = []
        for i in range(4):
            self.unknown3.append(self._io.read_f4le())

        self.spectrum = []
        for i in range((int(self.ipixlast) - int(self.ipixfirst)) - 1):
            self.spectrum.append(self._io.read_f4le())

        self.integration_ms = self._io.read_f4le()
        self.averaging = self._io.read_f4le()
        self.pixel_smoothing = self._io.read_f4le()
        self._dirty = False


    def _fetch_instances(self):
        pass
        for i in range(len(self.unknown2)):
            pass

        for i in range(len(self.unknown3)):
            pass

        for i in range(len(self.spectrum)):
            pass



    def _write__seq(self, io=None):
        super(AvantesRoh60, self)._write__seq(io)
        self._io.write_f4le(self.unknown1)
        self._io.write_f4le(self.wlintercept)
        self._io.write_f4le(self.wlx1)
        self._io.write_f4le(self.wlx2)
        self._io.write_f4le(self.wlx3)
        self._io.write_f4le(self.wlx4)
        for i in range(len(self.unknown2)):
            pass
            self._io.write_f4le(self.unknown2[i])

        self._io.write_f4le(self.ipixfirst)
        self._io.write_f4le(self.ipixlast)
        for i in range(len(self.unknown3)):
            pass
            self._io.write_f4le(self.unknown3[i])

        for i in range(len(self.spectrum)):
            pass
            self._io.write_f4le(self.spectrum[i])

        self._io.write_f4le(self.integration_ms)
        self._io.write_f4le(self.averaging)
        self._io.write_f4le(self.pixel_smoothing)


    def _check(self):
        if len(self.unknown2) != 9:
            raise kaitaistruct.ConsistencyError(u"unknown2", 9, len(self.unknown2))
        for i in range(len(self.unknown2)):
            pass

        if len(self.unknown3) != 4:
            raise kaitaistruct.ConsistencyError(u"unknown3", 4, len(self.unknown3))
        for i in range(len(self.unknown3)):
            pass

        if len(self.spectrum) != (int(self.ipixlast) - int(self.ipixfirst)) - 1:
            raise kaitaistruct.ConsistencyError(u"spectrum", (int(self.ipixlast) - int(self.ipixfirst)) - 1, len(self.spectrum))
        for i in range(len(self.spectrum)):
            pass

        self._dirty = False