1515from __future__ import annotations
1616
1717import logging
18+ import multiprocessing as mp
1819import os
20+ from concurrent .futures import ProcessPoolExecutor
21+ from itertools import repeat
22+ from math import ceil
1923from typing import TYPE_CHECKING
2024from typing import Any
25+
2126import numpy as np
27+ from psutil import cpu_count
2228from segy import SegyFile
23- from mdio .segy ._raw_trace_wrapper import SegyFileRawTraceWrapper
24- from math import ceil
25- from concurrent .futures import ProcessPoolExecutor
26- from itertools import repeat
29+ from segy .arrays import HeaderArray
2730from tqdm .auto import tqdm
2831from upath import UPath
29- from psutil import cpu_count
30- import multiprocessing as mp
3132
33+ from mdio .segy ._raw_trace_wrapper import SegyFileRawTraceWrapper
3234
33- from segy .arrays import HeaderArray
3435if TYPE_CHECKING :
3536 from mdio .segy ._workers import SegyFileArguments
3637
38+ try :
39+ from crc32c_dist_rs import DistributedCRC32C
40+ except ImportError :
41+ DistributedCRC32C = Any # type: ignore[assignment,misc]
42+
3743default_cpus = cpu_count (logical = True )
3844
3945logger = logging .getLogger (__name__ )
5864
5965
6066def header_scan_worker (
61- segy_file_kwargs : " SegyFileArguments" ,
67+ segy_file_kwargs : SegyFileArguments ,
6268 trace_range : tuple [int , int ],
6369 subset : list [str ] | None = None ,
6470) -> HeaderArray | tuple [HeaderArray , tuple [int , int , int ]]:
@@ -103,7 +109,6 @@ def header_scan_worker(
103109 # (singleton) so we can concat and assign stuff later.
104110 trace_header = np .array (trace_header , dtype = new_dtype , ndmin = 1 )
105111
106-
107112 # Calculate checksum from the raw bytes ALREADY IN MEMORY (NO ADDITIONAL I/O!)
108113 raw_bytes = traces .trace_buffer_array .tobytes ()
109114 partial_crc32c = calculate_bytes_crc32c (raw_bytes )
@@ -122,13 +127,13 @@ def header_scan_worker(
122127
123128 return HeaderArray (trace_header ), checksum_info
124129
130+
125131def parse_headers ( # noqa: PLR0913
126132 segy_file_kwargs : SegyFileArguments ,
127133 num_traces : int ,
128134 subset : list [str ] | None = None ,
129135 block_size : int = 10000 ,
130136 progress_bar : bool = True ,
131- calculate_checksum : bool = True ,
132137) -> HeaderArray | tuple [HeaderArray , int ]:
133138 """Read and parse given `byte_locations` from SEG-Y file.
134139
@@ -200,9 +205,7 @@ def parse_headers( # noqa: PLR0913
200205 )
201206
202207 if progress_bar is True :
203- desc = (
204- "Scanning SEG-Y & calculating checksum"
205- )
208+ desc = "Scanning SEG-Y & calculating checksum"
206209 lazy_work = tqdm (
207210 iterable = lazy_work ,
208211 total = n_blocks ,
@@ -215,6 +218,7 @@ def parse_headers( # noqa: PLR0913
215218
216219 return finalize_distributed_checksum (results , combiner )
217220
221+
218222def is_checksum_available () -> bool :
219223 """Check if checksum libraries are available.
220224
@@ -273,7 +277,7 @@ def calculate_bytes_crc32c(data: bytes) -> int:
273277 return int .from_bytes (crc .digest (), byteorder = "big" )
274278
275279
276- def create_distributed_crc32c (initial_bytes : bytes , total_length : int ) -> Any :
280+ def create_distributed_crc32c (initial_bytes : bytes , total_length : int ) -> DistributedCRC32C :
277281 """Create a distributed CRC32C combiner instance.
278282
279283 Args:
@@ -292,7 +296,7 @@ def create_distributed_crc32c(initial_bytes: bytes, total_length: int) -> Any:
292296
293297
294298def finalize_distributed_checksum (
295- results : list [tuple [HeaderArray , tuple [int , int , int ]]], combiner : Any
299+ results : list [tuple [HeaderArray , tuple [int , int , int ]]], combiner : DistributedCRC32C
296300) -> tuple [HeaderArray , int ]:
297301 """Finalize a distributed CRC32C checksum from scan results.
298302
@@ -309,8 +313,6 @@ def finalize_distributed_checksum(
309313 """
310314 require_checksum_libraries ()
311315
312- import numpy as np
313-
314316 headers : list [HeaderArray ] = []
315317 for result in results :
316318 headers .append (result [0 ])
@@ -334,4 +336,3 @@ def finalize_distributed_checksum(
334336 "create_distributed_crc32c" ,
335337 "finalize_distributed_checksum" ,
336338]
337-
0 commit comments