Skip to content

LCData

Class for reading LeCroy 584AM files

xaratustrah@github Sep-2018

Many thanks to github.com/nerdull for reverse engineering an old code by M. Hausmann from 1992

LCData

Bases: IQBase

Source code in iqtools/lcdata.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class LCData(IQBase):
    def __init__(self, filename):
        super().__init__(filename)

        # fixed features
        self.fs = 4e9
        self.center = 0

        # Additional fields in this subclass
        self.date_time = time.ctime(os.path.getctime(self.filename))

    def read(self, nframes=10, lframes=1024, sframes=0):
        """Read a section of the file.

        Args:
            nframes (int, optional): Number of frames to be read. Defaults to 10.
            lframes (int, optional): Length of each frame. Defaults to 1024.
            sframes (int, optional): Starting frame. Defaults to 0.
        """        

        self.read_samples(nframes * lframes, offset=sframes * lframes)

    def read_complete_file(self):
        """Reads a complete file.

        Returns:
            ndarray: Returns the complete data array
        """        

        filesize = os.path.getsize(self.filename)
        with open(self.filename, 'rb') as f:
            file_data = f.read()
        # 45th byte determines the endianness
        # one = little endian
        biglit = ''
        if struct.unpack_from('b', file_data, 45)[0]:
            biglit = '<'
        else:
            biglit = '>'

        hdr_len = struct.unpack_from(
            '{}I'.format(biglit), file_data, 47)[0] + 11
        self.nsamples_total = struct.unpack_from(
            '{}I'.format(biglit), file_data, 71)[0]

        self.vert_gain = struct.unpack_from(
            '{}f'.format(biglit), file_data, 167)[0]
        self.vert_offset = struct.unpack_from(
            '{}f'.format(biglit), file_data, 171)[0]
        self.horiz_interval = struct.unpack_from(
            '{}f'.format(biglit), file_data, 187)[0]
        self.horiz_offset = struct.unpack_from(
            '{}f'.format(biglit), file_data, 191)[0]
        self.vert_unit = struct.unpack_from('c', file_data, 207)[
            0].decode("utf-8")
        self.horiz_unit = struct.unpack_from('c', file_data, 255)[
            0].decode("utf-8")

        sec, mt, hr, dd, mm, yy = struct.unpack_from(
            '{}dbbbbI'.format(biglit), file_data, 307)
        try:
            dt_obj = datetime.datetime(yy, mm, dd, hr, mt, int(
                sec), int((sec - int(sec)) * 1e6))
            self.date_time = dt_obj.strftime("%Y-%m-%d_%H:%M:%S.%f")
        except ValueError:
            self.date_time = ''

        self.data_array = np.frombuffer(
            file_data, np.int8, offset=hdr_len) * self.vert_gain
        return self.data_array

read(nframes=10, lframes=1024, sframes=0)

Read a section of the file.

Parameters:

Name Type Description Default
nframes int

Number of frames to be read. Defaults to 10.

10
lframes int

Length of each frame. Defaults to 1024.

1024
sframes int

Starting frame. Defaults to 0.

0
Source code in iqtools/lcdata.py
29
30
31
32
33
34
35
36
37
38
def read(self, nframes=10, lframes=1024, sframes=0):
    """Read a section of the file.

    Args:
        nframes (int, optional): Number of frames to be read. Defaults to 10.
        lframes (int, optional): Length of each frame. Defaults to 1024.
        sframes (int, optional): Starting frame. Defaults to 0.
    """        

    self.read_samples(nframes * lframes, offset=sframes * lframes)

read_complete_file()

Reads a complete file.

Returns:

Name Type Description
ndarray

Returns the complete data array

Source code in iqtools/lcdata.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def read_complete_file(self):
    """Reads a complete file.

    Returns:
        ndarray: Returns the complete data array
    """        

    filesize = os.path.getsize(self.filename)
    with open(self.filename, 'rb') as f:
        file_data = f.read()
    # 45th byte determines the endianness
    # one = little endian
    biglit = ''
    if struct.unpack_from('b', file_data, 45)[0]:
        biglit = '<'
    else:
        biglit = '>'

    hdr_len = struct.unpack_from(
        '{}I'.format(biglit), file_data, 47)[0] + 11
    self.nsamples_total = struct.unpack_from(
        '{}I'.format(biglit), file_data, 71)[0]

    self.vert_gain = struct.unpack_from(
        '{}f'.format(biglit), file_data, 167)[0]
    self.vert_offset = struct.unpack_from(
        '{}f'.format(biglit), file_data, 171)[0]
    self.horiz_interval = struct.unpack_from(
        '{}f'.format(biglit), file_data, 187)[0]
    self.horiz_offset = struct.unpack_from(
        '{}f'.format(biglit), file_data, 191)[0]
    self.vert_unit = struct.unpack_from('c', file_data, 207)[
        0].decode("utf-8")
    self.horiz_unit = struct.unpack_from('c', file_data, 255)[
        0].decode("utf-8")

    sec, mt, hr, dd, mm, yy = struct.unpack_from(
        '{}dbbbbI'.format(biglit), file_data, 307)
    try:
        dt_obj = datetime.datetime(yy, mm, dd, hr, mt, int(
            sec), int((sec - int(sec)) * 1e6))
        self.date_time = dt_obj.strftime("%Y-%m-%d_%H:%M:%S.%f")
    except ValueError:
        self.date_time = ''

    self.data_array = np.frombuffer(
        file_data, np.int8, offset=hdr_len) * self.vert_gain
    return self.data_array