|
2 | 2 |
|
3 | 3 | import segyio |
4 | 4 |
|
5 | | -from .utils import c_endianness |
| 5 | +from .utils import ( |
| 6 | + FileDatasourceDescriptor, |
| 7 | + StreamDatasourceDescriptor, |
| 8 | + MemoryBufferDatasourceDescriptor |
| 9 | +) |
6 | 10 |
|
7 | 11 | def infer_geometry(f, metrics, iline, xline, strict): |
8 | 12 | try: |
@@ -148,16 +152,95 @@ def open(filename, mode="r", iline = 189, |
148 | 152 | solution = 'use r+ to open in read-write' |
149 | 153 | raise ValueError(', '.join((problem, solution))) |
150 | 154 |
|
151 | | - from . import _segyio |
152 | | - fd = _segyio.segyfd( |
153 | | - filename=str(filename), mode=mode, endianness=c_endianness(endian) |
| 155 | + return _open( |
| 156 | + FileDatasourceDescriptor(filename, mode), |
| 157 | + iline, xline, strict, ignore_geometry, endian |
154 | 158 | ) |
| 159 | + |
| 160 | + |
| 161 | +def open_with(stream, |
| 162 | + iline=189, |
| 163 | + xline=193, |
| 164 | + strict=True, |
| 165 | + ignore_geometry=False, |
| 166 | + endian='big', |
| 167 | + minimize_requests_number=True |
| 168 | + ): |
| 169 | + """ |
| 170 | + Opens a segy file from a stream. |
| 171 | +
|
| 172 | + Function behaves the same as `segyio.open`, but sources data from a finite |
| 173 | + stream instead of a file. Stream's close() will be called when SegyFile is |
| 174 | + closed. |
| 175 | +
|
| 176 | + Note that `segyio.open_with` can be very slow. `segyio.open` is generally a |
| 177 | + preferred option when speed matters. |
| 178 | +
|
| 179 | + Parameters |
| 180 | + ---------- |
| 181 | +
|
| 182 | + stream : file-like object |
| 183 | + Source of the data. It is up to the user to assure stream is opened in rb |
| 184 | + mode for reading and r+b mode for updating. |
| 185 | + minimize_requests_number : bool |
| 186 | + Configuration for some internal algorithms. True to minimize number of |
| 187 | + requests to the stream at the cost of higher memory usage. False to |
| 188 | + minimize memory usage at the cost of more requests to the stream. |
| 189 | +
|
| 190 | + See other common parameters at `segyio.open`. |
| 191 | + """ |
| 192 | + return _open( |
| 193 | + StreamDatasourceDescriptor( |
| 194 | + stream, |
| 195 | + minimize_requests_number |
| 196 | + ), |
| 197 | + iline, xline, strict, ignore_geometry, endian |
| 198 | + ) |
| 199 | + |
| 200 | + |
| 201 | +def open_from_memory(memory_buffer, |
| 202 | + iline=189, |
| 203 | + xline=193, |
| 204 | + strict=True, |
| 205 | + ignore_geometry=False, |
| 206 | + endian='big'): |
| 207 | + """ |
| 208 | + Opens a segy file from memory. |
| 209 | +
|
| 210 | + Function behaves the same as `segyio.open`, but expects file to fit |
| 211 | + completely into memory. Performance is expected to be significantly faster |
| 212 | + than using `segyio.open` or `segyio.open_with`. |
| 213 | +
|
| 214 | + Parameters |
| 215 | + ---------- |
| 216 | +
|
| 217 | + buffer : contiguous memory object |
| 218 | + Source of the data: complete SEGY file loaded into memory. Update |
| 219 | + operations assume memory is writable. |
| 220 | +
|
| 221 | + See other common parameters at `segyio.open`. |
| 222 | + """ |
| 223 | + return _open( |
| 224 | + MemoryBufferDatasourceDescriptor( |
| 225 | + memory_buffer |
| 226 | + ), |
| 227 | + iline, xline, strict, ignore_geometry, endian |
| 228 | + ) |
| 229 | + |
| 230 | + |
| 231 | +def _open(datasource_descriptor, |
| 232 | + iline=189, |
| 233 | + xline=193, |
| 234 | + strict=True, |
| 235 | + ignore_geometry=False, |
| 236 | + endian='big'): |
| 237 | + |
| 238 | + fd = datasource_descriptor.make_segyfile_descriptor(endian) |
155 | 239 | fd.segyopen() |
156 | 240 | metrics = fd.metrics() |
157 | 241 |
|
158 | 242 | f = segyio.SegyFile(fd, |
159 | | - filename = str(filename), |
160 | | - mode = mode, |
| 243 | + datasource_descriptor, |
161 | 244 | iline = int(iline), |
162 | 245 | xline = int(xline), |
163 | 246 | endian = endian, |
|
0 commit comments