def __enter__(self): """Context manager support.""" self._logical_record_index._enter() self.logical_files = [] for lr_index in range(len(self._logical_record_index)): # TODO: This is greedy and for IFLRs we could only read a partial amount for performance. # See FrameArray.x_axis_len_input_bytes file_logical_data = self._logical_record_index.get_file_logical_data(lr_index, 0, -1) assert file_logical_data.is_sealed() if not file_logical_data.lr_is_encrypted: if file_logical_data.lr_is_eflr: # EFLRs eflr = EFLR.ExplicitlyFormattedLogicalRecord(file_logical_data.lr_type, file_logical_data.logical_data) if len(self.logical_files) == 0 or self.logical_files[-1].is_next(eflr): self.logical_files.append(LogicalFile(self._logical_record_index, file_logical_data, eflr)) else: self.logical_files[-1].add_eflr(file_logical_data, eflr) else: # IFLRs if len(self.logical_files) == 0: raise ExceptionLogicalIndexCtor('IFLR when there are no Logical Files.') iflr = IFLR.IndirectlyFormattedLogicalRecord(file_logical_data.lr_type, file_logical_data.logical_data) if iflr.remain > 0: self.logical_files[-1].add_iflr(file_logical_data, iflr) # else: # logger.warning(f'Ignoring empty IFLR at {file_logical_data.position}') return self
def test_iflr_ctor_zero_not_empty(): ld = File.LogicalData(IFLR_ZERO_NOT_EMPTY) iflr = IFLR.IndirectlyFormattedLogicalRecord(1, ld) assert iflr.lr_type == 1 assert iflr.object_name == RepCode.ObjectName(O=11, C=0, I=b'0B') assert iflr.frame_number == 0 assert iflr.preamble_length == 6 assert iflr.remain == 36 assert iflr.remain == ld.remain
def test_iflr_ctor(frame_number, by): ld = File.LogicalData(by) iflr = IFLR.IndirectlyFormattedLogicalRecord(1, ld) assert iflr.lr_type == 1 assert iflr.object_name == RepCode.ObjectName(O=11, C=0, I=b'0B') assert iflr.frame_number == frame_number assert iflr.preamble_length == 6 assert iflr.remain == 36 assert iflr.remain == ld.remain
def populate_frame_array( self, frame_array: LogPass.FrameArray, frame_slice: typing.Union[Slice.Slice, Slice.Sample, None] = None, channels: typing.Union[typing.Set[typing.Hashable], None] = None, ) -> int: """Populates a FrameArray with channel values. frame_array must be a member of the LogPass in thisLogicalFile. frame_slice Allows partial population in the X axis. channels Allows partial population of specific channels. The FrameArray will be populated and this returns the number of frames populated. """ if self.log_pass is None: raise ExceptionLogicalFile( f'populate_frame_array(): when no Log Pass') if not id(frame_array) in [id(v) for v in self.log_pass.frame_arrays]: raise ExceptionLogicalFile( f'populate_frame_array(): given FrameArray is not in Log Pass') iflrs: XAxis.XAxis = self.iflr_position_map[frame_array.ident] if len(iflrs): # Set partial frames if frame_slice is not None: range_gen = frame_slice.gen_indices(len(iflrs)) num_frames = frame_slice.count(len(iflrs)) else: range_gen = range(len(iflrs)) num_frames = len(iflrs) # Set partial channels if channels is not None: frame_array.init_arrays_partial(num_frames, channels) else: frame_array.init_arrays(num_frames) # Now populate logger.debug( f'populate_frame_array(): len(iflrs): {len(iflrs)} slice: {frame_slice}' f' num_frames: {num_frames} range_gen: {range_gen}.') for array_index, frame_number in enumerate(range_gen): iflr_reference = iflrs[frame_number] fld: File.FileLogicalData = self._logical_record_index.get_file_logical_data_at_position( iflr_reference.logical_record_position) # Create an IFLR but we don't use it, just the remaining bytes in the Logical Data. _iflr = IFLR.IndirectlyFormattedLogicalRecord( fld.lr_type, fld.logical_data) if channels is not None: frame_array.read_partial(fld.logical_data, array_index, channels) else: frame_array.read(fld.logical_data, array_index) else: num_frames = 0 frame_array.init_arrays(num_frames) return num_frames
def test_example_iflr_process(): log_pass: LogPass.LogPass = _create_log_pass() # ObjectName(O=11, C=0, I=b'0B') frame_array: LogPass.FrameArray = log_pass[RepCode.ObjectName( 11, 0, b'0B')] frame_array.init_arrays(len(BYTES_IFLR)) # print() for frame_number, by in enumerate(BYTES_IFLR): ld = File.LogicalData(by) _iflr = IFLR.IndirectlyFormattedLogicalRecord(0, ld) # frame_array.read_x_axis(ld, frame_number=0) frame_array.read(ld, frame_number=frame_number) # print(frame_array) # print() expected = [ # X axis np.array([ [0.], [75197.], [154724.], [234606.], [311024.], [381102.], [386839.], [428193.], [447720.], [466339.], [489547.], [500559.], [523772.], [538638.], [542417.], [550409.], ]), np.array([ [0.], [0.50002027], [0.50002027], [0.7500017], [0.50002027], [0.99998325], [0.9699998], [0.9699998], [0.69999975], [1.0600001], [0.9699998], [0.8800001], [0.78999996], [1.7599998], [2.2000003], [2.9], ], ), np.array([ [0.], [0.], [0.], [0.], [0.], [0.], [200.44], [205.45], [206.18], [208.69], [202.7], [200.93], [255.77002], [243.87], [241.15], [240.7], ]), np.array([ [0.], [190.99757], [392.98987], [595.8777], [789.96594], [967.95013], [982.51935], [1087.5443], [1137.139], [1184.4233], [1243.3641], [1271.3306], [1330.2852], [1368.0354], [1377.6296], [1397.9094], ]), np.array([ [0.], [0.833423], [2.596255], [4.809544], [6.92684], [9.256786], [9.268364], [7.632412], [6.981416], [6.338459], [5.399805], [4.980779], [4.457969], [4.138596], [3.98476], [3.54544], ]), np.array([ [0.], [0.833423], [2.596255], [4.809544], [6.92684], [9.256786], [9.268364], [7.632412], [6.981416], [6.338459], [5.399805], [4.980779], [4.457969], [4.138596], [3.98476], [3.54544], ]), np.array([ [0.], [0.], [0.], [0.], [0.], [0.], [-0.043073], [-0.735641], [-1.049728], [-1.387169], [-1.841496], [-2.009587], [-2.565323], [-3.338264], [-3.632012], [-4.421124], ]), np.array([ [-9.9925000e+02], [7.9068176e-02], [0.0000000e+00], [3.7815217e-02], [3.7815217e-02], [8.4224798e-02], [3.9912241e+00], [2.4064228e-02], [1.6329297e-01], [2.3032904e-01], [7.0473805e-02], [1.0141353e-01], [3.9362201e-01], [7.9411954e-01], [1.4060384e+00], [1.0347618e+00], ]), np.array([ [-999.25], [-999.25], [-999.25], [-999.25], [-999.25], [-999.25], [50.3937], [56.4173], [58.4252], [58.4252], [62.4409], [62.4409], [62.4409], [62.4409], [62.4409], [58.4252], ]), ] for c, channel in enumerate(frame_array.channels): # print(channel.array) # np.testing.assert_array_almost_equal(channel.array, expected[c]) assert str(channel.array) == str(expected[c])
def test_example_iflr(): print() for by in BYTES_IFLR: ld = File.LogicalData(by) iflr = IFLR.IndirectlyFormattedLogicalRecord(0, ld)
def _iflr_and_logical_data_from_bytes(by: bytes) -> typing.Tuple[IFLR.IndirectlyFormattedLogicalRecord, File.LogicalData]: ld = File.LogicalData(by) iflr = IFLR.IndirectlyFormattedLogicalRecord(1, ld) return iflr, ld
def test_iflr_str(by, s): ld = File.LogicalData(by) iflr = IFLR.IndirectlyFormattedLogicalRecord(1, ld) assert str(iflr) == s
def scan_RP66V1_file_EFLR_IFLR(fobj: typing.BinaryIO, fout: typing.TextIO, **kwargs) -> None: """Scans the file reporting the individual EFLR and IFLR.""" verbose = kwargs.get('verbose', 0) if not verbose: fout.write(colorama.Fore.YELLOW + 'Use -v to see individual logical data.\n') # TODO: eflr_dump is never present dump_eflr = kwargs.get('eflr_dump', 0) eflr_set_type = kwargs.get('eflr_set_type', []) iflr_dump = kwargs.get('iflr_dump', 0) iflr_set_type = kwargs.get('iflr_set_type', []) # TODO: Use both of these # dump_bytes = kwargs.get('dump_bytes', 0) # dump_raw_bytes = kwargs.get('dump_raw_bytes', 0) # if not dump_bytes: # fout.write(colorama.Fore.YELLOW + 'Use -v and --dump-bytes to see actual first n bytes.\n') with _output_section_header_trailer('RP66V1 EFLR and IFLR Data Summary', '*', os=fout): with File.FileRead(fobj) as rp66_file: # TODO: use data_table.format_table vr_position = 0 header = [ f'{"Visible R":10}', f'{"LRSH":10}', f'{"Typ":3}', f'{" "}', f'{" "}', f'{"Length":8}', ] underline = ['-' * len(h) for h in header] if verbose: fout.write(' '.join(header) + '\n') fout.write(' '.join(underline) + '\n') for file_logical_data in rp66_file.iter_logical_records(): if file_logical_data.lr_is_eflr: if file_logical_data.lr_is_encrypted: if kwargs['encrypted']: if verbose: fout.write(colorama.Fore.MAGENTA + f'Encrypted EFLR: {file_logical_data}' + colorama.Style.RESET_ALL) else: fout.write(colorama.Fore.MAGENTA + f'Encrypted EFLR: {file_logical_data.position}' + colorama.Style.RESET_ALL) fout.write('\n') else: eflr = EFLR.ExplicitlyFormattedLogicalRecord(file_logical_data.lr_type, file_logical_data.logical_data) if dump_eflr and len(eflr_set_type) == 0 or eflr.set.type in eflr_set_type: lines = str(eflr).split('\n') for i, line in enumerate(lines): if i == 0: fout.write(colorama.Fore.MAGENTA + line + colorama.Style.RESET_ALL) else: fout.write(line) fout.write('\n') else: # IFLR if iflr_dump and verbose: if file_logical_data.lr_is_encrypted: if kwargs['encrypted']: if verbose: fout.write(colorama.Fore.MAGENTA + f'Encrypted IFLR: {file_logical_data}' + colorama.Style.RESET_ALL) else: fout.write(colorama.Fore.MAGENTA + f'Encrypted IFLR: {file_logical_data.position}' + colorama.Style.RESET_ALL) fout.write('\n') else: iflr = IFLR.IndirectlyFormattedLogicalRecord(file_logical_data.lr_type, file_logical_data.logical_data) if len(iflr_set_type) == 0 or iflr.object_name.I in iflr_set_type: fout.write(str(iflr)) fout.write('\n')