📄 bison2su.c
字号:
* Float 16 bit 2's complement mantissa, 4 bit exponent normalized float
* generated from 32 bit internal fixed format standard instrument storage
* format. Transmitted as follows: in groups of 5 words
*
* LSbyte of first sample mantissa
* MSbyte of first sample mantissa
* LSbyte of second sample mantissa
* MSbyte of second sample mantissa
* LSbyte of third sample mantissa
* MSbyte of third sample mantissa
* LSbyte of fourth sample mantissa
* MSbyte of fourth sample mantissa
*
* Exponent word:
* Least significant nibble of first byte for fourth word.
* Most significant nibble of first byte for third word.
* Least significant nibble of second byte for second word.
* Most significant nibble of first byte for first word.
*/
else if (BHi.data_for == 'F')
{
struct di {
int16 f0;
int16 f1;
int16 f2;
int16 f3;
uint16 gain;
};
struct di *d = (struct di*)Data;
int nblocks = (BHi.num_sam + 3)/4;
if (swap)
swap_short_2((uint16*)Data,nio_r/2);
d = &d[nblocks - 1];
for (i1=BHi.num_sam - 1; i1 > 0; i1 -= 4, d--)
{
float emap[] = {
1., 2., 4., 8.,
16., 32., 64., 128.,
256., 512., 1024., 2048.,
4096., 8192., 16384., 32768.
};
unsigned int gain = (unsigned)d->gain;
Data[i1 ] = d->f3 * emap[ (gain & 0x000f) ];
Data[i1-1] = d->f2 * emap[((gain & 0x00f0)>>4)];
Data[i1-2] = d->f1 * emap[((gain & 0x0f00)>>8)];
Data[i1-3] = d->f0 * emap[((gain & 0xf000)>>12)];
}
}
nio = fwrite(thd,bytes_trace_head,1,stdout);
if(nio != 1)
{
fprintf(stderr,"ERROR:writing trace header %d\n",i0);
perror("fwrite");
exit(1);
}
nio = fwrite(Data,bytes_trace_data,1,stdout);
if(nio != 1)
{
fprintf(stderr,"ERROR:writing trace data %d\n",i0);
perror("fwrite");
exit(1);
}
}
free(THDs);
free(Data);
return 0;
}
/*****************************************************************************
* convert date to gregorian day to day of year 1 <-> 365 or 366
* 4 year, 100 year, 400 year rules observed
*/
int
grgdoy( int iyr, int imo, int ida)
{
# define BIG 9999
int mo[]={ 0, 31, 59, 90,120,151,181,212,243,273,304,334, BIG,
0, 31, 60, 91,121,152,182,213,244,274,305,335, BIG };
int leap;
leap = ( !(iyr%4) && ( iyr%100 || !(iyr%400) ) )?13:0;
return((int)(mo[imo+leap-1] + ida));
# undef BIG
}
/*****************************************************************************
* determine endianness of this machine
*/
int
bigendian(void)
{
int32 a[2];
char *c;
c = (char*)a;
a[0] = 0x11223344;
a[1] = 0x55667788;
if( *c == 0x11 ) return 1;
if( *c == 0x44 ) return 0;
fprintf(stderr,"ERROR:cannot determine big/little endianness\n");
exit(1);
}
/**************************************************************************
swap_short_2 swap short integers in place
***************************************************************************/
void swap_short_2(uint16 *tni2, int cnt)
{
int i = cnt;
uint16 *v = (uint16*)tni2;
for(i = cnt; i-- > 0 ; v++)
{
/* *v=(((*v>>8) & 0xff) | ((*v & 0xff)<<8)); */
*v=( ((*v>>8) & 0xff) | ((*v << 8) & 0xff00) );
}
}
/**************************************************************************
swap_long_4 swap a long integers in place
***************************************************************************/
void swap_long_4(uint32 *tni4, int cnt)
{
int i = cnt;
uint32 *v = (uint32*)tni4;
for(i = cnt; i-- > 0 ; v++)
{
/* *v=(((*v>>24)&0xff) | ((*v&0xff)<<24) |
((*v>>8)&0xff00) | ((*v&0xff00)<<8)); */
*v=(((*v>>24) & 0xff) | ((*v<<24) & 0xff000000) |
((*v>>8) & 0xff00) | ((*v<<8) & 0xff0000));
}
}
/*****************************************************************************
* convert Bison header to internal representation
*/
void
Bhead_to_head(s_Bison_Header *bh,s_Bison_Header_i *ih)
{
int i;
/* set EOL */
# define BH_END(name) bh->name[sizeof(bh->name)-2]=\
bh->name[sizeof(bh->name)-1]='\0'
BH_END(f_num); BH_END(j_num); BH_END(date_time);
BH_END(op_note); BH_END(man_code); BH_END(rec_name);
BH_END(head_type);BH_END(data_for); BH_END(ins_mod);
BH_END(ins_soft);
BH_END(op1); BH_END(op2); BH_END(op3); BH_END(op4);
BH_END(num_chan); BH_END(n_r); BH_END(num_sam);
BH_END(sam_rate); BH_END(delay_time);
BH_END(hi_pass); BH_END(low_pass); BH_END(notch_fil);
BH_END(shot_loc); BH_END(shot_off); BH_END(geo_note);
# undef BH_END
ih->num_chan = atoi(bh->num_chan);
ih->num_sam = atoi(bh->num_sam);
ih->sample_rate = atof(bh->sam_rate);
ih->delay_time = atoi(bh->delay_time);
ih->rec_name = atoi(&bh->rec_name[3]);
ih->shot_off = atol(bh->shot_off);
ih->head_type = (bh->head_type[0] == ' ') ?
bh->head_type[1] : bh->head_type[0];
ih->data_for = (bh->data_for[0] == ' ') ?
bh->data_for[1] : bh->data_for[0];
# define EXTRACT_DT(val,pos) \
i = bh->date_time[pos+2]; bh->date_time[pos+2] = '\0'; \
ih->val = atoi(&bh->date_time[pos]); \
bh->date_time[pos+2] = i;
EXTRACT_DT(month,0); EXTRACT_DT(day,2); EXTRACT_DT(year,4);
EXTRACT_DT(hour,6); EXTRACT_DT(minute,8);
# undef EXTRACT_DT
ih->doy = grgdoy(ih->year,ih->month,ih->day);
fprintf(stderr,
"Job number :%s\n", bh->j_num);
fprintf(stderr,
"Y/M/D H:M DOY :%02d/%02d/%02d %02d:%02d %03d\n",
ih->year,ih->month,ih->day,ih->hour,ih->minute,
ih->doy);
fprintf(stderr,
"Manufacturer code :%s\n", bh->man_code);
fprintf(stderr,
"Record name :%s\n", bh->rec_name);
fprintf(stderr,
"Header type :%c\n", ih->head_type);
fprintf(stderr,
"Data format :%c\n", ih->data_for);
fprintf(stderr,
"Number of channels :%d\n", ih->num_chan);
fprintf(stderr,
"Samples per channel:%d\n", ih->num_sam);
fprintf(stderr,
"Sample rate (msec) :%f msec\n",
ih->sample_rate);
fprintf(stderr,
"Delay :%d\n",
ih->delay_time);
/* value check */
/* number of channels 4 digits always 1 for extended */
if(ih->num_chan < 1 || ih->num_chan > 48)
{
fprintf(stderr,"ERROR:invalid num_chan:%d\n",ih->num_chan);
exit(1);
}
/* number of samples 6 digits 000500-999999 */
if(ih->num_sam < 500 || ih->num_sam > 16384)
{
fprintf(stderr,"ERROR:invalid sample count:%d\n",ih->num_sam);
exit(1);
}
/* sample rate 4 alpha .001 to 99.9 msec */
if(ih->sample_rate < 0.001 || ih->sample_rate > 99.9)
{
fprintf(stderr,"ERROR:invalid sample rate:%f\n",
ih->sample_rate);
exit(1);
}
/* delay time 4 digits 0000 to 9999 msec */
if(ih->delay_time < 0 || ih->delay_time > 9999)
{
fprintf(stderr,"ERROR:invalid delay_time:%d\n",
ih->delay_time);
exit(1);
}
/* header type S/E (standard/extended) */
if(strchr("SE",ih->head_type) == NULL)
{
fprintf(stderr,"ERROR:invalid header type:%c\n"
" read from <%s>\n",
ih->head_type,bh->head_type);
exit(1);
}
/* data format W/L/F (word/long/float) */
if(strchr("WLF",ih->data_for) == NULL)
{
fprintf(stderr,"ERROR:invalid data type:%c\n"
" read from <%s>\n",
ih->data_for,bh->data_for);
exit(1);
}
/* date time, 2 digits each */
if(
ih->year < 1 || ih->year > 99
|| ih->month < 1 || ih->month > 12
|| ih->day < 1 || ih->day > 31
|| ih->hour < 0 || ih->hour > 23
|| ih->minute < 0 || ih->minute > 59
) {
fprintf(stderr,"ERROR:invalid date_time:\n"
" y m d h m = %2d %2d %2d %2d %2d\n"
" read from string <%s>\n",
ih->month,ih->day, ih->year,
ih->hour, ih->minute,
bh->date_time);
exit(1);
}
}
/*****************************************************************************
* convert Bison trace header to output header
*/
void
Bchan_to_thd(s_Bison_Channel *bc, s_Bison_Header_i *ih, s_Trace_head *th)
{
/* set EOL */
# define BC_END(name) bc->name[sizeof(bc->name)-2]=\
bc->name[sizeof(bc->name)-1]='\0'
BC_END(num_stack);
BC_END(fixed_gain);
BC_END(pol_stat);
BC_END(dc_low); BC_END(dc_med); BC_END(dc_high);
BC_END(ac_lm); BC_END(ac_mh);
# undef BC_END
th->H.d4[0] = 1; /* tracl */
th->H.d4[2] = ih->rec_name; /* fldr */
th->H.d4[4] = 0; /* ep */
/*th->H.s2[16] = 1; /* trid */
th->H.s2[14] = 1; /* trid */
/*th->H.s2[17] = atoi(bc->num_stack); /* nvs */
th->H.s2[15] = atoi(bc->num_stack); /* nvs */
/*th->H.s2[19] = 1; /* duse */
th->H.s2[17] = 1; /* duse */
th->H.d4[9] = ih->shot_off; /* offset */
th->H.s2[44] = 1; /* counit */
th->H.s2[52] = ih->delay_time; /* delrt */
th->H.s2[57] = ih->num_sam; /* ns */
th->H.s2[58] = floor(ih->sample_rate*1000); /* dt */
th->H.s2[78] = ih->year; /* year */
th->H.s2[79] = ih->doy; /* day of year */
th->H.s2[80] = ih->hour; /* hour */
th->H.s2[81] = ih->minute; /* minute */
th->H.s2[82] = 0; /* sec */
th->H.s2[83] = 1; /* timbas */
}
/************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -