|
12 | 12 | from segy.config import SegySettings |
13 | 13 | from segy.standards.codes import MeasurementSystem as segy_MeasurementSystem |
14 | 14 | from segy.standards.fields.trace import Rev0 as TraceHeaderFieldsRev0 |
| 15 | +from segy.schema import HeaderField |
| 16 | +from segy.schema import ScalarType as ScalarType2 |
15 | 17 |
|
16 | 18 | from mdio.api.io import _normalize_path |
17 | 19 | from mdio.api.io import to_mdio |
@@ -340,6 +342,45 @@ def _add_grid_override_to_metadata(dataset: Dataset, grid_overrides: dict[str, A |
340 | 342 | if grid_overrides is not None: |
341 | 343 | dataset.metadata.attributes["gridOverrides"] = grid_overrides |
342 | 344 |
|
| 345 | +def _scalar_to_size(scalar: ScalarType2) -> int: |
| 346 | + if scalar == ScalarType2.UINT8: |
| 347 | + return 1 |
| 348 | + elif scalar == ScalarType2.UINT16: |
| 349 | + return 2 |
| 350 | + elif scalar == ScalarType2.UINT32: |
| 351 | + return 4 |
| 352 | + elif scalar == ScalarType2.UINT64: |
| 353 | + return 8 |
| 354 | + elif scalar == ScalarType2.INT8: |
| 355 | + return 1 |
| 356 | + elif scalar == ScalarType2.INT16: |
| 357 | + return 2 |
| 358 | + elif scalar == ScalarType2.INT32: |
| 359 | + return 4 |
| 360 | + elif scalar == ScalarType2.INT64: |
| 361 | + return 8 |
| 362 | + elif scalar == ScalarType2.FLOAT32: |
| 363 | + return 4 |
| 364 | + elif scalar == ScalarType2.FLOAT64: |
| 365 | + return 8 |
| 366 | + elif scalar == ScalarType2.FLOAT16: |
| 367 | + return 2 |
| 368 | + elif scalar == ScalarType2.STRING8: |
| 369 | + return 8 |
| 370 | + else: |
| 371 | + raise ValueError(f"Invalid scalar type: {scalar}") |
| 372 | + |
| 373 | +def _customize_segy_spec(segy_spec: SegySpec) -> SegySpec: |
| 374 | + assigned_bytes = [] |
| 375 | + for field in segy_spec.trace.header.fields: |
| 376 | + byte = field.byte-1 |
| 377 | + for i in range(byte, byte + _scalar_to_size(field.format)): |
| 378 | + assigned_bytes.append(i) |
| 379 | + unassigned_bytes = [i for i in range(240) if i not in assigned_bytes] |
| 380 | + field_to_customize = [HeaderField(name=f"Field_{i}", format=ScalarType.UINT8, byte=i+1) for i in unassigned_bytes] |
| 381 | + segy_spec = segy_spec.customize(trace_header_fields=field_to_customize) |
| 382 | + return segy_spec |
| 383 | + |
343 | 384 |
|
344 | 385 | def _add_raw_headers_to_template(mdio_template: AbstractDatasetTemplate) -> AbstractDatasetTemplate: |
345 | 386 | """Add raw headers capability to the MDIO template by monkey-patching its _add_variables method. |
@@ -421,6 +462,9 @@ def segy_to_mdio( # noqa PLR0913 |
421 | 462 | input_path = _normalize_path(input_path) |
422 | 463 | output_path = _normalize_path(output_path) |
423 | 464 |
|
| 465 | + if os.getenv("MDIO__DO_RAW_HEADERS") == "1": |
| 466 | + segy_spec = _customize_segy_spec(segy_spec) |
| 467 | + |
424 | 468 | if not overwrite and output_path.exists(): |
425 | 469 | err = f"Output location '{output_path.as_posix()}' exists. Set `overwrite=True` if intended." |
426 | 470 | raise FileExistsError(err) |
|
0 commit comments