66from typing import TYPE_CHECKING
77
88import numpy as np
9+ from segy import SegyFile
910from segy .arrays import HeaderArray
1011
1112from mdio .core .config import MDIOSettings
1516
1617if TYPE_CHECKING :
1718 from zarr import Array as zarr_Array
18- from zarr import Group as zarr_Group
19- from segy import SegyFile
2019
2120from zarr .core .config import config as zarr_config
21+
2222from mdio .builder .schemas .v1 .stats import CenteredBinHistogram
2323from mdio .builder .schemas .v1 .stats import SummaryStatistics
2424from mdio .constants import fill_value_map
3131
3232def _init_worker (segy_file_kwargs : SegyFileArguments ) -> None :
3333 """Initialize worker process with persistent segy file handle.
34-
34+
3535 This function is called once per worker process to open the segy file,
3636 which is then reused across all tasks in that worker.
37-
37+
3838 Args:
3939 segy_file_kwargs: Arguments to open SegyFile instance.
4040 """
41- global _worker_segy_file
42-
43- from segy import SegyFile
41+ global _worker_segy_file # noqa: PLW0603
42+ # TODO(BrianMichell): Diagnose and fix handles not being cleaned up on cloud2cloud ingesions.
43+ # https://github.com/TGSAI/mdio-python/pull/712
44+ # https://github.com/TGSAI/mdio-python/pull/701
45+ # The reason for having a global variable is to reduce the number of GET requests for opening the file.
4446
4547 # Open the SEG-Y file once per worker
4648 _worker_segy_file = SegyFile (** segy_file_kwargs )
@@ -96,7 +98,7 @@ def trace_worker( # noqa: PLR0913
9698 grid_map : zarr_Array ,
9799) -> SummaryStatistics | None :
98100 """Writes a subset of traces from a region of the dataset of Zarr file.
99-
101+
100102 Uses pre-opened segy file from _init_worker and receives zarr arrays directly.
101103
102104 Args:
@@ -109,8 +111,8 @@ def trace_worker( # noqa: PLR0913
109111 Returns:
110112 SummaryStatistics object containing statistics about the written traces.
111113 """
112- global _worker_segy_file
113-
114+ global _worker_segy_file # noqa: PLW0602
115+
114116 # Use the pre-opened segy file from worker initialization
115117 segy_file = _worker_segy_file
116118
0 commit comments