📄 bison2su.c
字号:
/* Copyright (c) Colorado School of Mines, 2006.*//* All rights reserved. *//************************************************************************/
/*
* Modified:
* -1998/05/24 Anonymous
* -add many more error detection messages
* -use dynamic memory allocation for trace data to accomodate
* larger than previous 8192 size
* -retain all Bison channel headers in dynamically allocated array
* -convert input data to output data in place, no need for
* separate buffers
* -remove pow() call, use fast lookup map instead
* -change swap routines to work with arrays instead of single
* values
* -autodetect big/little endian, no need to give a swap flag
* -fix segy trace header fill for several variables going to the
* wrong location
* -fix the date in the segy output header and use the expected
* Day-Of-Year day in the segy trace header instead
* of the day of month
*/
/* program bison2su.c */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
/* Jens Hartmann 21.6.93 */
/* Institut fuer Geophysik Hamburg */
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
typedef float s_Data;
struct s_Trace_head {
union {
int32 d4[60];
int16 s2[120];
} H;
};
typedef struct s_Trace_head s_Trace_head;
/* Bison Header Struct */
struct s_Bison_Header { /* each header constant is followed by CR and LF */
char f_num[8]; /* file number 6 digits */
char j_num[14]; /* job number 6 digits */
char date_time[12]; /* date and time mmddyyhhmm */
char op_note[82]; /* operator note 80 alpha */
char man_code[10]; /* manufacturer code BISON-2 */
char rec_name[14]; /* record name 12 alpha */
char head_type[4]; /* header type S/E (standard/extended) */
char data_for[4]; /* data format W/L/F (word/long/float) */
char ins_mod[8]; /* instrument model 6 alpha */
char ins_soft[6]; /* instrument software 4 digits */
/* instrument digital processing history 12 alpha */
char op1[14]; /* operation #1 12 alpha */
char op2[14]; /* operation #2 12 alpha */
char op3[14]; /* operation #3 12 alpha */
char op4[14]; /* operation #4 12 alpha */
char num_chan[6]; /* number of channels 4 digits always 1 for extended */
char n_r[14]; /* 1-12 normal or revers 12 alpha */
char num_sam[8]; /* number of samples 6 digits 000500-999999 */
char sam_rate[6]; /* sample rate 4 alpha .001 to 99.9 msec */
char delay_time[6]; /* delay time 4 digits 0000 to 9999 msec */
char hi_pass[12]; /* hi-pass analog filter 4 digits and 6 alpha
examp 0150 BE 03 (150 Hz Bessel 3 pole) */
char low_pass[12]; /* low-pass analog filter 4 digits and 6 alpha */
char notch_fil[10]; /* analog notch filter 2 digits and 6 alpha */
char shot_loc[82]; /* shot location 80 alpha */
char shot_off[82]; /* shot offset 80 alpha */
char geo_note[82]; /* geophone array note 80 alpha */
};
typedef struct s_Bison_Header s_Bison_Header;
struct s_Bison_Header_i { /* internal converted version */
float sample_rate;
int swap;
int debug;
int num_chan;
int num_sam;
int delay_time;
int rec_name;
int shot_off;
int year;
int month;
int day;
int hour;
int minute;
int doy;
int head_type;
int data_for;
};
typedef struct s_Bison_Header_i s_Bison_Header_i;
/* Bison Channel Header Struct */
struct s_Bison_Channel {
char num_stack[6]; /* number of stacks chan l 4 digits */
char fixed_gain[6]; /* fixed gain chan l 4 digits (db) */
char pol_stat[6]; /* pol_stat_ ch l 4 alpha */
char dc_low[6]; /* dc low */
char dc_med[6]; /* dc med */
char dc_high[6]; /* dc high */
char ac_lm[6]; /* ac l-m */
char ac_mh[6]; /* ac m-h */
}; /* channel specific section repeated for each channel */
typedef struct s_Bison_Channel s_Bison_Channel;
int bigendian(void);
int grgdoy(int,int,int);
void swap_short_2(uint16*, int);
void swap_long_4 (uint32*, int);
void Bhead_to_head(s_Bison_Header*,s_Bison_Header_i*);
void Bchan_to_thd(s_Bison_Channel*, s_Bison_Header_i*, s_Trace_head*);
int
main(int argc,char *argv[])
{
s_Bison_Header Bison_Header;
s_Bison_Channel Bison_Channel;
s_Trace_head *THDs;
s_Trace_head *thd;
s_Bison_Header_i BHi;
s_Data *Data;
char etx_test[2]; /* two test characters */
int swap = 0;
int debug = 0;
int bytes_trace_head;
int bytes_trace_data;
int i0, i1, i2;
size_t nio;
size_t nio_r;
/* check native sizes and swap */
if( sizeof(int16) != 2 )
{
fprintf(stderr,"ERROR sizeof(int16)=%d != 2\n",sizeof(int16));
exit(1);
}
if( sizeof(int32) != 4 )
{
fprintf(stderr,"ERROR sizeof(int32)=%d != 4\n",sizeof(int32));
}
if( sizeof(float) != 4 )
{
fprintf(stderr,"ERROR sizeof(float)=%d != 4\n",sizeof(float));
}
swap = bigendian();
for(i0=1 ; i0 < argc; i0++)
{
if (strcmp(argv[i0],"swap") == 0 || strcmp(argv[i0],"-swap") == 0)
swap |= 2;
else if (strcmp(argv[i0],"-d") == 0)
debug=1;
}
BHi.swap = swap;
BHi.debug = debug;
/* report setup */
if(swap != 0)
fprintf(stderr,"bytes are swapped !\n");
if((swap & 2) && !(swap & 1))
fprintf(stderr,"no need to swap, swapping anyway!\n");
/* read a header */
nio = fread(&Bison_Header,sizeof(Bison_Header),1,stdin);
if(nio != 1)
{
fprintf(stderr,"ERROR:reading Bison_Header\n");
perror("fread");
exit(1);
}
Bhead_to_head(&Bison_Header,&BHi);
/* allocate space */
THDs = (s_Trace_head*) calloc(sizeof(*THDs),BHi.num_chan);
if(THDs == NULL)
{
fprintf(stderr,"ERROR:allocating input trace headers\n");
exit(1);
}
nio = sizeof(s_Data) * BHi.num_sam;
Data = (s_Data*) malloc(nio);
if(Data == NULL)
{
fprintf(stderr,"ERROR:allocating input trace data\n");
exit(1);
}
/* length of trace */
bytes_trace_head = sizeof(s_Trace_head);
bytes_trace_data = sizeof(s_Data) * BHi.num_sam;
/* loop over the channel headers */
for (i0=0;i0 < BHi.num_chan;i0++)
{
nio = fread(&Bison_Channel,sizeof(Bison_Channel),1,stdin);
if(nio != 1)
{
fprintf(stderr,"ERROR: reading Bison_Channel\n");
perror("fread");
exit(1);
}
Bchan_to_thd(&Bison_Channel,&BHi, &THDs[i0]);
fprintf(stderr,"Number of stacked traces :%s\n",
&Bison_Channel.num_stack);
}
/* read terminating characters */
nio = fread(&etx_test,1,2,stdin);
if(nio != 2)
{
fprintf(stderr,"ERROR: reading etx_test"
", expected 2, read %d\n", nio);
perror("fread");
exit(1);
}
if(etx_test[0] != 3 || etx_test[0] != 3)
{
fprintf(stderr,"ERROR: reading etx_test"
", expected <3><3>, read <%x><%x>\n",
etx_test[0],etx_test[1]);
}
/* read/write trace data */
switch(BHi.data_for)
{
case 'W':
nio_r = 2 * BHi.num_sam;
break;
case 'L':
nio_r = 4 * BHi.num_sam;
break;
case 'F':
nio_r = 2 * 5*((BHi.num_sam+3)/4);
break;
default:
fprintf(stderr,"Error:invalid data type %s\n",
BHi.data_for);
exit(1);
}
for (i0=0;i0 < BHi.num_chan;i0++)
{
thd = &THDs[i0];
thd->H.d4[1] = i0+1; /* tracr */
/*thd->H.d4[3] = i0+1; /* cdpt */
thd->H.d4[5] = i0+1; /* cdpt */
nio = fread(Data,nio_r,1,stdin);
if(nio != 1)
{
fprintf(stderr,"ERROR: reading data, trace %d\n",i0);
perror("fread");
exit(1);
}
/*
* Word 16 bit 2's complement fixed gain normalized channel by channel.
* Least significant byte is first inrecord.
*/
if (BHi.data_for == 'W')
{
int16 *v = (int16*)Data;
if (swap) swap_short_2((uint16*)Data,BHi.num_sam);
/* convert to output data type */
for (i1=BHi.num_sam;i1-- > 0;)
Data[i1] = v[i1];
}
/*
* Long 32 bit 2's complement fixed gain instrumental format,
* not normalized, least significant byte first.
*/
else if (BHi.data_for == 'L')
{
int32 *v = (int32*)Data;
if (swap) swap_long_4((uint32*)Data,BHi.num_sam);
/* convert to output data type */
for (i1=0;i1 < BHi.num_sam;i1++)
Data[i1] = v[i1];
}
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -