def test_all_cdps_map_to_a_trace_index(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh, dimensionality=2) for cdp_number in reader.cdp_numbers(): reader.trace_index(cdp_number)
def transform(scale_factor, in_filename, out_filename): with open(in_filename, 'rb') as in_file, \ open(out_filename, 'wb') as out_file: segy_reader = create_reader(in_file) transformed_dataset = ScaledSamplesDataset(segy_reader, scale_factor) write_segy(out_file, transformed_dataset)
def transform(scale_factor, in_filename, out_filename): with open(in_filename, 'rb') as in_file, \ open(out_filename, 'wb') as out_file: segy_reader = create_reader(in_file, progress=make_progress_indicator("Cataloging")) transformed_dataset = ScaledCoordinatesDataset(segy_reader, scale_factor) write_segy(out_file, transformed_dataset, progress=make_progress_indicator("Transforming"))
def test_all_inline_xlines_have_a_trace_index(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh, dimensionality=3) for num in reader.inline_xline_numbers(): assert reader.has_trace_index(num)
def load_save(in_filename, out_filename): with open(in_filename, 'rb') as in_file, \ open(out_filename, 'wb') as out_file: segy_reader = create_reader(in_file) print() print("Filename: ", segy_reader.filename) print("SEG Y revision: ", segy_reader.revision) print("Number of traces: ", segy_reader.num_traces()) print("Data format: ", segy_reader.data_sample_format_description) print("Dimensionality: ", segy_reader.dimensionality) try: print("Number of CDPs: ", segy_reader.num_cdps()) except AttributeError: pass try: print("Number of inlines: ", segy_reader.num_inlines()) print("Number of crosslines: ", segy_reader.num_xlines()) except AttributeError: pass print("=== BEGIN TEXTUAL REEL HEADER ===") for line in segy_reader.textual_reel_header: print(line[3:]) print("=== END TEXTUAL REEL HEADER ===") print() print("=== BEGIN EXTENDED TEXTUAL HEADER ===") print(segy_reader.extended_textual_header) print("=== END EXTENDED TEXTUAL_HEADER ===") write_segy(out_file, segy_reader)
def test_inline_xline_numbers_is_correct(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) assert sorted(reader.inline_xline_numbers()) == sorted( (idx, idx) for idx in range(dset.num_traces()))
def test_segy_revision_is_correct(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) assert reader.revision == dset.binary_reel_header.format_revision_num
def test_SegYReader_bytes_per_sample_is_correct(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) assert reader.bytes_per_sample == bytes_per_sample(dset.binary_reel_header)
def test_filename_reports_unknown_when_filename_unavailable(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) assert reader.filename == '<unknown>'
def test_inline_xlines_map_to_correct_trace_index(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh, dimensionality=3) for num in reader.inline_xline_numbers(): assert reader.trace_index(num) == num[0] assert reader.trace_index(num) == num[1]
def test_reading_from_non_file(dset): handle = io.BytesIO() write_segy(handle, dset) handle.seek(0) reader = create_reader(handle, dimensionality=dset.dimensionality) _compare_datasets(reader, dset)
def test_filename_correctly_reported_for_normal_files(tempdir, dset): segy_file = str(tempdir / 'segy') with open(segy_file, mode='wb') as fh: write_segy(fh, dset) with open(segy_file, mode='rb') as fh: reader = create_reader(fh) assert reader.filename == segy_file
def test_SegYReader_throws_TypeError_if_pickled_without_real_file_handle(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh, cache_directory=None) with pytest.raises(TypeError): pickle.dumps(reader)
def load_save(in_filename, out_filename): with open(in_filename, 'rb') as in_file, \ open(out_filename, 'wb') as out_file: segy_reader = create_reader( in_file, progress=make_progress_indicator("Cataloging")) write_segy(out_file, segy_reader, progress=make_progress_indicator("Copying"))
def test_SegYReader_throws_TypeError_when_pickling_from_closed_file_handle(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) fh.close() with pytest.raises(TypeError): pickle.dumps(reader)
def test_SegYReader_throws_TypeError_when_pickling_from_closed_file_handle( dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) fh.close() with pytest.raises(TypeError): pickle.dumps(reader)
def test_trace_header_format_is_requested_format(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh, trace_header_format=TraceHeaderRev1) # TODO: Currently we only generate datasets with rev1 headers. We need to # fix this. assert reader.trace_header_format_class is TraceHeaderRev1
def test_cache_used_if_found(self, tempdir): dset1 = dataset(num_dims=2).example() dset2 = dataset(num_dims=2).example() # This is hacky. We want to verify that create_reader will use a # chached reader if one exists, but we don't really have a way to # determine that directly (without timing, perhaps). So we're going to # try to trick the system. First we'll save one dataset and read it to # generate a cache. Then we'll do the same with another dataset . Then # we'll copy the first cache over the second, making sure to use the # right filename (since it's derived from the sha1 of the dataset). # Finally, we'll load the second dataset and request that the cache is # used. The returned reader should look like the first dataset, i.e. # the one who's cache we copied. # create a reliably different dset. hypothesis likes to make them # entirely equal. dset2.binary_reel_header.reel_num = 1 if dset1.binary_reel_header.reel_num == 0 else 0 # generate the cache for the first dataset segy_dir1 = tempdir / '1' segy_dir1.mkdir() segy_file1 = str(segy_dir1 / 'test.segy') cache_dir = '.segy_cache' with open(segy_file1, mode='wb') as fh: write_segy(fh, dset1) with open(segy_file1, mode='rb') as fh: create_reader(fh, cache_directory=cache_dir) # generate the cache for the second dataset segy_dir2 = tempdir / '2' segy_dir2.mkdir() segy_file2 = str(segy_dir2 / 'test.segy') with open(segy_file2, mode='wb') as fh: write_segy(fh, dset2) with open(segy_file2, mode='rb') as fh: create_reader(fh, cache_directory=cache_dir) # Replace the second cache with the first, preserving the filename. cache_path1 = segy_dir1 / cache_dir cache_file1 = next(cache_path1.iterdir()) cache_path2 = segy_dir2 / cache_dir cache_file2 = next(cache_path2.iterdir()) cache_file1.replace(cache_file2) # Now when we load the second dataset, we should get the cache of the # first. Sneaky! with open(segy_file2, mode='rb') as fh: reader = create_reader( fh, dimensionality=dset2.dimensionality, cache_directory=cache_dir) _compare_datasets(dset1, reader, compare_dimensionality=False)
def transform(scale_factor, in_filename, out_filename): with open(in_filename, 'rb') as in_file, \ open(out_filename, 'wb') as out_file: segy_reader = create_reader( in_file, progress=make_progress_indicator("Cataloging")) transformed_dataset = ScaledCoordinatesDataset(segy_reader, scale_factor) write_segy(out_file, transformed_dataset, progress=make_progress_indicator("Transforming"))
def test_trace_header_raises_ValueError_on_out_of_range(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) with pytest.raises(ValueError): reader.trace_header(-1) with pytest.raises(ValueError): reader.trace_header(reader.num_traces() + 1)
def transform(data_sample_format, in_filename, out_filename): with open(in_filename, 'rb') as in_file, \ open(out_filename, 'wb') as out_file: segy_reader = create_reader(in_file) sample_type = segy_reader.data_sample_format if sample_type != 'ibm': raise RuntimeError("Source file {} has {} sample type".format( in_filename, sample_type)) transformed_dataset = ConvertingDataset(segy_reader, data_sample_format) write_segy(out_file, transformed_dataset)
def test_trace_header_format_is_requested_format(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader( fh, trace_header_format=TraceHeaderRev1) # TODO: Currently we only generate datasets with rev1 headers. We need to # fix this. assert reader.trace_header_format_class is TraceHeaderRev1
def test_cache_used_if_found(self, tempdir): dset1 = dataset(num_dims=2).example() dset2 = dataset(num_dims=2).example() # This is hacky. We want to verify that create_reader will use a # chached reader if one exists, but we don't really have a way to # determine that directly (without timing, perhaps). So we're going to # try to trick the system. First we'll save one dataset and read it to # generate a cache. Then we'll do the same with another dataset . Then # we'll copy the first cache over the second, making sure to use the # right filename (since it's derived from the sha1 of the dataset). # Finally, we'll load the second dataset and request that the cache is # used. The returned reader should look like the first dataset, i.e. # the one who's cache we copied. # create a reliably different dset. hypothesis likes to make them # entirely equal. dset2.binary_reel_header.reel_num = 1 if dset1.binary_reel_header.reel_num == 0 else 0 # generate the cache for the first dataset segy_dir1 = tempdir / '1' segy_dir1.mkdir() segy_file1 = str(segy_dir1 / 'test.segy') cache_dir = '.segy_cache' with open(segy_file1, mode='wb') as fh: write_segy(fh, dset1) with open(segy_file1, mode='rb') as fh: create_reader(fh, cache_directory=cache_dir) # generate the cache for the second dataset segy_dir2 = tempdir / '2' segy_dir2.mkdir() segy_file2 = str(segy_dir2 / 'test.segy') with open(segy_file2, mode='wb') as fh: write_segy(fh, dset2) with open(segy_file2, mode='rb') as fh: create_reader(fh, cache_directory=cache_dir) # Replace the second cache with the first, preserving the filename. cache_path1 = segy_dir1 / cache_dir cache_file1 = next(cache_path1.iterdir()) cache_path2 = segy_dir2 / cache_dir cache_file2 = next(cache_path2.iterdir()) cache_file1.replace(cache_file2) # Now when we load the second dataset, we should get the cache of the # first. Sneaky! with open(segy_file2, mode='rb') as fh: reader = create_reader(fh, dimensionality=dset2.dimensionality, cache_directory=cache_dir) _compare_datasets(dset1, reader, compare_dimensionality=False)
def test_cache_version_mismatch_throws_TypeError(dset, tempdir): segy_file = tempdir / 'segy' cache_dir = 'cache_dir' with open(str(segy_file), mode='wb') as fh: write_segy(fh, dset) # Create reader to force creation of cache with open(str(segy_file), mode='rb') as fh: reader = create_reader(fh, cache_directory=cache_dir) state_dict = reader.__getstate__() state_dict['__version__'] = state_dict['__version__'] * 2 with pytest.raises(TypeError): reader.__setstate__(state_dict)
def test_ValueError_on_out_of_range_trace_index(self, dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) with pytest.raises(ValueError): reader.trace_samples(-1) with pytest.raises(ValueError): reader.trace_samples(reader.num_traces())
def test_cache_is_created_when_requested(self, dset, tempdir): segy_file = str(tempdir / 'test.segy') cache_dir = '.segy_cache' cache_path = tempdir / cache_dir with open(segy_file, mode='wb') as fh: write_segy(fh, dset) assert not cache_path.exists() with open(segy_file, mode='rb') as fh: reader = create_reader(fh, cache_directory=cache_dir) assert cache_path.exists()
def test_ValueError_on_out_of_range_start_sample(self, dset): assume(dset.num_traces() > 0) fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) with pytest.raises(ValueError): reader.trace_samples(0, start=-1) with pytest.raises(ValueError): reader.trace_samples(0, start=reader.num_trace_samples(0) + 1)
def transform(in_filename, out_filename): with open(in_filename, 'rb') as in_file, \ open(out_filename, 'wb') as out_file: print('Loading SEG-Y file') segy_reader = create_reader(in_file) print('Calculation starts...') angle_to_rotate, averaged_tool_face = _measure_rotation_angle(segy_reader) np.savetxt(in_filename[:-4]+'_RotationAngle.txt', angle_to_rotate, fmt='%.2f') np.savetxt(in_filename[:-4]+'_ToolFace.txt', averaged_tool_face, fmt='%.2f') transformed_dataset = RotatedDataset(segy_reader, angle_to_rotate) print('Rotating and saving SEG-Y file') write_segy(out_file, transformed_dataset)
def transform(in_filename, out_filename, maximum_fb_time): with open(in_filename, 'rb') as in_file, \ open(out_filename, 'wb') as out_file: print('Loading SEG-Y file') segy_reader = create_reader(in_file) print('Calculation starts...') first_arrival_time = FBTime(segy_reader, maximum_fb_time) np.savetxt(in_filename[:-4] + '_FirstBreakTime.txt', first_arrival_time, fmt='%.2f') transformed_dataset = TimedDataset(segy_reader, first_arrival_time) print('Saving SEG-Y file') write_segy(out_file, transformed_dataset)
def test_round_trip(tmpdir, dataset): segy_file = str(tmpdir / 'test.segy') with open(segy_file, mode='bw') as fh: write_segy(fh, dataset) with open(segy_file, mode='br') as fh: reader = create_reader( fh, trace_header_format=dataset._trace_header_type, dimensionality=dataset.dimensionality) assert dataset.textual_reel_header == reader.textual_reel_header assert are_equal(dataset.binary_reel_header, reader.binary_reel_header) assert dataset.extended_textual_header == reader.extended_textual_header assert dataset.dimensionality == reader.dimensionality
def _dataset_round_trip(rootdir, dataset, compare_dimensionality=True): """Write `dataset` to a file in `rootdir`, read it back in, and compare the original with the loaded version. """ segy_file = str(rootdir / 'test.segy') with open(segy_file, mode='bw') as fh: write_segy(fh, dataset) with open(segy_file, mode='br') as fh: reader = create_reader( fh, trace_header_format=dataset._trace_header_format, dimensionality=dataset.dimensionality) _compare_datasets(dataset, reader, compare_dimensionality)
def test_type_error_on_non_reader_cache_file(self, dset, tempdir): segy_file = str(tempdir / 'test.segy') cache_dir = '.segy_cache' with open(segy_file, mode='wb') as fh: write_segy(fh, dset) with open(segy_file, mode='rb') as fh: create_reader(fh, cache_directory=cache_dir) cache_dir = tempdir / cache_dir cache_file = next(cache_dir.iterdir()) with open(str(cache_file), mode='wb') as fh: pickle.dump('not a reader', fh) with pytest.raises(TypeError): with open(segy_file, mode='rb') as fh: create_reader(fh, cache_directory=cache_dir)
def test_SegYReader3D_raises_TypeError_on_null_line_catalog(): dataset = diagonal_dataset_3d().example() fh = io.BytesIO() write_segy(fh, dataset) fh.seek(0) reader = create_reader(fh, dimensionality=2) fh.seek(0) with pytest.raises(TypeError): SegYReader3D(fh=fh, textual_reel_header=reader.textual_reel_header, binary_reel_header=reader.binary_reel_header, extended_textual_headers=reader._extended_textual_headers, trace_offset_catalog=reader._trace_offset_catalog, trace_length_catalog=reader._trace_length_catalog, line_catalog=None, trace_header_format=reader.trace_header_format_class, encoding=reader.encoding)
def test_reading_when_cache_dir_is_empty(self, dset, tempdir): segy_file = str(tempdir / 'test.segy') cache_dir = '.segy_cache' with open(segy_file, mode='wb') as fh: write_segy(fh, dset) with open(segy_file, mode='rb') as fh: create_reader(fh, cache_directory=cache_dir) cache_dir = tempdir / cache_dir for cache_file in cache_dir.iterdir(): cache_file.unlink() with open(segy_file, mode='rb') as fh: reader = create_reader(fh, dimensionality=dset.dimensionality, cache_directory=cache_dir) _compare_datasets(reader, dset)
def test_SegYReader3D_raises_TypeError_on_null_line_catalog(): dataset = diagonal_dataset_3d().example() fh = io.BytesIO() write_segy(fh, dataset) fh.seek(0) reader = create_reader(fh, dimensionality=2) fh.seek(0) with pytest.raises(TypeError): SegYReader3D( fh=fh, textual_reel_header=reader.textual_reel_header, binary_reel_header=reader.binary_reel_header, extended_textual_headers=reader._extended_textual_headers, trace_offset_catalog=reader._trace_offset_catalog, trace_length_catalog=reader._trace_length_catalog, line_catalog=None, trace_header_format=reader.trace_header_format_class, encoding=reader.encoding)
def test_TypeError_unpickling_cache_with_missing_segy_file(dset, tempdir): segy_file = tempdir / 'segy' cache_dir = 'cache_dir' with open(str(segy_file), mode='wb') as fh: write_segy(fh, dset) # Force creation of cache with open(str(segy_file), mode='rb') as fh: create_reader(fh, cache_directory=cache_dir) # Delete the segy file segy_file.unlink() # Try to unpickle the cache cache_path = tempdir / cache_dir cache_file = next(cache_path.iterdir()) with open(str(cache_file), mode='rb') as fh: with pytest.raises(TypeError): pickle.load(fh)
unassigned_3 = field( Int16, offset=237, default=0, documentation="Unassigned 3") unassigned_4 = field( Int16, offset=239, default=0, documentation="Unassigned 4") in_filename = "data/rth.segy" out_filename = "data/rth_out2.segy" in_file = open(in_filename, 'rb') with open(out_filename, 'wb') as out_file: segy_reader_in = create_reader(in_file, trace_header_format=CustomTraceHeader) write_segy(out_file, segy_reader_in, trace_header_format=CustomTraceHeader) out_file = open(out_filename, 'rb') segy_reader_out = create_reader(out_file, trace_header_format=CustomTraceHeader) for trace_index in segy_reader_in.trace_indexes(): trace_offset = segy_reader_in._trace_offset_catalog[trace_index] print(trace_index, hex(trace_offset)) head0 = segy_reader_in.trace_header(trace_index) head1 = segy_reader_out.trace_header(trace_index) assert are_equal(head0, head1), "Error {}".format(trace_index) data0 = segy_reader_in.trace_samples(trace_index) data1 = segy_reader_out.trace_samples(trace_index) assert data0 == data1
def test_SegYReader_endianness_is_correct(dset, endian): fh = io.BytesIO() write_segy(fh, dset, endian=endian) fh.seek(0) reader = create_reader(fh, endian=endian) assert reader.endian == endian
def test_inline_xline_numbers_is_correct(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) assert sorted(reader.inline_xline_numbers()) == sorted((idx, idx) for idx in range(dset.num_traces()))
def test_num_xlines_is_correct(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh) assert reader.num_xlines() == dset.num_traces()
def test_heuristic_for_3D_works_as_expected(dset): fh = io.BytesIO() write_segy(fh, dset) fh.seek(0) reader = create_reader(fh, dimensionality=None) assert reader.dimensionality == 3
def test_create_reader_will_default_to_ascii_encoding(dset): fh = io.BytesIO() write_segy(fh, dset) reader = create_reader(fh, encoding=None) assert reader.encoding == 'ascii'