Exemple #1
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);
}
Exemple #2
0
int main (int argc, char *argv[])
{
    off_t *table, nleft, nbuf, n[SF_MAX_DIM], f[SF_MAX_DIM];
    int i, esize, dim, n1, n2, n3, m1, i2, i1, j1, jump;
    int m[SF_MAX_DIM], j[SF_MAX_DIM]; 
    float a, d[SF_MAX_DIM], o[SF_MAX_DIM];
    char key[7], *buf, buf0[BUFSIZ], *zero;
    bool verb;
    sf_file in=NULL;
    sf_file out=NULL;

    sf_init (argc,argv);
    in = sf_input ("in");
    out = sf_output ("out");
    
    esize = sf_esize(in);
    dim = sf_largefiledims(in,n);

    for (i=0; i < dim; i++) {
	/* get o's */
	snprintf(key,3,"o%d",i+1);
	if (!sf_histfloat(in,key,o+i)) o[i]=0.;
	
        /* get d's */
	snprintf(key,3,"d%d",i+1);
	if (!sf_histfloat(in,key,d+i)) d[i]=1.;

	/* get j's */
	snprintf(key,3,"j%d",i+1);
	if (!sf_getint(key,j+i)) {
	    /*( j#=(1,...) jump in #-th dimension )*/
	    snprintf(key,3,"d%d",i+1);
	    if (sf_getfloat(key,&a)) {
		/*( d#=(d1,d2,...) sampling in #-th dimension )*/
		j[i] = 0.5 + a/d[i];
	    } else {
		j[i] = 1;
	    }
	} 

	/* get f's */	
	snprintf(key,3,"f%d",i+1);
	if (!sf_getlargeint(key,f+i)) {
	    /*( f#=(0,...) window start in #-th dimension )*/
	    snprintf(key,5,"min%d",i+1);
	    if (sf_getfloat(key,&a)) {
		/*( min#=(o1,o2,,...) minimum in #-th dimension )*/
		f[i] = 0.5 + (a - o[i]) / d[i];
	    } else {
		f[i] = 0;
	    }
	}
	if (f[i] < 0) {
	    f[i] = n[i]+f[i];
#if defined(__cplusplus) || defined(c_plusplus)
	    if (f[i] < 0) sf_error("Negative f%d=%ld",
				   i+1,(long int) f[i]);
#else
	    if (f[i] < 0) sf_error("Negative f%d=%lld",
				   i+1,(long long int) f[i]);
#endif
	}

	/* new values for o and d */
	o[i] += f[i]*d[i]; 	
	d[i] *= j[i];

	/* get n's */
	snprintf(key,3,"n%d",i+1);
	if (!sf_getint(key,m+i)) { 
	    /*( n#=(0,...) window size in #-th dimension )*/
	    snprintf(key,5,"max%d",i+1);
	    if (sf_getfloat(key,&a)) {
		/*( max#=(o1+(n1-1)*d1,o2+(n1-1)*d2,,...) 
		  maximum in #-th dimension )*/
		m[i] = 1.5 + (a - o[i]) / d[i];
	    } else {
		m[i] = 1.5 + (n[i] - 1 - f[i]) / j[i];
	    }
	}
	while (1+(m[i]-1)*j[i] > n[i]) m[i]--;
    }

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

    if (verb) {
	for (i=0; i < dim; i++) {
	    if (m[i] != n[i]) 
		sf_warning("Cutting f%d=%d j%d=%d n%d=%d min%d=%g max%d=%g",
			   i+1,f[i],i+1,j[i],i+1,m[i],
			   i+1,o[i],i+1,o[i]+(m[i]-1)*d[i]);
	}
    }

    sf_fileflush(out,in);
    sf_setform(in,SF_NATIVE);
    sf_setform(out,SF_NATIVE);

    /* Now do the actual work */
    n2 = n3 = 1;
    for (i=1; i < dim; i++) {
	n2 *= m[i];
	n3 *= n[i];
    }

    m1 = m[0]*esize;
    n1 = (1+(m[0]-1)*j[0])*esize;
    jump = (j[0]-1) * esize;
    n[0] *= esize;
    f[0] *= esize;

    buf = sf_charalloc (n1);
    zero = sf_charalloc (n1);
    table = (off_t*) sf_alloc (n2+1,sizeof(off_t));
    memset(zero,0,n1);

    seektable(dim,n,m,f,j,n1,n2,n3,table);

    for (i2=0; i2 <= n2; i2++) {
	for (nleft=table[i2]; nleft > 0; nleft -= nbuf) {
	    nbuf = (BUFSIZ < nleft)? BUFSIZ: nleft;
	    sf_charread  (buf0,nbuf,in);
	    sf_charwrite (buf0,nbuf,out);
	}

	if (i2==n2) break;

	sf_charread(buf,n1,in);
	if (jump) {
	    for (i1=j1=0; i1 < m1; j1 += jump) {
		memset(buf+j1,0,esize);
		j1 += esize;
		i1 += esize;
	    }
	    sf_charwrite(buf,n1,out);
	} else {
	    sf_charwrite(zero,n1,out);
	}
    }


    exit (0);
}