-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathsegy_put_headers_mex.c
62 lines (49 loc) · 1.56 KB
/
segy_put_headers_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
#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" );
double* headers = mxGetPr( prhs[ 1 ] );
int field = mxGetScalar( prhs[ 2 ] );
struct segy_file_format fmt = filefmt( fp );
char traceheader[ SEGY_TRACE_HEADER_SIZE ];
/*
* check that the field is valid and writing it won't return an error. by
* checking it here we don't have to do it in the write loop
*/
err = segy_set_field( traceheader, field, 0 );
if( err != 0 ) {
msg1 = "segy:put_headers:invalid_field";
msg2 = "Invalid field value/header offset";
goto cleanup;
}
double* itr = headers;
for( int i = 0; i < fmt.traces; ++i, ++itr ) {
err = segy_traceheader( fp, i, traceheader, fmt.trace0, fmt.trace_bsize );
const int val = *itr;
if( err != 0 ) {
msg1 = "segy:put_headers:os";
msg2 = strerror( errno );
goto cleanup;
}
segy_set_field( traceheader, field, val );
err = segy_write_traceheader( fp, i, traceheader, fmt.trace0, fmt.trace_bsize );
if( err != 0 ) {
msg1 = "segy:put_headers:os";
msg2 = strerror( errno );
goto cleanup;
}
}
segy_close( fp );
return;
cleanup:
segy_close( fp );
mexErrMsgIdAndTxt( msg1, msg2 );
}