segyio 2.0.0a1__cp312-cp312-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
segyio/open.py ADDED
@@ -0,0 +1,306 @@
1
+ import numpy
2
+
3
+ import segyio
4
+
5
+ from .utils import (
6
+ FileDatasourceDescriptor,
7
+ StreamDatasourceDescriptor,
8
+ MemoryBufferDatasourceDescriptor,
9
+ to_c_endianness,
10
+ to_c_encoding
11
+ )
12
+
13
+ def infer_geometry(f, metrics, strict):
14
+ try:
15
+ cube_metrics = f.segyfd.cube_metrics()
16
+ f._sorting = cube_metrics['sorting']
17
+ iline_count = cube_metrics['iline_count']
18
+ xline_count = cube_metrics['xline_count']
19
+ offset_count = cube_metrics['offset_count']
20
+ metrics.update(cube_metrics)
21
+
22
+ ilines = numpy.zeros(iline_count, dtype=numpy.intc)
23
+ xlines = numpy.zeros(xline_count, dtype=numpy.intc)
24
+ offsets = numpy.zeros(offset_count, dtype=numpy.intc)
25
+
26
+ f.segyfd.indices(metrics, ilines, xlines, offsets)
27
+ f.interpret(ilines, xlines, offsets, f._sorting)
28
+
29
+ except:
30
+ if not strict:
31
+ f._ilines = None
32
+ f._xlines = None
33
+ f._offsets = None
34
+ else:
35
+ f.close()
36
+ raise
37
+
38
+ return f
39
+
40
+
41
+ def open(filename, mode="r", iline = None,
42
+ xline = None,
43
+ strict = True,
44
+ ignore_geometry = False,
45
+ endian = None,
46
+ encoding = None,
47
+ layout_xml = None
48
+ ):
49
+ """Open a segy file.
50
+
51
+ Opens a segy file and tries to figure out its sorting, inline numbers,
52
+ crossline numbers, and offsets, and enables reading and writing to this
53
+ file in a simple manner.
54
+
55
+ For reading, the access mode `r` is preferred. All write operations will
56
+ raise an exception. For writing, the mode `r+` is preferred (as `rw` would
57
+ truncate the file). Any mode with `w` will raise an error. The modes used
58
+ are standard C file modes; please refer to that documentation for a
59
+ complete reference.
60
+
61
+ Open should be used together with python's ``with`` statement. Please refer
62
+ to the examples. When the ``with`` statement is used the file will
63
+ automatically be closed when the routine completes or an exception is
64
+ raised.
65
+
66
+ By default, segyio tries to open in ``strict`` mode. This means the file will
67
+ be assumed to represent a geometry with consistent inline, crosslines and
68
+ offsets. If strict is False, segyio will still try to establish a geometry,
69
+ but it won't abort if it fails. When in non-strict mode is opened,
70
+ geometry-dependent modes such as iline will raise an error.
71
+
72
+ If ``ignore_geometry=True``, segyio will *not* try to build iline/xline or
73
+ other geometry related structures, which leads to faster opens. This is
74
+ essentially the same as using ``strict=False`` on a file that has no
75
+ geometry.
76
+
77
+ Parameters
78
+ ----------
79
+
80
+ filename : str
81
+ Path to file to open
82
+
83
+ mode : {'r', 'r+'}
84
+ File access mode, read-only ('r', default) or read-write ('r+')
85
+
86
+ iline : int or segyio.TraceField
87
+ Overrides inline field offset in the trace headers. If no value is set,
88
+ value defined in xml mapping is used and if this one is absent the
89
+ standard-defined value 189 is used. By setting this value user takes
90
+ over responsibility of assuring parsing can be done correctly.
91
+
92
+ xline : int or segyio.TraceField
93
+ Overrides crossline field offset in the trace headers. If no value is
94
+ set, value defined in xml mapping is used and if this one is absent the
95
+ standard-defined value 193 is used. By setting this value user takes
96
+ over responsibility of assuring parsing can be done correctly.
97
+
98
+ strict : bool, optional
99
+ Abort if a geometry cannot be inferred. Defaults to True.
100
+
101
+ ignore_geometry : bool, optional
102
+ Opt out on building geometry information, useful for e.g. shot
103
+ organised files. Defaults to False.
104
+
105
+ endian : {'big', 'msb', 'little', 'lsb'}
106
+ File endianness, big/msb or little/lsb. If not set, value would be read
107
+ from binary header fields 3297-3300.
108
+
109
+ encoding : {None, 'ebcdic', 'ascii'}
110
+ Encoding for text and strings for the whole file: None - auto detection
111
+ (default), ebcdic or ascii. ebcdic encoded strings would be translated
112
+ into latin-1 encoding as per SEG-Y specification. For ascii encoding
113
+ data would be returned as is.
114
+
115
+ layout_xml: bytearray
116
+ SEG-Y revision 2.1 D8 xml layout. Takes precedence over layout
117
+ definition xml inside the file.
118
+
119
+ Returns
120
+ -------
121
+
122
+ file : segyio.SegyFile
123
+ An open segyio file handle
124
+
125
+ Raises
126
+ ------
127
+
128
+ ValueError
129
+ If the mode string contains 'w', as it would truncate the file
130
+
131
+ Notes
132
+ -----
133
+
134
+ .. versionadded:: 1.1
135
+
136
+ .. versionchanged:: 1.8
137
+ endian argument
138
+
139
+ .. versionchanged:: 2.0
140
+ Support for SEG-Y revision 2.1
141
+
142
+ When a file is opened non-strict, only raw traces access is allowed, and
143
+ using modes such as ``iline`` raise an error.
144
+
145
+
146
+ Examples
147
+ --------
148
+
149
+ Open a file in read-only mode:
150
+
151
+ >>> with segyio.open(path, "r") as f:
152
+ ... print(f.ilines)
153
+ ...
154
+ [1, 2, 3, 4, 5]
155
+
156
+ Open a file in read-write mode:
157
+
158
+ >>> with segyio.open(path, "r+") as f:
159
+ ... f.trace = np.arange(100)
160
+
161
+ Open two files at once:
162
+
163
+ >>> with segyio.open(src_path) as src, segyio.open(dst_path, "r+") as dst:
164
+ ... dst.trace = src.trace # copy all traces from src to dst
165
+
166
+ Open a file little-endian file:
167
+
168
+ >>> with segyio.open(path, endian = 'little') as f:
169
+ ... f.trace[0]
170
+
171
+ """
172
+
173
+ if 'w' in mode:
174
+ problem = 'w in mode would truncate the file'
175
+ solution = 'use r+ to open in read-write'
176
+ raise ValueError(', '.join((problem, solution)))
177
+
178
+ return _open(
179
+ FileDatasourceDescriptor(filename, mode),
180
+ iline, xline, strict, ignore_geometry, endian, encoding, layout_xml
181
+ )
182
+
183
+
184
+ def open_with(stream,
185
+ iline=None,
186
+ xline=None,
187
+ strict=True,
188
+ ignore_geometry=False,
189
+ endian=None,
190
+ encoding=None,
191
+ minimize_requests_number=True,
192
+ layout_xml = None
193
+ ):
194
+ """
195
+ Opens a segy file from a stream.
196
+
197
+ Function behaves the same as `segyio.open`, but sources data from a finite
198
+ stream instead of a file. Stream's close() will be called when SegyFile is
199
+ closed.
200
+
201
+ Note that `segyio.open_with` can be very slow. `segyio.open` is generally a
202
+ preferred option when speed matters.
203
+
204
+ Parameters
205
+ ----------
206
+
207
+ stream : file-like object
208
+ Source of the data. It is up to the user to assure stream is opened in rb
209
+ mode for reading and r+b mode for updating.
210
+ minimize_requests_number : bool
211
+ Configuration for some internal algorithms. True to minimize number of
212
+ requests to the stream at the cost of higher memory usage. False to
213
+ minimize memory usage at the cost of more requests to the stream.
214
+
215
+ See other common parameters at `segyio.open`.
216
+ """
217
+ return _open(
218
+ StreamDatasourceDescriptor(
219
+ stream,
220
+ minimize_requests_number
221
+ ),
222
+ iline, xline, strict, ignore_geometry, endian, encoding, layout_xml
223
+ )
224
+
225
+
226
+ def open_from_memory(memory_buffer,
227
+ iline=None,
228
+ xline=None,
229
+ strict=True,
230
+ ignore_geometry=False,
231
+ endian=None,
232
+ encoding=None,
233
+ layout_xml = None
234
+ ):
235
+ """
236
+ Opens a segy file from memory.
237
+
238
+ Function behaves the same as `segyio.open`, but expects file to fit
239
+ completely into memory. Performance is expected to be significantly faster
240
+ than using `segyio.open` or `segyio.open_with`.
241
+
242
+ Parameters
243
+ ----------
244
+
245
+ buffer : contiguous memory object
246
+ Source of the data: complete SEGY file loaded into memory. Update
247
+ operations assume memory is writable.
248
+
249
+ See other common parameters at `segyio.open`.
250
+ """
251
+ return _open(
252
+ MemoryBufferDatasourceDescriptor(
253
+ memory_buffer
254
+ ),
255
+ iline, xline, strict, ignore_geometry, endian, encoding, layout_xml
256
+ )
257
+
258
+
259
+ def _open(datasource_descriptor,
260
+ iline=None,
261
+ xline=None,
262
+ strict=True,
263
+ ignore_geometry=False,
264
+ endian=None,
265
+ encoding=None,
266
+ layout_xml = None
267
+ ):
268
+
269
+ fd = datasource_descriptor.make_segyfile_descriptor()
270
+
271
+ if iline != None:
272
+ iline = int(iline)
273
+ if xline != None:
274
+ xline = int(xline)
275
+
276
+ fd.segyopen(
277
+ endianness=to_c_endianness(endian),
278
+ encoding=to_c_encoding(encoding),
279
+ iline=iline,
280
+ xline=xline,
281
+ layout_xml=layout_xml
282
+ )
283
+ metrics = fd.metrics()
284
+
285
+ f = segyio.SegyFile(fd,
286
+ datasource_descriptor,
287
+ endian = endian,
288
+ )
289
+
290
+ try:
291
+ dt = segyio.tools.dt(f, fallback_dt = 4000.0) / 1000.0
292
+ try:
293
+ t0 = fd.getdelay()
294
+ except RuntimeError:
295
+ t0 = 0.0
296
+ samples = metrics['samplecount']
297
+ f._samples = (numpy.arange(samples) * dt) + t0
298
+
299
+ except:
300
+ f.close()
301
+ raise
302
+
303
+ if ignore_geometry:
304
+ return f
305
+
306
+ return infer_geometry(f, metrics, strict)