-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsacio.c
712 lines (635 loc) · 20 KB
/
sacio.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*******************************************************************************
* sacio.c *
* SAC I/O functions: *
* read_sac_head read SAC header *
* read_sac read SAC binary data *
* read_sac_xy read SAC binary XY data *
* read_sac_pdw read SAC data in a partial data window (cut option) *
* write_sac Write SAC binary data *
* write_sac_xy Write SAC binary XY data *
* new_sac_head Create a new minimal SAC header *
* sac_head_index Find the offset of specified SAC head fields *
* issac Check if a file in in SAC format *
* *
* Author: Dongdong Tian @ USTC *
* *
* Revisions: *
* 2014-03-19 Dongdong Tian Modified from Prof. Lupei Zhu's code *
* 2014-08-02 Dongdong Tian Better function naming and coding style *
* 2014-08-13 Dongdong Tian Add new funtions: *
* - read_sac_xy *
* - write_sac_xy *
* - sac_head_index *
* 2016-03-01 Dongdong Tian Add new function: issac *
* *
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include "sacio.h"
/* function prototype for local use */
static void byte_swap (char *pt, size_t n);
static int check_sac_nvhdr (const int nvhdr);
static void map_chdr_in (char *memar, char *buff);
static int read_head_in (const char *name, SACHEAD *hd, FILE *strm);
static void map_chdr_out (char *memar, char *buff);
static int write_head_out (const char *name, SACHEAD hd, FILE *strm);
/* a SAC structure containing all null values */
static SACHEAD sac_null = {
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345., -12345., -12345., -12345., -12345.,
-12345 , -12345 , -12345 , -12345 , -12345 ,
-12345 , -12345 , -12345 , -12345 , -12345 ,
-12345 , -12345 , -12345 , -12345 , -12345 ,
-12345 , -12345 , -12345 , -12345 , -12345 ,
-12345 , -12345 , -12345 , -12345 , -12345 ,
-12345 , -12345 , -12345 , -12345 , -12345 ,
-12345 , -12345 , -12345 , -12345 , -12345 ,
-12345 , -12345 , -12345 , -12345 , -12345 ,
{ '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }, { '-','1','2','3','4','5',' ',' ' },
{ '-','1','2','3','4','5',' ',' ' }
};
/*
* read_sac_head
*
* Description: Read binary SAC header from file.
*
* IN:
* const char *name : File name
* OUT:
* SACHEAD *hd : SAC header
*
* Return: 0 if success, -1 if failed
*
*/
int read_sac_head(const char *name, SACHEAD *hd)
{
FILE *strm;
int lswap;
if ((strm = fopen(name, "rb")) == NULL) {
fprintf(stderr, "Unable to open %s\n", name);
return -1;
}
lswap = read_head_in(name, hd, strm);
fclose(strm);
return ((lswap == -1) ? -1 : 0);
}
/*
* read_sac
*
* Description: Read binary SAC data from file.
*
* IN:
* const char *name : file name
* OUT:
* SACHEAD *hd : SAC header to be filled
* Return: float pointer to the data array, NULL if failed.
*
*/
float *read_sac(const char *name, SACHEAD *hd)
{
FILE *strm;
float *ar;
int lswap;
size_t sz;
if ((strm = fopen(name, "rb")) == NULL) {
fprintf(stderr, "Unable to open %s\n", name);
return NULL;
}
lswap = read_head_in(name, hd, strm);
if (lswap == -1) {
fclose(strm);
return NULL;
}
sz = (size_t) hd->npts * SAC_DATA_SIZEOF;
if (hd->iftype == IXY) sz *= 2;
if ((ar = (float *)malloc(sz)) == NULL) {
fprintf(stderr, "Error in allocating memory for reading %s\n", name);
fclose(strm);
return NULL;
}
if (fread((char*)ar, sz, 1, strm) != 1) {
fprintf(stderr, "Error in reading SAC data %s\n", name);
free(ar);
fclose(strm);
return NULL;
}
fclose(strm);
if (lswap == TRUE) byte_swap((char*)ar, sz);
return ar;
}
/*
* read_sac_xy
*
* Description: read SAC XY binary file
*
* IN:
* const char *name : file name
* OUT:
* SACHEAD *hd : SAC head to be filled
* float *xdata : pointer for X
* float *ydata : pointer for Y
*
* Return: 0 for success, -1 for fail
*
*/
int read_sac_xy(const char *name, SACHEAD *hd, float *xdata, float *ydata)
{
float *data;
size_t npts;
if ((data = read_sac(name, hd)) == NULL) return -1;
npts = (size_t)hd->npts;
if ((xdata = (float *)malloc(npts*SAC_DATA_SIZEOF)) == NULL) {
fprintf(stderr, "Error in allocating memory for %s\n", name);
free(data);
return -1;
}
if ((ydata = (float *)malloc(npts*SAC_DATA_SIZEOF)) == NULL) {
fprintf(stderr, "Error in allocating memory for %s\n", name);
free(data);
free(xdata);
return -1;
}
memcpy(xdata, data , npts*SAC_DATA_SIZEOF);
memcpy(ydata, data+npts, npts*SAC_DATA_SIZEOF);
free(data);
return 0;
}
/*
* write_sac
*
* Description: write binary SAC data
*
* IN:
* const char *name : file name
* SACHEAD hd : header
* const float *ar : float data array
*
* Return:
* -1 : fail
* 0 : succeed
*
*/
int write_sac(const char *name, SACHEAD hd, const float *ar)
{
FILE *strm;
size_t sz;
if ((strm = fopen(name, "wb")) == NULL) {
fprintf(stderr, "Error in opening file for writing %s\n", name);
return -1;
}
if (write_head_out(name, hd, strm) == -1) {
fclose(strm);
return -1;
}
sz = (size_t)hd.npts * SAC_DATA_SIZEOF;
if (hd.iftype == IXY) sz *= 2;
if (fwrite(ar, sz, 1, strm) != 1) {
fprintf(stderr, "Error in writing SAC data for writing %s\n", name);
fclose(strm);
return -1;
}
fclose(strm);
return 0;
}
/*
* write_sac_xy
*
* Description: write binary SAC XY data
*
* IN:
* const char *name : file name
* SACHEAD hd : header
* const float *xdata : float data array for X
* const float *ydata : float data array for Y
*
* Return:
* -1 : fail
* 0 : succeed
*
*/
int write_sac_xy(const char *name, SACHEAD hd, const float *xdata, const float *ydata)
{
float *ar;
int npts;
int error;
size_t sz;
npts = hd.npts;
sz = (size_t)npts * SAC_DATA_SIZEOF;
if ((ar = (float *)malloc(sz*2)) == NULL) {
fprintf(stderr, "Error in allocating memory for file %s\n", name);
return -1;
}
memcpy(ar, xdata, sz);
memcpy(ar+npts, ydata, sz);
/* needed for XY data */
hd.iftype = IXY;
hd.leven = FALSE;
error = write_sac(name, hd, ar);
free(ar);
return error;
}
/*
* read_sac_pdw
*
* Description:
* Read portion of data from file.
*
* Arguments:
* const char *name : file name
* SACHEAD *hd : SAC header to be filled
* int tmark : time mark in SAC header
* -5 -> b;
* -4 -> e;
* -3 -> o;
* -2 -> a;
* 0-9 -> Tn;
* others -> t=0;
* float t1 : begin time is tmark + t1
* float t2 : end time is tmark + t2
*
* Return:
* float pointer to the data array, NULL if failed.
*
*/
float *read_sac_pdw(const char *name, SACHEAD *hd, int tmark, float t1, float t2)
{
FILE *strm;
int lswap;
float tref;
int nt1, nt2, npts, nn;
float *ar, *fpt;
if ((strm = fopen(name, "rb")) == NULL) {
fprintf(stderr, "Error in opening %s\n", name);
return NULL;
}
lswap = read_head_in(name, hd, strm);
if (lswap == -1) {
fclose(strm);
return NULL;
}
nn = (int)((t2-t1)/hd->delta);
if (nn<=0 || (ar = (float *)calloc((size_t)nn, SAC_DATA_SIZEOF)) == NULL) {
fprintf(stderr, "Errorin allocating memory for reading %s n=%d\n", name, nn);
fclose(strm);
return NULL;
}
tref = 0.;
if (tmark>=-5 && tmark<=9 && tmark!=-1) {
tref = *((float *) hd + TMARK + tmark);
if (fabs(tref+12345.)<0.1) {
fprintf(stderr, "Time mark undefined in %s\n", name);
free(ar);
fclose(strm);
return NULL;
}
}
t1 += tref;
nt1 = (int)((t1 - hd->b) / hd->delta);
nt2 = nt1 + nn;
npts = hd->npts;
hd->npts = nn;
hd->b = t1;
hd->e = t1 + nn * hd->delta;
if (nt1>npts || nt2 <0) return ar; /* return zero filled array */
/* maybe warnings are needed! */
if (nt1<0) {
fpt = ar - nt1;
nt1 = 0;
} else {
if (fseek(strm, nt1*SAC_DATA_SIZEOF, SEEK_CUR) < 0) {
fprintf(stderr, "Error in seek %s\n", name);
free(ar);
fclose(strm);
return NULL;
}
fpt = ar;
}
if (nt2>npts) nt2 = npts;
nn = nt2 - nt1;
if (fread((char *)fpt, (size_t)nn * SAC_DATA_SIZEOF, 1, strm) != 1) {
fprintf(stderr, "Error in reading SAC data %s\n", name);
free(ar);
fclose(strm);
return NULL;
}
fclose(strm);
if (lswap == TRUE) byte_swap((char*)ar, (size_t)nn*SAC_DATA_SIZEOF);
return ar;
}
/*
* new_sac_head
*
* Description: create a new SAC header with required fields
*
* IN:
* float dt : sample interval
* int ns : number of points
* float b0 : starting time
*/
SACHEAD new_sac_head(float dt, int ns, float b0)
{
SACHEAD hd = sac_null;
hd.delta = dt;
hd.npts = ns;
hd.b = b0;
hd.o = 0.;
hd.e = b0+(ns-1)*dt;
hd.iztype = IO;
hd.iftype = ITIME;
hd.leven = TRUE;
hd.nvhdr = SAC_HEADER_MAJOR_VERSION;
return hd;
}
/*
* sac_head_index
*
* Description: return the index of a specified sac head field
*
* In:
* const char *name : name of sac head field
* Return:
* index of a specified field in sac head
*
*/
int sac_head_index(const char *name)
{
const char fields[SAC_HEADER_NUMBERS+SAC_HEADER_STRINGS][10] = {
"delta", "depmin", "depmax", "scale", "odelta",
"b", "e", "o", "a", "internal1",
"t0", "t1", "t2", "t3", "t4",
"t5", "t6", "t7", "t8", "t9",
"f", "resp0", "resp1", "resp2", "resp3",
"resp4", "resp5", "resp6", "resp7", "resp8",
"resp9", "stla", "stlo", "stel", "stdp",
"evla", "evlo", "evel", "evdp", "mag",
"user0", "user1", "user2", "user3", "user4",
"user5", "user6", "user7", "user8", "user9",
"dist", "az", "baz", "gcarc", "internal2",
"internal3","depmen", "cmpaz", "cmpinc", "xminimum",
"xmaximum", "yminimum", "ymaximum", "unused1", "unused2",
"unused3", "unused4", "unused5", "unused6", "unused7",
"nzyear", "nzjday", "nzhour", "nzmin", "nzsec",
"nzmsec", "nvhdr", "norid", "nevid", "npts",
"internal4","nwfid", "nxsize", "nysize", "unused8",
"iftype", "idep", "iztype", "unused9", "iinst",
"istreg", "ievreg", "ievtyp", "iqual", "isynth",
"imagtyp", "imagsrc", "unused10", "unused11", "unused12",
"unused13", "unused14", "unused15", "unused16", "unused17",
"leven", "lpspol", "lovrok", "lcalda", "unused18",
"kstnm", "kevnm", "kevnmmore",
"khole", "ko", "ka",
"kt0", "kt1", "kt2",
"kt3", "kt4", "kt5",
"kt6", "kt7", "kt8",
"kt9", "kf", "kuser0",
"kuser1", "kuser2", "kcmpnm",
"knetwk", "kdatrd", "kinst",
};
int i;
for (i=0; i<SAC_HEADER_NUMBERS+SAC_HEADER_STRINGS; i++)
if ((strcasecmp(name, fields[i]) == 0)) return i;
return -1;
}
/*
* issac
*
* Description: check if a file is in SAC format
*
* In:
* const char *name : sac filename
* Return:
* -1 : fail
* TRUE : is a SAC file
* FALSE : not a SAC file
*
*/
int issac(const char *name)
{
FILE *strm;
int nvhdr;
if ((strm = fopen(name, "rb")) == NULL) {
fprintf(stderr, "Unable to open %s\n", name);
return -1;
}
if (fseek(strm, SAC_VERSION_LOCATION * SAC_DATA_SIZEOF, SEEK_SET)) return FALSE;
if (fread(&nvhdr, sizeof(int), 1, strm) != 1) return FALSE;
if (check_sac_nvhdr(nvhdr) == -1) return FALSE;
else return TRUE;
}
/******************************************************************************
* *
* Functions below are only for local use! *
* *
******************************************************************************/
/*
* byte_swap : reverse the byte order of 4 bytes int/float.
*
* IN:
* char *pt : pointer to byte array
* size_t n : number of bytes
* Return: none
*
* Notes:
* For 4 bytes,
* byte swapping means taking [0][1][2][3],
* and turning it into [3][2][1][0]
*/
static void byte_swap(char *pt, size_t n)
{
size_t i ;
char tmp ;
for (i=0; i<n; i+=4) {
tmp = pt[i+3];
pt[i+3] = pt[i];
pt[i] = tmp;
tmp = pt[i+2];
pt[i+2] = pt[i+1];
pt[i+1] = tmp;
}
}
/*
* check_sac_nvhdr
*
* Description: Determine the byte order of the SAC file
*
* IN:
* const int nvhdr : nvhdr from header
*
* Return:
* FALSE no byte order swap is needed
* TRUE byte order swap is needed
* -1 not in sac format ( nvhdr != SAC_HEADER_MAJOR_VERSION )
*
*/
static int check_sac_nvhdr(const int nvhdr)
{
int lswap = FALSE;
if (nvhdr != SAC_HEADER_MAJOR_VERSION) {
byte_swap((char*) &nvhdr, SAC_DATA_SIZEOF);
if (nvhdr == SAC_HEADER_MAJOR_VERSION)
lswap = TRUE;
else
lswap = -1;
}
return lswap;
}
/*
* map_chdr_in:
* map strings from buffer to memory
*/
static void map_chdr_in(char *memar, char *buff)
{
char *ptr1;
char *ptr2;
int i;
ptr1 = memar;
ptr2 = buff;
memcpy(ptr1, ptr2, 8);
*(ptr1+8) = '\0';
ptr1 += 9;
ptr2 += 8;
memcpy(ptr1, ptr2, 16);
*(ptr1+16) = '\0';
ptr1 += 18;
ptr2 += 16;
for (i=0; i<21; i++) {
memcpy(ptr1, ptr2, 8);
*(ptr1+8) = '\0';
ptr1 += 9;
ptr2 += 8;
}
}
/*
* read_head_in:
* read sac header in and deal with possible byte swap.
*
* IN:
* const char *name : file name, only for debug
* SACHEAD *hd : header to be filled
* FILE *strm : file handler
*
* Return:
* 0 : Succeed and no byte swap
* 1 : Succeed and byte swap
* -1 : fail.
*/
static int read_head_in(const char *name, SACHEAD *hd, FILE *strm)
{
char *buffer;
int lswap;
if (sizeof(float) != SAC_DATA_SIZEOF || sizeof(int) != SAC_DATA_SIZEOF) {
fprintf(stderr, "Mismatch in size of basic data type!\n");
return -1;
}
/* read numeric parts of the SAC header */
if (fread(hd, SAC_HEADER_NUMBERS_SIZE, 1, strm) != 1) {
fprintf(stderr, "Error in reading SAC header %s\n", name);
return -1;
}
/* Check Header Version and Endian */
lswap = check_sac_nvhdr(hd->nvhdr);
if (lswap == -1) {
fprintf(stderr, "Warning: %s not in sac format.\n", name);
return -1;
} else if (lswap == TRUE) {
byte_swap((char *)hd, SAC_HEADER_NUMBERS_SIZE);
}
/* read string parts of the SAC header */
if ((buffer = (char *)malloc(SAC_HEADER_STRINGS_SIZE)) == NULL) {
fprintf(stderr, "Error in allocating memory %s\n", name);
return -1;
}
if (fread(buffer, SAC_HEADER_STRINGS_SIZE, 1, strm) != 1) {
fprintf(stderr, "Error in reading SAC header %s\n", name);
free(buffer);
return -1;
}
map_chdr_in((char *)(hd)+SAC_HEADER_NUMBERS_SIZE, buffer);
free(buffer);
return lswap;
}
/*
* map_chdr_out:
* map strings from memory to buffer
*/
static void map_chdr_out(char *memar, char *buff)
{
char *ptr1;
char *ptr2;
int i;
ptr1 = memar;
ptr2 = buff;
memcpy(ptr2, ptr1, 8);
ptr1 += 9;
ptr2 += 8;
memcpy(ptr2, ptr1, 16);
ptr1 += 18;
ptr2 += 16;
for (i=0; i<21; i++) {
memcpy(ptr2, ptr1, 8);
ptr1 += 9;
ptr2 += 8;
}
}
/*
*
* write_head_out
*
* IN:
* const char *name : file name, only for debug
* SACHEAD hd : header to be written
* FILE *strm : file handler
*
* Return:
* -1 : failed.
* 0 : success.
*/
static int write_head_out(const char *name, SACHEAD hd, FILE *strm)
{
char *buffer;
if (sizeof(float) != SAC_DATA_SIZEOF || sizeof(int) != SAC_DATA_SIZEOF) {
fprintf(stderr, "Mismatch in size of basic data type!\n");
return -1;
}
if (fwrite(&hd, SAC_HEADER_NUMBERS_SIZE, 1, strm) != 1) {
fprintf(stderr, "Error in writing SAC data for writing %s\n", name);
return -1;
}
if ((buffer = (char *)malloc(SAC_HEADER_STRINGS_SIZE)) == NULL){
fprintf(stderr, "Error in allocating memory %s\n", name);
return -1;
}
map_chdr_out((char *)(&hd)+SAC_HEADER_NUMBERS_SIZE, buffer);
if (fwrite(buffer, SAC_HEADER_STRINGS_SIZE, 1, strm) != 1) {
fprintf(stderr, "Error in writing SAC data for writing %s\n", name);
return -1;
}
free(buffer);
return 0;
}