Skip to content

Commit 703885a

Browse files
committed
first commit
1 parent 02c8220 commit 703885a

14 files changed

+6145
-0
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# binary2segy
2+
Convert binary to seismic files in SeismicUnix format
3+
* some swap function
4+
```c
5+
void swap_short_2(short *tni2);
6+
void swap_u_short_2(unsigned short *tni2);
7+
void swap_int_4(int *tni4);
8+
void swap_u_int_4(unsigned int *tni4);
9+
void swap_long_4(long *tni4);
10+
void swap_u_long_4(unsigned long *tni4);
11+
void swap_float_4(float *tnf4);
12+
void swap_double_8(double *tndd8);
13+
void swaphval(segy *tr, int index);
14+
```

binary2segy.c

+199
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include "hc/bhdr.h"
5+
#include "hc/hdr.h"
6+
#include "hc/cjbsegy.h"
7+
#include "math.h"
8+
9+
typedef cjbsegy segy;
10+
11+
#define SU_NKEYS 80 /* Number of key header words */
12+
#define HDRBYTES 240 /* Bytes in the trace header */
13+
#define EBCBYTES 3200 /* Bytes in the card image EBCDIC block */
14+
#define BNYBYTES 400 /* Bytes in the binary coded block */
15+
#define SEGY_HDRBYTES 240 /* Bytes in the tape trace header */
16+
#define SEGY_NKEYS 71 /* Number of mandated header fields */
17+
#define BHED_NKEYS 27 /* Number of mandated binary fields */
18+
/***** SEG-Y header *********/
19+
Y_3200 y3200;
20+
bhed head_400;
21+
cjbsegy tr, vtr;
22+
/***** SU function *********/
23+
void swap_short_2(short *tni2);
24+
void swap_u_short_2(unsigned short *tni2);
25+
void swap_int_4(int *tni4);
26+
void swap_u_int_4(unsigned int *tni4);
27+
void swap_long_4(long *tni4);
28+
void swap_u_long_4(unsigned long *tni4);
29+
void swap_float_4(float *tnf4);
30+
void swap_double_8(double *tndd8);
31+
void swaphval(segy *tr, int index);
32+
33+
34+
/**
35+
* Complier
36+
*
37+
* -bash-4.1$ gcc -o a dat2su.c -lm
38+
* -bash-4.1$ ./a
39+
*/
40+
41+
void main()
42+
{
43+
int i,j,k,ntrace,ns,cc;
44+
char FN1[250]={"v1.su"};//你所需要的su道头的输入文件
45+
char FN2[250]={"v2.dat"};//你输入的二进制裸数据
46+
char FN3[250]={"v2.su"};//输出的文件:v1.su的道头+v2.dat的数据体
47+
48+
ntrace = 1700;//最大的道数:三维情况等于nx*ny*炮数, 二维情况等于nx*炮数 or ng*炮数
49+
50+
ns = 601;//每一道的纵向采样点数
51+
52+
FILE *fp1 = fopen(FN1,"rb");
53+
FILE *fp2 = fopen(FN2,"rb");
54+
FILE *fp3 = fopen(FN3,"wb");
55+
56+
float *trace = (float*)malloc(sizeof(float)*ns);
57+
58+
int itrace;
59+
for(itrace = 0; itrace < ntrace; itrace ++)
60+
{
61+
if(itrace%200 == 0)
62+
printf("itrace = %5d/%d;\n",itrace, ntrace);
63+
64+
fread(&tr, sizeof(cjbsegy), 1, fp1);
65+
66+
/**
67+
* don't do this swaphval function
68+
*/
69+
//for (k = 0; k < SU_NKEYS; ++k)
70+
// swaphval(&tr,k);
71+
72+
fread(trace, sizeof(float), ns, fp1);
73+
fread(trace, sizeof(float), ns, fp2);
74+
75+
for(j=0;j<ns;j++)
76+
swap_float_4(&trace[j]);
77+
78+
fwrite(&tr, sizeof(cjbsegy), 1, fp3);
79+
fwrite(trace, sizeof(float), ns, fp3);
80+
}
81+
82+
free(trace);
83+
84+
}//end of main
85+
86+
87+
void swap_short_2(short *tni2)
88+
/**************************************************************************
89+
swap_short_2 swap a short integer
90+
***************************************************************************/
91+
{
92+
*tni2=(((*tni2>>8)&0xff) | ((*tni2&0xff)<<8));
93+
}
94+
95+
void swap_u_short_2(unsigned short *tni2)
96+
/**************************************************************************
97+
swap_u_short_2 swap an unsigned short integer
98+
***************************************************************************/
99+
{
100+
*tni2=(((*tni2>>8)&0xff) | ((*tni2&0xff)<<8));
101+
}
102+
103+
void swap_int_4(int *tni4)
104+
/**************************************************************************
105+
swap_int_4 swap a 4 byte integer
106+
***************************************************************************/
107+
{
108+
*tni4=(((*tni4>>24)&0xff) | ((*tni4&0xff)<<24) |
109+
((*tni4>>8)&0xff00) | ((*tni4&0xff00)<<8));
110+
}
111+
112+
void swap_u_int_4(unsigned int *tni4)
113+
/**************************************************************************
114+
swap_u_int_4 swap an unsigned integer
115+
***************************************************************************/
116+
{
117+
*tni4=(((*tni4>>24)&0xff) | ((*tni4&0xff)<<24) |
118+
((*tni4>>8)&0xff00) | ((*tni4&0xff00)<<8));
119+
}
120+
121+
void swap_long_4(long *tni4)
122+
/**************************************************************************
123+
swap_long_4 swap a long integer
124+
***************************************************************************/
125+
{
126+
*tni4=(((*tni4>>24)&0xff) | ((*tni4&0xff)<<24) |
127+
((*tni4>>8)&0xff00) | ((*tni4&0xff00)<<8));
128+
}
129+
130+
void swap_u_long_4(unsigned long *tni4)
131+
/**************************************************************************
132+
swap_u_long_4 swap an unsigned long integer
133+
***************************************************************************/
134+
{
135+
*tni4=(((*tni4>>24)&0xff) | ((*tni4&0xff)<<24) |
136+
((*tni4>>8)&0xff00) | ((*tni4&0xff00)<<8));
137+
}
138+
139+
void swap_float_4(float *tnf4)
140+
/**************************************************************************
141+
swap_float_4 swap a float
142+
***************************************************************************/
143+
{
144+
int *tni4=(int *)tnf4;
145+
*tni4=(((*tni4>>24)&0xff) | ((*tni4&0xff)<<24) |
146+
((*tni4>>8)&0xff00) | ((*tni4&0xff00)<<8));
147+
}
148+
149+
void swap_double_8(double *tndd8)
150+
/**************************************************************************
151+
swap_double_8 swap a double
152+
***************************************************************************/
153+
{
154+
char *tnd8=(char *)tndd8;
155+
char tnc;
156+
157+
tnc= *tnd8;
158+
*tnd8= *(tnd8+7);
159+
*(tnd8+7)=tnc;
160+
161+
tnc= *(tnd8+1);
162+
*(tnd8+1)= *(tnd8+6);
163+
*(tnd8+6)=tnc;
164+
165+
tnc= *(tnd8+2);
166+
*(tnd8+2)= *(tnd8+5);
167+
*(tnd8+5)=tnc;
168+
169+
tnc= *(tnd8+3);
170+
*(tnd8+3)= *(tnd8+4);
171+
*(tnd8+4)=tnc;
172+
}
173+
void swaphval(segy *tr, int index)
174+
{
175+
register char *tp= (char *) tr;
176+
177+
switch(*(hdr[index].type)) {
178+
case 'h': swap_short_2((short*)(tp + hdr[index].offs));
179+
break;
180+
case 'u': swap_u_short_2((unsigned short*)(tp + hdr[index].offs));
181+
break;
182+
case 'i': swap_int_4((int*)(tp + hdr[index].offs));
183+
break;
184+
case 'p': swap_u_int_4((unsigned int*)(tp + hdr[index].offs));
185+
break;
186+
case 'l': swap_long_4((long*)(tp + hdr[index].offs));
187+
break;
188+
case 'v': swap_u_long_4((unsigned long*)(tp + hdr[index].offs));
189+
break;
190+
case 'f': swap_float_4((float*)(tp + hdr[index].offs));
191+
break;
192+
case 'd': swap_double_8((double*)(tp + hdr[index].offs));
193+
break;
194+
default: err("%s: %s: unsupported data type", __FILE__, __LINE__);
195+
break;
196+
}
197+
198+
}
199+

hc/README.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
头文件。

0 commit comments

Comments
 (0)