-
Notifications
You must be signed in to change notification settings - Fork 217
/
segy_get_traces_mex.c
77 lines (57 loc) · 1.95 KB
/
segy_get_traces_mex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <errno.h>
#include <string.h>
#include <segyio/segy.h>
#include "segyutil.h"
#include "matrix.h"
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
char* msg1;
char* msg2;
int err;
segy_file* fp = segyfopen( prhs[ 0 ], "rb" );
int first_trace = mxGetScalar( prhs[ 1 ] );
int last_trace = mxGetScalar( prhs[ 2 ] );
int notype = mxGetScalar( prhs[ 3 ] );
struct segy_file_format fmt = filefmt( fp );
char binary[ SEGY_BINARY_HEADER_SIZE ];
err = segy_binheader( fp, binary );
if( err != 0 ) {
msg1 = "segy:get_traces:binary";
msg2 = strerror( errno );
goto cleanup;
}
// if last_trace was defaulted we assign it to the last trace in the file
if( last_trace == -1 )
last_trace = fmt.traces - 1;
int traces = 1 + (last_trace - first_trace);
long long bufsize = (long long)fmt.samples * traces;
plhs[0] = mxCreateNumericMatrix( fmt.samples, traces, mxSINGLE_CLASS, mxREAL );
float* out = mxGetData( plhs[ 0 ] );
if( first_trace > last_trace ) {
msg1 = "segy:get_traces:bounds";
msg2 = "first trace must be smaller than last trace";
goto cleanup;
}
for( int i = first_trace; i <= last_trace; ++i ) {
err = segy_readtrace( fp, i, out, fmt.trace0, fmt.trace_bsize );
out += fmt.samples;
if( err != 0 ) {
msg1 = "segy:get_traces:segy_readtrace";
msg2 = strerror( errno );
goto cleanup;
}
}
segy_close( fp );
if( notype != -1 )
fmt.format = notype;
segy_to_native( fmt.format, bufsize, mxGetData( plhs[ 0 ] ) );
int interval;
segy_get_bfield( binary, SEGY_BIN_INTERVAL, &interval );
plhs[ 1 ] = mxCreateDoubleScalar( interval );
plhs[ 2 ] = mxCreateDoubleScalar( fmt.format );
return;
cleanup:
segy_close( fp );
mexErrMsgIdAndTxt( msg1, msg2 );
}