|
| 1 | +"""Coordinate extraction and unit resolution for SEG-Y ingestion.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from segy.standards.codes import MeasurementSystem as SegyMeasurementSystem |
| 10 | +from segy.standards.fields import binary as binary_header_fields |
| 11 | + |
| 12 | +from mdio.builder.schemas.v1.units import AngleUnitEnum |
| 13 | +from mdio.builder.schemas.v1.units import AngleUnitModel |
| 14 | +from mdio.builder.schemas.v1.units import LengthUnitEnum |
| 15 | +from mdio.builder.schemas.v1.units import LengthUnitModel |
| 16 | +from mdio.ingestion.coordinates import populate_dim_coordinates |
| 17 | +from mdio.ingestion.coordinates import populate_non_dim_coordinates |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from segy.arrays import HeaderArray as SegyHeaderArray |
| 21 | + from xarray import Dataset as xr_Dataset |
| 22 | + |
| 23 | + from mdio.builder.templates.base import AbstractDatasetTemplate |
| 24 | + from mdio.core.dimension import Dimension |
| 25 | + from mdio.core.grid import Grid |
| 26 | + from mdio.segy.file import SegyFileInfo |
| 27 | + |
| 28 | +logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +MEASUREMENT_SYSTEM_KEY = binary_header_fields.Rev0.MEASUREMENT_SYSTEM_CODE.model.name |
| 32 | +ANGLE_UNIT_KEYS = ["angle", "azimuth"] |
| 33 | +SPATIAL_UNIT_KEYS = [ |
| 34 | + "cdp_x", |
| 35 | + "cdp_y", |
| 36 | + "source_coord_x", |
| 37 | + "source_coord_y", |
| 38 | + "group_coord_x", |
| 39 | + "group_coord_y", |
| 40 | + "offset", |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +def _get_coordinates( |
| 45 | + grid: Grid, |
| 46 | + segy_headers: SegyHeaderArray, |
| 47 | + mdio_template: AbstractDatasetTemplate, |
| 48 | +) -> tuple[list[Dimension], dict[str, SegyHeaderArray]]: |
| 49 | + """Get the data dim and non-dim coordinates from the SEG-Y headers and MDIO template. |
| 50 | +
|
| 51 | + Select a subset of the segy_dimensions that corresponds to the MDIO dimensions |
| 52 | + The dimensions are ordered as in the MDIO template. |
| 53 | + The last dimension is always the vertical domain dimension |
| 54 | +
|
| 55 | + Args: |
| 56 | + grid: Inferred MDIO grid for SEG-Y file. |
| 57 | + segy_headers: Headers read in from SEG-Y file. |
| 58 | + mdio_template: The MDIO template to use for the conversion. |
| 59 | +
|
| 60 | + Returns: |
| 61 | + A tuple containing: |
| 62 | + - A list of dimension coordinates (1-D arrays). |
| 63 | + - A dict of non-dimension coordinates (str: N-D arrays). |
| 64 | + """ |
| 65 | + dimensions_coords = [grid.select_dim(name) for name in mdio_template.dimension_names] |
| 66 | + non_dim_coords = {name: np.array(segy_headers[name]) for name in mdio_template.coordinate_names} |
| 67 | + return dimensions_coords, non_dim_coords |
| 68 | + |
| 69 | + |
| 70 | +def _populate_coordinates( |
| 71 | + dataset: xr_Dataset, |
| 72 | + grid: Grid, |
| 73 | + coords: dict[str, SegyHeaderArray], |
| 74 | + spatial_coordinate_scalar: int, |
| 75 | +) -> tuple[xr_Dataset, list[str]]: |
| 76 | + """Populate dim and non-dim coordinates in the xarray dataset and write to Zarr. |
| 77 | +
|
| 78 | + This will write the xr Dataset with coords and dimensions, but empty traces and headers. |
| 79 | +
|
| 80 | + Args: |
| 81 | + dataset: The xarray dataset to populate. |
| 82 | + grid: The grid object containing the grid map. |
| 83 | + coords: The non-dim coordinates to populate. |
| 84 | + spatial_coordinate_scalar: The X/Y coordinate scalar from the SEG-Y file. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + Xarray dataset with filled coordinates and updated variables to drop after writing |
| 88 | + """ |
| 89 | + drop_vars_delayed = [] |
| 90 | + dataset, drop_vars_delayed = populate_dim_coordinates(dataset, grid, drop_vars_delayed=drop_vars_delayed) |
| 91 | + |
| 92 | + dataset, drop_vars_delayed = populate_non_dim_coordinates( |
| 93 | + dataset, |
| 94 | + grid, |
| 95 | + coordinates=coords, |
| 96 | + drop_vars_delayed=drop_vars_delayed, |
| 97 | + spatial_coordinate_scalar=spatial_coordinate_scalar, |
| 98 | + ) |
| 99 | + |
| 100 | + return dataset, drop_vars_delayed |
| 101 | + |
| 102 | + |
| 103 | +def _get_spatial_coordinate_unit(segy_file_info: SegyFileInfo) -> LengthUnitModel | None: |
| 104 | + """Get the coordinate unit from the SEG-Y headers.""" |
| 105 | + measurement_system_code = int(segy_file_info.binary_header_dict[MEASUREMENT_SYSTEM_KEY]) |
| 106 | + |
| 107 | + if measurement_system_code not in {s.value for s in SegyMeasurementSystem}: |
| 108 | + logger.warning( |
| 109 | + "Unexpected value in coordinate unit (%s) header: %s. Can't extract coordinate unit and will " |
| 110 | + "ingest without coordinate units.", |
| 111 | + MEASUREMENT_SYSTEM_KEY, |
| 112 | + measurement_system_code, |
| 113 | + ) |
| 114 | + return None |
| 115 | + |
| 116 | + if measurement_system_code == SegyMeasurementSystem.METERS: |
| 117 | + unit = LengthUnitEnum.METER |
| 118 | + if measurement_system_code == SegyMeasurementSystem.FEET: |
| 119 | + unit = LengthUnitEnum.FOOT |
| 120 | + |
| 121 | + return LengthUnitModel(length=unit) |
| 122 | + |
| 123 | + |
| 124 | +def _update_template_units(template: AbstractDatasetTemplate, unit: LengthUnitModel | None) -> AbstractDatasetTemplate: |
| 125 | + """Update the template with dynamic and some pre-defined units.""" |
| 126 | + new_units = {key: AngleUnitModel(angle=AngleUnitEnum.DEGREES) for key in ANGLE_UNIT_KEYS} |
| 127 | + |
| 128 | + if unit is None: |
| 129 | + template.add_units(new_units) |
| 130 | + return template |
| 131 | + |
| 132 | + for key in SPATIAL_UNIT_KEYS: |
| 133 | + current_value = template.get_unit_by_key(key) |
| 134 | + if current_value is not None: |
| 135 | + logger.warning("Unit for %s already in template. Will keep the original unit: %s", key, current_value) |
| 136 | + continue |
| 137 | + |
| 138 | + new_units[key] = unit |
| 139 | + |
| 140 | + template.add_units(new_units) |
| 141 | + return template |
0 commit comments