[ef16c0b] | 1 | """I provide a specialized Ammos Reader for IF data."""
|
---|
[752e2a9] | 2 |
|
---|
| 3 | from ammosreader.AmmosGlobalFrameBody import AmmosGlobalFrameBody
|
---|
| 4 | from ammosreader.AmmosIFDataHeader import AmmosIFDataHeader
|
---|
| 5 | from ammosreader.AmmosExtendedIFDataHeader import AmmosExtendedIFDataHeader
|
---|
| 6 | from ammosreader.AmmosGlobalFrameHeader import AmmosGlobalFrameHeader
|
---|
| 7 | from ammosreader.AmmosSingleFrame import AmmosSingleFrame
|
---|
| 8 | from ammosreader.AmmosIFDataBlock import AmmosIFDataBlock
|
---|
| 9 | from ammosreader.AmmosContainer import AmmosContainer
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | class AmmosIFReader():
|
---|
| 13 |
|
---|
[ef16c0b] | 14 | STANDARD_IF_DATA_HEADER_SIZE = 56
|
---|
| 15 | EXTENDED_IF_DATA_HEADER_SIZE = 76
|
---|
[752e2a9] | 16 |
|
---|
[ef16c0b] | 17 | def __init__(self, file_name):
|
---|
| 18 | super.__init__(file_name)
|
---|
[752e2a9] | 19 |
|
---|
| 20 | def read_next_global_frame_body_data_header(self):
|
---|
| 21 |
|
---|
[ef16c0b] | 22 | bytes = self.ammos_file.read(AmmosIFReader.STANDARD_IF_DATA_HEADER_SIZE)
|
---|
[752e2a9] | 23 |
|
---|
| 24 | # print("\nReading global frame body standard data header\n")
|
---|
[ef16c0b] | 25 | if ((not bytes) or (len(bytes) < AmmosIFReader.STANDARD_IF_DATA_HEADER_SIZE)):
|
---|
[752e2a9] | 26 | # print("Can not read all 56 bytes of global frame body data header")
|
---|
| 27 | return None
|
---|
| 28 |
|
---|
| 29 | data_header = AmmosIFDataHeader.from_bytes(bytes)
|
---|
| 30 | # print("Data header", data_header)
|
---|
| 31 | return data_header
|
---|
| 32 |
|
---|
| 33 | def read_next_global_frame_body_extended_data_header(self):
|
---|
| 34 |
|
---|
[ef16c0b] | 35 | bytes = self.ammos_file.read(AmmosIFReader.EXTENDED_IF_DATA_HEADER_SIZE)
|
---|
[752e2a9] | 36 | # print("\nReading global frame body extended data header\n")
|
---|
| 37 |
|
---|
[ef16c0b] | 38 | if ((not bytes) or (len(bytes) < AmmosIFReader.EXTENDED_IF_DATA_HEADER_SIZE)):
|
---|
[752e2a9] | 39 | # print("Can not read all ", 76, "bytes of global frame extended data header")
|
---|
| 40 | return None
|
---|
| 41 | extended_data_header = AmmosExtendedIFDataHeader.from_bytes(bytes)
|
---|
| 42 | # print("Extended data header", extended_data_header)
|
---|
| 43 | return extended_data_header
|
---|
| 44 |
|
---|
| 45 | def read_next_if_data_blocks(self, n, length):
|
---|
| 46 |
|
---|
| 47 | # FIXME: Describe the parameters better
|
---|
| 48 |
|
---|
| 49 | data_blocks = []
|
---|
| 50 |
|
---|
| 51 | block_length = 4 + length
|
---|
| 52 |
|
---|
| 53 | total = n*block_length
|
---|
| 54 |
|
---|
[ef16c0b] | 55 | byte_string = self.ammos_file.read(block_length)
|
---|
[752e2a9] | 56 |
|
---|
| 57 | if len(byte_string) != total:
|
---|
| 58 | # print("Can not read all", total, "bytes of data body")
|
---|
| 59 | return None
|
---|
| 60 |
|
---|
| 61 | for i in range(0, n):
|
---|
| 62 | result = byte_string[i*block_length:(i*block_length+block_length)]
|
---|
| 63 | data_blocks.append(AmmosIFDataBlock(result[0:4], result[4:]))
|
---|
| 64 |
|
---|
| 65 | return data_blocks
|
---|
| 66 |
|
---|
[ef16c0b] | 67 | def read_next_global_frame_body(self, data_header_length):
|
---|
[752e2a9] | 68 |
|
---|
| 69 | if_data_header = None
|
---|
| 70 |
|
---|
[ef16c0b] | 71 | if data_header_length == AmmosIFReader.STANDARD_IF_DATA_HEADER_SIZE:
|
---|
[752e2a9] | 72 | if_data_header = self.read_next_global_frame_body_data_header()
|
---|
[ef16c0b] | 73 | if data_header_length == AmmosIFReader.EXTENDED_IF_DATA_HEADER_SIZE:
|
---|
[752e2a9] | 74 | if_data_header = self.read_next_global_frame_body_extended_data_header()
|
---|
| 75 |
|
---|
| 76 | if if_data_header is None:
|
---|
| 77 | # print("Data header missing")
|
---|
| 78 | return None
|
---|
| 79 |
|
---|
| 80 | if_data_body = self.read_next_if_data_blocks(if_data_header.block_count, if_data_header.block_length)
|
---|
| 81 |
|
---|
| 82 | if if_data_body is None:
|
---|
| 83 | # print("Data body missing")
|
---|
| 84 | return None
|
---|
| 85 |
|
---|
| 86 | return AmmosGlobalFrameBody(if_data_header, if_data_body)
|
---|