Пример #1
0
int
main (int argc, char *argv[])
{
  const bool su = true;
  bool verb, xdr;
  char *filename, *trace;
  int format=0, ns, ntr, itrace[SF_NKEYS];
  off_t pos, nsegy;
  float dt;
  FILE *file;
  extern int fseeko (FILE * stream, off_t offset, int whence);

/**** Start common code with segy2rsf (16 lines) ****/
  sf_init (argc, argv);

  if (!sf_getbool ("verb", &verb))
    verb = false;
  /* Verbosity flag */

  if (!sf_getbool ("endian", &xdr))
    xdr = true;
  /* Whether to automatically estimate endianness or not */
  if (xdr)
    endian ();

  if (NULL == (filename = sf_getstring ("tape")))	/* input data */
    sf_error ("Need to specify tape=");

  if (NULL == (file = fopen (filename, "rb")))
    sf_error ("Cannot open \"%s\" for reading:", filename);

  pos = readfilesize (file);
/**** End common code with segy2rsf ****/

  /* Figure out ns and ntr */

  trace = sf_charalloc (SF_HDRBYTES);
  if (SF_HDRBYTES != fread (trace, 1, SF_HDRBYTES, file))
    sf_error ("Error reading first trace header");
  fseeko (file, 0, SEEK_SET);

  segy2head (trace, itrace, SF_NKEYS);
  ns = itrace[segykey ("ns")];
  dt = itrace[segykey ("dt")] / 1000000.;
  free (trace);

  nsegy = SF_HDRBYTES + ns * 4;
  ntr = pos / nsegy;

  su_or_segy_to_rsf (verb, su, ntr, format, ns, itrace, nsegy, file, dt);

  exit (0);
}
Пример #2
0
int main(int argc, char* argv[])
{
  sf_file in=NULL, out=NULL;
  int n1_traces;
  int n1_headers;

  char* header_format=NULL;
  sf_datatype typehead;
  /* kls do I need to add this?  sf_datatype typein; */
  float* fheader=NULL;
  float* intrace=NULL;
  int dim;
  off_t n_in[SF_MAX_DIM];
  int iaxis;
  int dim_output;
  int *indx_of_keys;
  bool label_argparmread,n_argparmread,o_argparmread,d_argparmread;
  char parameter[13];
  char* label[SF_MAX_DIM];
  off_t n_output[SF_MAX_DIM];
  off_t n_outheaders[SF_MAX_DIM];
  float o_output[SF_MAX_DIM];
  float d_output[SF_MAX_DIM];
  sf_axis output_axa_array[SF_MAX_DIM];
  sf_file output=NULL, outheaders=NULL;
  char* output_filename=NULL;
  char* outheaders_filename=NULL;
  
  sf_init (argc,argv);

   /*****************************/
  /* initialize verbose switch */
  /*****************************/
  /* verbose flag controls ammount of print */
  /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */
  /* fprintf(stderr,"read verbose switch.  getint reads command line.\n"); */
  if(!sf_getint("verbose",&verbose))verbose=1;
  /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  fprintf(stderr,"verbose=%d\n",verbose);
 
  /******************************************/
  /* input and output data are stdin/stdout */
  /******************************************/

  if(verbose>0)fprintf(stderr,"read infile name\n");  
  in = sf_input ("in");

  if(verbose>0)
    fprintf(stderr,"read outfile name (probably default to stdout\n");
  out = sf_output ("out");

  if (!sf_histint(in,"n1_traces",&n1_traces))
    sf_error("input data not define n1_traces");
  if (!sf_histint(in,"n1_headers",&n1_headers)) 
    sf_error("input data does not define n1_headers");

  header_format=sf_histstring(in,"header_format");
  if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
  else                                       typehead=SF_FLOAT;

  fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  fheader = sf_floatalloc(n1_headers);

  fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);
  intrace= sf_floatalloc(n1_traces);

  
/* maybe I should add some validation that n1== n1_traces+n1_headers+2
     and the record length read in the second word is consistent with 
     n1.  */

  /* put the history from the input file to the output */
  sf_fileflush(out,in);

  /********************************************************************/
  /* set up the output and outheaders files for the traces and headers */
  /********************************************************************/

  output_filename=sf_getstring("output");
  /* \n
     output trace filename. Required parameter with no default.
  */
  if(NULL==output_filename) sf_error("output is a required parameter");
  output=sf_output(output_filename);

  outheaders_filename=sf_getstring("outheaders");
  /* \n
     Output trace header file name.  Default is the input data
     file name, with the final .rsf changed to _hdr.rsf. 
  */  

  if(outheaders_filename==NULL){
    /* compute headers_filename from output_filename by replacing the final
       .rsf with _hdr.rsf */
    if(!(0==strcmp(output_filename+strlen(output_filename)-4,".rsf"))){
	fprintf(stderr,"parameter output, the name of the output file,\n");
	fprintf(stderr,"does not end with .rsf, so header filename cannot\n");
	fprintf(stderr,"be computed by replacing the final .rsf with\n");
	fprintf(stderr,"_hdr.rsf.\n");
	sf_error("default for outheaders parameter cannot be computed.");
    }
    outheaders_filename=malloc(strlen(output_filename)+60);
    strcpy(outheaders_filename,output_filename);
    strcpy(outheaders_filename+strlen(output_filename)-4,"_hdr.rsf\0");
    if(verbose>1)
      fprintf(stderr,"parameter outheader defaulted.  Computed to be #%s#\n",
			 outheaders_filename);
  }
  if(verbose>1)fprintf(stderr,"parameter outheader input or computed  #%s#\n",
		       outheaders_filename);
  outheaders=sf_output(outheaders_filename);
 
  /* get each of the axis information: 
     label2, n2, o2, d2,  
     label3, n3, o3, d3,
     etc
     label1, n1, o1, d1  is always defaulted from input */
  sf_putint   (output    ,"n1"    ,n1_traces );
  sf_putint   (outheaders,"n1"    ,n1_headers);
  sf_putfloat (outheaders,"d1"    ,1         );
  sf_putfloat (outheaders,"o1"    ,0         );
  sf_putstring(outheaders,"label1","none"    );
  sf_putstring(outheaders,"unit1" ,"none"    );
  
  dim_output=1;
  for (iaxis=1; iaxis<SF_MAX_DIM; iaxis++){
    label_argparmread=n_argparmread=o_argparmread=d_argparmread=false;
    sprintf(parameter,"label%d",iaxis+1);
    fprintf(stderr,"try to read %s\n",parameter);
    if ((label[iaxis]=sf_getstring(parameter))) {
      /*(label#=(2,...)  name of each of the axes. 
	   label1 is not changed from input. Each label must be a 
	   header key like cdp, cdpt, or ep.  The trace header 
	   values are used to define the output trace location in
	   the output file. )*/
      fprintf(stderr,"got %s=%s\n",parameter,label[iaxis]);
      sf_putstring(output    ,parameter,label[iaxis]);
      sf_putstring(outheaders,parameter,label[iaxis]);
      label_argparmread=true;
    }
    sprintf(parameter,"n%d",iaxis+1);
    fprintf(stderr,"try to read %s\n",parameter);
    if (sf_getlargeint  (parameter,&n_output[iaxis])) {
      /*( n#=(2,...) number of locations in the #-th dimension )*/ 
      fprintf(stderr,"got %s=%lld\n",parameter,(long long) n_output[iaxis]);
      sf_putint(output    ,parameter,n_output[iaxis]);
      sf_putint(outheaders,parameter,n_output[iaxis]);
      n_argparmread=true;
    }
    sprintf(parameter,"o%d",iaxis+1);
    if (sf_getfloat(parameter,&o_output[iaxis])) {
      /*( o#=(2,...) origin of the #-th dimension )*/ 
      sf_putfloat(output    ,parameter,o_output[iaxis]);
      sf_putfloat(outheaders,parameter,o_output[iaxis]);
      o_argparmread=true;
    }
    sprintf(parameter,"d%d",iaxis+1);
    if (sf_getfloat(parameter,&d_output[iaxis])) {
      /*( d#=(2,...) delta in the #-th dimension )*/ 
      sf_putfloat(output    ,parameter,d_output[iaxis]);
      sf_putfloat(outheaders,parameter,d_output[iaxis]);
      d_argparmread=true;
    }
    if(!label_argparmread && !n_argparmread && 
       !    o_argparmread &&  !d_argparmread){
      /* none of the parameter were read
	 you read all the parameters in the previous iteration
	 compute the output dimension and exit the loop */
      dim_output=iaxis;
      break;
    }
    if(label_argparmread && n_argparmread && o_argparmread && d_argparmread){
      /* all the parameters for thisi axis were read.  loop for next iaxis */
      if(verbose>0){
	fprintf(stderr,"label, n, i, and d read for iaxis%d\n",iaxis+1);
      }
    } else {
      sf_warning("working on iaxis=%d. Program expects to read\n",iaxis+1);
      sf_warning("label%d, n%d, i%d, and d%d from command line.\n",
		 iaxis+1,iaxis+1,iaxis+1,iaxis+1);
      if(!label_argparmread) sf_error("unable to read label%d\n",iaxis+1); 
      if(!n_argparmread    ) sf_error("unable to read n%d    \n",iaxis+1); 
      if(!o_argparmread    ) sf_error("unable to read o%d    \n",iaxis+1); 
      if(!d_argparmread    ) sf_error("unable to read d$d    \n",iaxis+1); 
    }
  }
  
  /* if the input file is higher dimention than the output, then the 
     size of all the higher dimensions must be set to 1. */
  /* kls use sf_axis to get structure for each axis with n, o, d, l, and u
     this can be used to compute the index for sf_seek */
  dim = sf_largefiledims(in,n_in);
  for (iaxis=dim_output; iaxis<dim; iaxis++){
    sprintf(parameter,"n%d",iaxis+1);
    sf_putint(output,parameter,1);
    sf_putint(outheaders,parameter,1);
  }
  /* for a test zero n_output and dim_output and see it you can read the 
     history file */

  sf_largefiledims(output,n_output);
  sf_largefiledims(outheaders,n_outheaders);
  if(verbose>1){
    for (iaxis=0; iaxis<SF_MAX_DIM; iaxis++){
      fprintf(stderr,"from sf_largefiledims(output.. n%d=%lld outheaders=%lld\n",
	      iaxis+1,(long long) n_output[iaxis],(long long) n_outheaders[iaxis]);
    }
  }

  
  /* the list header keys (including any extras) and get the index into 
     the header vector */
  segy_init(n1_headers,in);
  indx_of_keys=sf_intalloc(dim_output);
  for (iaxis=1; iaxis<dim_output; iaxis++){
    /* kls need to check each of these key names are in the segy header and
       make error message for invalid keys.  Of does segykey do this? NO, just
       segmentation fault. */
    if(verbose>0)fprintf(stderr,"get index of key for %s\n",label[iaxis]); 
    indx_of_keys[iaxis]=segykey(label[iaxis]);
  }

  sf_fileflush(output,in);

  sf_putint(outheaders,"n1",n1_headers);
  if(0==strcmp("native_int",sf_histstring(in,"header_format"))){
    sf_settype(outheaders,SF_INT);
  } else {
    sf_settype(outheaders,SF_FLOAT);
  }
  sf_fileflush(outheaders,in);

  fprintf(stderr,"start trace loop\n");

  /* kls maybe this should be in function sf_tahwritemapped_init */

  {
    off_t file_offset;
    off_t i_output[SF_MAX_DIM];
    float temp_float=0.0;

    for(iaxis=0; iaxis<SF_MAX_DIM; iaxis++){
      i_output[iaxis]=n_output[iaxis]-1;
    }
    file_offset=sf_large_cart2line(dim_output,n_output,i_output)*sizeof(float);
    sf_seek(output,file_offset,SEEK_SET);
    sf_floatwrite(&temp_float,1,output);
    i_output[0]=n_outheaders[0]-1;
    file_offset=sf_large_cart2line(dim_output,n_outheaders,i_output)*
                      sizeof(float);
    sf_seek(outheaders,file_offset,SEEK_SET);
    sf_floatwrite(&temp_float,1,outheaders);
  }

  for (iaxis=0; iaxis<SF_MAX_DIM; iaxis++){
      /* sf_axis temp; */
    output_axa_array[iaxis]=sf_iaxa(output,iaxis+1);
    if(verbose>2){
	/* temp=output_axa_array[iaxis]; */
      fprintf(stderr,"axis=%d sf_n(output_axa_array[iaxis])=%d\n",
	              iaxis+1,sf_n(output_axa_array[iaxis]));
    }
    /* kls why does this fail? 
       fprintf(stderr,"temp->n=%d\n",temp->n);
    */
  }

  /***************************/
  /* start trace loop        */
  /***************************/
  fprintf(stderr,"start trace loop\n");
  while (0==get_tah(intrace, fheader, n1_traces, n1_headers, in)){
    if(verbose>1){
      for(iaxis=2; iaxis<dim_output; iaxis++){
	fprintf(stderr,"label[%d]=%s",
		iaxis,label[iaxis]);
	if(typehead == SF_INT)fprintf(stderr,"%d",
				      ((int*)fheader)[indx_of_keys[iaxis]]);
	else                  fprintf(stderr,"%f",
				      fheader[indx_of_keys[iaxis]]);
      }
      fprintf(stderr,"\n");
    }
    tahwritemapped(verbose,intrace, fheader, 
		   n1_traces, n1_headers,
		   output, outheaders,
		   typehead, output_axa_array,
		   indx_of_keys, dim_output,
		   n_output,n_outheaders);
    /**********************************************/
    /* write trace and headers to the output pipe */
    /**********************************************/
    put_tah(intrace, fheader, n1_traces, n1_headers, out);
  }

  exit(0);
}
Пример #3
0
int main(int argc, char* argv[])
{
  int verbose;
  sf_file in=NULL, out=NULL;
  int n1_traces;
  int n1_headers;

  char* header_format=NULL;
  sf_datatype typehead;
  /* kls do I need to add this?  sf_datatype typein; */
  float* fheader=NULL;
  float* intrace=NULL;
  float* fprevheader=NULL;
  int numkeys;
  int ikey;
  char** list_of_keys;
  int *indx_of_keys;
  char* skey;
  int indx_of_skey;
  int skeyvalue;
  bool pkeychanged;
  int itrace=0;

  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  sf_init (argc,argv);

  /* verbose flag controls ammount of print */
  /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */
  /* fprintf(stderr,"read verbose switch.  getint reads command line.\n"); */
  if(!sf_getint("verbose",&verbose))verbose=1;
    /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  sf_warning("verbose=%d",verbose);
 
  /******************************************/
  /* input and output data are stdin/stdout */
  /******************************************/

  if(verbose>0)fprintf(stderr,"read in file name\n");  
  in = sf_input ("in");

  if(verbose>0)fprintf(stderr,"read out file name\n");
  out = sf_output ("out");

  if (!sf_histint(in,"n1_traces",&n1_traces))
    sf_error("input data not define n1_traces");
  if (!sf_histint(in,"n1_headers",&n1_headers)) 
    sf_error("input data does not define n1_headers");

  header_format=sf_histstring(in,"header_format");
  if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
  else                                       typehead=SF_FLOAT;

  if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  fheader = sf_floatalloc(n1_headers);
  fprevheader = sf_floatalloc(n1_headers);
 
  if(verbose>0)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);
  intrace= sf_floatalloc(n1_traces);

  if(verbose>0)fprintf(stderr,"call list of keys\n");
 
  /* this sf_getstring will create parameter descrpiton in the self doc */
  sf_getstring("pkey"); 
  /* \n
     A comma seperated list of primary header keys to monitor to determine 
     gathers.  The trace number in the gather is counted and put in the
     skey header location.
     \n
  */
  list_of_keys=sf_getnstring("pkey",&numkeys);
  /* List of the primary keys monitored to determine gathers. */

  if(list_of_keys==NULL)
    sf_error("The required parameter \"pkey\" was not found.");
  /* I wanted to use sf_getstrings, but it seems to want a colon seperated
     list of keys (eg key=offset:ep:fldr:cdp) and I wanted a comma seperated
     list of keys (eg key=offset:ep:fldr:cdp).
  numkeys=sf_getnumpars("pkey");
  if(numkeys==0)
    sf_error("The required parameter \"pkey\" was not found.");
  fprintf(stderr,"alloc list_of_keys numkeys=%d\n",numkeys);
  list_of_keys=(char**)sf_alloc(numkeys,sizeof(char*)); 
  sf_getstrings("pkey",list_of_keys,numkeys);
  */
  /* print the list of keys */
  if(verbose>1){
    fprintf(stderr,"numkeys=%d\n",numkeys);
    for(ikey=0; ikey<numkeys; ikey++){
      fprintf(stderr,"list_of_keys[%d]=%s\n",ikey,list_of_keys[ikey]);
    }
  }
  if(NULL==(skey=sf_getstring("skey")))
    sf_error("the required parameter \"skey\" was not found");
  /* The name of the secondary key created by the program. */
  
  /* maybe I should add some validation that n1== n1_traces+n1_headers+2
     and the record length read in the second word is consistent with 
     n1.  */

  /**********************************************************/
  /* end code block for standard tah Trace And Header setup */
  /* continue with any sf_puthist this tah program calls to */
  /* add to the history file                                */
  /**********************************************************/

  /* put the history from the input file to the output */
  sf_fileflush(out,in);

  /********************************************************/
  /* continue initialization specific to this tah program */
  /********************************************************/

  /* segy_init gets the list header keys required by segykey function  */
  segy_init(n1_headers,in);
  indx_of_keys=sf_intalloc(numkeys);
  for (ikey=0; ikey<numkeys; ikey++){
    /* kls need to check each of these key names are in the segy header and
       make error message for invalid keys.  Of does segykey do this? NO, just
       segmentation fault. */
    indx_of_keys[ikey]=segykey(list_of_keys[ikey]);
  }
  indx_of_skey=segykey(skey);

  /***************************/
  /* start trace loop        */
  /***************************/
  if(verbose>0)fprintf(stderr,"start trace loop\n");
  skeyvalue=0;
 
  itrace=0;
  while (0==get_tah(intrace, fheader, n1_traces, n1_headers, in)){
    if(verbose>1)fprintf(stderr,"process the tah in sftahgethw\n");
    /********************/
    /* process the tah. */
    /********************/
    /* this program computes a secondary key.  If one of the headers in 
       pkey changes, skey is set to 1.  Otherwise skey is the previous skey+1
    */
    pkeychanged=false;
    if(itrace>0){
      for(ikey=0; ikey<numkeys; ikey++){
	if(typehead == SF_INT){
	  if(((int*)fheader    )[indx_of_keys[ikey]]!=
	     ((int*)fprevheader)[indx_of_keys[ikey]]){
	    pkeychanged=true;
	    break;
	  }
	} else {
	  if(fheader[indx_of_keys[ikey]]!=fprevheader[indx_of_keys[ikey]]){
	    pkeychanged=true;
	    break;
	  }
	}
      }
    }
    if(pkeychanged) skeyvalue=1;
    else skeyvalue++;
    if(typehead == SF_INT) ((int*)fheader)[indx_of_skey]=skeyvalue;
    else                          fheader [indx_of_skey]=skeyvalue;
    
    if(skeyvalue==1){
      /* this is a new pkey, save the header so you know when it changes */
      memcpy(fprevheader,fheader,n1_headers*sizeof(int));
    }
    
    /***************************/
    /* write trace and headers */
    /***************************/
    put_tah(intrace, fheader, n1_traces, n1_headers, out);
    itrace++;
  }

  exit(0);
}
Пример #4
0
int main(int argc, char* argv[])
{
  int verbose;
  sf_file in=NULL, out=NULL;
  int n1_traces;
  int n1_headers;

  char* header_format=NULL;
  sf_datatype typehead;
  /* kls do I need to add this?  sf_datatype typein; */
  float* fheader=NULL;
  float* intrace=NULL;
  float    sx;
  float    sy;
  float    gx;
  float    gy;
  float    offset;
  float offx, offy, midx, midy;
  float scalco, scale;
  int indx_sx;
  int indx_sy;
  int indx_gx;
  int indx_gy;
  int indx_offset;
  int indx_scalco;

  float v, dx, dy, x0, y0, t0, t0xy, t, off_dotprod_dip;
  int it;
  float d1, o1;
  
 /*****************************/
  /* initialize verbose switch */
  /*****************************/
  sf_init (argc,argv);

  if(!sf_getint("verbose",&verbose))verbose=1;
      /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  sf_warning("verbose=%d",verbose);
 
  if(!sf_getfloat("v",&v))sf_error("v, a required parameter not input");
  if(!sf_getfloat("dx",&dx))sf_error("dx, a required parameter not input");
  if(!sf_getfloat("dy",&dy))sf_error("dy, a required parameter not input");
  if(!sf_getfloat("x0",&x0))sf_error("x0, a required parameter not input");
  if(!sf_getfloat("y0",&y0))sf_error("y0, a required parameter not input");
  if(!sf_getfloat("t0",&t0))sf_error("t0, a required parameter not input");

  /******************************************/
  /* input and output data are stdin/stdout */
  /******************************************/

  if(verbose>0)fprintf(stderr,"read in file name\n");  
  in = sf_input ("in");

  if(verbose>0)fprintf(stderr,"read out file name\n");
  out = sf_output ("out");

  if (!sf_histint(in,"n1_traces",&n1_traces))
    sf_error("input data not define n1_traces");
  if (!sf_histint(in,"n1_headers",&n1_headers)) 
    sf_error("input data does not define n1_headers");

  header_format=sf_histstring(in,"header_format");
  if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
  else                                       typehead=SF_FLOAT;

  if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  fheader = sf_floatalloc(n1_headers);
 
  if(verbose>0)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);
  intrace= sf_floatalloc(n1_traces);



  /* maybe I should add some validation that n1== n1_traces+n1_headers+2
     and the record length read in the second word is consistent with 
     n1.  */

  /**********************************************************/
  /* end code block for standard tah Trace And Header setup */
  /* continue with any sf_puthist this tah program calls to */
  /* add to the history file                                */
  /**********************************************************/

  /* put the history from the input file to the output */
  sf_fileflush(out,in);

  /********************************************************/
  /* continue initialization specific to this tah program */
  /********************************************************/
  if (!sf_histfloat(in,"d1",&d1))
    sf_error("input data not define d1");
  if (!sf_histfloat(in,"o1",&o1))
    sf_error("input data not define o1");

  /* segy_init gets the list header keys required by segykey function  */
  segy_init(n1_headers,in);

  indx_sx    =segykey("sx"    );
  indx_sy    =segykey("sy"    );
  indx_gx    =segykey("gx"    );
  indx_gy    =segykey("gy"    );
  indx_offset=segykey("offset");
  indx_scalco=segykey("scalco");

  /***************************/
  /* start trace loop        */
  /***************************/
  if(verbose>0)fprintf(stderr,"start trace loop\n");
  while (0==get_tah(intrace, fheader, n1_traces, n1_headers, in)){
    if(verbose>1)fprintf(stderr,"process the tah in sftahgethw\n");
    /********************/
    /* process the tah. */
    /********************/
    if(typehead == SF_INT){
      /* just cast the header to int so the print works */
      scalco=((int*)fheader)[indx_scalco];
      if(scalco==0)scale=1;
      if(scalco<0)scale=-1/scalco;
      else scale=scalco;
      sx    =scale*((int*)fheader)[indx_sx    ];
      sy    =scale*((int*)fheader)[indx_sy    ];
      gx    =scale*((int*)fheader)[indx_gx    ];
      gy    =scale*((int*)fheader)[indx_gy    ];
      offset=((int*)fheader)[indx_offset];
    } else {
      scalco=fheader[indx_scalco];
      if(scalco==0)scale=1;
      if(scalco<0)scale=-1/scalco;
      else scale=scalco;
      sx    =scale*fheader[indx_sx    ];
      sy    =scale*fheader[indx_sy    ];
      gx    =scale*fheader[indx_gx    ];
      gy    =scale*fheader[indx_gy    ];
      offset=fheader[indx_offset];
    }
    offx=sx-gx;
    offy=sy-gy;
    midx=(sx+gx)/2.0;
    midy=(sy+gy)/2.0;
    if(verbose>2){
      fprintf(stderr, "sx    =%e",sx    );
      fprintf(stderr," sy    =%e",sy    );
      fprintf(stderr," gx    =%e",gx    );
      fprintf(stderr," gy    =%e",gy    );
      fprintf(stderr," offset=%e",offset);
      fprintf(stderr," offx=%e",offx);
      fprintf(stderr," offy=%e",offy);
      fprintf(stderr," newoff=%e",sqrt(offx*offx+offy*offy));
      fprintf(stderr," midx=%e",midx);
      fprintf(stderr," midy=%e",midy);
      fprintf(stderr,"\n");
    }
    /* zero the output trace */
    memset(intrace,0,n1_traces*sizeof(float));
    /* compute time of the spike and put it on the trace */
    /* t=t0; */
    t0xy=t0+(midx-x0)*dx+(midy-y0)*dy;
    /* t=t0xy; */
    /* no dmo term t=sqrt(t0xy*t0xy+(offset*offset)/(v*v)); */
    off_dotprod_dip=offx*dx+offy*dy;
    t=sqrt(t0xy*t0xy+(offset*offset)/(v*v)-off_dotprod_dip*off_dotprod_dip/4.0);
    if(t>=o1){
      it=(t-o1)/d1;
      if(it>0 && it<n1_traces){
	intrace[it]=it+1-(t-o1)/d1;
      }
      if(it+1>0 && it+1<n1_traces){
	intrace[it+1]=(t-o1)/d1-it;
      }
    }
    /***************************/
    /* write trace and headers */
    /***************************/
    put_tah(intrace, fheader, n1_traces, n1_headers, out);
  }

  exit(0);
}
Пример #5
0
int main(int argc, char* argv[])
{
  int verbose;
  sf_file in=NULL, out=NULL;
  int n1_traces;
  int n1_headers;

  char* header_format=NULL;
  sf_datatype typehead;
  /* kls do I need to add this?  sf_datatype typein; */
  float* fheader=NULL;
  float* intrace=NULL;
  int numkeys;
  int ikey;
  char** list_of_keys;
  int *indx_of_keys;
  int indx_time;
  bool pkeychanged;
  float* stktrace=NULL;
  float* stkheader=NULL;
  float* time_variant_fold=NULL;
  int eof_get_tah;
  int fold;
  int itrace=0;
  int ntaper;
  int numxmute;
  int numtmute;
  float* taper;
  char **list_of_floats;
  float* xmute;
  float* tmute;
  int indx_of_offset;
  float offset;
  float d1;
  float o1;
  float mute_start;
  int imute_start;
  int indx_taper;


  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  sf_init (argc,argv);

  if(!sf_getint("verbose",&verbose))verbose=1;
    /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  sf_warning("verbose=%d",verbose);
 
  /******************************************/
  /* input and output data are stdin/stdout */
  /******************************************/

  if(verbose>0)fprintf(stderr,"read in file name\n");  
  in = sf_input ("in");

  if(verbose>0)fprintf(stderr,"read out file name\n");
  out = sf_output ("out");

  if (!sf_histint(in,"n1_traces",&n1_traces))
    sf_error("input data not define n1_traces");
  if (!sf_histint(in,"n1_headers",&n1_headers)) 
    sf_error("input data does not define n1_headers");

  header_format=sf_histstring(in,"header_format");
  if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
  else                                       typehead=SF_FLOAT;

  if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  fheader = sf_floatalloc(n1_headers);
 
  if(verbose>0)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);
  intrace= sf_floatalloc(n1_traces);

  if(verbose>0)fprintf(stderr,"call list of keys\n");
 
  if(!(list_of_keys=sf_getnstring("key",&numkeys)))
    fprintf(stderr,"key not input\n");
  /*  List of header keys to monitor to determine when to break between 
      gathers.  A gather is a sequence of traces with the same value for
      all the header keys.  Stack summs traces in the gather, divides
      by the fold, and outputs the stack trace. */

  if(verbose>0)fprintf(stderr,"after sf_getnstring\n");

  if(list_of_keys==NULL)
    sf_error("The required parameter \"key\" was not found.");
  /* I wanted to use sf_getstrings, but it seems to want a colon seperated
     list of keys (eg key=offset:ep:fldr:cdp) and I wanted a comma seperated
     list of keys (eg key=offset:ep:fldr:cdp).
  numkeys=sf_getnumpars("key");
  if(numkeys==0)
    sf_error("The required parameter \"key\" was not found.");
  fprintf(stderr,"alloc list_of_keys numkeys=%d\n",numkeys);
  list_of_keys=(char**)sf_alloc(numkeys,sizeof(char*)); 
  sf_getstrings("key",list_of_keys,numkeys);
  */
  /* print the list of keys */
  if(verbose>1){
    fprintf(stderr,"numkeys=%d\n",numkeys);
    for(ikey=0; ikey<numkeys; ikey++){
      fprintf(stderr,"list_of_keys[%d]=%s\n",ikey,list_of_keys[ikey]);
    }
  }
  
  /* maybe I should add some validation that n1== n1_traces+n1_headers+2
     and the record length read in the second word is consistent with 
     n1.  */

  /**********************************************************/
  /* end code block for standard tah Trace And Header setup */
  /* continue with any sf_puthist this tah program calls to */
  /* add to the history file                                */
  /**********************************************************/

  /* put the history from the input file to the output */
  sf_fileflush(out,in);

  /********************************************************/
  /* continue initialization specific to this tah program */
  /********************************************************/

  /* segy_init gets the list header keys required by segykey function  */
  segy_init(n1_headers,in);
  indx_of_keys=sf_intalloc(numkeys);
  for (ikey=0; ikey<numkeys; ikey++){
    /* kls need to check each of these key names are in the segy header and
       make error message for invalid keys.  Of does segykey do this? NO, just
       segmentation fault. */
    indx_of_keys[ikey]=segykey(list_of_keys[ikey]);
  }
  /* get the mute parameters */
  if(NULL==(list_of_floats=sf_getnstring("xmute",&numxmute))){
    xmute=NULL;
    numxmute=0;
  } else {
    xmute=sf_floatalloc(numxmute);
    if(!sf_getfloats("xmute",xmute,numxmute))sf_error("unable to read xmute");
  }
  if(NULL==(list_of_floats=sf_getnstring("tmute",&numtmute))){
    tmute=NULL;
    numtmute=0;
  } else {
    tmute=sf_floatalloc(numtmute);
    if(!sf_getfloats("tmute",tmute,numtmute))sf_error("unable to read tmute");
  }
  if(numxmute!=numtmute)sf_error("bad mute parameters: numxmute!=numtmute");
  if(numxmute<=0) {
      ntaper=0;
      indx_of_offset=0;
      taper=NULL;
  } else {
    if(!sf_getint("ntaper",&ntaper))ntaper=12;
    /* \n
       length of the taper on the stack mute
    */
    taper=sf_floatalloc(ntaper);
    for(indx_time=0; indx_time<ntaper; indx_time++){
      float val_sin=sin((indx_time+1)*SF_PI/(2*ntaper));
      taper[indx_time]=val_sin*val_sin;
    }
    indx_of_offset=segykey("offset");
  }

  if (!sf_histfloat(in,"d1",&d1))
    sf_error("input data does not define d1");
  if (!sf_histfloat(in,"o1",&o1))
    sf_error("input data does not define o1");

  stktrace          = sf_floatalloc(n1_traces);
  stkheader         = sf_floatalloc(n1_headers);
  time_variant_fold = sf_floatalloc(n1_traces);

  /***************************/
  /* start trace loop        */
  /***************************/
  if(verbose>0)fprintf(stderr,"start trace loop\n");
 
  itrace=0;
  eof_get_tah=get_tah(intrace, fheader, n1_traces, n1_headers, in);
  fold=0;
  while (!eof_get_tah){
    if(verbose>1 && itrace<5)fprintf(stderr,"process the tah in sftahstack\n");
    /********************/
    /* process the tah. */
    /********************/
    /* this program stacks sequential traces with matching pkey.  If one of the 
       headers in pkey changes, the summed trace is divided by the time 
       variant fold and output.
    */
    if(fold==0){
      memcpy(stkheader,fheader,n1_headers*sizeof(int));
      for(indx_time=0; indx_time<n1_traces; indx_time++){
	time_variant_fold[indx_time]=0.0;
	stktrace[indx_time]=0.0;
      }
    }
    if(xmute==NULL)mute_start=o1;
    else{
      if(typehead == SF_INT)offset=((int  *)fheader)[indx_of_offset];
      else                  offset=((float*)fheader)[indx_of_offset];
      intlin(numxmute,xmute,tmute,
	     tmute[0],tmute[numxmute-1],1,
	     &offset,&mute_start);
      if(mute_start<o1)mute_start=o1;
    }
    imute_start=(int)(((mute_start-o1)/d1)+.5);
    if(0)fprintf(stderr,"imute_start=%d\n",imute_start);
    for(; imute_start<n1_traces && intrace[imute_start]==0;
	  imute_start++); /* increate imute_start to first non-zero sample */
    if(0)fprintf(stderr,"updated imute_start=%d\n",imute_start);
    for(indx_time=imute_start, indx_taper=0; 
	indx_time<imute_start+ntaper && indx_time<n1_traces; 
	indx_time++, indx_taper++){
      stktrace[indx_time]+=taper[indx_taper]*intrace[indx_time];
      time_variant_fold[indx_time]+=taper[indx_taper];
    }
    for(; indx_time<n1_traces; indx_time++){
      stktrace[indx_time]+=intrace[indx_time];
      time_variant_fold[indx_time]++;
    }

    fold++;
    eof_get_tah=get_tah(intrace, fheader, n1_traces, n1_headers, in);

    /* did any of the header keys in indx_of_keys change? */
    pkeychanged=false;
    if(itrace>0){
      for(ikey=0; ikey<numkeys; ikey++){
	if(typehead == SF_INT){
	  if(((int*)fheader  )[indx_of_keys[ikey]]!=
	     ((int*)stkheader)[indx_of_keys[ikey]]){
	    pkeychanged=true;
	    break;
	  }
	} else {
	  if(fheader[indx_of_keys[ikey]]!=stkheader[indx_of_keys[ikey]]){
	    pkeychanged=true;
	    break;
	  }
	}
      }
    }
    /* if one of the headers changes, apply fold recovery, output trace, and 
       set fold=0.  Fold=0 will initialize the stktrace to zero at top f loop*/
    if(pkeychanged){
      /***********************************/
      /* divide by the time variant fold */
      /***********************************/
      if(verbose>1)fprintf(stderr,"pkeychanged.  divide by fold\n");
      for(indx_time=0; indx_time<n1_traces; indx_time++){
	if(time_variant_fold[indx_time]<1.0)time_variant_fold[indx_time]=1.0;
	stktrace[indx_time]/=time_variant_fold[indx_time];
      }   
      /* kls set any headers? Maybe fold? offset? */
      /***************************/
      /* write trace and headers */
      /***************************/
      if(verbose>1)fprintf(stderr,"put_tah\n");
      put_tah(stktrace, stkheader, n1_traces, n1_headers, out);
      fold=0;
    }
    itrace++;
  }

  exit(0);
}
Пример #6
0
int main(int argc, char* argv[])
{
  int verbose;
  sf_file in=NULL, out=NULL;
  int n1_traces;
  int n1_headers;

  char* header_format=NULL;
  sf_datatype typehead;
  /* kls do I need to add this?  sf_datatype typein; */
  float* fheader=NULL;
  float* intrace=NULL;
  float* fprevheader=NULL;
  int numkeys;
  int ikey;
  char** list_of_keys;
  char* slockey;
  int indx_tracex;
  int indx_tracey;
  int indx_sloc;
  double this_x;
  double this_y;
  double *tracex;
  double *tracey;
  int *slocarray;
  int slocarray_indx;

  int size_tracexy_array;
  int num_tracexy;

  int sloc;
  int itrace=0;

  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  sf_init (argc,argv);

  /* verbose flag controls ammount of print */
  /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */
  /* fprintf(stderr,"read verbose switch.  getint reads command line.\n"); */
  if(!sf_getint("verbose",&verbose))verbose=1;
    /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  sf_warning("verbose=%d",verbose);
 
  /******************************************/
  /* input and output data are stdin/stdout */
  /******************************************/

  if(verbose>0)fprintf(stderr,"read in file name\n");  
  in = sf_input ("in");

  if(verbose>0)fprintf(stderr,"read out file name\n");
  out = sf_output ("out");

  if (!sf_histint(in,"n1_traces",&n1_traces))
    sf_error("input data not define n1_traces");
  if (!sf_histint(in,"n1_headers",&n1_headers)) 
    sf_error("input data does not define n1_headers");

  header_format=sf_histstring(in,"header_format");
  if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
  else                                       typehead=SF_FLOAT;

  if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  fheader = sf_floatalloc(n1_headers);
 
  if(verbose>0)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);
  intrace= sf_floatalloc(n1_traces);

  if(verbose>0)fprintf(stderr,"call list of keys\n");
 
  /* the next line will create a parameter description in the selfdoc */
  sf_getstring("slocxy");
  /* \n
     two keys that are the trace headers names of the x,y coordinate 
     to use to identify surface locations and compute the trace header
     value for slockey \n */
  list_of_keys=sf_getnstring("slocxy",&numkeys);

  if(list_of_keys==NULL)
    sf_error("The required parameter \"slocxy\" was not found.");
  /* I wanted to use sf_getstrings, but it seems to want a colon seperated
     list of keys (eg key=offset:ep:fldr:cdp) and I wanted a comma seperated
     list of keys (eg key=offset:ep:fldr:cdp).
  */
  if(numkeys!=2) sf_error("There must be exactly two header names in slocxy");

  /* print the list of keys */
  if(verbose>=1){
    fprintf(stderr,"numkeys=%d\n",numkeys);
    fprintf(stderr,"input trace xcoord=%s\n",list_of_keys[0]);
    fprintf(stderr,"input trace ycoord=%s\n",list_of_keys[1]);

    for(ikey=0; ikey<numkeys; ikey++){
      fprintf(stderr,"list_of_keys[%d]=%s\n",ikey,list_of_keys[ikey]);
    }
  }
  if(NULL==(slockey=sf_getstring("slockey")))
    sf_error("the required parameter \"slockey\" was not found");
  /* The name of the sloc key created by the program. */
  
  /* maybe I should add some validation that n1== n1_traces+n1_headers+2
     and the record length read in the second word is consistent with 
     n1.  */

  /**********************************************************/
  /* end code block for standard tah Trace And Header setup */
  /* continue with any sf_puthist this tah program calls to */
  /* add to the history file                                */
  /**********************************************************/

  /* put the history from the input file to the output */
  sf_fileflush(out,in);

  /********************************************************/
  /* continue initialization specific to this tah program */
  /********************************************************/

  /* segy_init gets the list header keys required by segykey function  */
  segy_init(n1_headers,in);
  indx_tracex=segykey(list_of_keys[0]);
  indx_tracey=segykey(list_of_keys[1]);
  indx_sloc=segykey(slockey);

  size_tracexy_array=100;
  num_tracexy=0;

  tracex=malloc(size_tracexy_array*sizeof(double));
  tracey=malloc(size_tracexy_array*sizeof(double));
  slocarray=malloc(size_tracexy_array*sizeof(int));

  /***************************/
  /* start trace loop        */
  /***************************/
  if(verbose>0)fprintf(stderr,"start trace loop\n");
 
  itrace=0;
  while (0==get_tah(intrace, fheader, n1_traces, n1_headers, in)){
    if(verbose>1)fprintf(stderr,"process the tah in sftahgethw\n");
    /********************/
    /* process the tah. */
    /********************/
    /* this program computes a secondary key.  If one of the headers in 
       pkey changes, skey is set to 1.  Otherwise skey is the previous skey+1
    */

    if(typehead == SF_INT){
      this_x=((int*)fheader    )[indx_tracex];
      this_y=((int*)fheader    )[indx_tracey];
    } else {
      this_x=       fheader     [indx_tracex];
      this_y=       fheader     [indx_tracey];
    }
    slocarray_indx=tahinsert_unique_xy(&tracex,&tracey,&slocarray,
		                       &num_tracexy,&size_tracexy_array,
		                       this_x, this_y);
    sloc=slocarray[slocarray_indx];
    
    if(typehead == SF_INT) ((int*)fheader)[indx_sloc]=sloc;
    else                          fheader [indx_sloc]=sloc;
    
    /***************************/
    /* write trace and headers */
    /***************************/
    put_tah(intrace, fheader, n1_traces, n1_headers, out);
    itrace++;
  }

  exit(0);
}
Пример #7
0
int main(int argc, char* argv[])
{
    bool segy;
    int i, i1, i2, n1, n2, n3, n, nt, len, nkey, row;
    sf_file in, out;
    int mem; /* for avoiding int to off_t typecast warning */
    off_t memsize;
    char *eq, *output, *key, *arg;
    float **ftra=NULL, **fbuf=NULL, **fst=NULL, d2, o2;
    int **itra=NULL, **ibuf=NULL, **ist=NULL;
    sf_datatype type;

    sf_init (argc,argv);
    in = sf_input ("in");
    out = sf_output ("out");

    sf_putint(out,"N",0);
    sf_putint(out,"T",1);
    sf_putint(out,"input",2);

    type = sf_gettype(in);

    if (SF_FLOAT != type && SF_INT != type) sf_error("Need float or int input");

    if (!sf_getbool("segy",&segy)) segy=true;
    /* if SEGY headers */

    if (!sf_histint(in,"n1",&n1)) n1=1;
    if (!sf_histint(in,"n2",&n2)) n2=1;
    n3 = sf_leftsize(in,2); /* left dimensions after the first two */

    if (segy) {
	segy_init(n1,in);
    } else {
	other_init(n1,in);
    }

    if (NULL != (key = sf_getstring("key"))) { 
	/* key to replace */
	row = segykey(key);
	free(key);
    } else {
	if (!sf_getint("nkey",&row)) row=-1;
	/* number of key to replace */
    }	
    if (row > n1) sf_error("nkey=%d is too large, need nkey <= %d",row,n1);
    
    if (n1 > 1) {
	if (n2 > 1) { /* input: many keys */
	    if (row < 0) sf_putint(out,"n1",1);
	} else { /* input: one key, arranged in n1 */
	    n2 = n1;
	    n1 = 1;
	}
    }


    for (i=0; i < n1; i++) {
	sf_putint(out,segykeyword(i),i+3);
    }

    for (i=1; i< argc; i++) { /* collect inputs */
	arg = argv[i];
	eq =  strchr(arg,'=');
	if (NULL == eq) continue; /* not a parameter */
	if (0 == strncmp(arg,"output",6) ||
	    0 == strncmp(arg,    "--",2)) continue; /* not a key */

	len = (size_t) (eq-arg);
	key = sf_charalloc(len+1);
	memcpy(key,arg,len);
	key[len]='\0';

	if (sf_getint(key,&nkey))
	    sf_putint(out,key,nkey+3);
	free(key);
    }
  
    if (!sf_histfloat(in,n1>1? "d2":"d1",&d2)) d2=1.;
    if (!sf_histfloat(in,n1>1? "o2":"o1",&o2)) o2=0.;

    if (NULL == (output = sf_getstring("output"))) sf_error("Need output=");
    /* Describes the output in a mathematical notation. */

    if (!sf_getint("memsize",&mem))
        mem=sf_memsize();
    /* Max amount of RAM (in Mb) to be used */
    memsize = mem * (1<<20); /* convert Mb to bytes */

    len = sf_math_parse (output,out,type);

    /* number of traces for optimal I/O */
    nt = SF_MAX(1,memsize/((2*n1+len+6)*sizeof(float)));

    if (SF_FLOAT == type) { /* float type */
	ftra = sf_floatalloc2(n1,nt);
	fbuf = sf_floatalloc2(nt,n1+3);
	fst  = sf_floatalloc2(nt,len+3);
    } else {               /* int type */
	itra = sf_intalloc2(n1,nt);
	ibuf = sf_intalloc2(nt,n1+3);
	ist  = sf_intalloc2(nt,len+3);
    }

    for (n=n2*n3; n > 0; n -= nt) {
	if (n < nt) nt=n;

	if (SF_FLOAT == type) { 
	    sf_floatread(ftra[0],n1*nt,in);
	} else {
	    sf_intread(itra[0],n1*nt,in);
	}

	for (i2=0; i2 < nt; i2++) {
	    if (SF_FLOAT == type) { 
		fbuf[0][i2]=(float) i2;  /* N */
		fbuf[1][i2]=o2+i2*d2;    /* T */
		fbuf[2][i2]=ftra[0][i2]; /* input */
	    } else {
		ibuf[0][i2]=i2;          /* N */
		ibuf[1][i2]=o2+i2*d2;    /* T */
		ibuf[2][i2]=itra[0][i2]; /* input */
	    }
	}
	for (i1=0; i1 < n1; i1++) {
	    for (i2=0; i2 < nt; i2++) {
		if (SF_FLOAT == type) { 
		    fbuf[i1+3][i2]=ftra[i2][i1];
		} else {
		    ibuf[i1+3][i2]=itra[i2][i1];
		}
	    }
	}
	
	if (SF_FLOAT == type) { 
	    sf_math_evaluate (len, nt, fbuf, fst);
	    if (row < 0) {
		sf_floatwrite(fst[1],nt,out);
	    } else {
		for (i2=0; i2 < nt; i2++) {
		    ftra[i2][row] = fst[1][i2];
		}
		sf_floatwrite(ftra[0],n1*nt,out);
	    }
	} else {
	    sf_int_math_evaluate (len, nt, ibuf, ist);
	    if (row < 0) {
		sf_intwrite(ist[1],nt,out);
	    } else {
		for (i2=0; i2 < nt; i2++) {
		    itra[i2][row] = ist[1][i2];
		}
		sf_intwrite(itra[0],n1*nt,out);
	    }
	}
    }

    exit(0);
}
Пример #8
0
int main(int argc, char* argv[])
{
  sf_file infile=NULL, out=NULL, inheaders=NULL;
  int n1_traces;
  int n1_headers;

  int n_traces, n_headers; 
  sf_datatype typehead;
  sf_datatype typein;
/*  float* header=NULL; */
  char* infile_filename=NULL;
  char* headers_filename=NULL;
  /*
  indxkey** pntrs_to_indxkeys;
  char* sort;
  char** sortheadernames;
  char** sorttokens1;
  double* signsortname;
  double* sortmin;
  double* sortmax;
  double* sortinc;
  int n_traces_sort;
  int n_pntrs_to_indxkeys;
  int i1_headers;
  int* indx_of_keys;
  int isortheader;
  int sizeof_my_indxkey;
  bool allzero;
  bool passrangetest;
  */
  char* ilinekey;
/*  char* xlinekey; */
  int ilineinc;
  int indx_ilinekey;
/*  int indx_xlinekey; */
  float ilinemin;
  float ilinemax;
  float xlinemin;
  float xlinemax;
  int headers_in_buffer;
  int i_header;
  int ii_header;
  int n_headers_read;
  int n_headers_left;
  double doubleiline;
  float** header_buf=NULL;
  int indx_number;
  int min_indx_number;
  int max_indx_number;

  
  sf_init (argc,argv);

  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  /* verbose flag controls amount of print */
  /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */
  /* fprintf(stderr,"read verbose switch.  getint reads command line.\n"); */
  if(!sf_getint("verbose",&verbose))verbose=1;
  /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  if(verbose>0)fprintf(stderr,"verbose=%d\n",verbose);
 
  /*****************************************/
  /* initialize the input and output files */
  /*****************************************/
  if(verbose>0)fprintf(stderr,"read name of input file name\n");
  
  infile_filename=sf_getstring("input");
  /* \n
     Input file for traces amplitudes
  */
  if(infile_filename==NULL) infile = sf_input ("in");
  else infile = sf_input (infile_filename);

  if(verbose>0)
    fprintf(stderr,"set up output file for tah - should be stdout\n");
  out = sf_output ("out");

  if(verbose>0)fprintf(stderr,"read name of input headers file\n");
  headers_filename=sf_getstring("headers");
  /* \n
     Trace header file name.  Default is the input data file
     name, with the final .rsf changed to _hdr.rsf 
  */

  if(headers_filename==NULL){
    /* compute headers_filename from infile_filename by replacing the final
       .rsf with _hdr.rsf */
    if(!(0==strcmp(infile_filename+strlen(infile_filename)-4,".rsf"))){
	fprintf(stderr,"parameter input, the name of the input file, does\n");
	fprintf(stderr,"not end with .rsf, so header filename cannot be\n");
	fprintf(stderr,"computed by replacing the final .rsf with _hdr.rsf.\n");
	sf_error("default for headers parameter cannot be computed.");
    }
    /* allocate header file name to be length of infile_filename +4+1 to allow 
       _hdr to be inserted and \0 to be appended.  */
    headers_filename=malloc(strlen(infile_filename)+4+1);
    headers_filename[0]='\0'; /* make string zero length, null terminated */
    strcpy(headers_filename,infile_filename);
    strcpy(headers_filename+strlen(infile_filename)-4,"_hdr.rsf\0");
    if(verbose>2)
      fprintf(stderr,"parameter header defaulted.  Computed to be #%s#\n",
			 headers_filename);
  }
  if(verbose>2)
    fprintf(stderr,"parameter header input or computed  #%s#\n",
		       headers_filename);

  inheaders = sf_input(headers_filename);

  if (!sf_histint(infile,"n1",&n1_traces))
    sf_error("input file does not define n1");
  if (!sf_histint(inheaders,"n1",&n1_headers)) 
    sf_error("input headers file does not define n1");

  n_traces=sf_leftsize(infile,1);
  n_headers=sf_leftsize(inheaders,1);
  if(verbose>0)
    fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);

  if(n_traces!=n_headers){
    fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);
    sf_error("number of traces and headers must be the same");
  }

  typehead = sf_gettype(inheaders);
  if (SF_INT != typehead && SF_FLOAT != typehead ) 
    sf_error("Need float or int input headers.");

  typein = sf_gettype(infile);
  if (SF_FLOAT != typein ) 
    sf_error("input file must contain floats.");
  
  /* must be float or int */
  if(verbose>1)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  /* header = sf_floatalloc(n1_headers); */
 
  /* I make stdout just trace header and one trace amplitude for now.
     I do not think it will have a use and will delete it later */
  if(verbose>1)
    fprintf(stderr,"need to add 2 words, record type and record length.\n");
  sf_putint(out,"n1",1+n1_headers+2);
  sf_putint(out,"n1_traces",1);

  /*************************************************************************/
  /* put info into history so later tah programs can figure out the headers*/
  /*************************************************************************/
  if(verbose>1)fprintf(stderr,"put n1_headers to history\n");
  sf_putint(out,"n1_headers",n1_headers);
  if(verbose>1)fprintf(stderr,"test typehead\n");
  if(SF_INT == typehead) sf_putstring(out,"header_format","native_int");
    else                 sf_putstring(out,"header_format","native_float");
  sf_putstring(out,"headers",headers_filename);
  /* the list of any extra header keys */
  if(verbose>1)fprintf(stderr,"segy_init\n");
  segy_init(n1_headers,inheaders);
  if(verbose>1)fprintf(stderr,"segy2hist\n");
  segy2hist(out,n1_headers);
  
  /* put the history from the input file to the output */
  if(verbose>1)fprintf(stderr,"fileflush out\n");
  sf_fileflush(out,infile);
  
  /* get the index keys.  Normally this will be iline and xline */
  if(!(ilinekey=sf_getstring("iline")))ilinekey="iline";
  /* \n
     header key for the main index key.  This should be iline, but you 
     may have non-standard trace headers or a wierd use of this program 
  */
/*  if(!(xlinekey=sf_getstring("xline")))xlinekey="xline"; */
  /* \n
     header key for the secondary index key.  This should be xline, but you
     may have non-standard trace headers or a wierd use of this program 
  */
  
  /* ilinemin, ilinemax, xlinemin, and xlinemaxcan be used to remove
     be trace headers with nulls. They default to -1e31, +1e31 */
  if(!sf_getfloat("ilinemin",&ilinemin))ilinemin=-1e31;
  /* \n
     minimum "iline" header key to include in the index.  Use this parameter
     to remove null trace headers or traces outside project area.
  */
  if(!sf_getfloat("ilinemax",&ilinemax))ilinemax=-1e31;
  /* \n
     maximum "iline" header key to include in the index.  Use this parameter
     to remove null trace headers or traces outside project area.
  */
  if(!sf_getfloat("xlinemin",&xlinemin))xlinemin=-1e31;
  /* \n
     minimum "xline" header key to include in the index.  Use this parameter
     to remove null trace headers or traces outside project area.
  */
  if(!sf_getfloat("xlinemax",&xlinemax))xlinemax=-1e31;
  /* \n
     maximum "xline" header key to include in the index.  Use this parameter
     to remove null trace headers or traces outside project area.
  */

  sf_getstring("indxdir");
  /* \n
     The name of the directory containing the iline,xline index.  This 
     directory will be in DATAPATH (probably the environment variable). The 
     directory also continues a file "filenames", a list of the trace and 
     header files that contributes to this index. The directory contains files 
     with names "indx#" here # is an integer multiple of ilineinc. These files 
     contains a record for each contributing trace with filenumber, 
     tracenumber, and the trace header. The file containing the trace is 
     determined using the  can be read by using the filenumber and the 
     "filenames" file.  The tracenumber defines the location of the trace 
     in the file.
  */

  if(!sf_getint("ilineinc",&ilineinc))ilineinc=10;
  /* \n
     incrment in iline for the index
  */

  indx_ilinekey=segykey(ilinekey);
/*  indx_xlinekey=segykey(xlinekey); */

  fprintf(stderr,"process the filenames file\n");  
  /* kls process the file, "filenames"
     -if it does not exist create it
     -if it already exists, count the number files already in "filenemae"
     -add the traceheaders and traces files to "filenames"
     -remenber the file_number of this trace/header input file
  */

  /* make buffer 1 gbyte, about 25 million traces. that should be reasonable */
  headers_in_buffer=1000*1000*1000/(4*n1_headers);
  header_buf=sf_floatalloc2(n1_headers,headers_in_buffer);
  /* sf_alloc2 for array of trace headers */
  if(verbose>0)fprintf(stderr,"start loop to read headers to build sort key\n");
  for (i_header=0; i_header<n_headers; i_header+=headers_in_buffer){
    /* read smaller of headers_in_buffer and number of headers left to read */
    fprintf(stderr,"read a buffer of trace headers\n");
    n_headers_read=headers_in_buffer;
    n_headers_left=n_headers-i_header;
    if(n_headers_read>n_headers_left)n_headers_read=n_headers_left;
    fprintf(stderr,"read words=%d\n",n1_headers*n_headers_read);
    sf_floatread(&header_buf[0][0],n1_headers*n_headers_read,inheaders);
    fprintf(stderr,"read the headers\n");
    /* find the minimum and maximum indx_number this buffer contributes to */
    if(typehead==SF_FLOAT)doubleiline=        header_buf [0][indx_ilinekey];
    else                  doubleiline=((int**)header_buf)[0][indx_ilinekey];
    indx_number=rounddouble(doubleiline/((double)ilineinc))*ilineinc;
    min_indx_number=max_indx_number=indx_number;
    fprintf(stderr,"loop to find min/max\n");
    for (ii_header=0; ii_header<n_headers_read; ii_header++){
      if(typehead==SF_FLOAT){
	doubleiline=        header_buf [ii_header][indx_ilinekey];
      }else{                  
	doubleiline=((int**)header_buf)[ii_header][indx_ilinekey];
      }
      fprintf(stderr,"ii_header=%d,doubleiline=%f %d\n",
              ii_header,doubleiline,
	      ((int**)header_buf)[ii_header][indx_ilinekey]);
      indx_number=rounddouble(doubleiline/((double)ilineinc))*ilineinc;
      if(min_indx_number>indx_number)min_indx_number=indx_number;
      if(max_indx_number<indx_number)max_indx_number=indx_number;
    }
    fprintf(stderr,"min_indx_number=%d, max_indx_number=%d\n",
	    min_indx_number   ,max_indx_number);
    /* kls pass over each indx_file:
       -open indx_file (create it if it does not exist)
       -loop over all the headers in header_buf
          -write filenumber, traceindx, and the trace header to the end of
	     indx_file 
       -close the indx_file   */
    /* to convert relative to absolute path:
       #include <stdio.h>
       #include <stdlib.h>
       int main(int c,char**v) {
  
         fprintf(stderr,"%s\n",realpath(v[1],0));
         return 0;
       }

    */







  } /* end of the for loop that reads a buffer of headers */









  exit(0);
}
Пример #9
0
int main(int argc, char* argv[])
{
  sf_file infile=NULL, out=NULL, inheaders=NULL;
  int n1_traces;
  int n1_headers;

  int n_traces, n_headers; 
  sf_datatype typehead;
  sf_datatype typein;
  float* header=NULL;
  float* intrace=NULL;
  int i_trace;
  char* infile_filename=NULL;
  char* headers_filename=NULL;
  bool makeheader;
  int iaxis;
  char parameter[13];
  char* label_in[SF_MAX_DIM+1];
  int n_in[SF_MAX_DIM+1];
  float o_in[SF_MAX_DIM+1];
  float d_in[SF_MAX_DIM+1];
  int indx_of_keys[SF_MAX_DIM+1];
  int traceindx[SF_MAX_DIM+1];
  float tracecoord[SF_MAX_DIM+1];
  int indxleft;
  
  sf_init (argc,argv);

  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  if(!sf_getint("verbose",&verbose))verbose=1;
  /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  if(verbose>0)fprintf(stderr,"verbose=%d\n",verbose);
 
  /*****************************************/
  /* initialize the input and output files */
  /*****************************************/
  if(verbose>0)fprintf(stderr,"read name of input file name\n");
  
  infile_filename=sf_getstring("input");
  /* \n
     Input file for traces amplitudes.  You can list a file here, has the 
     input file name will be used to compute the name of the header files.

     The input trace amplitudes can also be read from standard input by
     just supplying standard input and omitting this paramater,  This 
     is useful it you wish to do sopme initial processing of the input
     rsf file containing the trace amplitudes.  This is useful if you need 
     to change input axis labels to use the makeheader=yes.
  */
  if(infile_filename==NULL) infile = sf_input ("in");
  else infile = sf_input (infile_filename);

  /* get the axis labels on the input file */
  /* these will be used if users requests makeheaders */
  for (iaxis=2; iaxis<SF_MAX_DIM+1; iaxis++){
    sprintf(parameter,"label%d",iaxis);
    if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
    /* get label, o, n, and i for each axis.  These will be used to 
       make headers from axis (if that option is requested) */
    if (!(label_in[iaxis]=sf_histstring(infile,parameter))) {
      label_in[iaxis]="none";
    }
    sprintf(parameter,"o%d",iaxis);
    if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
    if (!sf_histfloat(infile,parameter,&o_in[iaxis])){
      label_in[iaxis]="none";
    }
    sprintf(parameter,"d%d",iaxis);
    if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
    if (!sf_histfloat(infile,parameter,&d_in[iaxis])){
      label_in[iaxis]="none";
    }
    sprintf(parameter,"n%d",iaxis);
    if(verbose>1)fprintf(stderr,"first try to read %s\n",parameter);
    if (!sf_histint(infile,parameter,&n_in[iaxis])){
      n_in[iaxis]=1;
    }
    if(verbose>1){
      fprintf(stderr,"on axis=%d got label,o,d,n=%s,%f,%f,%d \n",iaxis,
	      label_in[iaxis],o_in[iaxis],d_in[iaxis],n_in[iaxis]);
    }
  }
  /* compute index_of_keys after calling segy_init */ 






  if(verbose>0)
    fprintf(stderr,"set up output file for tah - should be stdout\n");
  out = sf_output ("out");

  if(verbose>0)fprintf(stderr,"read name of input headers file\n");
  headers_filename=sf_getstring("headers");
  /* \n
     Trace header file name.  Default is the input data file
     name, with the final .rsf changed to _hdr.rsf 
  */

  if(headers_filename==NULL){
    /* compute headers_filename from infile_filename by replacing the final
       .rsf with _hdr.rsf */
    if(!(0==strcmp(infile_filename+strlen(infile_filename)-4,".rsf"))){
	fprintf(stderr,"parameter input, the name of the input file, does\n");
	fprintf(stderr,"not end with .rsf, so header filename cannot be\n");
	fprintf(stderr,"computed by replacing the final .rsf with _hdr.rsf.\n");
	sf_error("default for headers parameter cannot be computed.");
    }
    headers_filename=malloc(strlen(infile_filename)+60);
    strcpy(headers_filename,infile_filename);
    strcpy(headers_filename+strlen(infile_filename)-4,"_hdr.rsf\0");
    if(verbose>2){
      fprintf(stderr,"parameter header defaulted.  Computed to be #%s#\n",
	      headers_filename);
    }
  }
  if(verbose>2)fprintf(stderr,"parameter header input or computed  #%s#\n",
		       headers_filename);

  if(!sf_getbool("makeheader",&makeheader))makeheader=false;
  /* \n
     Option to load headers using the input file axis labels.  If axis 
     label2 through label9 match a header key then that coordinate is
     loaded to the traces header.  This can be used to load the source
     coordinate to the sx header location.  This may require changing
     the axis label because Madagascar axis labels are not the same as
     segy trace headers.  for example axis 2 coordiante can be loaded in
     sx trace header by:
        <spike.rsf sfput label2=sx \\
           | sftahread headers=spike_hdr.rsf makeheader=y \\ 
           | sftahgethw key=sx >/dev/null
 */
  
  inheaders = sf_input(headers_filename);

  if (!sf_histint(infile,"n1",&n1_traces))
    sf_error("input file does not define n1");
  if (!sf_histint(inheaders,"n1",&n1_headers)) 
    sf_error("input headers file does not define n1");






  n_traces=sf_leftsize(infile,1);
  n_headers=sf_leftsize(inheaders,1);
  if(verbose>0)
    fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);

  if(n_traces!=n_headers){
    fprintf(stderr,"n_traces=%d, n_headers=%d\n",n_traces,n_headers);
    sf_error("number of traces and headers must be the same");
  }

  typehead = sf_gettype(inheaders);
  if (SF_INT != typehead && SF_FLOAT != typehead ) 
    sf_error("Need float or int input headers.");

  typein = sf_gettype(infile);
  if (SF_FLOAT != typein ) 
    sf_error("input file must contain floats.");
  
  /* must be float or int */
  if(verbose>1)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  header = sf_floatalloc(n1_headers);
 
  if(verbose>1)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);

  intrace= sf_floatalloc(n1_traces);

  if(verbose>1)fprintf(stderr,"need to add 2 words.  Record type and record length\n");
  sf_putint(out,"n1",n1_traces+n1_headers+2);
  sf_putint(out,"n1_traces",n1_traces);

  /*************************************************************************/
  /* put info into history so later tah programs can figure out the headers*/
  /*************************************************************************/
  if(verbose>1)fprintf(stderr,"put n1_headers to history\n");
  sf_putint(out,"n1_headers",n1_headers);
  if(verbose>1)fprintf(stderr,"test typehead\n");
  if(SF_INT == typehead) sf_putstring(out,"header_format","native_int");
    else                 sf_putstring(out,"header_format","native_float");
  sf_putstring(out,"headers",headers_filename);
  /* the list of any extra header keys */
  if(verbose>1)fprintf(stderr,"segy_init\n");
  segy_init(n1_headers,inheaders);
  if(verbose>1)fprintf(stderr,"segy2hist\n");
  segy2hist(out,n1_headers);
  if(0)  {
    int tempint;
    tempint=segykey("ep");
    fprintf(stderr,"ep tempint=%d\n",tempint);
    tempint=segykey("epx");
    fprintf(stderr,"epx tempint=%d\n",tempint);
  }
  if(makeheader){
    for (iaxis=2; iaxis<SF_MAX_DIM+1; iaxis++){
      if(0==strcmp("none",label_in[iaxis])){
	indx_of_keys[iaxis]=-1;
      } else {
	indx_of_keys[iaxis]=segykey(label_in[iaxis]);
	if(indx_of_keys[iaxis]<0){
	  sf_warning("************************************************");
	  sf_warning("************************************************");
	  sf_warning("axis %d has label that is not a header key",iaxis);
	  sf_warning("This axis label will not be loaded");
	  sf_warning("************************************************");
	  sf_warning("************************************************");
	}
      }
      if(verbose>1){
	fprintf(stderr,"indx_of_keys[%d]=%d\n",iaxis,indx_of_keys[iaxis]);
      }
    }
  }
  /* put the history from the input file to the output */
  if(verbose>1)fprintf(stderr,"fileflush out\n");
  sf_fileflush(out,infile);

  if(verbose>0)fprintf(stderr,"start trace loop n_traces=%d\n",n_traces);
  for (i_trace=0; i_trace<n_traces; i_trace++){
    if(verbose>2 ||(verbose>0 && i_trace<5)){
      fprintf(stderr,"i_trace=%d\n",i_trace);
    }
    /**************************/
    /* read trace and headers */
    /**************************/
    sf_floatread(header,n1_headers,inheaders);
    sf_floatread(intrace,n1_traces,infile);

    if(makeheader){
      indxleft=i_trace;
      for (iaxis=2; iaxis<SF_MAX_DIM+1; iaxis++){
	traceindx[iaxis]=indxleft%n_in[iaxis];
	indxleft/=n_in[iaxis];
	tracecoord[iaxis]=o_in[iaxis]+traceindx[iaxis]*d_in[iaxis];
	if(verbose>1){
	  fprintf(stderr,"i_trace=%d,iaxis=%d,tracecoord[iaxis]=%f\n",
		  i_trace,   iaxis,   tracecoord[iaxis]);
	}
	if(indx_of_keys[iaxis]>=0){
	  /* kls what's needed to add to make this work with integer headers?*/
	  header[indx_of_keys[iaxis]]=tracecoord[iaxis];
	}
      }
    }
    /***************************/
    /* write trace and headers */
    /***************************/
    /* trace and header will be preceeded with words:
       1- type record: 4 charactors 'tah '.  This will support other
          type records like 'htah', hidden trace and header.
       2- the length of the length of the trace and header. */
    put_tah(intrace, header, n1_traces, n1_headers, out);
  }

  exit(0);
}
Пример #10
0
int main(int argc, char* argv[])
{
    int verbose;
    sf_file in=NULL, out=NULL;
    int n1_traces;
    int n1_headers;

    char* header_format=NULL;
    sf_datatype typehead;
    /* kls do I need to add this?  sf_datatype typein; */
    float* fheader=NULL;
    float* intrace=NULL;
    float* outtrace=NULL;
    float* bandlimittrace=NULL;
    float* rmsall=NULL;
    float* rmsband=NULL;
    float* scalars=NULL;
    float* sum_of_scalars=NULL;
    int indx_time;
    int itrace=0;
    int ntaper;
    int numxstart;
    int numtstart;
    float* taper;
    char **list_of_floats;
    float* xstart;
    float* tstart;
    int indx_of_offset;
    float offset;
    float d1;
    float o1;
    float time_start;
    int itime_start;
    int itime_stop;
    int indx_taper;
    float wagc;
    int lenagc;

    float fmin=5;
    float fmax=95;
    float finc=5;
    int num_centerfreq;
    int firstlive;
    int lastlive;
    float pnoise;

    kiss_fftr_cfg cfg,icfg;
    sf_complex *fft1;
    sf_complex *fft2;
    int nfft;
    int nf;
    float df;
    float scale;
    int ifreq;
    float cntrfreq;
    
    /*****************************/
    /* initialize verbose switch */
    /*****************************/
    sf_init (argc,argv);

    if(!sf_getint("verbose",&verbose))verbose=1;
    /* \n
       flag to control amount of print
       0 terse, 1 informative, 2 chatty, 3 debug
    */
    sf_warning("verbose=%d",verbose);
 
    /******************************************/
    /* input and output data are stdin/stdout */
    /******************************************/

    if(verbose>0)fprintf(stderr,"read in file name\n");  
    in = sf_input ("in");

    if(verbose>0)fprintf(stderr,"read out file name\n");
    out = sf_output ("out");

    if (!sf_histint(in,"n1_traces",&n1_traces))
	sf_error("input data not define n1_traces");
    if (!sf_histint(in,"n1_headers",&n1_headers)) 
	sf_error("input data does not define n1_headers");

    header_format=sf_histstring(in,"header_format");
    if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
    else                                       typehead=SF_FLOAT;

    if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
    fheader = sf_floatalloc(n1_headers);
 
    /*  allocate intrace and outtrace after initialize fft and 
	make them padded */
  
    /* maybe I should add some validation that n1== n1_traces+n1_headers+2
       and the record length read in the second word is consistent with 
       n1.  */

    /**********************************************************/
    /* end code block for standard tah Trace And Header setup */
    /* continue with any sf_puthist this tah program calls to */
    /* add to the history file                                */
    /**********************************************************/

    /* put the history from the input file to the output */
    sf_fileflush(out,in);

    /********************************************************/
    /* continue initialization specific to this tah program */
    /********************************************************/





    /* segy_init gets the list header keys required by segykey function  */
    segy_init(n1_headers,in);

    if(0==1) {  
      /* infuture may want to have offset dependent agc design start time */
      /* get the mute parameters */
      if(NULL==(list_of_floats=sf_getnstring("xstart",&numxstart))){
	xstart=NULL;
	sf_error("xstart is a required parameter in sftahagc");
      } else {
	xstart=sf_floatalloc(numxstart);
	if(!sf_getfloats("xstart",xstart,numxstart))sf_error("unable to read xstart");
      }
      if(NULL==(list_of_floats=sf_getnstring("tstart",&numtstart))){
	tstart=NULL;
	sf_error("xstart is a required parameter in sftahagc");
      } else {
	tstart=sf_floatalloc(numtstart);
	if(!sf_getfloats("tstart",tstart,numtstart))sf_error("unable to read tstart");
      }
      if(numxstart!=numtstart)sf_error("bad mute parameters: numxstart!=numtstart");
      if(!sf_getint("ntaper",&ntaper))ntaper=12;
    }
    /* \n
       length of the taper on the stack mute
    */
    /* is this of use for agc?
    taper=sf_floatalloc(ntaper);
    for(indx_time=0; indx_time<ntaper; indx_time++){
	float val_sin=sin((indx_time+1)*SF_PI/(2*ntaper));
	taper[indx_time]=val_sin*val_sin;
    }
    */
    indx_of_offset=segykey("offset");
    
    if (!sf_histfloat(in,"d1",&d1))
      sf_error("input data does not define d1");
    if (!sf_histfloat(in,"o1",&o1))
      sf_error("input data does not define o1");

    /* check wagc for reasonableness.  I input 250 (not .25 and ,250) */  
    if(!sf_getfloat("wagc",&wagc))wagc=-1;
    /* \n
       length of the agc window in seconds
    */
    if(wagc<0)sf_error("wagc is a required parameter in sftahagc");

    lenagc=wagc/d1+1.5; /* length of the agc window in samples */
 
    if (!sf_getfloat("pnoise",  &pnoise)) pnoise = 0.01;
    /* relative additive noise level */
    
    if (!sf_getfloat("fmin",&fmin))fmin=5;
    /* minimum frequency band */
    if (!sf_getfloat("fmax",&fmax))fmax=95;
    /* maximum frequency band */
    if (!sf_getfloat("finc",&finc))finc=5;
    /* frequency band increment */

    /* initialize kiss_fft kls */

    nfft  = kiss_fft_next_fast_size(n1_traces+200);
    nf    = nfft/2+1;
    df    = 1./(nfft*d1);
    scale = 1./nfft;
    cfg   = kiss_fftr_alloc(nfft,0,NULL,NULL);
    icfg  = kiss_fftr_alloc(nfft,1,NULL,NULL);
    fft1  = sf_complexalloc(nf);
    fft2  = sf_complexalloc(nf);
    if(verbose>0)fprintf(stderr,"fmax=%f\n",(nf-1)*df);

    /* allocate input and output traces with enough padding for
       ffts */
    if(verbose>0)
      fprintf(stderr,"allocate padded intrace.  n1_traces=%d nfft=%d\n",
	                                        n1_traces,   nfft);
    intrace       =sf_floatalloc(nfft); /* must be padded for input to fft */
    bandlimittrace=sf_floatalloc(nfft); /* must be padded for input to fft */
    outtrace= sf_floatalloc(n1_traces);
    rmsall        =sf_floatalloc(n1_traces);
    rmsband        =sf_floatalloc(n1_traces);
    scalars       =sf_floatalloc(n1_traces);
    sum_of_scalars=sf_floatalloc(n1_traces);
    num_centerfreq=(fmax-fmin)/finc+1.95;
    if(fabs(fmax-(fmin+(num_centerfreq-1)*finc))>.01){
      fprintf(stderr,"*************************************\n");
      fprintf(stderr,"*************************************\n");
      fprintf(stderr,"*************************************\n");
      fprintf(stderr,"fmin-fmax is not a multiple of finc\n");
      fprintf(stderr,"fmin=%f, fmax=%f, finc=%f\n",fmin,fmax,finc);
      fprintf(stderr,"*************************************\n");
      fprintf(stderr,"*************************************\n");
      sf_error("illegal combination of fmin,fmax,fminc");
    }

    /***************************/
    /* start trace loop        */
    /***************************/
    if(verbose>0)fprintf(stderr,"start trace loop\n");
 
    itrace=0;
    while (!(get_tah(intrace, fheader, n1_traces, n1_headers, in))){
	if(verbose>1 || (verbose==1 && itrace<5)){
	    fprintf(stderr,"process tah %d in sftahagc\n",itrace);
	}
	/********************/
	/* process the tah. */
	/********************/

	if(typehead == SF_INT)offset=((int  *)fheader)[indx_of_offset];
	else                  offset=((float*)fheader)[indx_of_offset];
	/* maybe latter add agc design start time
	intlin(numxstart,xstart,tstart,
	       tstart[0],tstart[numxstart-1],1,
	       &offset,&time_start);
	if(time_start<o1)time_start=o1;

	itime_start=(int)(((time_start-o1)/d1)+.5);
	*/
	/* find firstlive and lastlive */
        if(verbose>2)fprintf(stderr,"find firstlive and lastlive\n");
	for (firstlive=0; firstlive<n1_traces; firstlive++){
	  if(intrace[firstlive] != 0) break;
	}
	for (lastlive=n1_traces-1;lastlive>=0; lastlive--){
	  if(intrace[lastlive] != 0) break;
	}
	/* kls need to catch a zero trace */
        if(verbose>2)
	  fprintf(stderr,"firstlive=%d, lastlive=%d\n",firstlive,lastlive);
	/* zero the padded area on the input trace */
	for (indx_time=n1_traces; indx_time<nfft; indx_time++){
	  intrace[indx_time]=0;
	}
	
	/********************************************************/
	/* apply the SPECtral BALancing:                        */
	/* 1 compute the input trace rms in agclen gates        */
	/* 2 fft to frequency domain                            */
	/* 3 for each frequency band                            */
	/*   3.1 extract (ramp) the frequency band              */
	/*   3.2 convert freqency band to time,                 */
	/*   3.3 compute the rms of the frequency band          */
	/*   3.4 agc scalars 1/(freq_band_rms + whitenoise*rms) */ 
	/*   3.3 agc the frequency band                         */
	/*   3.4 sum to output                                  */
        /*   3.5 sum scalars to sum of scalars                  */
	/* 4 divide by the sum of scalars                       */
	/********************************************************/
	compute_rms(intrace, rmsall,
		    lenagc, n1_traces,
                    firstlive, lastlive);
	/* scale rms by whitenoise factor */
	if(verbose>2)fprintf(stderr,"put_tah rmsall\n");
	if(0) put_tah(intrace, fheader, n1_traces, n1_headers, out);
        if(0) put_tah(rmsall, fheader, n1_traces, n1_headers, out);
	for (indx_time=0; indx_time<n1_traces; indx_time++){
	  rmsall[indx_time]*=(1.0/num_centerfreq);
	}

	if(verbose>2)fprintf(stderr,"fftr\n");
	kiss_fftr(cfg, intrace, (kiss_fft_cpx*) fft1);

	/* zero the output trace and the sum_of_scalars */
	for (indx_time=0; indx_time<n1_traces; indx_time++){
	  outtrace[indx_time]=0.0;
	  sum_of_scalars[indx_time]=0.0;
	}
	for (cntrfreq=fmin; cntrfreq<=fmax; cntrfreq+=finc){
	  if(verbose>2)fprintf(stderr,"cntrfreq=%f\n",cntrfreq);
          /* zero frequencies before the band */
	  for (ifreq=0; ifreq<(int)((cntrfreq-finc)/df); ifreq++){
	    fft2[ifreq]=0.0;
	  }
	  /* triangular weight the selected  frequency band */
	  for (ifreq=(int)((cntrfreq-finc)/df); 
	       ifreq<(int)((cntrfreq+finc)/df) && ifreq<nf;
	       ifreq++){
            float weight;
	    if(ifreq>0){
	      weight=(1.0-fabs(ifreq*df-cntrfreq)/finc)/nfft;
	      fft2[ifreq]=weight*fft1[ifreq];
	    }
	  }
          /* zero frequencies larger than the band */
	  for (ifreq=(int)((cntrfreq+finc)/df); ifreq<nf; ifreq++){
	    fft2[ifreq]=0.0;
	  } 
	  /* inverse fft back to time domain */
          if(verbose>2)fprintf(stderr,"fftri\n");
	  kiss_fftri(icfg,(kiss_fft_cpx*) fft2, bandlimittrace);
          /* apply agc to the bandlimittrace and sum scalars to 
             sum_of_scalars */
	  if(verbose>2)fprintf(stderr,"agc_apply\n");
	  compute_rms(bandlimittrace, rmsband,
		    lenagc, n1_traces,
                    firstlive, lastlive);
	  if(verbose>2)fprintf(stderr,"sum to ouput\n");
	  for (indx_time=0; indx_time<n1_traces; indx_time++){
	    /* old poor 
	       scalars[indx_time]=1.0/
                   (rmsband[indx_time]+pnoise*rmsall[indx_time]);
	    */
	    if(1)scalars[indx_time]=rmsband[indx_time]/
                     (rmsband[indx_time]*rmsband[indx_time]+
		      pnoise*rmsall[indx_time]*rmsall[indx_time]);
	    else scalars[indx_time]=1.0;
	  }
	  for (indx_time=0; indx_time<n1_traces; indx_time++){
	    bandlimittrace[indx_time]*=scalars[indx_time];
	    outtrace[indx_time]+=bandlimittrace[indx_time];
	  }
	  for (indx_time=0; indx_time<n1_traces; indx_time++){
	    sum_of_scalars[indx_time]+=scalars[indx_time];
	  }
          if(0)
	    put_tah(bandlimittrace, fheader, n1_traces, n1_headers, out);
	  if(0)
	    put_tah(rmsband, fheader, n1_traces, n1_headers, out);
	  if(0)
	    put_tah(scalars, fheader, n1_traces, n1_headers, out);
	}
        if(0)put_tah(sum_of_scalars, fheader, n1_traces, n1_headers, out);
        if(1){	
	   /* divide output by sum of scalars */
	  for (indx_time=0; indx_time<n1_traces; indx_time++){
	    outtrace[indx_time]/=sum_of_scalars[indx_time];
	  }
	} 
	if (1)
	  put_tah(outtrace, fheader, n1_traces, n1_headers, out);
	itrace++;
    }

    exit(0);
}
Пример #11
0
int main(int argc, char* argv[])
{
    int verbose;
    float o1,o2,o3;
    int n1,n2,n3;
    float d1,d2,d3;
    char* label1;
    char* label2;
    char* label3;

    int n1_input;
    int n_input;
    float* hdrin;
    int idx_offset,idx_xline,idx_iline;
    int i_input;
    float*** foldplot;

    sf_file in=NULL, out=NULL;

    sf_init (argc,argv);
    in = sf_input ("in");
    out = sf_output ("out");

    sf_putint(out,"input",2);

    /* verbose flag controls ammount of print */
    /*( verbose=1 0 terse, 1 informative, 2 chatty, 3 debug ) */
    if(!sf_getint("verbose",&verbose))verbose=1;

    /* Axis1, 2 and 3 define the bins for the output fold map.  These
       are usually (offset,xline,offset), but you might want to compute some
       other histogram.  This can be done by selecting other segy headers
       using label1, 2 and 3.
    */

    /* get o1, n1, d1, label1 for axis1.  Do the same for axis2 and 3. */

    /*( o1=-32.5 required parameter.  Minimum label1 - usually min offset ) */
    if(!sf_getfloat("o1",&o1))sf_error("o1 is a required parameter");
    /*( o2=32 required parameter.  Minimum label2 - usually min xline ) */
    if(!sf_getfloat("o2",&o2))sf_error("o2 is a required parameter");
    /*( o3=32 required parameter.  Minimum label3 - usually min iline ) */
    if(!sf_getfloat("o3",&o3))sf_error("o3 is a required parameter");

    /*( n1=96 required parameter.  Number label1 - usually number offset ) */
    if(!sf_getint("n1",&n1))sf_error("n1 is a required parameter");
    /*( n1=623 required parameter.  Number label2 - usually number xline ) */
    if(!sf_getint("n2",&n2))sf_error("n2 is a required parameter");
    /*( n1=623 required parameter.  Number label3 - usually number iline ) */
    if(!sf_getint("n3",&n3))sf_error("n3 is a required parameter");

    /*( d1=110 required parameter.  delta label1 - usually delta offset ) */
    if(!sf_getfloat("d1",&d1))sf_error("d1 is a required parameter");
    /*( d2=1 required parameter.  delta label2 - usually delta xline ) */
    if(!sf_getfloat("d2",&d2))sf_error("d2 is a required parameter");
    /*( d3=1 required parameter.  delta label3 - usually delta iline ) */
    if(!sf_getfloat("d3",&d3))sf_error("d3 is a required parameter");

    /*( label1=offset  header for axis1 - usually offset ) */
    if(NULL == (label1 = sf_getstring("label1")))label1="offset";
    /*( label2=xline  header for axis2 - usually xline or cdp ) */
    if(NULL == (label2 = sf_getstring("label2")))label2="cdp";
    /*( label3=iline  header for axis3 - usually iline ) */
    if(NULL == (label3 = sf_getstring("label3")))label3="iline";

    if (SF_FLOAT != sf_gettype (in)) sf_error("Need float input");

    /* Find out the length of headers (vectors) in the input file and the
       number of locations to loop over
    */

    if (!sf_histint(in,"n1",&n1_input))
        sf_error("input file does not define n1");

    n_input = sf_leftsize(in,1); /* left dimensions after the first two */

    /* allocate space for the one location from the input file */
    hdrin = sf_floatalloc(n1_input);

    /* The output file will have new shape.  The axis have new lengths and
       names.  Write this information to the output history file.
    */
    if(verbose >=1)fprintf(stderr,"write axis information to output history\n");
    sf_putfloat(out,"o1",o1);
    sf_putfloat(out,"o2",o2);
    sf_putfloat(out,"o3",o3);

    sf_putint(out,"n1",n1);
    sf_putint(out,"n2",n2);
    sf_putint(out,"n3",n3);

    sf_putfloat(out,"d1",d1);
    sf_putfloat(out,"d2",d2);
    sf_putfloat(out,"d3",d3);

    sf_putstring(out,"label1",label1);
    sf_putstring(out,"label2",label2);
    sf_putstring(out,"label3",label3);

    /* get the location of the three labels in the header */
    idx_offset=segykey (label1);
    idx_xline =segykey (label2);
    idx_iline =segykey (label3);

    /* allocate the output data.  This is one big array in memory */
    foldplot=sf_floatalloc3(n1,n2,n3);

    /* loop over the input locations.  Read a trace header and increment
       the fold in each bin.  This is a 3d histogram.
    */

    if(verbose >=1)fprintf(stderr,"loop processing input trace headers\n");
    for (i_input=0 ; i_input<n_input; i_input++) {
        int iiline,ixline,ioffset;
        sf_floatread(hdrin,n1_input,in);
        ioffset=roundf((hdrin[idx_offset]-o1)/d1);
        ixline =roundf((hdrin[idx_xline ]-o2)/d2);
        iiline =roundf((hdrin[idx_iline ]-o3)/d3);
        if(verbose>2) {
            fprintf(stderr,"offset=%f,xline=%f,iline=%f\n",
                    hdrin[idx_offset],
                    hdrin[idx_xline ],
                    hdrin[idx_iline ]);
            fprintf(stderr,"ioffset=%d,ixline=%d,iiline=%d\n",
                    ioffset   ,ixline   ,iiline);
        }
        if(ioffset>=0 && ioffset< n1 &&
                ixline >=0 && ixline < n2 &&
                iiline >=0 && iiline < n3 ) {
            if(verbose>2)
                fprintf(stderr,"increment fold at %d,%d,%d\n",
                        iiline,ixline,ioffset);
            foldplot[iiline][ixline][ioffset]++;
        }
    }
    if(verbose >=1)fprintf(stderr,"write foldplot to output\n");
    /* write the output in one big write */
    sf_floatwrite(&(foldplot[0][0][0]),n1*n2*n3,out);

    exit(0);
}
Пример #12
0
int main(int argc, char* argv[])
{
  int verbose;
  sf_file in=NULL, out=NULL;
  int n1_traces;
  int n1_headers;

  char* header_format=NULL;
  sf_datatype typehead;
  /* kls do I need to add this?  sf_datatype typein; */
  float* fheader=NULL;
  float* intrace=NULL;
  int numkeys;
  int ikey;
  char** list_of_keys;
  int *indx_of_keys;
  float** superbin_traces;
  float** superbin_headers;
  int itrace;
  int iitrace;
  int indx_time;
  bool eof_get_tah;
  bool end_of_gather;
  int fold;
  int superbin_maxfold;
/*  float* outtrace=NULL; 
    float* outheader=NULL;  
    int* ioutheader=NULL;  */
  float* stktrace=NULL;
  float* stkheader=NULL;
  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  sf_init (argc,argv);

  if(!sf_getint("verbose",&verbose))verbose=1;
      /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  sf_warning("verbose=%d",verbose);
 
  /******************************************/
  /* input and output data are stdin/stdout */
  /******************************************/

  if(verbose>0)fprintf(stderr,"read in file name\n");  
  in = sf_input ("in");

  if(verbose>0)fprintf(stderr,"read out file name\n");
  out = sf_output ("out");

  if (!sf_histint(in,"n1_traces",&n1_traces))
    sf_error("input data not define n1_traces");
  if (!sf_histint(in,"n1_headers",&n1_headers)) 
    sf_error("input data does not define n1_headers");

  header_format=sf_histstring(in,"header_format");
  if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
  else                                       typehead=SF_FLOAT;

  if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  fheader = sf_floatalloc(n1_headers);
 
  if(verbose>0)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);
  intrace= sf_floatalloc(n1_traces);

  if(verbose>0)fprintf(stderr,"call list of keys\n");
 
  list_of_keys=sf_getnstring("key",&numkeys);
  if(list_of_keys==NULL)
    sf_error("The required parameter \"key\" was not found.");
  /* I wanted to use sf_getstrings, but it seems to want a colon seperated
     list of keys (eg key=offset:ep:fldr:cdp) and I wanted a comma seperated
     list of keys (eg key=offset:ep:fldr:cdp).
  numkeys=sf_getnumpars("key");
  if(numkeys==0)
    sf_error("The required parameter \"key\" was not found.");
  fprintf(stderr,"alloc list_of_keys numkeys=%d\n",numkeys);
  list_of_keys=(char**)sf_alloc(numkeys,sizeof(char*)); 
  sf_getstrings("key",list_of_keys,numkeys);
  */
  /* print the list of keys */
  if(verbose>1){
    fprintf(stderr,"numkeys=%d\n",numkeys);
    for(ikey=0; ikey<numkeys; ikey++){
      fprintf(stderr,"list_of_keys[%d]=%s\n",ikey,list_of_keys[ikey]);
    }
  }
  
  /* maybe I should add some validation that n1== n1_traces+n1_headers+2
     and the record length read in the second word is consistent with 
     n1.  */

  /**********************************************************/
  /* end code block for standard tah Trace And Header setup */
  /* continue with any sf_puthist this tah program calls to */
  /* add to the history file                                */
  /**********************************************************/

  /* put the history from the input file to the output */
  sf_fileflush(out,in);

  /********************************************************/
  /* continue initialization specific to this tah program */
  /********************************************************/

  /* segy_init gets the list header keys required by segykey function  */
  segy_init(n1_headers,in);
  indx_of_keys=sf_intalloc(numkeys);
  for (ikey=0; ikey<numkeys; ikey++){
    /* kls need to check each of these key names are in the segy header and
       make error message for invalid keys.  Of does segykey do this? NO, just
       segmentation fault. */
    indx_of_keys[ikey]=segykey(list_of_keys[ikey]);
  }
  stktrace          = sf_floatalloc(n1_traces);
  stkheader         = sf_floatalloc(n1_headers);

  /* I used Mtahstack.c and Mtahgethw to for inspiration for data flow */

  /* allocate processing arrays */
  superbin_maxfold=1000;
  superbin_traces =sf_floatalloc2(n1_traces , superbin_maxfold);
  superbin_headers=sf_floatalloc2(n1_headers, superbin_maxfold);
  
  /* allocate output trace arrays */
  /* outtrace  = sf_floatalloc(n1_traces); 
     outheader = sf_floatalloc(n1_headers);
     ioutheader=(int*)outheader; */

   /***************************/
  /* start trace loop        */
  /***************************/
  if(verbose>0)fprintf(stderr,"start trace loop\n");

  if(verbose>0)fprintf(stderr,"start the the trace read loop\n");
  itrace=0;
  eof_get_tah=get_tah(intrace, fheader, n1_traces, n1_headers, in);
  fold=0;
  while (!eof_get_tah){
    if(verbose>1 || (verbose==1 && itrace<5)){
      fprintf(stderr,"process the tah in sftahstack\n");
      for(ikey=0; ikey<numkeys; ikey++){
	fprintf(stderr," %s=",list_of_keys[ikey]);
	if(typehead == SF_INT){
	  /* just cast the header to int so the print works */
	  fprintf(stderr,"%d",((int*)fheader)[indx_of_keys[ikey]]);
	} else {
	  fprintf(stderr,"%f",fheader[indx_of_keys[ikey]]);
	}
      } /* end of the for(ikey..) loop */
      fprintf(stderr,"\n");
    }
    /********************/
    /* process the tah. */
    /********************/
    /* loop reading traces in supergather */
    
    if(fold==0){
      if(verbose>1)fprintf(stderr,"start a new gather\n"); 
      /* apply any initialization */
      memcpy(stkheader,fheader,n1_headers*sizeof(int));
    }
    
    if(fold>=superbin_maxfold){
      /* increase buffer sizes */
      superbin_maxfold*=1.2;
      superbin_traces =sf_floatrealloc2(superbin_traces, 
					n1_traces , superbin_maxfold);
      superbin_headers=sf_floatrealloc2(superbin_headers, 
					n1_headers, superbin_maxfold);
    }
    memcpy(superbin_headers[fold],fheader,n1_headers*sizeof(int));
    memcpy(superbin_traces [fold],intrace,n1_traces*sizeof(int));
    fold++;

    /* get the next trace so you can test the current and next headers
       to determine if this is the end of a superbin */
    eof_get_tah=get_tah(intrace, fheader, n1_traces, n1_headers, in);

    /* this is the end of gather if eof was encounterred or if at least one 
       of the header keys in indx_of_keys changed. */
    end_of_gather=eof_get_tah;

    /* if there is no next trace, do not try to test the header keys.  
       You already know it is the end of the superbin. */
    if(!eof_get_tah){ 
      if(fold>1){
	for(ikey=0; ikey<numkeys; ikey++){
	    if(typehead == SF_INT){
	      if(((int*)fheader  )[indx_of_keys[ikey]]!=
		 ((int*)stkheader)[indx_of_keys[ikey]]){
		end_of_gather=true;
		break;
	      }
	    } else {
	      if(((float*)fheader  )[indx_of_keys[ikey]]!=
		 ((float*)stkheader)[indx_of_keys[ikey]]){
		end_of_gather=true;
		break;	      
	      }
	    } /* end if(typehead == SF_INT)...else */
	  } /* end for(ikey=0; ikey<0*numkeys; ikey++) */
      }
    } /* end if(!eof_get_tah) the code segment to test hdrs for key change */


    /* if this is the last trace in the gather, process the gather and
       set fold=0.  Fold=0 will cause gather initialization on the next
       iteration through the loop */
    if(end_of_gather){
      /***********************************/
      /* process the supergather         */
      /***********************************/
      /* OK.  You have a superbin in memory.  Do your 5D interpolation. */
      if(verbose>1)fprintf(stderr,"end_of_gather.  process it.\n");

      /* this loop just prints some headers. */
      if(verbose>1){
	fprintf(stderr,"process gather\n");
      }
      /* remove the click for the gather. */
      
      for(indx_time=0; indx_time<n1_traces; indx_time++){
	stktrace[indx_time]=0.0;
      }
      for (iitrace=0; iitrace<fold; iitrace++){
	for(indx_time=0; indx_time<n1_traces; indx_time++){
	  stktrace[indx_time]+=superbin_traces[iitrace][indx_time];
	}
      }
      for (iitrace=0; iitrace<fold; iitrace++){
	float scalar;
	scalar=dot(superbin_traces[iitrace],stktrace,n1_traces)/
	       dot(stktrace,stktrace,n1_traces);
	for(indx_time=0; indx_time<n1_traces; indx_time++){
	  superbin_traces[iitrace][indx_time]-=scalar*stktrace[indx_time];
	}
      }


      /* now you need loop to create the  output traces and hdrs and write 
	 them to the output pipe with put_tah */
      /***************************/
      /* write trace and headers */
      /***************************/
      for (iitrace=0; iitrace<fold; iitrace++){
	put_tah(superbin_traces[iitrace],superbin_headers[iitrace], n1_traces, 
		n1_headers, out);
      }
      fold=0;
    }
    itrace++;
  }

  exit(0);
}
Пример #13
0
int main(int argc, char* argv[])
{
  int verbose;
  sf_file in=NULL, out=NULL;
  int n1_traces;
  int n1_headers;

  char* header_format=NULL;
  sf_datatype typehead;
  /* kls do I need to add this?  sf_datatype typein; */
  float* fheader=NULL;
  float* intrace=NULL;
  float o1;
  float d1;
  int indx_offset;
  float* local_sloth=NULL;
  float v0;
  int indx_time;
  float offset;
  float offset2;
  float* r_index_tx_of_it0=NULL;
  float* r_index_t0_of_itx=NULL;
  int start_indx_nmo;
  int start_indx_nmo_tx;
  float* outtrace=NULL;
  float itrace=0;
  float nmostretch;
  char* offsetname;
  float lmute;
  bool inv;
  /******************************************************/
  /* code block for standard tah Trace And Header setup */
  /******************************************************/

  sf_init (argc,argv);

  /*****************************/
  /* initialize verbose switch */
  /*****************************/
  if(!sf_getint("verbose",&verbose))verbose=1;
  /* \n
     flag to control amount of print
     0 terse, 1 informative, 2 chatty, 3 debug
  */
  sf_warning("verbose=%d",verbose);
 
  /******************************************/
  /* input and output data are stdin/stdout */
  /******************************************/

  if(verbose>0)fprintf(stderr,"read in file name\n");  
  in = sf_input ("in");

  if(verbose>0)fprintf(stderr,"read out file name\n");
  out = sf_output ("out");

  if (!sf_histint(in,"n1_traces",&n1_traces))
    sf_error("input data not define n1_traces");
  if (!sf_histint(in,"n1_headers",&n1_headers)) 
    sf_error("input data does not define n1_headers");

  header_format=sf_histstring(in,"header_format");
  if(strcmp (header_format,"native_int")==0) typehead=SF_INT;
  else                                       typehead=SF_FLOAT;

  if(verbose>0)fprintf(stderr,"allocate headers.  n1_headers=%d\n",n1_headers);
  fheader = sf_floatalloc(n1_headers);
 
  if(verbose>0)fprintf(stderr,"allocate intrace.  n1_traces=%d\n",n1_traces);
  intrace= sf_floatalloc(n1_traces);

  /* maybe I should add some validation that n1== n1_traces+n1_headers+2
     and the record length read in the second word is consistent with 
     n1.  */
  /**********************************************************/
  /* end code block for standard tah Trace And Header setup */
  /* continue with any sf_puthist this tah program calls to */
  /* add to the history file                                */
  /**********************************************************/

  /* put the history from the input file to the output */
  sf_fileflush(out,in);


  /********************************************************/
  /* continue initialization specific to this tah program */
  /********************************************************/
  if (!sf_histfloat(in,"d1",&d1))
    sf_error("input data does not define d1");
  if (!sf_histfloat(in,"o1",&o1))
    sf_error("input data does not define o1");
  /* Kls should read label1 and verify it is time  
  if (!sf_histstring(in,"label1",&label1))
    sf_error("input data not define label1");
  */

  /* segy_init gets the list header keys required by segykey function  */
  segy_init(n1_headers,in);
  /* get index to keys I will be using */
  if(NULL==(offsetname=sf_getstring("offset")))offsetname="offset";
  /* name of the header key to use for offset (usually just offset) */
  indx_offset=segykey(offsetname);
  /* kls what other header keys do I use?  inline? xline? cdp? */

  /* get the parameter for the maximum nmo stretch. */
  if (!sf_getfloat("str",&nmostretch)) nmostretch=0.5;
  /* maximum stretch allowed */

  if (!sf_getfloat("lmute",&lmute)) lmute=12.*d1; 
  /* length of the mute zone in seconds */
  if(verbose>0)  fprintf(stderr,"lmute=%f seconds.\n",lmute);
  lmute/=d1;
  if(!sf_getbool("inv",&inv)) inv=false;
  /* if y, do inverse nmo.  Otherwise forward nmo */ 
   
  if(verbose>0){
    if(inv)fprintf(stderr,"inv=true\n");
    else fprintf(stderr,"inv=false\n");
  }
   
  /* set up velocity function ( really (1/v)**2, sloth */
  local_sloth=sf_floatalloc(n1_traces);
  /* just constant velocity today */
  if(1==1){
    char** list_of_floats;
    float* vnmo;
    float* tnmo;
    int numvnmo;
    int numtnmo;
    float t0;

    if(verbose>1)fprintf(stderr,"read vnmo/tnmo\n");
    /* use this fundtion to find out number of velocities and time 
       input in vnmo and tnmo */
    list_of_floats=sf_getnstring("vnmo",&numvnmo);
    /* list of NMO velocities for the time in tnmo */
    if(verbose>1){
      int i;
      fprintf(stderr,"numvnmo=%d\n",numvnmo);
      for (i=0; i<numvnmo; i++){
	fprintf(stderr,"velocities=%s\n",list_of_floats[i]);
      }
    }
    /* should free this list of strings, but that is only a little memory */\
    list_of_floats=sf_getnstring("tnmo",&numtnmo);
    /* NMO times for the vnmo velocities. */
    if(verbose>1){
      int i;
      for (i=0; i<numtnmo; i++){
	fprintf(stderr,"times=%s\n",list_of_floats[i]);
      }
    }
    if(numvnmo!=numtnmo){
      sf_error("number vnmo floats=%d != number tnmo floats=%d",
	       numvnmo,numtnmo);
    }
    if(numvnmo==0)sf_error("vnmo parameter is required");
    vnmo=sf_floatalloc(numvnmo);
    tnmo=sf_floatalloc(numtnmo);
    if(verbose>1)fprintf(stderr,"sf_getfloats(vnmo)");
    if (!sf_getfloats("vnmo",vnmo,numvnmo))
      sf_error("unable to read vnmo");
    /* list of NMO velocities for the tnmo times. */
    if(verbose>1)fprintf(stderr,"sf_getfloats(tnmo)");
    if (!sf_getfloats("tnmo",tnmo,numtnmo))
      sf_error("unable to read tnmo");
    /* list of NMO times for the vnmo velocities. */
    if(verbose>1){
      for(indx_time=0; indx_time<numvnmo; indx_time++){
	fprintf(stderr,"indx=%d, vnmo=%f, tnmo=%f\n",
		indx_time,vnmo[indx_time],tnmo[indx_time]);
      }
    }
    if(verbose>1)fprintf(stderr,"interpolate the velocity\n");
    for(indx_time=0; indx_time<n1_traces; indx_time++){
      t0=indx_time*d1+o1;
      intlin(numtnmo,tnmo,vnmo,vnmo[0],vnmo[numvnmo-1],1,&t0,&v0);
      local_sloth[indx_time]=1.0/(v0*v0);
    }    
  } 
  /* kls
     previous if(1==1) clause if for it no input velocity file
     need to add else { set up the input velocity file (ie open the file(
     inside the trace loop need to get iline and xline form trace heder
     and use that to read the velocity at (iline,xline) into local_sloth
     then compute local_sloth as 1/(v*v)

     will need to get input velocity shape:
       if (!sf_histint(invelocity,"n1",&n1_velocity))
          sf_error("input velocity file does not define n1");
     test n1_velocity!=n1_trace error
     and do the same for d1, o2, d2, etc 
   */

  if(verbose>0)fprintf(stderr,"allocate arrays for the trace loop\n");
  r_index_tx_of_it0=sf_floatalloc(n1_traces);
  r_index_t0_of_itx=sf_floatalloc(n1_traces);
  outtrace         =sf_floatalloc(n1_traces);
  

  /***************************/
  /* start trace loop        */
  /***************************/
  if(verbose>0)fprintf(stderr,"start trace loop\n");
  while (0==get_tah(intrace, fheader, n1_traces, n1_headers, in)){
    if(verbose>1)fprintf(stderr,"process the tah in sftahnmo\n");
    /********************/
    /* process the tah. */
    /********************/
    /* this program applies moveout */
    /* kls this should be only be done when velocity (local_sloth) 
       or offset changes */
    if(typehead == SF_INT){   
      /* just cast the header to int so the print works */
      offset=((int*)fheader)[indx_offset];
    } else {
      offset=       fheader [indx_offset];
    }
    offset2=offset*offset;
    for(indx_time=0; indx_time<n1_traces; indx_time++){
      float tx, t0;
      t0=indx_time*d1+o1;
      tx=sqrt(t0*t0+offset2*local_sloth[indx_time]);
      r_index_tx_of_it0[indx_time]=(tx-o1)/d1;
      if(itrace==0 && verbose>4){
	fprintf(stderr,"indx_time=%d, tx=%f, sloth=%g, offset=%f, t0=%f\n",
		        indx_time   , tx   , local_sloth[indx_time]  ,
                                                       offset   , t0);
      }
    }
    /* kls nmo start time should depend on the nmo stretch limit.
       Find the excessive stretch closest to the bottom of the trace.  This 
       is the last time nmstrewtch is violated.  It is OK to apply nmo
       to the rest of the trace. */
    for (start_indx_nmo=n1_traces-1; start_indx_nmo>1; start_indx_nmo--){
      /* pfrintf(stderr,"r_indx[it]=%f, rindx */
      if((r_index_tx_of_it0[start_indx_nmo  ]-
	  r_index_tx_of_it0[start_indx_nmo-1])  <nmostretch) break;
    }
    if(inv){
      start_indx_nmo_tx = 1.0+r_index_tx_of_it0[start_indx_nmo];
      if(start_indx_nmo_tx>n1_traces-2)start_indx_nmo_tx = n1_traces-2;
      /* compute r_index_t0_of_itx from r_index_tx_of_it0 */
      yxtoxy(n1_traces-start_indx_nmo,1.0, start_indx_nmo,
	     &r_index_tx_of_it0[start_indx_nmo],
 
	     n1_traces-start_indx_nmo_tx,1.0, start_indx_nmo_tx,
	     -1,n1_traces,&r_index_t0_of_itx[start_indx_nmo_tx]);
    }
    /* kls inverse nmo? will need more code */
    /* do nmo via 8-point sinc interpolation */
    /* void ints8r (int nxin, float dxin, float fxin,      
                    float yin[], float yinl, float yinr, 
		    int nxout, float xout[], 
		    float yout[]) */
    if(!inv){
      ints8r(n1_traces,1.0,0,
	     intrace,0.0,0.0,
	     n1_traces-start_indx_nmo,&r_index_tx_of_it0[start_indx_nmo],
	     &outtrace[start_indx_nmo]);
      /* zero above the start time */
      for(indx_time=0; indx_time<start_indx_nmo; indx_time++){
	outtrace[indx_time]=0.0;
      }
      /* apply linear ramp kls */
      for (indx_time=start_indx_nmo; 
	   indx_time<start_indx_nmo+lmute && indx_time<n1_traces;
	   indx_time++){
	outtrace[indx_time] *= (float)(indx_time-start_indx_nmo+1)/(float)lmute;
      }
    }else{
      ints8r(n1_traces,1.0,0,
	     intrace,0.0,0.0,
	     n1_traces-start_indx_nmo_tx,&r_index_t0_of_itx[start_indx_nmo_tx],
	     &outtrace[start_indx_nmo_tx]);      
      /* zero above the start time */
      for(indx_time=0; indx_time<start_indx_nmo_tx; indx_time++){
	outtrace[indx_time]=0.0;
      }
    }
    /***************************/
    /* write trace and headers */
    /***************************/
    put_tah(outtrace, fheader, n1_traces, n1_headers, out);
    itrace++;
  }
  
  exit(0);
}