-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsaclh.c
494 lines (414 loc) · 17.1 KB
/
saclh.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* Copyright (c) 2021 Nils Maercklin */
/************************************************************************
SACLH - List SAC header values, including different time format options
*************************************************************************
Author: Nils Maercklin,
RISSC, University of Naples, Italy, October 2008
Version: 2008-11-05
Notes: Input files must be in SAC binary format (NVHDR=6). If multiple
traces are read from stdin, correct NPTS, LEVEN, and IFTYPE values
are required.
Reference (SAC file format):
SAC - Seismic Analysis Code, User's manual,
http://www.iris.edu/manuals/sac/manual.html (last checked: 2008-10-30)
*************************************************************************/
#include "nmsaclib.h" /* SAC-related functions, and other stuff */
/* Self-documentation */
const char *sdoc[] = {
" ",
" SACLH - List SAC Header field values ",
" ",
" Usage: saclh [-k] field_names [options] -f sac_files ",
" saclh <stdin [-k] field_names [options] ",
" ",
" Optional parameters: ",
" -f list of SAC binary files (alternatively read from stdin) ",
" -k list of SAC header field names to be printed to stdout ",
" -s \\t output field separator (default: tab-separated list) ",
" -b 1 remove some blanks from character fields ",
" (0: none, 1: at end, 2: at beginning and end) ",
" -u arbitrary output string for undefined header values ",
" (default: UNDEF, \"+\" prints the true header value) ",
" -h flag: include column description line in output list ",
" -v flag: print warning messages ",
" -y 1970 epoch year ",
" ",
" All SAC header fields, and NTR (trace number), FILE (SAC file name), ",
" and Z (zero time of file) can be specified. ",
" Times are printed in seconds relative to Z (default), as epoch time ",
" in seconds (prepend modifier +E), or as a human-readable string of ",
" date and time (+H, +N) or of the time of the day only (+T). ",
" For enum fields symbolic values (+N) or a description (+D) may be ",
" printed instead of the corresponding integer value. ",
" ",
" Example: ",
" List station names and first-arrivals (header value and time string): ",
" saclh kstnm a +ha -f *.sac ",
" ",
" NM, 2008-11-05 ",
" ",
NULL};
/* Definitions */
#define INDEX_ZTIME 1000
#ifdef IS_SACHDR_TIME
#undef IS_SACHDR_TIME
#endif
#define IS_SACHDR_TIME(id) (( ( (id)>=5 && (id)<=8 ) || \
( (id)>=SAC_HEADER_TMARK_POSITION && (id)<=SAC_HEADER_TMARK_POSITION+10 ))\
|| ( (id)==INDEX_ZTIME ) ? 1 : 0 )
#define IS_SACHDR_ENUM(id) \
(( (id)>=SAC_HEADER_ENUM_MIN && (id)<=SAC_HEADER_ENUM_MAX ) ? 1 : 0 )
/* Prototypes of functions used internally */
void print_enum(SACHEAD hd, int index, char modif, char *undef);
void print_time(SACHEAD hd, int index, char modif, int eyear, char *undef);
int check_zdate(SACHEAD hd);
int check_ztime(SACHEAD hd);
int
main(int argc, char **argv)
{
/* Variables */
FILE *fp=NULL; /* input file pointer */
register int i,j,k,iarg; /* loop indices */
SACHEAD hd; /* SAC header */
int isfile; /* flag: file names (position of "-f" option) */
int headerline=0; /* flag: output header line */
int kstart=1; /* flag: key (position of "-k" option) */
int verbose=0; /* flag: print warnings */
int issac=0; /* flag: SAC header appears to be valid */
int rmblank=2; /* remove-whitespace option for character fields */
char *fieldsep=NULL;/* output field separator */
char *undef=NULL; /* output string for undefined values */
char *key=NULL; /* SAC header field name */
int index; /* SAC header field index */
int ival; /* value of integer field */
float fval; /* value of float field */
char string[17]; /* string buffer */
int ntr=0; /* input trace counter */
char modif=' '; /* format modifier */
int eyear=1970; /* epoch year */
/* Print documentation */
if (argc==1) {
i=0;
while (sdoc[i]) fprintf(stderr, "%s\n", sdoc[i++]);
return (EXIT_FAILURE);
}
/* Command line options */
if (!(igetpar(argc, argv, "-y", &eyear))) eyear=1970;
if (!(igetpar(argc, argv, "-b", &rmblank))) rmblank=1;
if (!(sgetpar(argc, argv, "-s", &fieldsep))) fieldsep="\t";
if (!(sgetpar(argc, argv, "-u", &undef))) undef=SAC_HEADER_UNDEFINED;
if (strlen(undef)==0 || (strlen(undef)>0 && undef[0]=='+')) undef=NULL;
if (!(isfile=getflag(argc, argv, "-f"))) isfile=0;
if (!(headerline=getflag(argc, argv, "-h"))) headerline=0;
if (!(verbose=getflag(argc, argv, "-v"))) verbose=0;
if ((kstart=getflag(argc, argv, "-k"))) kstart++;
else kstart = 1;
/* Print field names as header line */
if (headerline) {
for (j=kstart,k=0; (j<argc && argv[j][0]!='-'); j++,k++) {
if (argv[j][0]=='+' && strlen(argv[j])>2) key = argv[j]+2;
else key = argv[j];
printf("%s%d=%s", (k) ? fieldsep : "", k+1, key);
}
printf("\n");
}
/* Loop over SAC files (or read from stdin) */
for (iarg=isfile+1; (iarg<argc && argv[iarg][0]!='-') || !isfile; iarg++) {
/* Read SAC binary file header */
if (isfile) {
if ((fp=fopen(argv[iarg], "r")) && \
(fread(&hd, sizeof(SACHEAD), 1, fp) == 1)) {
issac = 1;
fclose(fp);
}
else {
issac = 0;
if (verbose) {
fprintf(stderr, "%s: can't read %s\n", argv[0], argv[iarg]);
}
}
}
else {
if (fread(&hd, sizeof(SACHEAD), 1, stdin) != 1) break;
issac = 1;
}
/* Byte-swapping, and check, if hd appears to be SAC header */
if (issac && NEED_BYTESWAP(hd.nvhdr)) {
swab4((char*) &hd, SAC_HEADER_SIZE_NUMBERS);
if (NEED_BYTESWAP(hd.nvhdr)) {
issac = 0;
if (verbose) {
fprintf(stderr, "%s: invalid header in %s\n", \
argv[0], (isfile) ? argv[iarg] : "stdin" );
}
}
}
/* Increment trace counter and print newline */
if (issac && ++ntr>1) printf("\n");
/* Loop over specified SAC header fields */
for (j=kstart,k=0; (j<argc && argv[j][0]!='-' && issac); j++,k++) {
/* Get modifier and SAC header field name */
if (argv[j][0]=='+' && strlen(argv[j])>2) {
modif = argv[j][1];
key = argv[j]+2;
}
else {
modif = ' ';
key = argv[j];
}
/* Get index of header field */
index = sac_hdr_index(key);
/* Print field separator */
if (k) printf("%s", fieldsep);
/* Process "pseudo fields" and invalid field names */
if (index < 0) {
if (STRCEQ(key,"ntr")) {
printf("%d", ntr);
}
else if (STRCEQ(key,"file")) {
printf("%s", (isfile) ? argv[iarg] : "stdin");
}
else if (STRCEQ(key,"z")) {
print_time(hd, INDEX_ZTIME, modif, eyear, undef);
}
else if (STRCEQ(key,"kztime")) {
print_time(hd, INDEX_ZTIME, 't', eyear, undef);
}
else {
printf("%s", "(NULL)");
if (ntr==1 && verbose) {
fprintf(stderr, "%s: invalid field name %s\n", \
argv[0], key);
}
}
}
/* Process time and enum fields (special formatting otions) */
else if (IS_SACHDR_TIME(index)) {
print_time(hd, index, modif, eyear, undef);
}
else if (IS_SACHDR_ENUM(index)) {
print_enum(hd, index, modif, undef);
}
/* Process other float fields */
else if (IS_SACHDR_FLOAT(index)) {
fval = get_sac_hdr_float(&hd, index);
if (fval != SAC_HEADER_FLOAT_UNDEFINED || !undef) {
printf("%g", fval);
}
else {
printf("%s", undef);
}
}
/* Process other integer fields */
else if (IS_SACHDR_INT(index)) {
ival = get_sac_hdr_int(&hd, index);
if (ival != SAC_HEADER_FLOAT_UNDEFINED || !undef) {
printf("%d", ival);
}
else {
printf("%s", undef);
}
}
/* Process character fields */
else {
get_sac_hdr_string(&hd, index, rmblank, string);
if (strncmp(string,SAC_HEADER_CHAR_UNDEFINED,6) || !undef) {
printf("%s", string);
}
else {
printf("%s", undef);
}
}
} /* end of key-loop */
/* Skip data part, if input from stdin (1 or 2 data sections) */
if (!isfile) {
ival = (!hd.leven || hd.iftype==IRLIM || hd.iftype==IAMPH) ? 2 : 1;
for (j=0; j<ival*hd.npts; j++) {
if (fread(&fval, sizeof(float), 1, stdin) != 1) {
printf("\n");
break;
}
}
}
} /* end of file-loop */
/* Print newline */
if (issac) printf("\n");
return EXIT_SUCCESS;
}
/************************************************************************/
/* Functions used internally */
/************************************************************************/
int check_zdate(SACHEAD hd)
/************************************************************************
check_zdate - check definition of file reference date (zero time)
Input:
hd SAC header structure (see SACHEAD typedef)
Output: returns 1 (true) or 0 (false)
Author: Nils Maercklin, 2008-10-05
*************************************************************************/
{
if (hd.nzyear != SAC_HEADER_INT_UNDEFINED && \
hd.nzjday != SAC_HEADER_INT_UNDEFINED) {
return 1;
}
else {
return 0;
}
}
int check_ztime(SACHEAD hd)
/************************************************************************
check_ztime - check definition of file reference time (zero time)
Input:
hd SAC header structure (see SACHEAD typedef)
Output: returns 1 (true) or 0 (false)
Author: Nils Maercklin, 2008-10-05
*************************************************************************/
{
if (hd.nzhour != SAC_HEADER_INT_UNDEFINED && \
hd.nzmin != SAC_HEADER_INT_UNDEFINED && \
hd.nzsec != SAC_HEADER_INT_UNDEFINED && \
hd.nzmsec != SAC_HEADER_INT_UNDEFINED) {
return 1;
}
else {
return 0;
}
}
void print_enum(SACHEAD hd, int index, char modif, char *undef)
/************************************************************************
print_enum - print SAC enum header field value to stdout
Input:
hd SAC header structure (see SACHEAD typedef)
index index of header field (enum field assumed)
modif output format modifier (see switch statement)
undef string used for undefined header fields
Output: prints field value to stdout
Author: Nils Maercklin, 2008-10-05
*************************************************************************/
{
int ival = get_sac_hdr_int(&hd, index);
if (ival==SAC_HEADER_INT_UNDEFINED) {
if (!undef) printf("%d", ival);
else printf("%s", undef);
}
else {
switch (modif) {
case 'N': /* Enum name */
case 'n':
printf("%s", sac_enum_name(ival, 0) );
break;
case 'D': /* Enum description */
case 'd':
printf("%s", sac_enum_name(ival, 1) );
break;
case ' ': /* Header field value */
default:
printf("%d", ival);
break;
}
}
}
void print_time(SACHEAD hd, int index, char modif, int eyear, char *undef)
/************************************************************************
print_time - print SAC time header field value to stdout
Input:
hd SAC header structure (see SACHEAD typedef)
index index of header field (time), or INDEX_ZTIME
modif output format modifier (see switch statement)
eyear epoch year
undef string used for undefined header fields
Output: prints field value to stdout
Author: Nils Maercklin, 2008-10-05
*************************************************************************/
{
double etime=0.0;
float fval=0.0;
float sec=0.0;
int month=0;
int day=0;
int year,hour,minute;
int valid_jday=0;
/* Get seconds of zerotime as float */
sec = ((float)hd.nzsec) + 0.001*((float)hd.nzmsec);
/* Convert Julian day to human date */
if (check_zdate(hd)) {
valid_jday = jday_to_hdate(hd.nzyear, hd.nzjday, &month, &day);
}
/* Get header value */
if (index == INDEX_ZTIME) {
fval = 0.0;
}
else {
fval = get_sac_hdr_float(&hd, index);
}
/* Print time */
if (fval == SAC_HEADER_FLOAT_UNDEFINED) {
if (!undef) printf("%g", fval);
else printf("%s", undef);
}
else {
switch (modif) {
case 'E': /* Epoch time in seconds */
case 'e':
if (check_zdate(hd) && check_ztime(hd)) {
etime = date_to_etime(eyear, hd.nzyear, hd.nzjday, \
hd.nzhour, hd.nzmin, sec) + ((double) fval);
printf("%.4lf", etime);
}
else {
if (!undef) printf("%g", fval);
else printf("%s", undef);
}
break;
case 'H': /* Human date and time string */
case 'h':
if (valid_jday && check_ztime(hd)) {
etime = date_to_etime(eyear, hd.nzyear, hd.nzjday, \
hd.nzhour, hd.nzmin, sec) + ((double) fval);
etime_to_date(etime, eyear, &year, &day, &hour, \
&minute, &sec);
jday_to_hdate(year, day, &month, &day);
printf("%04d-%02d-%02d %02d:%02d:%07.4f", \
year, month, day, hour, minute, sec);
}
else {
if (!undef) printf("%g", fval);
else printf("%s", undef);
}
break;
case 'N': /* NORSAR time string */
case 'n':
if (check_zdate(hd) && check_ztime(hd)) {
etime = date_to_etime(eyear, hd.nzyear, hd.nzjday, \
hd.nzhour, hd.nzmin, sec) + ((double) fval);
etime_to_date(etime, eyear, &year, &day, &hour, \
&minute, &sec);
printf("%04d-%03d:%02d.%02d.%06.3f", \
year, day, hour, minute, sec);
}
else {
if (!undef) printf("%g", fval);
else printf("%s", undef);
}
break;
case 'T': /* Time of day (hour, minute, second) */
case 't':
if (check_ztime(hd)) {
etime = date_to_etime(eyear, eyear, 1, \
hd.nzhour, hd.nzmin, sec) + ((double) fval);
etime_to_date(etime, eyear, &year, &day, &hour, \
&minute, &sec);
printf("%02d:%02d:%07.4f", hour, minute, sec);
}
else {
if (!undef) printf("%g", fval);
else printf("%s", undef);
}
break;
default: /* Header value */
printf("%g", fval);
break;
}
}
}
/* END OF FILE */