-
Notifications
You must be signed in to change notification settings - Fork 217
/
segy_put_traces_mex.c
68 lines (51 loc) · 1.73 KB
/
segy_put_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
#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 ], "r+b" );
plhs[ 0 ] = mxDuplicateArray( prhs[ 1 ] );
int first_trace = mxGetScalar( prhs[ 2 ] );
int last_trace = mxGetScalar( prhs[ 3 ] );
int notype = mxGetScalar( prhs[ 4 ] );
struct segy_file_format fmt = filefmt( fp );
if( notype != -1 )
fmt.format = notype;
// 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;
if( first_trace > last_trace ) {
msg1 = "segy:get_traces:bounds";
msg2 = "first trace must be smaller than last trace";
goto cleanup;
}
float* out = mxGetData( plhs[ 0 ] );
segy_from_native( fmt.format, fmt.samples * fmt.traces, out );
float* itr = out;
for( int i = first_trace; i <= last_trace; ++i ) {
err = segy_writetrace( fp, i, itr, fmt.trace0, fmt.trace_bsize );
itr += fmt.samples;
if( err != 0 ) {
msg1 = "segy:put_traces:segy_writetrace";
msg2 = strerror( errno );
fmt.traces = i;
goto cleanup;
}
}
segy_close( fp );
segy_to_native( fmt.format, bufsize, out );
plhs[ 1 ] = mxCreateDoubleScalar( fmt.format );
return;
cleanup:
segy_close( fp );
segy_to_native( fmt.format, bufsize, out );
mexErrMsgIdAndTxt( msg1, msg2 );
}