-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsacio.c
More file actions
4819 lines (4600 loc) · 127 KB
/
sacio.c
File metadata and controls
4819 lines (4600 loc) · 127 KB
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* @brief sac I/O and manipulation
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <float.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/** @cond NO_DOCS */
#define SAC_NULL_HEADER_REQUIRED /**< @private Define a fully NULL sac header */
/** @endcond */
#include <sacio.h>
#include "time64.h"
#include "strip.h"
#include "defs.h"
#include "geodesic.h"
#define ERROR_NOT_A_SAC_FILE 1317 /**< @brief Not a sac file */
#define ERROR_OVERWRITE_FLAG_IS_OFF 1303 /**< @brief Overwrite flag, lovrok is set to 0 */
#define ERROR_WRITING_FILE 115 /**< @brief Error writing sac file */
#define ERROR_READING_FILE 114 /**< @brief Error reading sac file */
#define ERROR_FILE_DOES_NOT_EXIST 108 /**< @brief Error file does not exist */
#define ERROR_OPENING_FILE 101 /**< @brief Error opening sac file */
#define SAC_OK 0 /**< @brief Success, everything is ok */
#define SAC_HEADER_SIZEOF_NUMBER (sizeof(float)) /**< @brief Size of header values in bytes */
#define SAC_DATA_SIZE SAC_HEADER_SIZEOF_NUMBER /**< @brief Size of data values in bytes */
#define SAC_HEADER_VERSION_6 6 /**< @brief Sac version no 6 */
#define SAC_HEADER_VERSION_7 7 /**< @brief Sac version no 7 */
#define SAC_HEADER_MAJOR_VERSION SAC_HEADER_VERSION_6 /**< @brief Current Major sac version number */
#define SAC_HEADER_MAX_VERSION SAC_HEADER_VERSION_7 /**< @brief Current Maximum sac version number */
#define SAC_VERSION_LOCATION 76 /**< @brief Offset in 4-byte words of the header version */
/**
* @defgroup sac sac
* @brief sac file I/O and manipulation
*
*/
/**
* @brief Minimum of two values
* @private
*/
#define MIN(a,b) ((a < b) ? a : b )
/**
* @brief Maximum of two values
* @private
*/
#define MAX(a,b) ((a > b) ? a : b )
/** \cond NO_DOCS */
sacmeta * sac_meta_new();
sac * sac_read_internal(char *filename, int read_data, int *nerr);
double calc_e_even(sac *s);
void sac_write_internal(sac *s, char *filename, int write_data, int swap, int *nerr);
static float array_max(float *y, int n);
static float array_min(float *y, int n);
static float array_mean(float *y, int n);
sac_hdr * sac_hdr_new();
int sac_get_time_ref(sac *s, timespec64 *t);
/** \endcond */
/**
* @brief Get a sac header floating value. Error are returned as SAC_FLOAT_UNDEFINED
* @private
*/
static double
sac_float(sac *s, int id) {
double v = 0;
if(!sac_get_float(s, id, &v)) {
return SAC_FLOAT_UNDEFINED;
}
return v;
}
static int
sac_int(sac *s, int id) {
int v = 0;
if(!sac_get_int(s, id, &v)) {
return SAC_INT_UNDEFINED;
}
return v;
}
/**
* @brief Get begin value
* @private
*/
#define B(s) sac_float(s, SAC_B)
/**
* @brief Get end value
* @private
*/
#define E(s) sac_float(s, SAC_E)
/**
* @brief Get delta time value
* @private
*/
#define DT(s) sac_float(s, SAC_DELTA)
/**
* @param SAC_HEADER_FLOATS
* Number of Floating point values in the SAC Header
* Size: \p SAC_HEADER_SIZEOF_NUMBER bytes
*
*/
#define SAC_HEADER_FLOATS 70 /* 4 bytes (real or float) */
/**
* @param SAC_HEADER_INTEGERS
* Number of Integer values in the SAC Header
* Size: \p SAC_HEADER_SIZEOF_NUMBER bytes
*
*/
#define SAC_HEADER_INTEGERS 15
/**
* @param SAC_HEADER_ENUMS
* Number of Enumerated values in the SAC Header
* Size: \p SAC_HEADER_SIZEOF_NUMBER bytes
*
*/
#define SAC_HEADER_ENUMS 20
/**
* @param SAC_HEADER_LOGICALS
* Number of Logical values in the SAC Header
* Size: \p SAC_HEADER_SIZEOF_NUMBER bytes
*
*/
#define SAC_HEADER_LOGICALS 5
/**
* @param SAC_HEADER_STRINGS
* Number of strings in the SAC Header. The number here is in reality
* one less as the number below as of header version 6. The second
* value, the event name is twice as long as any other characer string
*
*/
#define SAC_HEADER_STRINGS 24 /* 9 bytes (characters) actually 23 + 1 */
/**
* @param SAC_HEADER_NUMBERS
* Number of numeric values in the SAC Header
* Size: \p SAC_HEADER_SIZEOF_NUMBER bytes
*
*/
#define SAC_HEADER_NUMBERS ( SAC_HEADER_FLOATS + SAC_HEADER_INTEGERS + SAC_HEADER_ENUMS + SAC_HEADER_LOGICALS )
/** @cond NO_DOCS */
/**
* @param SAC_HEADER_STRINGS_SIZE_FILE
* Size of file character header data block in four byte increments
* @note 8 * \p SAC_HEADER_STRINGS
*/
#define SAC_HEADER_STRINGS_SIZE_FILE ( 2 * SAC_HEADER_STRINGS )
/**
* @param SAC_HEADER_STRING_LENGTH_FILE
* Size of a character string stored on disk for a SAC header
* Strings are stored without the C string termination character
*/
#define SAC_HEADER_STRING_LENGTH_FILE 8
/**
* @param SAC_HEADER_STRING_SIZE_BYTES_FILE
* Size of string header in file measured in bytes
* SAC_HEADER_STRINGS * SAC_HEADER_STRINGS_LENGTH_FILE
*/
#define SAC_HEADER_STRINGS_SIZE_BYTES_FILE ( SAC_HEADER_STRINGS * SAC_HEADER_STRING_LENGTH_FILE )
/**
* @param SAC_HEADER_NUMBERS_SIZE_BYTES_FILE
* Size of numbers header in file measured in bytes
* SAC_HEADER_NUMBERS * SAC_HEADER_SIZEOF_NUMBER
*/
#define SAC_HEADER_NUMBERS_SIZE_BYTES_FILE ( SAC_HEADER_NUMBERS * SAC_HEADER_SIZEOF_NUMBER )
/** \endcond */
/**
* @brief Read a sac file
*
* @ingroup sac
* @memberof sac
*
* @details Read a sac file, data and header
*
* @param filename file to read data and header from
* @param nerr status code, 0 on success, non-zero on header
*
* @return sac file structure, NULL on failure
*
* Reading a sac file should result in a pointer to a sac-file structure
* @code
* int nerr = 0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* assert_ne(s, NULL);
* assert_eq(nerr, 0);
* @endcode
*
* Reading a file that does not exist will return a NULL pointer and
* an error code of 108
* @code
* int nerr = 0;
* fprintf(stderr, "Error expected - non existant file\n");
* sac *s = sac_read("non-existant-file", &nerr);
* assert_eq(s, NULL);
* assert_eq(nerr, 108);
* fprintf(stderr, "Error expected - non existant file: Done\n");
* @endcode
*
* Reading a file that exists but is not a sac file will return a NULL
* pointer and an error code of 1317
* @code
* int nerr = 0;
* fprintf(stderr, "Error expected - wrong file type\n");
* sac *s = sac_read("sacio.c", &nerr);
* assert_eq(s, NULL);
* assert_eq(nerr, 1317);
* fprintf(stderr, "Error expected - wrong file type: Done\n");
* @endcode
*
*/
sac *
sac_read(char *filename, int *nerr) {
return sac_read_internal(filename, 1, nerr);
}
/**
* @brief Read a sac file header
*
* @ingroup sac
* @memberof sac
*
* @details Read a sac file header
*
* @param filename file to read sac header from
* @param nerr status code, 0 on success, non-zero on failure
*
* @return sac file structure, NULL on failure
*
* @code
* int nerr = 0;
* sac *s = sac_read_header("t/test_io_small.sac", &nerr);
* assert_eq(nerr, 0);
* assert_eq(s->x, NULL);
* assert_eq(s->y, NULL);
* assert_ne(s->h, NULL);
* @endcode
*/
sac *
sac_read_header(char *filename, int *nerr) {
return sac_read_internal(filename, 0, nerr);
}
/**
* @brief write a sac file to disk
*
* @ingroup sac
* @memberof sac
*
* @details write a sac file to disk, header and data
*
* @param s sac file to write to disk
* @param filename file to write to
* @param nerr status code, 0 on success, non-zero on error
*
* Example of writing a sac file
* @code
* int nerr = 0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* assert_eq(nerr, 0);
* sac_write(s, "t/test_io_small.sac.tmp", &nerr);
* assert_eq(nerr, 0);
* @endcode
*/
void
sac_write(sac *s, char *filename, int *nerr) {
sac_write_internal(s, filename, SAC_WRITE_HEADER_AND_DATA, s->m->swap, nerr);
}
/**
* @brief write a sac header to disk
*
* @ingroup sac
* @memberof sac
*
* @details write a sac header to disk
*
* @param s sac header to write to disk
* @param filename file to write to, must exist and be a sac file
* @param nerr status code, 0 on success, non-zero on error
*
* Example of writing a sac header
* @code
* int nerr = 0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* assert_eq(nerr, 0);
* sac_write_header(s, "t/test_io_small.sac", &nerr);
* assert_eq(nerr, 0);
* @endcode
*
* A file that does not exist should return a error code of 108
* @code
* int nerr = 0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* assert_eq(nerr, 0);
* sac_write_header(s, "non-existant-file", &nerr );
* assert_eq(nerr, 108);
* @endcode
*
* A file that is not a sac file should return an error code of 1317
* @code
* int nerr = 0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* assert_eq(nerr, 0);
* sac_write_header(s, "sacio.c", &nerr);
* assert_eq(nerr, 1317);
* @endcode
*/
void
sac_write_header(sac *s, char *filename, int *nerr) {
sac_write_internal(s, filename, SAC_WRITE_HEADER, s->m->swap, nerr);
}
/**
* @brief Identify if a header is a time value
*
* @ingroup sac
* @memberof sac
*
* @details Check is the sac header value is a time value
*
* @param hid sac ::HeaderID
*
* @return True if header value is a time value type
*
* @code
* assert_eq(sac_is_timeval(SAC_B), 1);
* assert_eq(sac_is_timeval(SAC_DEPMAX), 0);
* @endcode
*/
int
sac_is_timeval(int hid) {
switch(hid) {
case SAC_B: case SAC_E: case SAC_O: case SAC_A: case SAC_F:
case SAC_T0: case SAC_T1: case SAC_T2: case SAC_T3:
case SAC_T4: case SAC_T5: case SAC_T6: case SAC_T7:
case SAC_T8: case SAC_T9:
return 1;
break;
default:
break;
}
return 0;
}
/**
* @brief X-Macro for sac_f64_new()
* @private
*/
#define X(name,key) z->key = SAC_FLOAT_UNDEFINED;
/**
* @brief create a new 64 bit float sac header value
*
* @ingroup sac
* @memberof sac
* @private
*
* @details create a new 64 bit float sac header value and set all values to SAC_FLOAT_UNDEFINED
*
* @return newly created 64 bit sac header
*/
sac_f64 *
sac_f64_new() {
sac_f64 *z;
z = (sac_f64 *) malloc(sizeof(sac_f64));
if(z) {
SAC_F64
}
return z;
}
#undef X
/**
* @brief X-Macro for sac_f32_new()
* @private
*/
#define X(name,key) case SAC_##name: s->h->key = (float) value; break;
/**
* @brief set a 32-bit float value
*
* @ingroup sac
* @memberof sac
* @private
*
* @details set a 32-bit float value in the sac header
*
* @param s sac file
* @param n sac ::HeaderID
* @param value value to set (value is converted from double to float)
*
* @return 1 on success, 0 on error
*/
int
sac_set_f32(sac *s, int n, double value) {
switch(n) {
SAC_F32
default:
fprintf(stderr, "Error in sac_set_f32(): Unknown type: %d\n", n);
return 0;
}
return 1;
}
#undef X
/**
* @brief set a 64-bit float value
*
* @ingroup sac
* @memberof sac
* @private
*
* @details set a 64-bit float value in the sac header, values not in the 64-bit header
* are set in the 32-bit header
*
* @param s sac file
* @param n sac ::HeaderID
* @param value value to set
*
* @return return type
*/
int
sac_set_f64(sac *s, int n, double value) {
switch(n) {
#define X(name,key) case SAC_##name: s->z->key = value; s->h->key = (float) value; break;
SAC_F64
#undef X
default:
return sac_set_f32(s, n, (float) value);
//fprintf(stderr, "Error in sac_set_f64(): Unknown type: %d\n", n);
break;
}
return 1;
}
/**
* @brief get a 32-bit float value
*
* @ingroup sac
* @memberof sac
* @private
*
* @details get a 32-bit float value from the sac header
*
* @param s sac file
* @param n sac ::HeaderID
* @param v value to get (must be double precision / 64-bit)
*
* @return 1 on success, 0 on failure
*/
int
sac_get_f32(sac *s, int n, double *v) {
switch(n) {
#define X(name,key) case SAC_##name: *v = s->h->key; break;
SAC_F32
#undef X
default:
fprintf(stderr, "Error in sac_get_f32(): Unknown type: %d\n", n);
return 0;
}
return 1;
}
/**
* @brief get a 64-bit float value
*
* @ingroup sac
* @memberof sac
* @private
*
* @details get a 64-bit float value from the sac header. If the value is not in
* the 64-bit header, value is taken from the 32-bit header
*
* @param s sac file
* @param n sac ::HeaderID
* @param v value to get
*
* @return return type
*/
int
sac_get_f64(sac *s, int n, double *v) {
switch(n) {
#define X(name,key) case SAC_##name: *v = s->z->key; break;
SAC_F64
#undef X
default:
return sac_get_f32(s, n, v);
break;
}
return 1;
}
/**
* @brief Copy all values from the 64-bit to 32-bit float header
*
* @ingroup sac
* @memberof sac
* @private
*
* @details Copy all values from the 64-bit to 32-bit float header
*
* @param s sac file
*
*/
void
sac_copy_f64_to_f32(sac *s) {
#define X(name,key) s->h->key = (float) s->z->key;
SAC_F64
#undef X
}
/**
* @brief Copy all values from the 32-bit to 64-bit float header
*
* @ingroup sac
* @memberof sac
* @private
*
* @details Copy all values from the 32-bit to 64-bit float header
*
* @param s sac file
*
*/
void
sac_copy_f32_to_f64(sac *s) {
#define X(name,key) s->z->key = s->h->key;
SAC_F64
#undef X
}
/**
* @brief Set a time pick value
*
* @ingroup sac
* @memberof sac
*
* @details Set a time pick value in a sac header
*
* @param s sac file
* @param n pick number
* @param value timing value to set (64 bit value)
*
* @return 1 on success, 0 on failure
*
* Exaple of setting the time pick t3 to 13.50
* @code
* int nerr = 0;
* double t = 0.0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* sac_set_pick(s, 3, 13.50);
* sac_get_float(s, SAC_T3, &t);
* assert_eq(t, 13.50);
* @endcode
*/
int
sac_set_pick(sac *s, int n, double value) {
switch(n) {
case 0: return sac_set_float(s, SAC_T0, value); break;
case 1: return sac_set_float(s, SAC_T1, value); break;
case 2: return sac_set_float(s, SAC_T2, value); break;
case 3: return sac_set_float(s, SAC_T3, value); break;
case 4: return sac_set_float(s, SAC_T4, value); break;
case 5: return sac_set_float(s, SAC_T5, value); break;
case 6: return sac_set_float(s, SAC_T6, value); break;
case 7: return sac_set_float(s, SAC_T7, value); break;
case 8: return sac_set_float(s, SAC_T8, value); break;
case 9: return sac_set_float(s, SAC_T9, value); break;
default:
fprintf(stderr, "Error in sac_set_pick(): Unknown pick: %d\n", n);
return 0;
break;
}
return 1;
}
/**
* @brief Get a timing pick
*
* @ingroup sac
* @memberof sac
*
* @details Get a timing pick from the sac header
*
* @param s sac file
* @param n pick number
* @param t return value (64-bit value)
*
* @return 1 on success, 0 on failure
*
* Example of getting a time pick by its number, 0 to 9
* @code
* int nerr = 0;
* double t = 0.0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* sac_set_pick(s, 3, 13.50);
* sac_get_pick(s, 3, &t);
* assert_eq(t, 13.50);
* @endcode
*/
int
sac_get_pick(sac *s, int n, double *t) {
switch(n) {
case 0: return sac_get_float(s, SAC_T0, t); break;
case 1: return sac_get_float(s, SAC_T1, t); break;
case 2: return sac_get_float(s, SAC_T2, t); break;
case 3: return sac_get_float(s, SAC_T3, t); break;
case 4: return sac_get_float(s, SAC_T4, t); break;
case 5: return sac_get_float(s, SAC_T5, t); break;
case 6: return sac_get_float(s, SAC_T6, t); break;
case 7: return sac_get_float(s, SAC_T7, t); break;
case 8: return sac_get_float(s, SAC_T8, t); break;
case 9: return sac_get_float(s, SAC_T9, t); break;
default:
fprintf(stderr, "Error in sac_get_pick(): Unknown pick: %d\n", n);
return 0;
break;
}
return 1;
}
/**
* @brief Set the header version to 7
*
* @ingroup sac
* @memberof sac
*
* @param s sac file
*
* @details Set the header version to `7`.
* See sac::sac_get_float() and sac::sac_set_float() for the details
* about how the different header version impact which floating point
* values are set and get.
*
* @code
* int nerr = 0;
* double t = 0.0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* sac_set_float(s, SAC_DELTA, 1.0/3.0);
*
* // Get delta, 32bit value, from the v6 header
* sac_get_float(s, SAC_DELTA, &t);
* assert_ne(t, 1.0/3.0);
*
* // Switch to the v7 header
* sac_set_v7(s);
*
* // Get delta, 64bit value, from the v7 footer
* sac_get_float(s, SAC_DELTA, &t);
* assert_eq(t, 1.0/3.0);
* @endcode
*/
void
sac_set_v7(sac *s) {
s->h->nvhdr = SAC_HEADER_VERSION_7;
}
/**
* @brief Set the header version to 6
*
* @ingroup sac
* @memberof sac
*
* @param s sac file
*
* @details Set the header version to `6`.
* See sac::sac_get_float() and sac::sac_set_float() for the details
* about how the different header version impact which floating point
* values are set and get.
* @code
* int nerr = 0;
* double t = 0.0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* sac_set_float(s, SAC_DELTA, 1.0/3.0);
*
* // Switch to the v7 header
* sac_set_v7(s);
*
* // Get delta, 64bit value, from the v7 footer
* sac_get_float(s, SAC_DELTA, &t);
* assert_eq(t, 1.0/3.0);
*
* // Switch to the v6 header
* sac_set_v6(s);
*
* // Get delta, 32bit value, from the v6 header
* sac_get_float(s, SAC_DELTA, &t);
* assert_ne(t, 1.0/3.0);
*
* @endcode
*/
void
sac_set_v6(sac *s) {
s->h->nvhdr = SAC_HEADER_VERSION_6;
}
/**
* @brief create a new sac file structure
*
* @ingroup sac
* @memberof sac
*
* @details create a new sac file structure. Header and meta data are allocated.
* Data components are not allocated and should be done with sac_alloc()
* after setting \p the type of file and the number of data points
*
* @return newly created sac file structure
*
* Create a new sac file in memory
* @code
* int v = 0;
* sac *s = sac_new();
* assert_ne(s, NULL);
* assert_eq(s->x, NULL);
* assert_eq(s->y, NULL);
*
* sac_get_int(s, SAC_EVEN, &v);
* assert_eq(v, TRUE);
*
* sac_get_int(s, SAC_POLARITY, &v);
* assert_eq(v, FALSE);
*
* sac_get_int(s, SAC_OVERWRITE, &v);
* assert_eq(v, TRUE);
*
* sac_get_int(s, SAC_CALC_DIST_AZ, &v);
* assert_eq(v, TRUE);
*
* sac_get_int(s, SAC_FILE_TYPE, &v);
* assert_eq(v, ITIME);
* @endcode
*/
sac *
sac_new() {
sac *s;
s = (sac *) malloc(sizeof(sac));
if (s) {
s->h = sac_hdr_new();
if (!s->h) {
goto ERROR;
}
s->m = sac_meta_new();
if (!s->m) {
goto ERROR;
}
s->z = sac_f64_new();
if(!s->z) {
goto ERROR;
}
s->n = 1;
s->y = NULL;
s->x = NULL;
s->sddhdr = NULL;
}
return s;
ERROR:
sac_free(s);
return NULL;
}
/**
* @brief free a sac file structure
*
* @ingroup sac
* @memberof sac
*
* @details free a sac file structure. Free any data components, meta
* data, header and other associated components
*
* @param s sac file struture to free
*
* Example of creating and freeing a sac file in memory
* @code
* sac *s = sac_new();
* sac_set_int(s, SAC_NPTS, 100);
* sac_alloc(s);
* assert_ne(s->y, NULL);
* sac_free(s);
*
* @endcode
*/
void
sac_free(sac * s) {
if (s) {
FREE(s->h);
FREE(s->x);
FREE(s->y);
FREE(s->z);
if (s->m) {
FREE(s->m->filename);
}
FREE(s->m);
FREE(s->sddhdr);
FREE(s);
}
}
/**
* @brief allocate the sac data portion of a sac strucutre
*
* @ingroup sac
* @memberof sac
*
* @details allocate the sac data poriton of a sac structure. The memory
* size the data and components are determined by sac_comps() and
* the number of points \p npts. Any previous data is freed
* before allocation
*
* @param s sac file to allocate the data for
*
* Example of creating and allocating space for data in a sac file in memory
* @code
* sac *s = sac_new();
* sac_set_int(s, SAC_NPTS, 100);
* sac_alloc(s);
* assert_ne(s->y, NULL);
* assert_eq(s->x, NULL);
*
* // Set the file type to Amplitude/Phase (2 components)
* sac_set_int(s, SAC_FILE_TYPE, IAMPH);
* sac_alloc(s);
* assert_ne(s->y, NULL);
* assert_ne(s->x, NULL);
* @endcode
*
*/
void
sac_alloc(sac * s) {
size_t n = 0;
if (!s) {
return;
}
FREE(s->y);
FREE(s->x);
if(s->h->npts <= 0) {
return;
}
n = sizeof(float) * (size_t) s->h->npts;
s->y = (float *) malloc(n);
memset(s->y, 0, n);
if (sac_comps(s) == 2) {
s->x = (float *) malloc(n);
memset(s->x, 0, n);
}
}
#define COPY_MOVE(dst, src, n) do { \
memcpy(dst, src, n); \
dst[n] = 0; \
src += n; \
dst += (n+1); \
} while(0) ;
#define SAC_ALPHA_FLOAT_LINES 14
#define SAC_ALPHA_INT_LINES 8
#define SAC_ALPHA_STRING_LINES 8
#define SAC_ALPHA_FLOAT_FMT "%#15.7g"
#define SAC_ALPHA_INT_FMT "%10d"
#define SAC_ALPHA_STRING_FMT "%8s"
#define SAC_ALPHA_DOUBLE_FMT "%.17g" // https://stackoverflow.com/a/21162120
/**
* @brief Write a sac file in alpha-numeric format
*
* @details Write a sac file into an alpha-numeric format
*
* @param s sac file to write
* @param filename file to write to
* @param nerr error status code
*
* @code
* int nerr = 0;
* sac *s = sac_read("t/test_io_small.sac", &nerr);
* assert_eq(nerr, 0);
*
* // Write the sac file into alpha-numeric format
* sac_write_alpha(s, "t/test_io_small_alpha.sac.tmp", &nerr);
* assert_eq(nerr, 0);
* @endcode
*
*/
void
sac_write_alpha(sac *s, char *filename, int *nerr) {
FILE *fp = NULL;
float *f = NULL;
double *d = NULL;
char *c = NULL;
int *v = NULL;
int i = 0, j = 0;
if(!s || !filename) {
*nerr = 1301;
goto error;
}
if(!(fp = fopen(filename, "w"))) {
*nerr = 101;
goto error;
}
f = &(s->h->_delta);
for(j = 0; j < SAC_ALPHA_FLOAT_LINES; j++) {
for(i = 0; i < 5; i++) {
fprintf(fp, SAC_ALPHA_FLOAT_FMT, *f);
f++;
}
fprintf(fp, "\n");
}
v = &(s->h->nzyear);
for(j = 0; j < SAC_ALPHA_INT_LINES; j++) {
for(i = 0; i < 5; i++) {
fprintf(fp, SAC_ALPHA_INT_FMT, *v);
v++;
}
fprintf(fp, "\n");
}
c = s->h->kstnm;
fprintf(fp, "%8s%16s\n", s->h->kstnm, s->h->kevnm);
c += (8+1)*3;
for(i = 1; i < SAC_ALPHA_STRING_LINES; i++) {
for(j = 0; j < 3; j++) {
fprintf(fp, SAC_ALPHA_STRING_FMT, c); c += 9;
}
fprintf(fp, "\n");
}
for(j = 0; j < sac_comps(s); j++) {
f = (j == 0) ? s->y : s->x;
for(i = 0; i < s->h->npts; i++) {
fprintf(fp, SAC_ALPHA_FLOAT_FMT, f[i]);
if(i % 5 == 4 && i+1 != s->h->npts) {
fprintf(fp,"\n");
}
}
fprintf(fp, "\n");
}
if(s->h->nvhdr == SAC_HEADER_VERSION_7) {
// Version 7 Header
d = &(s->z->_delta);
for(i = 0; i < (int)(sizeof(sac_f64)/sizeof(double)); i++) {
fprintf(fp, SAC_ALPHA_DOUBLE_FMT "\n", *d);
d++;
}
}
error:
if(fp) {
fclose(fp);
}
}
/**
* @brief Read a alpha-numeric sac file
*
* @details Read a sac file in alpha-numeric format
*
* @param filename alpha numeric sac file to read from
* @param nerr error status code
*
* @returns sac file, NULL on error
*
* Example of reading an alpha-numeric sac file
* @code
* int nerr = 0;
* sac *s = sac_read_alpha("t/test_spec_alpha.sac", &nerr);
* assert_eq(nerr, 0);
* assert_ne(s, NULL);
* @endcode
*
* Reading a non-existant file returns a NULL pointer and nerr is set to 101
* @code
* int nerr = 0;
* sac *s = sac_read_alpha("non-existant-file", &nerr);
* assert_eq(nerr, 101);
* assert_eq(s, NULL);
* @endcode
*
* Reading a file that is not a alpha-numeric file returns a NULL pointer and
* nerr is set to 1319
* @code
* int nerr = 0;