|
26 | 26 | logger = logging.getLogger(__name__) |
27 | 27 |
|
28 | 28 |
|
| 29 | +def _create_delayed_trace_dimension_transform(headers_subset: HeaderArray, position: int) -> callable: |
| 30 | + """Create a delayed transform function that adds a trace dimension and its coordinate. |
| 31 | + |
| 32 | + This function creates a closure that captures the headers_subset and position, |
| 33 | + but defers the actual computation until the transform is executed by the dataset builder. |
| 34 | + The transform adds both the trace dimension and a corresponding coordinate. |
| 35 | + |
| 36 | + Args: |
| 37 | + headers_subset: The header array containing trace information |
| 38 | + position: The position where the trace dimension should be inserted |
| 39 | + |
| 40 | + Returns: |
| 41 | + A callable that can be used as a transform function |
| 42 | + """ |
| 43 | + def delayed_transform(builder): |
| 44 | + from mdio.builder.schemas.dtype import ScalarType |
| 45 | + |
| 46 | + # Calculate the trace dimension size at execution time |
| 47 | + if "trace" in headers_subset.dtype.names: |
| 48 | + trace_size = int(np.max(headers_subset["trace"])) |
| 49 | + else: |
| 50 | + # Fallback: if trace field doesn't exist, we need to determine size differently |
| 51 | + raise ValueError("Trace field not found in headers_subset when executing delayed transform") |
| 52 | + |
| 53 | + # Add the trace dimension |
| 54 | + trace_dimension = NamedDimension(name="trace", size=trace_size) |
| 55 | + builder.push_dimension(trace_dimension, position=position, new_dim_chunk_size=1) |
| 56 | + |
| 57 | + # Add the corresponding coordinate for the trace dimension |
| 58 | + builder.add_coordinate( |
| 59 | + "trace", |
| 60 | + dimensions=("trace",), |
| 61 | + data_type=ScalarType.INT32, |
| 62 | + ) |
| 63 | + |
| 64 | + return builder |
| 65 | + |
| 66 | + return delayed_transform |
| 67 | + |
| 68 | + |
29 | 69 | def get_grid_plan( # noqa: C901 |
30 | 70 | segy_file: SegyFile, |
31 | 71 | chunksize: tuple[int, ...] | None, |
@@ -70,7 +110,8 @@ def get_grid_plan( # noqa: C901 |
70 | 110 |
|
71 | 111 | if grid_overrides.get("HasDuplicates", False): |
72 | 112 | pos = len(template.dimension_names) - 1 # TODO: Implement the negative position case... |
73 | | - template._queue_transform(lambda builder: builder.push_dimension(NamedDimension(name="trace", size=np.max(headers_subset["trace"])), position=pos, new_dim_chunk_size=1)) |
| 113 | + # Use the delayed transform function instead of a simple lambda |
| 114 | + template._queue_transform(_create_delayed_trace_dimension_transform(headers_subset, pos)) |
74 | 115 | horizontal_dimensions = (*horizontal_dimensions, "trace") |
75 | 116 |
|
76 | 117 | dimensions = [] |
|
0 commit comments