1 | """I provide a specialized Ammos Reader for IF data."""
|
---|
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 |
|
---|
14 | STANDARD_IF_DATA_HEADER_SIZE = 56
|
---|
15 | EXTENDED_IF_DATA_HEADER_SIZE = 76
|
---|
16 |
|
---|
17 | def __init__(self, file_name):
|
---|
18 | super.__init__(file_name)
|
---|
19 |
|
---|
20 | def read_next_global_frame_body_data_header(self):
|
---|
21 |
|
---|
22 | bytes = self.ammos_file.read(AmmosIFReader.STANDARD_IF_DATA_HEADER_SIZE)
|
---|
23 |
|
---|
24 | # print("\nReading global frame body standard data header\n")
|
---|
25 | if ((not bytes) or (len(bytes) < AmmosIFReader.STANDARD_IF_DATA_HEADER_SIZE)):
|
---|
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 |
|
---|
35 | bytes = self.ammos_file.read(AmmosIFReader.EXTENDED_IF_DATA_HEADER_SIZE)
|
---|
36 | # print("\nReading global frame body extended data header\n")
|
---|
37 |
|
---|
38 | if ((not bytes) or (len(bytes) < AmmosIFReader.EXTENDED_IF_DATA_HEADER_SIZE)):
|
---|
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 |
|
---|
55 | byte_string = self.ammos_file.read(block_length)
|
---|
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 |
|
---|
67 | def read_next_global_frame_body(self, data_header_length):
|
---|
68 |
|
---|
69 | if_data_header = None
|
---|
70 |
|
---|
71 | if data_header_length == AmmosIFReader.STANDARD_IF_DATA_HEADER_SIZE:
|
---|
72 | if_data_header = self.read_next_global_frame_body_data_header()
|
---|
73 | if data_header_length == AmmosIFReader.EXTENDED_IF_DATA_HEADER_SIZE:
|
---|
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)
|
---|