[ef16c0b] | 1 | """I provide a specialized Ammos Reader for IF data."""
|
---|
[4455f2b] | 2 | import logging
|
---|
[752e2a9] | 3 |
|
---|
[2a9faed] | 4 | from ammosreader.AbstractAmmosReader import AbstractAmmosReader
|
---|
[752e2a9] | 5 | from ammosreader.AmmosGlobalFrameBody import AmmosGlobalFrameBody
|
---|
| 6 | from ammosreader.AmmosIFDataHeader import AmmosIFDataHeader
|
---|
| 7 | from ammosreader.AmmosExtendedIFDataHeader import AmmosExtendedIFDataHeader
|
---|
[4455f2b] | 8 | from ammosreader.AmmosIFDataBody import AmmosIFDataBody
|
---|
[752e2a9] | 9 | from ammosreader.AmmosIFDataBlock import AmmosIFDataBlock
|
---|
[4455f2b] | 10 | from ammosreader.AmmosIFDataBlockHeader import AmmosIFDataBlockHeader
|
---|
[752e2a9] | 11 |
|
---|
| 12 |
|
---|
[2a9faed] | 13 | class AmmosIFReader(AbstractAmmosReader):
|
---|
[4455f2b] | 14 | """I read the IF data embedded in an R&S AMMOS recording."""
|
---|
[752e2a9] | 15 |
|
---|
[ef16c0b] | 16 | def __init__(self, file_name):
|
---|
[2a9faed] | 17 | super().__init__(file_name)
|
---|
[752e2a9] | 18 |
|
---|
| 19 | def read_next_global_frame_body_data_header(self):
|
---|
| 20 |
|
---|
[4455f2b] | 21 | header_size = AmmosIFDataHeader.HEADER_SIZE
|
---|
[752e2a9] | 22 |
|
---|
[4455f2b] | 23 | bytes = self.ammos_file.read(header_size)
|
---|
[752e2a9] | 24 |
|
---|
[4455f2b] | 25 | logging.info("\nReading global frame body standard data header\n")
|
---|
| 26 | if ((not bytes) or (len(bytes) < header_size)):
|
---|
| 27 | logging.debug("Can not read all %s bytes of global frame body data header", header_size)
|
---|
| 28 | return None
|
---|
| 29 | return AmmosIFDataHeader.from_bytes(bytes)
|
---|
[752e2a9] | 30 |
|
---|
| 31 | def read_next_global_frame_body_extended_data_header(self):
|
---|
[4455f2b] | 32 | """
|
---|
| 33 | I return the next global frame body extended data header from current position in file.
|
---|
[752e2a9] | 34 |
|
---|
[4455f2b] | 35 | :return: the next Ammos Extended IF data header or None if incomplete
|
---|
| 36 | :rtype: AmmosExtendedIFDataHeader
|
---|
| 37 | """
|
---|
| 38 | header_size = AmmosExtendedIFDataHeader.HEADER_SIZE
|
---|
[752e2a9] | 39 |
|
---|
[4455f2b] | 40 | bytes = self.ammos_file.read(header_size)
|
---|
| 41 |
|
---|
| 42 | logging.info("\nReading global frame body extended data header\n")
|
---|
| 43 | if ((not bytes) or (len(bytes) < header_size)):
|
---|
| 44 | logging.debug("Can not read all %s bytes of global frame extended data header", header_size)
|
---|
[752e2a9] | 45 | return None
|
---|
[4455f2b] | 46 | return AmmosExtendedIFDataHeader.from_bytes(bytes)
|
---|
| 47 |
|
---|
| 48 | def read_next_if_data_body(self, number_of_data_blocks, data_length):
|
---|
| 49 | """
|
---|
| 50 | I return the next data body read from current position in file.
|
---|
[752e2a9] | 51 |
|
---|
[4455f2b] | 52 | :param number_of_data_blocks: the number of data blocks inside the body
|
---|
| 53 | :type number_of_data_blocks: int
|
---|
[752e2a9] | 54 |
|
---|
[4455f2b] | 55 | :param data_length: the length of the raw data inside a single block
|
---|
| 56 | :type data_length: int
|
---|
| 57 | """
|
---|
| 58 | header_size = AmmosIFDataBlockHeader.HEADER_SIZE
|
---|
[752e2a9] | 59 |
|
---|
[4455f2b] | 60 | data_body = AmmosIFDataBody()
|
---|
[752e2a9] | 61 |
|
---|
[4455f2b] | 62 | block_length = header_size + data_length
|
---|
[752e2a9] | 63 |
|
---|
[4455f2b] | 64 | total = number_of_data_blocks*block_length
|
---|
[752e2a9] | 65 |
|
---|
[ef16c0b] | 66 | byte_string = self.ammos_file.read(block_length)
|
---|
[752e2a9] | 67 |
|
---|
| 68 | if len(byte_string) != total:
|
---|
[4455f2b] | 69 | logging.debug("Can not read all %s bytes of data body", total)
|
---|
[752e2a9] | 70 | return None
|
---|
| 71 |
|
---|
[4455f2b] | 72 | for i in range(0, number_of_data_blocks):
|
---|
[752e2a9] | 73 | result = byte_string[i*block_length:(i*block_length+block_length)]
|
---|
[4455f2b] | 74 | data_body.add_data_block(AmmosIFDataBlock(AmmosIFDataBlockHeader.from_bytes(result[0:header_size]),
|
---|
| 75 | result[header_size:]))
|
---|
[752e2a9] | 76 |
|
---|
[4455f2b] | 77 | return data_body
|
---|
[752e2a9] | 78 |
|
---|
[ef16c0b] | 79 | def read_next_global_frame_body(self, data_header_length):
|
---|
[4455f2b] | 80 | """
|
---|
| 81 | I return the next global frame body read from current position in file.
|
---|
[752e2a9] | 82 |
|
---|
[4455f2b] | 83 | :param data_header_length: the length of the data header
|
---|
| 84 | :type data_header_length: int
|
---|
| 85 | """
|
---|
[752e2a9] | 86 | if_data_header = None
|
---|
| 87 |
|
---|
[4455f2b] | 88 | if data_header_length == AmmosIFDataHeader.HEADER_SIZE:
|
---|
[752e2a9] | 89 | if_data_header = self.read_next_global_frame_body_data_header()
|
---|
[4455f2b] | 90 | if data_header_length == AmmosExtendedIFDataHeader.HEADER_SIZE:
|
---|
[752e2a9] | 91 | if_data_header = self.read_next_global_frame_body_extended_data_header()
|
---|
| 92 |
|
---|
| 93 | if if_data_header is None:
|
---|
[4455f2b] | 94 | logging.debug("Data header missing")
|
---|
[752e2a9] | 95 | return None
|
---|
| 96 |
|
---|
[2a9faed] | 97 | if_data_body = self.read_next_if_data_body(if_data_header.block_count, if_data_header.block_length)
|
---|
[752e2a9] | 98 |
|
---|
| 99 | if if_data_body is None:
|
---|
[2a9faed] | 100 | logging.debug("Data body missing")
|
---|
[752e2a9] | 101 | return None
|
---|
| 102 |
|
---|
| 103 | return AmmosGlobalFrameBody(if_data_header, if_data_body)
|
---|
[2a9faed] | 104 |
|
---|
| 105 | def data(self):
|
---|
| 106 | return b"".join([each.global_frame_body.data_body.data for each in self.container.global_frames])
|
---|