Skip to content

Commit d9db029

Browse files
committed
Fix structured array forcing endian re-conversion
1 parent 826cc43 commit d9db029

File tree

1 file changed

+71
-16
lines changed

1 file changed

+71
-16
lines changed

tests/unit/test_disaster_recovery_wrapper.py

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def extract_header_bytes_from_file(
123123
header_offset = 3600
124124

125125
# Each trace: 240 byte header + samples
126-
trace_size = 240 + 1501 * 4 # Assuming 1501 samples, 4 bytes each
126+
# trace_size = 240 + 1501 * 4 # Assuming 1501 samples, 4 bytes each
127+
trace_size = 240 + 1 * 4 # Assuming 1 sample, 4 bytes each
127128
trace_offset = header_offset + trace_index * trace_size
128129

129130
f.seek(trace_offset + byte_start - 1) # SEGY is 1-based
@@ -143,7 +144,8 @@ def test_header_validation_configurations(
143144

144145
# Create test SEGY file
145146
num_traces = 10
146-
samples_per_trace = 1501
147+
# samples_per_trace = 1501
148+
samples_per_trace = 1
147149

148150
spec = self.create_test_segy_file(
149151
spec=basic_segy_spec,
@@ -176,14 +178,20 @@ def test_header_validation_configurations(
176178
segy_path, trace_idx, 193, 4
177179
)
178180

181+
def extract_bytes_vectorized(data, start_byte, end_byte):
182+
all_bytes = np.frombuffer(data.tobytes(), dtype=np.uint8)
183+
bytes_per_element = data.dtype.intemsize
184+
reshaped= all_bytes.reshape(-1, bytes_per_element)
185+
return reshaped[:, start_byte:end_byte]
186+
179187
# Convert raw headers to bytes for comparison
180188
if raw_headers is not None:
181189
# Extract inline and crossline from raw headers
182-
raw_inline_bytes = np.frombuffer(
183-
raw_headers["inline"].tobytes(), dtype=np.uint8
190+
raw_inline_bytes = extract_bytes_vectorized(
191+
raw_headers, 189, 193
184192
)[:4]
185-
raw_crossline_bytes = np.frombuffer(
186-
raw_headers["crossline"].tobytes(), dtype=np.uint8
193+
raw_crossline_bytes = extract_bytes_vectorized(
194+
raw_headers, 193, 197
187195
)[:4]
188196

189197
print(f"Transformed headers: {transformed_headers.tobytes()}")
@@ -209,7 +217,8 @@ def test_header_validation_no_transforms(
209217

210218
# Create test SEGY file
211219
num_traces = 5
212-
samples_per_trace = 1501
220+
# samples_per_trace = 1501
221+
samples_per_trace = 1
213222

214223
spec = self.create_test_segy_file(
215224
spec=basic_segy_spec,
@@ -241,15 +250,24 @@ def test_multiple_traces_validation(
241250
self, temp_dir: Path, basic_segy_spec: SegySpec, segy_config: dict
242251
) -> None:
243252
"""Test validation with multiple traces at once."""
253+
if True:
254+
import segy
255+
print(segy.__version__)
244256
config_name = segy_config["name"]
245257
endianness = segy_config["endianness"]
246258
data_format = segy_config["data_format"]
247259

248-
segy_path = temp_dir / f"test_multiple_traces_{config_name}.segy"
260+
print(f"Config name: {config_name}")
261+
print(f"Endianness: {endianness}")
262+
print(f"Data format: {data_format}")
263+
264+
# segy_path = temp_dir / f"test_multiple_traces_{config_name}.segy"
265+
segy_path = Path(f"test_multiple_traces_{config_name}.segy")
249266

250267
# Create test SEGY file with more traces
251268
num_traces = 25 # 5x5 grid
252-
samples_per_trace = 1501
269+
# samples_per_trace = 1501
270+
samples_per_trace = 1
253271

254272
spec = self.create_test_segy_file(
255273
spec=basic_segy_spec,
@@ -272,6 +290,8 @@ def test_multiple_traces_validation(
272290
do_reverse_transforms=True
273291
)
274292

293+
first = True
294+
275295
# Validate each trace
276296
for trace_idx in range(num_traces):
277297
# Extract bytes from disk
@@ -282,13 +302,47 @@ def test_multiple_traces_validation(
282302
segy_path, trace_idx, 193, 4
283303
)
284304

305+
if first:
306+
print(raw_headers.dtype)
307+
print(raw_headers.shape)
308+
first = False
309+
285310
# Extract from raw headers
286-
raw_inline_bytes = np.frombuffer(
287-
raw_headers["inline"][trace_idx].tobytes(), dtype=np.uint8
288-
)[:4]
289-
raw_crossline_bytes = np.frombuffer(
290-
raw_headers["crossline"][trace_idx].tobytes(), dtype=np.uint8
291-
)[:4]
311+
# Note: We need to extract bytes directly from the structured array to preserve endianness
312+
# Getting a scalar and calling .tobytes() loses endianness information
313+
if raw_headers.ndim == 0:
314+
# Single trace case
315+
raw_data_bytes = raw_headers.tobytes()
316+
inline_offset = raw_headers.dtype.fields['inline'][1]
317+
crossline_offset = raw_headers.dtype.fields['crossline'][1]
318+
inline_size = raw_headers.dtype.fields['inline'][0].itemsize
319+
crossline_size = raw_headers.dtype.fields['crossline'][0].itemsize
320+
321+
raw_inline_bytes = np.frombuffer(
322+
raw_data_bytes[inline_offset:inline_offset+inline_size], dtype=np.uint8
323+
)
324+
raw_crossline_bytes = np.frombuffer(
325+
raw_data_bytes[crossline_offset:crossline_offset+crossline_size], dtype=np.uint8
326+
)
327+
else:
328+
# Multiple traces case
329+
raw_data_bytes = raw_headers[trace_idx:trace_idx+1].tobytes()
330+
inline_offset = raw_headers.dtype.fields['inline'][1]
331+
crossline_offset = raw_headers.dtype.fields['crossline'][1]
332+
inline_size = raw_headers.dtype.fields['inline'][0].itemsize
333+
crossline_size = raw_headers.dtype.fields['crossline'][0].itemsize
334+
335+
raw_inline_bytes = np.frombuffer(
336+
raw_data_bytes[inline_offset:inline_offset+inline_size], dtype=np.uint8
337+
)
338+
raw_crossline_bytes = np.frombuffer(
339+
raw_data_bytes[crossline_offset:crossline_offset+crossline_size], dtype=np.uint8
340+
)
341+
342+
print(f"Raw inline bytes: {raw_inline_bytes.tobytes()}")
343+
print(f"Inline bytes disk: {inline_bytes_disk.tobytes()}")
344+
print(f"Raw crossline bytes: {raw_crossline_bytes.tobytes()}")
345+
print(f"Crossline bytes disk: {crossline_bytes_disk.tobytes()}")
292346

293347
# Compare
294348
assert np.array_equal(raw_inline_bytes, inline_bytes_disk), \
@@ -313,7 +367,8 @@ def test_different_index_types(
313367

314368
# Create test SEGY file
315369
num_traces = 10
316-
samples_per_trace = 1501
370+
# samples_per_trace = 1501
371+
samples_per_trace = 1
317372

318373
spec = self.create_test_segy_file(
319374
spec=basic_segy_spec,

0 commit comments

Comments
 (0)