-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsegyFunc.cpp
2671 lines (2620 loc) · 82.8 KB
/
csegyFunc.cpp
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
/*
The below code was copied from the SegYIO segy.c file
downloaded from https://github.com/equinor/segyio
And Madagascar SegY reader
*/
#include "csegyFunc.h"
#include "csegydlg.h"
#include <utility>
#include <cstring>
#include <bitset>
//namespace GnV //GnGViewer
//{
CSegYFunc::CSegYFunc() :
little_endian(false)
{
if (is_little_endian())
this->little_endian = true;
}
bool CSegYFunc::is_little_endian()
{
/****************************************************
* From http://stackoverflow.com/questions/12791864/ *
* c-program-to-check-little-vs-big-endian *
****************************************************/
volatile quint32 i = 0x01234567;
return (*((uint8_t*)(&i))) == 0x67;
}
// Convert in place the char array arr[narr]: EBC to ASCII
void CSegYFunc::ebc2asc(int narr, char* arr)
{
int i;
unsigned char j;
for (i = 0; i < narr; i++)
{
j = (unsigned char)arr[i];
arr[i] = (char)EBCtoASC[j];
}
}
qint32 CSegYFunc::convert4(const char* buf) //convert buf to 4-byte int32_t
{
union {
byte b[4];
int32_t s;
} x;
std::memcpy(x.b, buf, 4);
if (little_endian) {
std::swap(x.b[0], x.b[3]);
std::swap(x.b[1], x.b[2]);
}
return x.s;
}
qint16 CSegYFunc::convert2(const char* buf) //convert buf to 2-byte int16_t
{
union {
byte b[2];
int16_t s;
} x;
std::memcpy(x.b, buf, 2);
if (little_endian) std::swap(x.b[0], x.b[1]);
return x.s;
}
//Below Code is from SegYIO
#define IEEEMAX 0x7FFFFFFF
#define IEMAXIB 0x611FFFFF
#define IEMINIB 0x21200000
#ifdef HOST_BIG_ENDIAN
#define HOST_LSB 0
#define HOST_MSB 1
#else
#define HOST_LSB 1
#define HOST_MSB 0
#endif
#if defined __GNUC__
#define bswap64(x) __builtin_bswap64((x))
#define bswap32(x) __builtin_bswap32((x))
#define bswap16(x) __builtin_bswap16((x))
#else
#define bswap64(v) ( ((v & 0x00000000000000FFull) << 56) \
| ((v & 0x000000000000FF00ull) << 40) \
| ((v & 0x0000000000FF0000ull) << 24) \
| ((v & 0x00000000FF000000ull) << 8) \
| ((v & 0x000000FF00000000ull) >> 8) \
| ((v & 0x0000FF0000000000ull) >> 24) \
| ((v & 0x00FF000000000000ull) >> 40) \
| ((v & 0xFF00000000000000ull) >> 56) \
)
#define bswap32(v) ( (((v) & 0x000000FF) << 24) \
| (((v) & 0x0000FF00) << 8) \
| (((v) & 0x00FF0000) >> 8) \
| (((v) & 0xFF000000) >> 24) \
)
#define bswap16(v) ( (((v) & 0x00FF) << 8) \
| (((v) & 0xFF00) >> 8) \
)
#endif // __GNUC__
inline void CSegYFunc::ibm_native(void* buf) {
static int it[8] = { 0x21800000, 0x21400000, 0x21000000, 0x21000000,
0x20c00000, 0x20c00000, 0x20c00000, 0x20c00000 };
static int mt[8] = { 8, 4, 2, 2, 1, 1, 1, 1 };
unsigned int manthi, iexp, inabs;
int ix;
uint32_t u;
memcpy(&u, buf, sizeof(u));
manthi = u & 0x00ffffff;
ix = manthi >> 21;
iexp = ((u & 0x7f000000) - it[ix]) << 1;
manthi = manthi * mt[ix] + iexp;
inabs = u & 0x7fffffff;
if (inabs > IEMAXIB) manthi = IEEEMAX;
manthi = manthi | (u & 0x80000000);
u = (inabs < IEMINIB) ? 0 : manthi;
memcpy(buf, &u, sizeof(u));
}
uint16_t CSegYFunc::htobe16(uint16_t v) {
#if HOST_LSB
return bswap16(v);
#else
return v;
#endif
}
uint32_t CSegYFunc::be32toh(uint32_t v) {
#if HOST_LSB
return bswap32(v);
#else
return v;
#endif
}
inline void CSegYFunc::native_ibm(void* buf) {
static int it[4] = { 0x21200000, 0x21400000, 0x21800000, 0x22100000 };
static int mt[4] = { 2, 4, 8, 1 };
unsigned int manthi, iexp, ix;
uint32_t u;
memcpy(&u, buf, sizeof(u));
ix = (u & 0x01800000) >> 23;
iexp = ((u & 0x7e000000) >> 1) + it[ix];
manthi = (mt[ix] * (u & 0x007fffff)) >> 3;
manthi = (manthi + iexp) | (u & 0x80000000);
u = (u & 0x7fffffff) ? manthi : 0;
memcpy(buf, &u, sizeof(u));
}
void CSegYFunc::ibm2ieee(void* to, const void* from)
{
uint32_t u;
memcpy(&u, from, sizeof(u));
u = be32toh(u);
ibm_native(&u);
memcpy(to, &u, sizeof(u));
}
void CSegYFunc::ibm2float(float_t* y, const char* num)
{
//std::valarray<quint32> ibmSamp (numSamp);
quint32 x, s, f;
const quint32 MAXFLOAT = 0x7F7FFFFF;
int32_t e;
x = convert4(num);
//check for special case of zero
if ((x & 0x7fffffff) == 0)
*y = 0.0;
//fetch the sign
s = x & 0x80000000;
//exponent (removing excess 64)
e = ((x & 0x7f000000) >> 24) - 64;
//and fraction
f = x & 0x00ffffff;
//convert scale factor from base-16 to base-2
if (e >= 0) {
e <<= 2;
}
else {
e = -((-e) << 2);
}
//convert exponent for 24 bit fraction to 23 bit fraction
e -= 1;
//normalize the fraction
if (f != 0) {
while ((f & 0x00800000) == 0) {
f <<= 1;
e -= 1;
}
}
//drop the '1' preceeding the binary point
f &= 0x007fffff;
//convert exponent to excess 127 and store the number
if ((e += 127) >= 255) {
s |= MAXFLOAT;
}
else if (e > 0) {
s |= (e << 23) | f;
}
std::memcpy(&y, &s, 4);
}
//}
//#define _POSIX_SOURCE /* fileno */
//
///* 64-bit off_t in ftello */
//#define _POSIX_C_SOURCE 200808L
//#define _FILE_OFFSET_BITS 64
//
//#if defined(_WIN32) || defined(_MSC_VER)
// /* MultiByteToWideChar */
//#include <windows.h>
//#endif
//
//#ifdef HAVE_MMAP
//#define _POSIX_SOURCE
//#include <sys/mman.h>
//#endif //HAVE_MMAP
//
//#ifdef HAVE_SYS_STAT_H
//#include <sys/types.h>
//#include <sys/stat.h>
//#endif //HAVE_SYS_STAT_H
//
//#include <assert.h>
//#include <limits.h>
//#include <math.h>
//#include <stdbool.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//
//#include <segyio/segy.h>
//#include <segyio/util.h>
//
////static const unsigned char CSegYFunc::a2e[256] = {
//// 0, 1, 2, 3, 55, 45, 46, 47, 22, 5, 37, 11, 12, 13, 14, 15,
//// 16, 17, 18, 19, 60, 61, 50, 38, 24, 25, 63, 39, 28, 29, 30, 31,
//// 64, 79, 127,123,91, 108,80, 125,77, 93, 92, 78, 107,96, 75, 97,
//// 240,241,242,243,244,245,246,247,248,249,122,94, 76, 126,110,111,
//// 124,193,194,195,196,197,198,199,200,201,209,210,211,212,213,214,
//// 215,216,217,226,227,228,229,230,231,232,233,74, 224,90, 95, 109,
//// 121,129,130,131,132,133,134,135,136,137,145,146,147,148,149,150,
//// 151,152,153,162,163,164,165,166,167,168,169,192,106,208,161,7,
//// 32, 33, 34, 35, 36, 21, 6, 23, 40, 41, 42, 43, 44, 9, 10, 27,
//// 48, 49, 26, 51, 52, 53, 54, 8, 56, 57, 58, 59, 4, 20, 62, 225,
//// 65, 66, 67, 68, 69, 70, 71, 72, 73, 81, 82, 83, 84, 85, 86, 87,
//// 88, 89, 98, 99, 100,101,102,103,104,105,112,113,114,115,116,117,
//// 118,119,120,128,138,139,140,141,142,143,144,154,155,156,157,158,
//// 159,160,170,171,172,173,174,175,176,177,178,179,180,181,182,183,
//// 184,185,186,187,188,189,190,191,202,203,204,205,206,207,218,219,
//// 220,221,222,223,234,235,236,237,238,239,250,251,252,253,254,255
////};
////
////static const unsigned char CSegYFunc::e2a[256] = {
//// 0, 1, 2, 3, 156,9, 134,127,151,141,142, 11,12, 13, 14, 15,
//// 16, 17, 18, 19, 157,133,8, 135,24, 25, 146,143,28, 29, 30, 31,
//// 128,129,130,131,132,10, 23, 27, 136,137,138,139,140,5, 6, 7,
//// 144,145,22, 147,148,149,150,4, 152,153,154,155,20, 21, 158,26,
//// 32, 160,161,162,163,164,165,166,167,168,91, 46, 60, 40, 43, 33,
//// 38, 169,170,171,172,173,174,175,176,177,93, 36, 42, 41, 59, 94,
//// 45, 47, 178,179,180,181,182,183,184,185,124,44, 37, 95, 62, 63,
//// 186,187,188,189,190,191,192,193,194,96, 58, 35, 64, 39, 61, 34,
//// 195,97, 98, 99, 100,101,102,103,104,105,196,197,198,199,200,201,
//// 202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208,
//// 209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215,
//// 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
//// 123,65, 66, 67, 68, 69, 70, 71, 72, 73, 232,233,234,235,236,237,
//// 125,74, 75, 76, 77, 78, 79, 80, 81, 82, 238,239,240,241,242,243,
//// 92, 159,83, 84, 85, 86, 87, 88, 89, 90, 244,245,246,247,248,249,
//// 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 250,251,252,253,254,255
////};
//
////static int CSegYFunc::encode(char* dst,
//// const char* src,
//// const unsigned char* conv,
//// size_t n) {
//// for (size_t i = 0; i < n; ++i)
//// dst[i] = (char)conv[(unsigned char)src[i]];
////
//// return SEGY_OK;
////}
//
//#ifdef HOST_BIG_ENDIAN
//#define HOST_LSB 0
//#define HOST_MSB 1
//#else
//#define HOST_LSB 1
//#define HOST_MSB 0
//#endif
//
//#if defined __GNUC__
//#define bswap64(x) __builtin_bswap64((x))
//#define bswap32(x) __builtin_bswap32((x))
//#define bswap16(x) __builtin_bswap16((x))
//#else
//#define bswap64(v) ( ((v & 0x00000000000000FFull) << 56) \
// | ((v & 0x000000000000FF00ull) << 40) \
// | ((v & 0x0000000000FF0000ull) << 24) \
// | ((v & 0x00000000FF000000ull) << 8) \
// | ((v & 0x000000FF00000000ull) >> 8) \
// | ((v & 0x0000FF0000000000ull) >> 24) \
// | ((v & 0x00FF000000000000ull) >> 40) \
// | ((v & 0xFF00000000000000ull) >> 56) \
// )
//
//#define bswap32(v) ( (((v) & 0x000000FF) << 24) \
// | (((v) & 0x0000FF00) << 8) \
// | (((v) & 0x00FF0000) >> 8) \
// | (((v) & 0xFF000000) >> 24) \
// )
//
//#define bswap16(v) ( (((v) & 0x00FF) << 8) \
// | (((v) & 0xFF00) >> 8) \
// )
//#endif // __GNUC__
//
//static uint16_t CSegYFunc::htobe16(uint16_t v) {
//#if HOST_LSB
// return bswap16(v);
//#else
// return v;
//#endif
//}
//
//static uint32_t CSegYFunc::htobe32(uint32_t v) {
//#if HOST_LSB
// return bswap32(v);
//#else
// return v;
//#endif
//}
//
//static uint16_t CSegYFunc::be16toh(uint16_t v) {
//#if HOST_LSB
// return bswap16(v);
//#else
// return v;
//#endif
//}
//
//
///*
// * DEPRECATED
// * ebcdic2ascii and ascii2ebcdic are deprecated in favour of the length-aware
// * encode. They will be removed in segyio2. These functions were never public
// * (in the sense they're not available in headers), but currently have external
// * linkage.
// */
////void CSegYFunc::ebcdic2ascii(const char* ebcdic, char* ascii) {
//// size_t len = strlen(ebcdic);
//// encode(ascii, ebcdic, e2a, len);
//// ascii[len] = '\0';
////}
////
////void CSegYFunc::ascii2ebcdic(const char* ascii, char* ebcdic) {
//// size_t len = strlen(ascii);
//// encode(ebcdic, ascii, a2e, len);
//// ebcdic[len] = '\0';
////}
//
//static inline void CSegYFunc::native_ibm(void* buf) {
// static int it[4] = { 0x21200000, 0x21400000, 0x21800000, 0x22100000 };
// static int mt[4] = { 2, 4, 8, 1 };
// unsigned int manthi, iexp, ix;
// uint32_t u;
//
// memcpy(&u, buf, sizeof(u));
//
// ix = (u & 0x01800000) >> 23;
// iexp = ((u & 0x7e000000) >> 1) + it[ix];
// manthi = (mt[ix] * (u & 0x007fffff)) >> 3;
// manthi = (manthi + iexp) | (u & 0x80000000);
// u = (u & 0x7fffffff) ? manthi : 0;
// memcpy(buf, &u, sizeof(u));
//}
//
//void CSegYFunc::ibm2ieee(void* to, const void* from) {
// uint32_t u;
// memcpy(&u, from, sizeof(u));
// u = be32toh(u);
//
// ibm_native(&u);
// memcpy(to, &u, sizeof(u));
//}
//
//void CSegYFunc::ieee2ibm(void* to, const void* from) {
// uint32_t u;
// memcpy(&u, from, sizeof(u));
//
// native_ibm(&u);
// u = be32toh(u);
// memcpy(to, &u, sizeof(u));
//}
////
/////* Lookup table for field sizes. All values not explicitly set are 0 */
////static int CSegYFunc::field_size[] = {
//// [SEGY_TR_CDP_X] = 4,
//// [SEGY_TR_CDP_Y] = 4,
//// [SEGY_TR_CROSSLINE] = 4,
//// [SEGY_TR_ENERGY_SOURCE_POINT] = 4,
//// [SEGY_TR_ENSEMBLE] = 4,
//// [SEGY_TR_FIELD_RECORD] = 4,
//// [SEGY_TR_GROUP_WATER_DEPTH] = 4,
//// [SEGY_TR_GROUP_X] = 4,
//// [SEGY_TR_GROUP_Y] = 4,
//// [SEGY_TR_INLINE] = 4,
//// [SEGY_TR_NUMBER_ORIG_FIELD] = 4,
//// [SEGY_TR_NUM_IN_ENSEMBLE] = 4,
//// [SEGY_TR_OFFSET] = 4,
//// [SEGY_TR_RECV_DATUM_ELEV] = 4,
//// [SEGY_TR_RECV_GROUP_ELEV] = 4,
//// [SEGY_TR_SEQ_FILE] = 4,
//// [SEGY_TR_SEQ_LINE] = 4,
//// [SEGY_TR_SHOT_POINT] = 4,
//// [SEGY_TR_SOURCE_DATUM_ELEV] = 4,
//// [SEGY_TR_SOURCE_DEPTH] = 4,
//// [SEGY_TR_SOURCE_ENERGY_DIR_MANT] = 4,
//// [SEGY_TR_SOURCE_MEASURE_MANT] = 4,
//// [SEGY_TR_SOURCE_SURF_ELEV] = 4,
//// [SEGY_TR_SOURCE_WATER_DEPTH] = 4,
//// [SEGY_TR_SOURCE_X] = 4,
//// [SEGY_TR_SOURCE_Y] = 4,
//// [SEGY_TR_TRANSDUCTION_MANT] = 4,
//// [SEGY_TR_UNASSIGNED1] = 4,
//// [SEGY_TR_UNASSIGNED2] = 4,
////
//// [SEGY_TR_ALIAS_FILT_FREQ] = 2,
//// [SEGY_TR_ALIAS_FILT_SLOPE] = 2,
//// [SEGY_TR_COORD_UNITS] = 2,
//// [SEGY_TR_CORRELATED] = 2,
//// [SEGY_TR_DATA_USE] = 2,
//// [SEGY_TR_DAY_OF_YEAR] = 2,
//// [SEGY_TR_DELAY_REC_TIME] = 2,
//// [SEGY_TR_DEVICE_ID] = 2,
//// [SEGY_TR_ELEV_SCALAR] = 2,
//// [SEGY_TR_GAIN_TYPE] = 2,
//// [SEGY_TR_GAP_SIZE] = 2,
//// [SEGY_TR_GEOPHONE_GROUP_FIRST] = 2,
//// [SEGY_TR_GEOPHONE_GROUP_LAST] = 2,
//// [SEGY_TR_GEOPHONE_GROUP_ROLL1] = 2,
//// [SEGY_TR_GROUP_STATIC_CORR] = 2,
//// [SEGY_TR_GROUP_UPHOLE_TIME] = 2,
//// [SEGY_TR_HIGH_CUT_FREQ] = 2,
//// [SEGY_TR_HIGH_CUT_SLOPE] = 2,
//// [SEGY_TR_HOUR_OF_DAY] = 2,
//// [SEGY_TR_INSTR_GAIN_CONST] = 2,
//// [SEGY_TR_INSTR_INIT_GAIN] = 2,
//// [SEGY_TR_LAG_A] = 2,
//// [SEGY_TR_LAG_B] = 2,
//// [SEGY_TR_LOW_CUT_FREQ] = 2,
//// [SEGY_TR_LOW_CUT_SLOPE] = 2,
//// [SEGY_TR_MEASURE_UNIT] = 2,
//// [SEGY_TR_MIN_OF_HOUR] = 2,
//// [SEGY_TR_MUTE_TIME_END] = 2,
//// [SEGY_TR_MUTE_TIME_START] = 2,
//// [SEGY_TR_NOTCH_FILT_FREQ] = 2,
//// [SEGY_TR_NOTCH_FILT_SLOPE] = 2,
//// [SEGY_TR_OVER_TRAVEL] = 2,
//// [SEGY_TR_SAMPLE_COUNT] = 2,
//// [SEGY_TR_SAMPLE_INTER] = 2,
//// [SEGY_TR_SCALAR_TRACE_HEADER] = 2,
//// [SEGY_TR_SEC_OF_MIN] = 2,
//// [SEGY_TR_SHOT_POINT_SCALAR] = 2,
//// [SEGY_TR_SOURCE_ENERGY_DIR_EXP] = 2,
//// [SEGY_TR_SOURCE_GROUP_SCALAR] = 2,
//// [SEGY_TR_SOURCE_MEASURE_EXP] = 2,
//// [SEGY_TR_SOURCE_MEASURE_UNIT] = 2,
//// [SEGY_TR_SOURCE_STATIC_CORR] = 2,
//// [SEGY_TR_SOURCE_TYPE] = 2,
//// [SEGY_TR_SOURCE_UPHOLE_TIME] = 2,
//// [SEGY_TR_STACKED_TRACES] = 2,
//// [SEGY_TR_SUBWEATHERING_VELO] = 2,
//// [SEGY_TR_SUMMED_TRACES] = 2,
//// [SEGY_TR_SWEEP_FREQ_END] = 2,
//// [SEGY_TR_SWEEP_FREQ_START] = 2,
//// [SEGY_TR_SWEEP_LENGTH] = 2,
//// [SEGY_TR_SWEEP_TAPERLEN_END] = 2,
//// [SEGY_TR_SWEEP_TAPERLEN_START] = 2,
//// [SEGY_TR_SWEEP_TYPE] = 2,
//// [SEGY_TR_TAPER_TYPE] = 2,
//// [SEGY_TR_TIME_BASE_CODE] = 2,
//// [SEGY_TR_TOT_STATIC_APPLIED] = 2,
//// [SEGY_TR_TRACE_ID] = 2,
//// [SEGY_TR_TRANSDUCTION_EXP] = 2,
//// [SEGY_TR_TRANSDUCTION_UNIT] = 2,
//// [SEGY_TR_WEATHERING_VELO] = 2,
//// [SEGY_TR_WEIGHTING_FAC] = 2,
//// [SEGY_TR_YEAR_DATA_REC] = 2,
////};
////
////#define HEADER_SIZE SEGY_TEXT_HEADER_SIZE
////
/////*
//// * Supporting same byte offsets as in the segy specification, i.e. from the
//// * start of the *text header*, not the binary header.
//// */
////static int bfield_size[] = {
//// [-HEADER_SIZE + SEGY_BIN_JOB_ID] = 4,
//// [-HEADER_SIZE + SEGY_BIN_LINE_NUMBER] = 4,
//// [-HEADER_SIZE + SEGY_BIN_REEL_NUMBER] = 4,
//// [-HEADER_SIZE + SEGY_BIN_EXT_TRACES] = 4,
//// [-HEADER_SIZE + SEGY_BIN_EXT_AUX_TRACES] = 4,
//// [-HEADER_SIZE + SEGY_BIN_EXT_SAMPLES] = 4,
//// [-HEADER_SIZE + SEGY_BIN_EXT_SAMPLES_ORIG] = 4,
//// [-HEADER_SIZE + SEGY_BIN_EXT_ENSEMBLE_FOLD] = 4,
////
//// [-HEADER_SIZE + SEGY_BIN_TRACES] = 2,
//// [-HEADER_SIZE + SEGY_BIN_AUX_TRACES] = 2,
//// [-HEADER_SIZE + SEGY_BIN_INTERVAL] = 2,
//// [-HEADER_SIZE + SEGY_BIN_INTERVAL_ORIG] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SAMPLES] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SAMPLES_ORIG] = 2,
//// [-HEADER_SIZE + SEGY_BIN_FORMAT] = 2,
//// [-HEADER_SIZE + SEGY_BIN_ENSEMBLE_FOLD] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SORTING_CODE] = 2,
//// [-HEADER_SIZE + SEGY_BIN_VERTICAL_SUM] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SWEEP_FREQ_START] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SWEEP_FREQ_END] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SWEEP_LENGTH] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SWEEP] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SWEEP_CHANNEL] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SWEEP_TAPER_START] = 2,
//// [-HEADER_SIZE + SEGY_BIN_SWEEP_TAPER_END] = 2,
//// [-HEADER_SIZE + SEGY_BIN_TAPER] = 2,
//// [-HEADER_SIZE + SEGY_BIN_CORRELATED_TRACES] = 2,
//// [-HEADER_SIZE + SEGY_BIN_BIN_GAIN_RECOVERY] = 2,
//// [-HEADER_SIZE + SEGY_BIN_AMPLITUDE_RECOVERY] = 2,
//// [-HEADER_SIZE + SEGY_BIN_MEASUREMENT_SYSTEM] = 2,
//// [-HEADER_SIZE + SEGY_BIN_IMPULSE_POLARITY] = 2,
//// [-HEADER_SIZE + SEGY_BIN_VIBRATORY_POLARITY] = 2,
//// [-HEADER_SIZE + SEGY_BIN_TRACE_FLAG] = 2,
//// [-HEADER_SIZE + SEGY_BIN_EXT_HEADERS] = 2,
////
//// [-HEADER_SIZE + SEGY_BIN_SEGY_REVISION] = 1,
//// [-HEADER_SIZE + SEGY_BIN_SEGY_REVISION_MINOR] = 1,
////
//// [-HEADER_SIZE + SEGY_BIN_UNASSIGNED1] = 0,
//// [-HEADER_SIZE + SEGY_BIN_UNASSIGNED2] = 0,
////};
////
/////*
//// * Determine the file size in bytes. If this function succeeds, the file
//// * pointer will be reset to wherever it was before this call. If this call
//// * fails for some reason, the return value is 0 and the file pointer location
//// * will be determined by the behaviour of fseek.
//// *
//// * sys/stat.h is POSIX, but is well enough supported by Windows. The long long
//// * data type is required to support files >4G (as long only guarantees 32 bits).
//// */
////#ifdef HAVE_SYS_STAT_H
////static int file_size(FILE* fp, long long* size) {
////
//// /*
//// * the file size will be unaccurate unless userland buffers are flushed if
//// * the file is new or appended to
//// */
//// if (fflush(fp) != 0) return SEGY_FWRITE_ERROR;
////#ifdef HAVE_FSTATI64
//// // this means we're on windows where fstat is unreliable for filesizes >2G
//// // because long is only 4 bytes
//// struct _stati64 st;
//// const int err = _fstati64(fileno(fp), &st);
////#else
//// struct stat st;
//// const int err = fstat(fileno(fp), &st);
////#endif
////
//// if (err != 0) return SEGY_FSEEK_ERROR;
//// *size = st.st_size;
//// return SEGY_OK;
////}
////#endif //HAVE_SYS_STAT_H
////
////static int formatsize(int format) {
//// switch (format) {
//// case SEGY_IBM_FLOAT_4_BYTE: return 4;
//// case SEGY_SIGNED_INTEGER_4_BYTE: return 4;
//// case SEGY_SIGNED_INTEGER_8_BYTE: return 8;
//// case SEGY_SIGNED_SHORT_2_BYTE: return 2;
//// case SEGY_FIXED_POINT_WITH_GAIN_4_BYTE: return 4;
//// case SEGY_IEEE_FLOAT_4_BYTE: return 4;
//// case SEGY_IEEE_FLOAT_8_BYTE: return 8;
//// case SEGY_SIGNED_CHAR_1_BYTE: return 1;
//// case SEGY_UNSIGNED_CHAR_1_BYTE: return 1;
//// case SEGY_UNSIGNED_INTEGER_4_BYTE: return 4;
//// case SEGY_UNSIGNED_SHORT_2_BYTE: return 2;
//// case SEGY_UNSIGNED_INTEGER_8_BYTE: return 8;
//// /* 3-byte values are not supported yet, report as invalid-args */
//// case SEGY_SIGNED_CHAR_3_BYTE: //return 3;
//// case SEGY_UNSIGNED_INTEGER_3_BYTE: //return 3;
//// case SEGY_NOT_IN_USE_1:
//// case SEGY_NOT_IN_USE_2:
//// default:
//// return -1;
//// }
////}
////
/////*
//// * addr is NULL if mmap is not found under compilation or if the file is
//// * not requested mmap'd. If so, the fallback code path of FILE* is taken
//// */
////
////#define MODEBUF_SIZE 5
////struct segy_file_handle {
//// void* addr;
//// void* cur;
//// FILE* fp;
//// size_t fsize;
//// char mode[MODEBUF_SIZE];
//// int writable;
//// int elemsize;
//// int lsb;
////};
////
////segy_file* segy_open(const char* path, const char* mode) {
////
//// if (!path || !mode) return NULL;
////
//// // append a 'b' if it is not passed by the user; not a problem on unix, but
//// // windows and other platforms fail without it
//// char binary_mode[MODEBUF_SIZE] = { 0 };
//// strncpy(binary_mode, mode, 3);
////
//// size_t mode_len = strlen(binary_mode);
//// if (binary_mode[mode_len - 1] != 'b') binary_mode[mode_len] = 'b';
////
//// // Account for invalid mode. On unix this is fine, but windows crashes the
//// // process if mode is invalid
//// if (!strstr("rb" "wb" "ab" "r+b" "w+b" "a+b", binary_mode))
//// return NULL;
////
////#ifdef _WIN32
//// /*
//// * fun with windows.
//// * Reported in https://github.com/Statoil/segyio/issues/266
//// *
//// * Windows uses UTF-16 internally, also for path names, and fopen does not
//// * handle UTF-16 strings on Windows. It's assumed input is UTF-8 or ascii ,
//// * and a simple conversion to the UTF-16 (wchar_t) version with _wfopen
//// * makes non-ascii paths work on Windows.
//// */
//// wchar_t wmode[MODEBUF_SIZE] = { 0 };
//// mbstowcs(wmode, binary_mode, strlen(binary_mode));
////
//// /*
//// * call MultiByteToWideChar twice, once to figure out the size of the
//// * resulting wchar string, and once to do the actual conversion
//// */
//// int wpathlen = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
//// wchar_t* wpath = calloc(wpathlen + 1, sizeof(wchar_t));
//// MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, wpathlen);
//// FILE* fp = _wfopen(wpath, wmode);
//// free(wpath);
////#else
//// FILE* fp = fopen(path, binary_mode);
////#endif
////
//// if (!fp) return NULL;
////
//// segy_file* file = calloc(1, sizeof(segy_file));
////
//// if (!file) {
//// fclose(fp);
//// return NULL;
//// }
////
//// file->fp = fp;
//// strcpy(file->mode, binary_mode);
////
//// bool rw = strstr(file->mode, "+") || strstr(file->mode, "w");
//// if (rw) file->writable = 1;
////
//// // assume a size of 4-bytes-per-element, until the set_format function
//// // tells us otherwise.
//// file->elemsize = 4;
////
//// return file;
////}
////
////int segy_mmap(segy_file* fp) {
////#ifndef HAVE_MMAP
//// return SEGY_MMAP_INVALID;
////#else
////
//// /* don't re-map; i.e. multiple consecutive calls should be no-ops */
//// if (fp->addr) return SEGY_OK;
////
//// long long fsize;
//// int err = file_size(fp->fp, &fsize);
////
//// if (err != 0) return SEGY_FSEEK_ERROR;
////
//// const int prot = fp->writable ? PROT_READ | PROT_WRITE : PROT_READ;
////
//// int fd = fileno(fp->fp);
//// void* addr = mmap(NULL, fsize, prot, MAP_SHARED, fd, 0);
////
//// // cppcheck-suppress memleak
//// if (addr == MAP_FAILED) return SEGY_MMAP_ERROR;
////
//// fp->addr = fp->cur = addr;
//// fp->fsize = fsize;
////
//// fclose(fp->fp);
////
//// return SEGY_OK;
////#endif //HAVE_MMAP
////}
////
////int segy_flush(segy_file* fp, bool async) {
////
//// // flush is a no-op for read-only files
//// if (!fp->writable) return SEGY_OK;
////
////#ifdef HAVE_MMAP
//// if (fp->addr) {
//// int flag = async ? MS_ASYNC : MS_SYNC;
//// int syncerr = msync(fp->addr, fp->fsize, flag);
//// if (syncerr != 0) return syncerr;
//// return SEGY_OK;
//// }
////#endif //HAVE_MMAP
////
//// int flusherr = fflush(fp->fp);
////
//// if (flusherr != 0) return SEGY_FWRITE_ERROR;
////
//// return SEGY_OK;
////}
////
////long long segy_ftell(segy_file* fp) {
////#ifdef HAVE_FTELLO
//// off_t pos = ftello(fp->fp);
//// assert(pos != -1);
//// return pos;
////#elif HAVE_FTELLI64
//// // assuming we're on windows. This function is a little rough, but only
//// // meant for testing - it's not a part of the public interface.
//// return _ftelli64(fp->fp);
////#else
//// assert(false);
////#endif
////}
////
////int segy_close(segy_file* fp) {
//// int err = segy_flush(fp, false);
////
////#ifdef HAVE_MMAP
//// if (!fp->addr) goto no_mmap;
////
//// // let errors unmapping take preference over flush-errors, as it is far
//// // more severe
//// // cppcheck-suppress redundantAssignment
//// err = munmap(fp->addr, fp->fsize);
//// if (err != 0)
//// err = SEGY_MMAP_ERROR;
////
//// free(fp);
//// return err;
////
////no_mmap:
////#endif //HAVE_MMAP
////
//// fclose(fp->fp);
//// free(fp);
//// return err;
////}
////
////static int get_field(const char* header,
//// const int* table,
//// int field,
//// int32_t* f) {
////
//// const int bsize = table[field];
//// uint32_t buf32 = 0;
//// uint16_t buf16 = 0;
//// uint8_t buf8 = 0;
////
//// switch (bsize) {
//// case 4:
//// memcpy(&buf32, header + (field - 1), 4);
//// *f = (int32_t)be32toh(buf32);
//// return SEGY_OK;
////
//// case 2:
//// memcpy(&buf16, header + (field - 1), 2);
//// *f = (int16_t)be16toh(buf16);
//// return SEGY_OK;
////
//// case 1:
//// memcpy(&buf8, header + (field - 1), 1);
//// *f = buf8;
//// return SEGY_OK;
////
//// case 0:
//// default:
//// return SEGY_INVALID_FIELD;
//// }
////}
////
////int segy_get_field(const char* traceheader, int field, int* f) {
//// if (field < 0 || field >= SEGY_TRACE_HEADER_SIZE)
//// return SEGY_INVALID_FIELD;
////
//// return get_field(traceheader, field_size, field, f);
////}
////
////int segy_get_bfield(const char* binheader, int field, int32_t* f) {
//// field -= SEGY_TEXT_HEADER_SIZE;
////
//// if (field < 0 || field >= SEGY_BINARY_HEADER_SIZE)
//// return SEGY_INVALID_FIELD;
////
//// return get_field(binheader, bfield_size, field, f);
////}
////
////static int set_field(char* header, const int* table, int field, int32_t val) {
//// const int bsize = table[field];
////
//// uint32_t buf32;
//// uint16_t buf16;
//// uint8_t buf8;
////
//// switch (bsize) {
//// case 4:
//// buf32 = htobe32((uint32_t)val);
//// memcpy(header + (field - 1), &buf32, sizeof(buf32));
//// return SEGY_OK;
////
//// case 2:
//// buf16 = htobe16((uint16_t)val);
//// memcpy(header + (field - 1), &buf16, sizeof(buf16));
//// return SEGY_OK;
////
//// case 1:
//// buf8 = (uint8_t)val;
//// memcpy(header + (field - 1), &buf8, sizeof(buf8));
//// return SEGY_OK;
////
//// case 0:
//// default:
//// return SEGY_INVALID_FIELD;
//// }
////}
////
////int segy_set_field(char* traceheader, int field, int val) {
//// if (field < 0 || field >= SEGY_TRACE_HEADER_SIZE)
//// return SEGY_INVALID_FIELD;
////
//// return set_field(traceheader, field_size, field, val);
////}
////
////int segy_set_bfield(char* binheader, int field, int val) {
//// field -= SEGY_TEXT_HEADER_SIZE;
////
//// if (field < 0 || field >= SEGY_BINARY_HEADER_SIZE)
//// return SEGY_INVALID_FIELD;
////
//// return set_field(binheader, bfield_size, field, val);
////}
////
////static int slicelength(int start, int stop, int step) {
//// if (step == 0) return 0;
////
//// if ((step < 0 && stop >= start) ||
//// (step > 0 && start >= stop)) return 0;
////
//// if (step < 0) return (stop - start + 1) / step + 1;
////
//// /*
//// * cppcheck 1.84 introduced a false-positive on this as a result of
//// * value-flow analysis not realising that step can *never* be zero here, as
//// * it would trigger the early return.
//// */
//// // cppcheck-suppress zerodivcond
//// return (stop - start - 1) / step + 1;
////}
////
////static int32_t bswap_header_word(int32_t f, int word_size) {
//// if (word_size == 4)
//// return bswap32(f);
////
//// /*
//// * The casts are here are necessary
//// *
//// * First, it must be converted to a *signed* short (to preserve negative
//// * numbers). The narrowing is safe because the source is 2 bytes anyway.
//// *
//// * The behaviour when this is implicitly wrong was discovered in [1].
//// *
//// * Then, it must be byteswapped with bswap16. When using the shifts is
//// * probably fine as the types are also cast in the macro and are
//// * compatible, but the builtins have this signature [2]:
//// *
//// * uint16_t __builtin_bswap16 (uint16_t x)
//// *
//// * which means that if the value is *negative* it will be interpreted as
//// * unsigned and very much positive. When it is then implicitly converted
//// * in the return type it is widened, and int32 can fit uint16 maximum just
//// * fine.
//// *
//// * [1] https://github.com/equinor/segyio/issues/368
//// * [2] http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
//// */
//// return (int16_t)bswap16((int16_t)f);
////}
////
////int segy_field_forall(segy_file* fp,
//// int field,
//// int start,
//// int stop,
//// int step,
//// int* buf,
//// long trace0,
//// int trace_bsize) {
//// int err;
//// // do a dummy-read of a zero-init'd buffer to check args
//// int32_t f;
//// char header[SEGY_TRACE_HEADER_SIZE] = { 0 };
//// err = segy_get_field(header, field, &f);
//// if (err != SEGY_OK) return SEGY_INVALID_ARGS;
////
//// // since segy_get_field didn't fail earlier, it is safe to look up the word
//// // size
//// const int word_size = field_size[field];
////
//// int slicelen = slicelength(start, stop, step);
////
//// // check *once* that we don't look past the end-of-file
//// // checking seek error inside the loop is a performance killer
//// err = segy_seek(fp, start, trace0, trace_bsize);
//// if (err != SEGY_OK) return err;
//// const int end = start + step * (slicelen - 1);
//// err = segy_seek(fp, end, trace0, trace_bsize);
//// if (err != SEGY_OK) return err;
////
//// const int lsb = fp->lsb;
////
////#ifdef HAVE_MMAP
//// if (fp->addr) {
//// for (int i = start; slicelen > 0; i += step, ++buf, --slicelen) {
//// segy_seek(fp, i, trace0, trace_bsize);
//// get_field(fp->cur, field_size, field, &f);
//// if (lsb) f = bswap_header_word(f, word_size);
//// *buf = f;
//// }
////
//// return SEGY_OK;
//// }
////#endif //HAVE_MMAP
////
//// /*
//// * non-mmap path. Doing multiple freads is slow, so instead the *actual*
//// * offset is computed, not just the start of the header, and that's copied
//// * into the correct offset in our local buffer. Note that byte offsets are
//// * exposed 1-indexed (to stay consistent with the specification), but the
//// * words in the buffer rely on 0-based offsets.
//// *
//// * Always read 4 bytes to be sure, there's no significant cost difference.