-
Notifications
You must be signed in to change notification settings - Fork 172
/
Mtahread.c
361 lines (316 loc) · 12.5 KB
/
Mtahread.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* Read Trace And Header from separate files, combine, write to pipe
tah is the abbreviation of Trace And Header. Madagascar programs
that begin with sftah are designed to:
1- read trace and headers from separate rsf files and write them to
standard output (ie sftahread)
2- filter programs that read and write standard input/output and
process the tah data (eg sftahnmo, sftahstack)
3- read tah data from standard input and write separate rsf files for
the trace and headers data (ie sftahwrite)
These programs allow Seismic Unix (su) like processing in Madagascar.
Some programs have su like names.
Some programs in this suite are sftahread, sftahgethw, sftahhdrmath,
and sftahwrite.
EXAMPLE:
sftahread \\
verbose=1 \\
input=npr3_gathers.rsf \\
| sftahnmo \\
verbose=1 \\
tnmo=0,.373,.619,.826,.909,1.017,1.132,1.222,1.716,3.010 \\
vnmo=9086,10244,11085,10803,10969,11578,12252,12669,14590,17116 \\
| sftahstack key=iline,xline verbose=1 \\
| sftahwrite \\
verbose=1 \\
label2="xline" o2=1 n2=188 d2=1 \\
label3="iline" o3=1 n3=345 d3=1 \\
output=mappedstack.rsf \\
>/dev/null
sfgrey <mappedstack.rsf | sfpen
In this example the cmp sorted prestack data, npr3_gathers.rsf, are
read by sftahread. The headers are in the file npr3_gathers_hdr.rsf,
the headers parameter default. The headers are merged with the trace
amplitudes and the tah data sent down the pipe for nmo and stack. The
sftahwrite writes the trace data to mappedstack.rsf and the headers
are written to the file mappedstack_hdr.rsf. The order of the data in
the output file is defined by the iline and xline trace headers, so the
data order is (time,xline,iline). Finally, the output volume is
displayed using sfgrey.
*/
/*
Program change history:
date Who What
04/26/2013 Karl Schleicher Original program
01/10/2014 Karl Schleicher Provide headers default, improve documentation
06/05/2015 Karl Schleicher add makeheader=y to load axis coords to hdrs
*/
#include <string.h>
#include <rsf.h>
#include <rsfsegy.h>
#include "tahsub.h"
/* sparingly make some global variables. */
int verbose;
int main(int argc, char* argv[])
{
sf_file infile=NULL, out=NULL, inheaders=NULL;
int n1_traces;
int n1_headers;
int n_traces, n_headers;
sf_datatype typehead;
sf_datatype typein;
float* fheader=NULL;
int* iheader=NULL;
float* intrace=NULL;
int i_trace;
char* infile_filename=NULL;
char* headers_filename=NULL;
bool makeheader;
int iaxis;
char parameter[13];
char* label_in[SF_MAX_DIM+1];
int n_in[SF_MAX_DIM+1];
float o_in[SF_MAX_DIM+1];
float d_in[SF_MAX_DIM+1];
int indx_of_keys[SF_MAX_DIM+1];
int indx_of_ns;
int indx_of_dt;
int traceindx[SF_MAX_DIM+1];
float tracecoord[SF_MAX_DIM+1];
int indxleft;
sf_init (argc,argv);
/*****************************/
/* initialize verbose switch */
/*****************************/
if(!sf_getint("verbose",&verbose))verbose=1;
/* \n
flag to control amount of print
0 terse, 1 informative, 2 chatty, 3 debug
*/
if(verbose>0)fprintf(stderr,"verbose=%d\n",verbose);
/*****************************************/
/* initialize the input and output files */
/*****************************************/
if(verbose>0)fprintf(stderr,"read name of input file name\n");
infile_filename=sf_getstring("input");
/* \n
Input file for traces amplitudes. You can list a file here, has the
input file name will be used to compute the name of the header files.
The input trace amplitudes can also be read from standard input by
just supplying standard input and omitting this paramater, This
is useful it you wish to do sopme initial processing of the input
rsf file containing the trace amplitudes. This is useful if you need
to change input axis labels to use the makeheader=yes.
*/
if(infile_filename==NULL) infile = sf_input ("in");
else infile = sf_input (infile_filename);
/* get the axis labels on the input file */
/* these will be used if users requests makeheaders */
for (iaxis=1; iaxis<SF_MAX_DIM+1; iaxis++){
sprintf(parameter,"label%d",iaxis);
if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
/* get label, o, n, and i for each axis. These will be used to
make headers from axis (if that option is requested) */
if (!(label_in[iaxis]=sf_histstring(infile,parameter))) {
label_in[iaxis]="none";
}
sprintf(parameter,"o%d",iaxis);
if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
if (!sf_histfloat(infile,parameter,&o_in[iaxis])){
label_in[iaxis]="none";
}
sprintf(parameter,"d%d",iaxis);
if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
if (!sf_histfloat(infile,parameter,&d_in[iaxis])){
label_in[iaxis]="none";
}
sprintf(parameter,"n%d",iaxis);
if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
if (!sf_histint(infile,parameter,&n_in[iaxis])){
n_in[iaxis]=1;
}
if(verbose>1){
fprintf(stderr,"on axis=%d got label,o,d,n=%s,%f,%f,%d \n",iaxis,
label_in[iaxis],o_in[iaxis],d_in[iaxis],n_in[iaxis]);
}
}
/* compute index_of_keys after calling segy_init */
if(verbose>0)
fprintf(stderr,"set up output file for tah - should be stdout\n");
out = sf_output ("out");
if(verbose>0)fprintf(stderr,"read name of input headers file\n");
headers_filename=sf_getstring("headers");
/* \n
Trace header file name. Default is the input data file
name, with the final .rsf changed to _hdr.rsf
*/
if(headers_filename==NULL){
/* compute headers_filename from infile_filename by replacing the final
.rsf with _hdr.rsf */
if(!(0==strcmp(infile_filename+strlen(infile_filename)-4,".rsf"))){
fprintf(stderr,"parameter input, the name of the input file, does\n");
fprintf(stderr,"not end with .rsf, so header filename cannot be\n");
fprintf(stderr,"computed by replacing the final .rsf with _hdr.rsf.\n");
sf_error("default for headers parameter cannot be computed.");
}
headers_filename=malloc(strlen(infile_filename)+60);
strcpy(headers_filename,infile_filename);
strcpy(headers_filename+strlen(infile_filename)-4,"_hdr.rsf\0");
if(verbose>2){
fprintf(stderr,"parameter header defaulted. Computed to be #%s#\n",
headers_filename);
}
}
if(verbose>2)fprintf(stderr,"parameter header input or computed #%s#\n",
headers_filename);
if(!sf_getbool("makeheader",&makeheader))makeheader=false;
/* \n
Option to load headers using the input file axis labels. If axis
label2 through label9 match a header key then that coordinate is
loaded to the traces header. This can be used to load the source
coordinate to the sx header location. This may require changing
the axis label because Madagascar axis labels are not the same as
segy trace headers. for example axis 2 coordiante can be loaded in
sx trace header by:
<spike.rsf sfput label2=sx \\
| sftahread headers=spike_hdr.rsf makeheader=y \\
| sftahgethw key=sx >/dev/null
*/
inheaders = sf_input(headers_filename);
if (!sf_histint(infile,"n1",&n1_traces))
sf_error("input file does not define n1");
if (!sf_histint(inheaders,"n1",&n1_headers))
sf_error("input headers file does not define n1");
n_traces=sf_leftsize(infile,1);
n_headers=sf_leftsize(inheaders,1);
if(verbose>0)
fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);
if(n_traces!=n_headers){
fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);
sf_error("number of traces and headers must be the same");
}
typehead = sf_gettype(inheaders);
if (SF_INT != typehead && SF_FLOAT != typehead )
sf_error("Need float or int input headers.");
typein = sf_gettype(infile);
if (SF_FLOAT != typein )
sf_error("input file must contain floats.");
/* must be float or int */
if(verbose>1)fprintf(stderr,"allocate headers. n1_headers=%d\n",n1_headers);
fheader = sf_floatalloc(n1_headers);
iheader = (int*)fheader;
if(verbose>1)fprintf(stderr,"allocate intrace. n1_traces=%d\n",n1_traces);
intrace= sf_floatalloc(n1_traces);
if(verbose>1)fprintf(stderr,"need to add 2 words. Record type and record length\n");
sf_putint(out,"n1",n1_traces+n1_headers+2);
sf_putint(out,"n1_traces",n1_traces);
/*************************************************************************/
/* put info into history so later tah programs can figure out the headers*/
/*************************************************************************/
if(verbose>1)fprintf(stderr,"put n1_headers to history\n");
sf_putint(out,"n1_headers",n1_headers);
if(verbose>1)fprintf(stderr,"test typehead\n");
if(SF_INT == typehead) sf_putstring(out,"header_format","native_int");
else sf_putstring(out,"header_format","native_float");
sf_putstring(out,"headers",headers_filename);
/* the list of any extra header keys */
if(verbose>1)fprintf(stderr,"segy_init\n");
segy_init(n1_headers,inheaders);
if(verbose>1)fprintf(stderr,"segy2hist\n");
segy2hist(out,n1_headers);
if(0) {
int tempint;
tempint=segykey("ep");
fprintf(stderr,"ep tempint=%d\n",tempint);
tempint=segykey("epx");
fprintf(stderr,"epx tempint=%d\n",tempint);
}
/* get indx_of_ns and indx_of_dt that will also be loaded */
indx_of_ns=segykey("ns");
indx_of_dt=segykey("dt");
if(makeheader){
for (iaxis=2; iaxis<SF_MAX_DIM+1; iaxis++){
if(0==strcmp("none",label_in[iaxis])){
indx_of_keys[iaxis]=-1;
} else {
indx_of_keys[iaxis]=segykey(label_in[iaxis]);
if(indx_of_keys[iaxis]<0){
sf_warning("************************************************");
sf_warning("************************************************");
sf_warning("axis %d has label that is not a header key",iaxis);
sf_warning("This axis label will not be loaded");
sf_warning("************************************************");
sf_warning("************************************************");
}
}
if(verbose>1){
fprintf(stderr,"indx_of_keys[%d]=%d\n",iaxis,indx_of_keys[iaxis]);
}
}
if(verbose>1){
fprintf(stderr,"indx_of_ns=%d\n",indx_of_ns);
fprintf(stderr,"indx_of_dt=%d\n",indx_of_dt);
}
}
/* put the history from the input file to the output */
if(verbose>1)fprintf(stderr,"fileflush out\n");
sf_fileflush(out,infile);
if(verbose>0)fprintf(stderr,"start trace loop n_traces=%d\n",n_traces);
for (i_trace=0; i_trace<n_traces; i_trace++){
if(verbose>2 ||(verbose>0 && i_trace<5)){
fprintf(stderr,"i_trace=%d\n",i_trace);
}
/**************************/
/* read trace and headers */
/**************************/
sf_floatread(fheader,n1_headers,inheaders);
sf_floatread(intrace,n1_traces,infile);
if(makeheader){
indxleft=i_trace;
for (iaxis=2; iaxis<SF_MAX_DIM+1; iaxis++){
traceindx[iaxis]=indxleft%n_in[iaxis];
indxleft/=n_in[iaxis];
tracecoord[iaxis]=o_in[iaxis]+traceindx[iaxis]*d_in[iaxis];
if(verbose>1){
fprintf(stderr,"i_trace=%d,iaxis=%d,tracecoord[iaxis]=%f\n",
i_trace, iaxis, tracecoord[iaxis]);
}
if(indx_of_keys[iaxis]>=0){
if(typehead==SF_FLOAT)fheader[indx_of_keys[iaxis]]=tracecoord[iaxis];
else iheader[indx_of_keys[iaxis]]=SF_NINT(tracecoord[iaxis]);
}
}
/* load ns and dt headers */
if(verbose>1)fprintf(stderr,"n1_traces=%d\n",n1_traces);
if(typehead==SF_FLOAT){
fheader[indx_of_ns]=n1_traces;
/* fprintf(stderr,"fheader[indx_of_ns]=%e\n",fheader[indx_of_ns]); */
}else{
iheader[indx_of_ns]=n1_traces;
/* fprintf(stderr,"iheader[indx_of_ns]=%d\n",iheader[indx_of_ns]); */
}
if(verbose>1){
fprintf(stderr,"label_in[1]=%s\n", label_in[1]);
}
/* strcmp returns 0 if both are equal */
if(strcmp(label_in[1],"Time")==0){
/*convert seconds to microseconds */
if(typehead==SF_FLOAT){
fheader[indx_of_dt]=d_in[1]*1000000.0;
/* fprintf(stderr,"fheader[indx_of_dt]=%e\n",fheader[indx_of_dt]); */
} else {
iheader[indx_of_dt]=SF_NINT(d_in[1]*1000000.0);
/* fprintf(stderr,"iheader[indx_of_dt]=%d\n",iheader[indx_of_dt]); */
}
}
}
/***************************/
/* Write trace and headers */
/***************************/
/* trace and header will be preceeded with words:
1- type record: 4 charactors 'tah '. This will support other
type records like 'htah', hidden trace and header.
2- the length of the length of the trace and header. */
put_tah(intrace, fheader, n1_traces, n1_headers, out);
}
exit(0);
}