def test_dfsr_invalid_tell(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'dfsr-prerequisites.lis') content = headers + [ 'data/lis/records/curves/dfsr-simple.lis.part', 'data/lis/records/curves/fdata-simple.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] fpath = os.path.join(str(tmpdir), 'dfsr-invalid-tell.lis') content = headers + [ 'data/lis/records/curves/fdata-simple.lis.part', 'data/lis/records/curves/fdata-simple.lis.part', 'data/lis/records/curves/dfsr-simple.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): with pytest.raises(ValueError) as exc: _ = lis.curves(f, dfs) assert "Could not find DFS record at tell" in str(exc.value)
def test_entries(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'entries.lis') content = headers + [ 'data/lis/records/curves/dfsr-entries-defined.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] assert len(dfs.entries) == 17 # TODO: better interface, now user doesn't know what entries mean entries = {entry.type: entry.value for entry in dfs.entries} assert entries[0] == 60 assert entries[1] == 0 assert entries[2] == 0 assert entries[3] == 32 assert entries[4] == 0 #"none" assert entries[5] == 255 #"meters" assert entries[6] == -4 assert entries[7] == "INCH" assert entries[8] == 60 assert entries[9] == "INCH" assert entries[10] == "UNDEFINE" #field undefined, should be missing assert entries[11] == 8 assert entries[12] == -15988.0 assert entries[13] == 1 #True assert entries[14] == "INCH" assert entries[15] == 73 #reprc_int_32 assert entries[16] == 1
def test_fdata_repcodes_fixed_size(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'fdata-repcodes-fixed.lis') content = headers + [ 'data/lis/records/curves/dfsr-repcodes-fixed.lis.part', 'data/lis/records/curves/fdata-repcodes-fixed.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] fmt = core.dfs_formatstring(dfs) assert fmt == 'bsilefrp' curves = lis.curves(f, dfs) assert curves['BYTE'] == [89] assert curves['I8 '] == [-128] assert curves['I16 '] == [153] assert curves['I32 '] == [-153] assert curves['F16 '] == [1.0] assert curves['F32 '] == [-1.0] assert curves['F32L'] == [-0.25] assert curves['F32F'] == [153.25]
def test_dfsr_subtype1(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'dfsr-subtype1.lis') content = headers + [ 'data/lis/records/curves/dfsr-subtype1.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] ch1 = dfs.specs[0] assert ch1.mnemonic == "CH01" assert ch1.service_id == "testCH" assert ch1.service_order_nr == " 1234 " assert ch1.units == "INCH" #assert ch1.api_codes == "45310011" #unsupported assert ch1.filenr == 1 assert ch1.reserved_size == 4 assert ch1.samples == 1 assert ch1.reprc == 73 #should this be int32 instead? #assert ch1.process_indicators == "FF FF FF FF FF" #unsupported curves = lis.curves(f, dfs) assert len(curves) == 0
def test_entries_default_values(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'entries-default.lis') content = headers + [ 'data/lis/records/curves/dfsr-entries-default.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] assert len(dfs.entries) == 17 entries = {entry.type: entry.value for entry in dfs.entries} assert entries[0] == 60 assert entries[1] == 0 assert entries[2] == 0 assert entries[3] == None assert entries[4] == 1 #"up" assert entries[5] == 1 #"feet" assert entries[6] == None assert entries[7] == ".1IN" assert entries[8] == None assert entries[9] == ".1IN" assert entries[10] == None #field undefined, should be missing assert entries[11] == None #assert entries[12] == -999.25 or None: unknown assert entries[13] == 0 #False assert entries[14] == ".1IN" assert entries[15] == None assert entries[16] == 0
def test_inforec_empty(): path = 'data/lis/records/inforec_06.lis' f, = lis.load(path) wellsite = f.wellsite_data()[0] assert wellsite.isstructured() == False assert len(wellsite.components()) == 0
def test_fdata_fast_channel_depth(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'fast-channel-depth.lis') content = headers + [ 'data/lis/records/curves/dfsr-fast-depth.lis.part', 'data/lis/records/curves/fdata-fast-depth-1.lis.part', 'data/lis/records/curves/fdata-fast-depth-2.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] with pytest.raises(RuntimeError) as exc: _ = lis.curves(f, dfs) assert "please explicitly specify which to read" in str(exc.value) # Curves with sampling rate x1 the index. curves = lis.curves(f, dfs, sample_rate=1) expected = np.array([1, 3, 5, 7]) np.testing.assert_array_equal(curves['DEPT'], expected) expected = np.array([4, 7, 10, 13]) np.testing.assert_array_equal(curves['CH02'], expected) # Curves with sampling rate x2 the index. curves = lis.curves(f, dfs, sample_rate=2) expected = np.array([0, 1, 2, 3, 4, 5, 6, 7]) np.testing.assert_array_equal(curves['DEPT'], expected) expected = np.array([2, 3, 5, 6, 8, 9, 11, 12]) np.testing.assert_array_equal(curves['CH01'], expected)
def test_entries_size_0_values(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'entries-size-0.lis') content = headers + [ 'data/lis/records/curves/dfsr-entries-size-0.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] assert len(dfs.entries) == 16 entries = {entry.type: entry.value for entry in dfs.entries} assert entries[0] == None #defined with size 0 assert dfs.record_type == None assert dfs.spec_block_type == None assert dfs.frame_size == None assert dfs.direction == None assert dfs.optical_log_depth_units == None assert dfs.reference_point == None assert dfs.reference_point_units == None assert dfs.spacing == None assert dfs.spacing_units == None assert dfs.max_frames == None assert dfs.absent_value == None assert dfs.depth_mode == None assert dfs.depth_units == None assert dfs.depth_reprc == None assert dfs.spec_block_subtype == None
def test_inforec_unstructured(filename): # inforec_03.lis and inforec_04.lis are identical except for the type_nb # field in the CBs. In 03 all CB's have type_nb==0, in 04 the value of # type_nb vary. Regardless, none of them are table records and should # behave the same. path = 'data/lis/records/' + filename f, = lis.load(path) wellsite = f.wellsite_data()[0] components = wellsite.components() assert wellsite.isstructured() == False assert len(components) == 15 expected = [ 'WN ', 'ALLO', ' ', ' ', '15/9-F-15 ', 'CN ', 'ALLO', ' ', ' ', 'StatoilHydro', 'SRVC', 'ALLO', ' ', ' ', 'Geoservices ' ] for i, cb in enumerate(components): assert cb.component == expected[i] with pytest.raises(ValueError): _ = wellsite.table() with pytest.raises(ValueError): _ = wellsite.table_name()
def test_fdata_fast_channel_dimensional(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'fast-channel-dimensional.lis') content = headers + [ 'data/lis/records/curves/dfsr-fast-dimensional.lis.part', 'data/lis/records/curves/fdata-fast-dimensional.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] curves = lis.curves(f, dfs, sample_rate=1) expected = np.array([1, 9]) np.testing.assert_array_equal(curves['CH01'], expected) expected = np.array([8, 16]) np.testing.assert_array_equal(curves['CH03'], expected) # Curves with sampling rate x2 the index. curves = lis.curves(f, dfs, sample_rate=2) expected = np.array([0, 1, 5, 9]) np.testing.assert_array_equal(curves['CH01'], expected) expected = np.array([[2, 3, 4], [5, 6, 7], [10, 11, 12], [13, 14, 15]]) np.testing.assert_array_equal(curves['CH02'], expected)
def test_entries(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'entries.lis') content = headers + [ 'data/lis/records/curves/dfsr-entries-defined.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] assert len(dfs.entries) == 17 entries = {entry.type: entry.value for entry in dfs.entries} assert entries[0] == 60 # Terminator, no interface defined assert entries[10] == "UNDEFINE" # field undefined, should be missing assert dfs.record_type == 0 assert dfs.spec_block_type == 0 assert dfs.frame_size == 32 assert dfs.direction == 0 #"none" assert dfs.optical_log_depth_units == 255 #"meters" assert dfs.reference_point == -4 assert dfs.reference_point_units == "INCH" assert dfs.spacing == 60 assert dfs.spacing_units == "INCH" assert dfs.max_frames == 8 assert dfs.absent_value == -15988.0 assert dfs.depth_mode == 1 #True assert dfs.depth_units == "INCH" assert dfs.depth_reprc == 73 #reprc_int_32 assert dfs.spec_block_subtype == 1
def test_fdata_repcodes_fixed_size(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'fdata-repcodes-fixed.lis') content = headers + [ 'data/lis/records/curves/dfsr-repcodes-fixed.lis.part', 'data/lis/records/curves/fdata-repcodes-fixed.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] indexfmt, fmt = dfsr_fmtstr(dfs, sample_rate=1) assert indexfmt == 'b1' assert fmt == 's1i1l1e1f1r1p1' curves = lis.curves(f, dfs) assert curves['BYTE'] == [89] assert curves['I8 '] == [-128] assert curves['I16 '] == [153] assert curves['I32 '] == [-153] assert curves['F16 '] == [1.0] assert curves['F32 '] == [-1.0] assert curves['F32L'] == [-0.25] assert curves['F32F'] == [153.25]
def test_fixed_value_encoding(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'encoded-fixed-value.dlis') content = [ 'data/lis/records/RHLR-1.lis.part', 'data/lis/records/THLR-1.lis.part', 'data/lis/records/FHLR-1.lis.part', 'data/lis/records/FTLR-1.lis.part', 'data/lis/records/TTLR-encoded.lis.part', 'data/lis/records/RTLR-1.lis.part', ] merge_lis_prs(fpath, content) prev_encodings = dlisio.common.get_encodings() dlisio.common.set_encodings([]) try: f, = lis.load(fpath) trailer = f.tape.trailer() with pytest.warns(UnicodeWarning): assert trailer.name == b'\xec\xc5\xce\xd4\xc1\x30\x30\x31' dlisio.common.set_encodings(['koi8_r']) trailer = f.tape.trailer() assert trailer.name == 'Лента001' finally: dlisio.common.set_encodings(prev_encodings) f.close()
def test_inforec_cut_in_component(): path = 'data/lis/records/inforec-cut-in-value.lis.part' f, = lis.load(path) with pytest.raises(RuntimeError) as exc: _ = f.wellsite_data() assert "2 bytes left in record, expected at least 4" in str(exc.value)
def test_inforec_cut_in_fixed_size(): path = 'data/lis/records/inforec-cut-in-fixed.lis.part' f, = lis.load(path) with pytest.raises(RuntimeError) as exc: _ = f.wellsite_data() assert "lis::component_block: 10 bytes left in record" in str(exc.value)
def test_dfsr_subtype0(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'dfsr-subtype0.lis') content = headers + [ 'data/lis/records/curves/dfsr-subtype0.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] ch1 = dfs.specs[0] assert ch1.mnemonic == "CH01" assert ch1.service_id == "testCH" assert ch1.service_order_nr == " 1234 " assert ch1.units == "INCH" assert ch1.api_log_type == 2 assert ch1.api_curve_type == 179 assert ch1.api_curve_class == 96 assert ch1.api_modifier == 59 assert ch1.filenr == 1 assert ch1.reserved_size == 4 assert ch1.process_level == 255 assert ch1.samples == 1 assert ch1.reprc == 73 curves = lis.curves(f, dfs) assert len(curves) == 0
def test_fdata_fast_channel_ints(tmpdir, merge_lis_prs): fpath = os.path.join(str(tmpdir), 'fast-channel-ints.lis') content = headers + [ 'data/lis/records/curves/dfsr-fast-int.lis.part', 'data/lis/records/curves/fdata-fast-int.lis.part', ] + trailers merge_lis_prs(fpath, content) with lis.load(fpath) as (f, ): dfs = f.data_format_specs()[0] with pytest.raises(RuntimeError) as exc: _ = lis.curves(f, dfs) assert "Multiple sampling rates in file" in str(exc.value) curves = lis.curves(f, dfs, sample_rate=1) assert curves.dtype == np.dtype([('CH01', 'i4'), ('CH03', 'i4')]) expected = np.array([1, 5]) np.testing.assert_array_equal(curves['CH01'], expected) expected = np.array([4, 8]) np.testing.assert_array_equal(curves['CH03'], expected) curves = lis.curves(f, dfs, sample_rate=2) assert curves.dtype == np.dtype([('CH01', 'i4'), ('CH02', 'i2')]) expected = np.array([0, 1, 3, 5]) np.testing.assert_array_equal(curves['CH01'], expected) expected = np.array([2, 3, 6, 7]) np.testing.assert_array_equal(curves['CH02'], expected)
def test_dfsr_fmtstring(): path = 'data/lis/MUD_LOG_1.LIS' with lis.load(path) as (lf, *tail): dfsr = lf.data_format_specs()[0] fmt = core.dfs_formatstring(dfsr) assert fmt == 'f' * 44
def test_dfsr_fmtstring(): path = 'data/lis/MUD_LOG_1.LIS' with lis.load(path) as (_, lf, _): dfsr = lf.dfsr()[0] fmt = core.dfs_formatstring(dfsr) assert fmt == 'D' * 44
def test_filehandles_closed(tmpdir): # Copy the test file to a tmpdir in order to make this test reliable. tmp = str(tmpdir.join('file')) shutil.copyfile('data/lis/layouts/layout_tif_01.lis', tmp) with lis.load(tmp) as files: assert len(files) == 4 os.remove(tmp)
def test_inforec_table_name(): path = 'data/lis/records/inforec_01.lis' f, = lis.load(path) wellsite = f.wellsite_data()[0] assert wellsite.isstructured() name = wellsite.table_name() assert name == 'CONS'
def test_filehandles_closed(tmpdir): # Copy the test file to a tmpdir in order to make this test reliable. tmp = str(tmpdir.join('file')) shutil.copyfile('data/lis/layouts/layout_tif_01.lis', tmp) load_failure_escape = str(tmpdir.join('truncated.lis')) shutil.copyfile('data/lis/layouts/truncated_15.lis', load_failure_escape) with lis.load(tmp) as files: assert len(files) == 4 errorhandler = common.ErrorHandler(critical=common.Actions.LOG_ERROR) # file is truncated, but 2 LFs were already processed successfully with lis.load(load_failure_escape, error_handler=errorhandler) as files: assert len(files) == 2 os.remove(tmp) os.remove(load_failure_escape)
def f(fpath): prev_encodings = dlisio.common.get_encodings() dlisio.common.set_encodings([]) try: f, = lis.load(fpath) yield f finally: f.close() dlisio.common.set_encodings(prev_encodings)
def test_dfsr_fmtstring(): path = 'data/lis/MUD_LOG_1.LIS' with lis.load(path) as (lf, *tail): dfsr = lf.data_format_specs()[0] indexfmt, fmt = dfsr_fmtstr(dfsr, sample_rate=1) assert indexfmt == 'f1' assert fmt == 'f1' * 43
def test_missing_ftlr(tmpdir, merge_lis_prs, assert_info): # A file with missing delimiter: # # file.lis # | # |->Reel 1 # | |-> Tape 1 # | |-> Logical File 1 # |->Reel 2 # |-> Tape 2 # |-> Logical File 2 # fpath = os.path.join(str(tmpdir), 'missing_first_rh.dlis') content = [ 'data/lis/records/RHLR-1.lis.part', 'data/lis/records/THLR-1.lis.part', 'data/lis/records/FHLR-1.lis.part', # Missing first FTLR 'data/lis/records/TTLR-1.lis.part', 'data/lis/records/RTLR-1.lis.part', 'data/lis/records/RHLR-2.lis.part', 'data/lis/records/THLR-2.lis.part', 'data/lis/records/FHLR-2.lis.part', 'data/lis/records/FTLR-2.lis.part', 'data/lis/records/TTLR-2.lis.part', 'data/lis/records/RTLR-2.lis.part', ] merge_lis_prs(fpath, content) with lis.load(fpath) as (f1, f2, *tail): assert len(tail) == 0 # Check that f1, f2 is partitioned correctly by investigating their # FHLR/FTLR. # Check that f1, f2 "belongs" to the correct Reel and Tape by # investigating the assigned RHLR/RTLR and THLR/TTLR. The fields # checked are arbitrary as long as they can uniquely identify the # record. # f1 belongs to Reel 1 and Tape 1 assert f1.header().file_name == 'LISTST.001' assert f1.trailer() == None assert_info('No File Trailer Logical Record') print(f1.explicits()) assert f1.tape.header().prev_tape_name == ' ' * 8 assert f1.tape.trailer().next_tape_name == 'Tape0002' assert f1.reel.header().prev_reel_name == ' ' * 8 assert f1.reel.trailer().next_reel_name == 'Reel5678' # f2 belongs to Reel 2 and Tape 2 assert f2.header().file_name == 'LISTST.002' assert f2.trailer().file_name == 'LISTST.002' assert f2.tape.header().prev_tape_name == 'Tape0001' assert f2.tape.trailer().next_tape_name == ' ' * 8 assert f2.reel.header().prev_reel_name == 'Reel1234' assert f2.reel.trailer().next_reel_name == ' ' * 8
def test_filehandles_closed_when_load_fails(tmpdir, merge_lis_prs, assert_error): # majority of exceptions in load are hard to invoke, so they are not tested empty = os.path.join(str(tmpdir), 'empty.lis') merge_lis_prs(empty, []) truncated = str(tmpdir.join('truncated.lis')) shutil.copyfile('data/lis/layouts/truncated_15.lis', truncated) with pytest.raises(OSError): _ = lis.load(empty) # file is truncated, but 2 LFs was already processed successfully with lis.load(truncated) as files: assert len(files) == 2 # TODO: update with explicit raise here as well. assert_error("Indexing failed") os.remove(empty) os.remove(truncated)
def test_tape_trailer(): with lis.load('data/lis/MUD_LOG_1.LIS') as (f, *tail): tape = f.tape.trailer() assert tape.service_name == ' ' * 6 assert tape.date == ' ' * 8 assert tape.origin_of_data == ' ' * 4 assert tape.name == 'Geotape ' assert tape.continuation_number == '01' assert tape.next_tape_name == ' ' * 8 assert tape.comment == ' ' * 74
def test_dfsr_dtype(): path = 'data/lis/MUD_LOG_1.LIS' with lis.load(path) as (lf, *tail): dfsr = lf.data_format_specs()[0] dtype = dfsr_dtype(dfsr, sample_rate=1) expected = np.dtype([(ch.mnemonic, np.float32) for ch in dfsr.specs]) assert dtype == expected
def test_file_header(): with lis.load('data/lis/MUD_LOG_1.LIS') as (f, *tail): header = f.header() assert header.file_name == 'LIS1 .001' assert header.service_sublvl_name == ' ' * 6 assert header.version_number == ' ' * 8 assert header.date_of_generation == ' ' * 8 assert header.max_pr_length == ' 1024' assert header.file_type == ' ' * 2 assert header.prev_file_name == ' ' * 10
def test_dfsr_dtype(): path = 'data/lis/MUD_LOG_1.LIS' with lis.load(path) as (_, lf, _): dfsr = lf.dfsr()[0] dtype = lis.dfsr_dtype(dfsr) expected = np.dtype([(ch.mnemonic, np.float32) for ch in dfsr.specs]) assert dtype == expected