def test_write_update_sequential(gsf_test_data: GsfDatafile): with gsfpy.open_gsf(gsf_test_data.path, FileMode.GSF_UPDATE) as gsf_file: _, record = gsf_file.read(RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING) orig_beam_flags = record.mb_ping.beam_flags[: record.mb_ping.number_beams] beam_flag_updates = {3: 1, 4: 1, 5: 1, 6: 1} for i, value in beam_flag_updates.items(): record.mb_ping.beam_flags[i] = c_ubyte(value) gsf_file.seek(SeekOption.GSF_PREVIOUS_RECORD) gsf_file.write(record, RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING) with gsfpy.open_gsf(gsf_test_data.path, FileMode.GSF_READONLY) as reopened_gsf_file: _, updated_record = reopened_gsf_file.read( RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING ) for i in range(updated_record.mb_ping.number_beams): if i in beam_flag_updates: assert_that(updated_record.mb_ping.beam_flags[i]).is_equal_to( beam_flag_updates[i] ) else: assert_that(updated_record.mb_ping.beam_flags[i]).is_equal_to( orig_beam_flags[i] )
def test_read_by_index(gsf_test_data: GsfDatafile): with gsfpy.open_gsf(gsf_test_data.path, FileMode.GSF_READONLY) as gsf_file_by_seq: # Read a couple of records forwards, so its at position 3. # Records are 1-indexed. # This is using read because Seek only does start/end/previous not next. gsf_file_by_seq.read(RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING) gsf_file_by_seq.read(RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING) _, third_record_by_seq = gsf_file_by_seq.read( RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING ) with gsfpy.open_gsf( gsf_test_data.path, FileMode.GSF_READONLY_INDEX ) as gsf_file_by_idx: _, third_record_by_idx = gsf_file_by_idx.read( RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING, 3 ) number_beams_by_seq = third_record_by_seq.mb_ping.number_beams number_beams_by_idx = third_record_by_idx.mb_ping.number_beams assert_that(number_beams_by_seq).is_equal_to(number_beams_by_idx) beam_angle_by_seq = third_record_by_seq.mb_ping.beam_angle[:number_beams_by_seq] beam_angle_by_idx = third_record_by_idx.mb_ping.beam_angle[:number_beams_by_idx] assert_that(beam_angle_by_seq).is_equal_to(beam_angle_by_idx) beam_flags_by_seq = third_record_by_seq.mb_ping.beam_flags[:number_beams_by_seq] beam_flags_by_idx = third_record_by_idx.mb_ping.beam_flags[:number_beams_by_idx] assert_that(beam_flags_by_seq).is_equal_to(beam_flags_by_idx)
def test_seek_success(gsf_test_data_03_06): """ Open the test GSF file, seek to end of file, then close. """ # Act with open_gsf(gsf_test_data_03_06.path) as gsf_file: gsf_file.seek(SeekOption.GSF_END_OF_FILE)
def test_open_gsf_buffered_success(gsf_test_data_03_06): """ Open the test GSF file in buffered mode, then close. """ # Act with open_gsf(gsf_test_data_03_06.path, buffer_size=100) as _: pass
def test_open_gsf_success(gsf_test_data_03_06): """ Open the test GSF file, then close. """ # Act with open_gsf(gsf_test_data_03_06.path) as _: pass
def test_direct_access_write_and_read_success(tmp_path): """ Create, update and read. First sequentially, then using direct access """ tmp_gsf_file_path = tmp_path / "temp.gsf" # Create a file with 3 records comment_1 = b"Comment #1" comment_2 = b"Comment #2" comment_3 = b"Comment #3" comment_4 = b"Comment #4" # Write sequentially with open_gsf(tmp_gsf_file_path, mode=FileMode.GSF_CREATE) as gsf_file: gsf_file.write(_new_comment(comment_1), RecordType.GSF_RECORD_COMMENT) gsf_file.write(_new_comment(comment_2), RecordType.GSF_RECORD_COMMENT) gsf_file.write(_new_comment(comment_3), RecordType.GSF_RECORD_COMMENT) # Update using direct access with open_gsf(tmp_gsf_file_path, mode=FileMode.GSF_UPDATE_INDEX) as gsf_file: gsf_file.write(_new_comment(comment_4), RecordType.GSF_RECORD_COMMENT, 2) # Read sequentially with open_gsf(tmp_gsf_file_path) as gsf_file: _, record_1 = gsf_file.read() assert_that(string_at(record_1.comment.comment)).is_equal_to(comment_1) _, record_2 = gsf_file.read() assert_that(string_at(record_2.comment.comment)).is_equal_to(comment_4) _, record_3 = gsf_file.read() assert_that(string_at(record_3.comment.comment)).is_equal_to(comment_3) assert_that( gsf_file.read).raises(GsfException).when_called_with().is_equal_to( "[-23] GSF End of File Encountered") # Read using direct access with open_gsf(tmp_gsf_file_path, mode=FileMode.GSF_READONLY_INDEX) as gsf_file: _, direct_access_record = gsf_file.read(RecordType.GSF_RECORD_COMMENT, 2) assert_that(string_at( direct_access_record.comment.comment)).is_equal_to(comment_4)
def test_update_by_index(gsf_test_data: GsfDatafile): first_record_index = 1 third_record_index = 3 with gsfpy.open_gsf(gsf_test_data.path, FileMode.GSF_UPDATE_INDEX) as gsf_file: _, record = gsf_file.read( RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING, third_record_index ) orig_beam_flags = record.mb_ping.beam_flags[: record.mb_ping.number_beams] beam_flag_updates = {3: 1, 4: 1, 5: 1, 6: 1} for i, value in beam_flag_updates.items(): record.mb_ping.beam_flags[i] = c_ubyte(value) gsf_file.write( record, RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING, third_record_index ) with gsfpy.open_gsf( gsf_test_data.path, FileMode.GSF_READONLY_INDEX ) as reopened_gsf_file: _, read_gsf_records = reopened_gsf_file.read( RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING, third_record_index ) read_ping_record = read_gsf_records.mb_ping updated_flags = read_ping_record.beam_flags[: record.mb_ping.number_beams] for i in range(read_ping_record.number_beams): if i in beam_flag_updates: assert_that(beam_flag_updates[i]).is_equal_to( read_ping_record.beam_flags[i] ) else: assert_that(orig_beam_flags[i]).is_equal_to( read_ping_record.beam_flags[i] ) _, first_gsf_records = reopened_gsf_file.read( RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING, first_record_index ) first_rec_flags = first_gsf_records.mb_ping.beam_flags[ : record.mb_ping.number_beams ] assert_that(updated_flags).is_not_equal_to(first_rec_flags)
def test_write_success(gsf_test_data_03_06): """ Write a single comment record to a new GSF file """ # Arrange tmp_gsf_file_path = path.join(tempfile.gettempdir(), "temp.gsf") comment = b"My first comment" # Act with open_gsf(tmp_gsf_file_path, mode=FileMode.GSF_CREATE) as gsf_file: gsf_file.write(_new_comment(comment), RecordType.GSF_RECORD_COMMENT) # Assert # Read comment from newly created file to check it is as expected with open_gsf(tmp_gsf_file_path) as gsf_file: _, record = gsf_file.read(RecordType.GSF_RECORD_COMMENT) assert_that(string_at(record.comment.comment)).is_equal_to(comment)
def test_get_number_records_failure(gsf_test_data_03_06): """ Open the test GSF file in GSF_READONLY mode, attempt to count the number of GSF_RECORD_SWATH_BATHYMETRY_PING records and verify the exception. """ # Act with open_gsf(gsf_test_data_03_06.path) as gsf_file: assert_that( gsf_file.get_number_records).raises(GsfException).when_called_with( RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING).is_equal_to( "[-3] GSF Error illegal access mode")
def test_get_number_records_success(gsf_test_data_03_06): """ Open the test GSF file in GSF_READONLY_INDEX mode, count the number of GSF_RECORD_SWATH_BATHYMETRY_PING records, then close. """ # Act with open_gsf(gsf_test_data_03_06.path, FileMode.GSF_READONLY_INDEX) as gsf_file: number_of_pings = gsf_file.get_number_records( RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING) assert_that(number_of_pings).is_equal_to(8)
def test_read(gsf_test_data: GsfDatafile): with gsfpy.open_gsf(gsf_test_data.path, FileMode.GSF_READONLY) as gsf_file: _, record = gsf_file.read(RecordType.GSF_RECORD_SWATH_BATHYMETRY_PING, 1) assert_that(record.mb_ping.number_beams).is_equal_to(gsf_test_data.num_beams) for i in range(record.mb_ping.number_beams): # fmt: off assert_that(record.mb_ping.beam_angle[i]) \ .described_as(f"beam_angle[{i}]") \ .is_greater_than_or_equal_to(-180) \ .is_less_than_or_equal_to(180)
def test_read_buffered_success(gsf_test_data_03_06): """ Read a comment record from a GSF file using a buffer. """ # Act with open_gsf(gsf_test_data_03_06.path, buffer_size=1) as gsf_file: _, record = gsf_file.read(RecordType.GSF_RECORD_COMMENT) # Assert assert_that(string_at(record.comment.comment)).is_equal_to( (b"Bathy converted from HIPS file: " b"M:\\CCOM_Processing\\CARIS_v9\\HIPS\\HDCS_Data\\EX1604" b"\\Okeanos_2016\\2016-083\\0029_20160323_185603_EX1604_MB"))