Friday, January 4, 2013

C Code SEGYHEADER Dumper

1. C Code untuk SEGY Header (32 bits): Membaca setiap 10 trace
Pada rangkaian pembahasan sebelumnya saya telah menjelaskan penggunaan Perl untuk membaca header dari segy. Pada bagian ini saya akan menjelaskan bagaimana caranya membaca header dari segy dengan menggunakan code c.

Copy code di bawah ini dengan text editor lalu beri nama scan_segy_header.c
Pada terminal linux ketik:
gcc -o scan_segy_header scan_segy_header.c

Jalankan:
./scan_segy_header file.sgy

Pada Linux yang lain tanpa ./

Code C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
main(argc,argv)
int argc;
char *argv[];
{
int ipar,ipar2,ipar3,ipar4,ipar5;
int sam, trace;
int i, bit, j , num, n;
int force=0;
float sampleint;
char buffer[400],buffer2[3200],buffer3[240],*buffer4;
char *headers, ans[100];
int precision=4;

register union {
   char *charptr;
   int *intptr;
   float *floatptr;
   short *shortptr;
   } allptr;

FILE *inptr, *fout;
inptr=fopen(argv[argc-1],"r");
i=1;

/* read EBCDIC header, file header and first trace header */
ipar2=fread(buffer2,1,3200,inptr);
ipar=fread(buffer,1,400,inptr);
trace=0;
allptr.charptr=buffer;
printf("%-30s%10d\n","Jobid:",ntohl(*allptr.intptr++));
printf("%-30s%10d\n","Line no:",ntohl(*allptr.intptr++));
printf("%-30s%10d\n","Reel no:",ntohl(*allptr.intptr++));
printf("%-30s%10d\n","No of traces in 1st gather:",ntohs(*allptr.shortptr++));
printf("%-30s%10d\n","No of AUX traces:",ntohl(*allptr.shortptr++));
printf("%-30s%10d\n","Sample interv (us):",ntohs(*allptr.shortptr++));

sampleint=(float)ntohs(*allptr.shortptr);
sampleint*=0.001;

printf("%-30s%10d\n","Field sample interv  (us):",ntohs(*allptr.shortptr++));
if( !force ) sam=(int)ntohs(*allptr.shortptr++);
printf("%-30s%10d\n","Samples per trace:",sam);

/* allocate memory for traces values */
buffer4=(char*)malloc(precision*sam*sizeof(char));
headers=(char*)malloc(10*240*sizeof(char));
printf("%-30s%10d\n","Field samples per trace:",(int)ntohs(*allptr.shortptr++));
printf("%-30s%10d\n","Format code (probably 1) :",(int)ntohs(*allptr.shortptr++));

/* read the 10 first trace headers */
strcpy(ans,"y");
num=10;
trace=1;

while( strncmp( ans ,"y", 1)==0 || strncmp( ans ,"Y", 1 )==0 ) {

  for( i=0;i<10;i++) {
    ipar = fread( headers+(i*240), 1, 240, inptr);
    if( ipar==0 ) break;
    else ipar4= fread( buffer4,1,sam*precision,inptr);
    }
  num=i;
  allptr.charptr=headers;
  printf("FIRST 10 TRACES HEADERS 32bits INTEGER\n");
  printf("%8s","Trace");
  for( i=0;i<num;i++)
      printf("%9d", trace + i);
  printf("\n");

  printf("%8s","CDP");
  for( i=0;i<num;i++)
      printf("%9d", (int)ntohl(*(allptr.intptr+60*i+52)));
  printf("\n");

   for(bit=0;bit<=238;bit+=4) {
  
    printf("%3d-%3d:", bit+1, bit+4);
    for( i=0;i<num;i++)
      printf("%9d", (int)ntohl(*(allptr.intptr+60*i+bit/4)));
    printf("\n");
    }

   trace+=num;

  if( num == 10 ) {
           fprintf(stdout, "\n More ([y]/n) ? ");
           fgets( ans, sizeof(ans), stdin );
          if( strlen(ans) < 1 ) strncpy( ans , "y", 1);
    }
  else strcpy(ans, "n");
  }

free(buffer4);
free(headers);
fclose(inptr);
exit(0);
}



2. C Code untuk SEGY Header (32 bits): Menampilkan satistik min-max dari setiap byte#.


#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>

main(argc,argv)
int argc;
char *argv[];
{
int ipar,ipar2,ipar3,ipar4,ipar5;
int sam, trace;
int i, bit, j , num;
int tab[61], val;
float sampleint;
int force=0;
char buffer[400],buffer2[3200],buffer3[240],*buffer4;
int precision=4;

struct {
   int min[61];
   int max[61];
   int inc[61];
   } stat;

register union {
   char *charptr;
   int *intptr;
   float *floatptr;
   short *shortptr;
   } allptr;

FILE *inptr, *fout;
inptr=fopen(argv[argc-1],"r");

if(inptr==NULL) {
   fprintf(stderr,"Unable to open %s\n",argv[argc-1]);
   fprintf(stderr,"Abort.\n");
   exit(1);
   }
i=1;

ipar2=fread(buffer2,1,3200,inptr);
ipar=fread(buffer,1,400,inptr);

trace=0;
allptr.charptr=buffer;

if( !force) sam=(int) ntohs(*(allptr.shortptr+10));

/* allocate memory for traces values */
buffer4=(char*)malloc(precision*sam*sizeof(char));
ipar=1;

/* Initialize stats */
for(i=0;i<61;i++) {
    stat.min[i]=(int)1E9;
    stat.max[i]=(int)-1E9;
    stat.inc[i]=0;
    }

while( ipar > 0 ) {

    ipar = fread( buffer3, 1,   240, inptr);

    if( ipar ) {

    ipar4= fread( buffer4, 1, sam*precision, inptr);
 
        allptr.charptr=buffer3;
 
        for(i=0;i<61;i++) {
   
          val = (int) ntohl(*(allptr.intptr+i));

          if( val < stat.min[i] )
               stat.min[i] = val;
          if( val > stat.max[i] )
               stat.max[i] = val;
          }
        }
  }
/*print stats */
printf("\nSTATS PER 32 bits HEADER WORD\nBit #     Min      Max\n");
for(bit=0;bit<60;bit++) {
    printf("%3d-%3d:", 4*bit+1, 4*bit+4);
    printf("%9d", stat.min[bit]);
    printf("%9d", stat.max[bit]);
    printf("\n");
    }

free(buffer4);
fclose(inptr);
exit(0);
}

 
3. C Code untuk SEGY Header (32 bits): Mengekstrak byte tertentu untuk seluruh trace.

Gambar berikut contoh untuk mengekstrak isi pada byte yang dinyatakan pada command line dengan hasilnya di-outputkan pada headerfile.txt.

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
main(argc,argv)
int argc;
char *argv[];
{
int ipar,ipar2,ipar3,ipar4,ipar5;
int sam, trace;
int i, bit, j , num;
int tab[61], val;
float sampleint;
int force=0;
char buffer[400],buffer2[3200],buffer3[240],*buffer4;
int precision=4;
int btmin, bt;

register union {
   char *charptr;
   int *intptr;
   float *floatptr;
   short *shortptr;
   } allptr;

FILE *inptr, *fout;
inptr=fopen(argv[argc-1],"r");
ipar2=fread(buffer2,1,3200,inptr);
ipar=fread(buffer,1,400,inptr);
trace=0;

allptr.charptr=buffer;
if( !force) sam=(int) ntohs(*(allptr.shortptr+10));
buffer4=(char*)malloc(precision*sam*sizeof(char));
trace=0;
ipar=1;
num=1;

while( ipar > 0 ) {
    ipar = fread( buffer3, 1,   240, inptr);
    if( ipar ) {
    ipar4= fread( buffer4, 1, sam*precision, inptr);
        allptr.charptr=buffer3;
        for(j=1;j<argc-1;j++) {
        bt = atoi(argv[j]);
         btmin=(bt-1)/4;
          for(i=btmin;i<btmin+1;i++) {
          val = (int) ntohl(*(allptr.intptr+i));
          }
          trace+=num;
          printf("%9d", val);
          }
          printf("\n");
          }
}
free(buffer4);
fclose(inptr);
exit(0);
}

No comments: