Esempio n. 1
0
void sf_out(sf_file out        /* output file */, 
	    int axis           /* join axis */,
	    const char *iname  /* name of the input file */)
/*< prepare output >*/
{
    char *oname, cmdline[SF_CMDLEN];
    int ndim;
    off_t n[SF_MAX_DIM];
    sf_file inp;
    FILE *ofile=NULL;

    ofile = sf_tempfile(&oname,"w+b");
    fclose(ofile);

    snprintf(cmdline,SF_CMDLEN,"%s %s --dryrun=y < %s > %s",
	     command,splitcommand,iname,oname);
    sf_system(cmdline);
    
    inp = sf_input(oname);
    ndim = sf_largefiledims (inp,n);
    
    if (axis > ndim) axis=ndim;

    snprintf(nkey,5,"n%d",axis);
    axis--;
    sizes(inp,axis,ndim,n,&size1,&size2);
    
    sf_setformat(out,sf_histstring(inp,"data_format"));
    sf_fileflush(out,inp);
    
    sf_setform(out,SF_NATIVE);
    sf_rm(oname,true,false,false);
}
Esempio n. 2
0
int main(int argc, char* argv[])
{
    int n1, n2, i2, esize;
    off_t pos;
    struct skey *sorted;
    float *unsorted;
    char *trace, *header;
    sf_file in, head, out;

    sf_init (argc,argv);
    in = sf_input ("in");
    out = sf_output ("out");
 
    header = sf_getstring("head");
    /* header file */
    if (NULL == header) { 
	header = sf_histstring(in,"head");
	if (NULL == header) sf_error("Need head=");
    }

    head = sf_input(header);
    if (SF_FLOAT != sf_gettype(head))
	sf_error("Need float header");
    n2 = sf_filesize(head);
 
    unsorted = sf_floatalloc(n2);
    sorted = (struct skey*) sf_alloc(n2,sizeof(struct skey));
    
    sf_floatread(unsorted,n2,head);
    for (i2 = 0; i2 < n2; i2++) {
	sorted[i2].key = unsorted[i2];
	sorted[i2].pos = i2;
    }
    free (unsorted);
    sf_fileclose(head);

    qsort(sorted,n2,sizeof(struct skey),key_compare);
 
    if (!sf_histint(in,"n1",&n1)) n1=1;
    esize = sf_esize(in);
    n1 *= esize;

    trace = sf_charalloc(n1);

    sf_unpipe(in,((off_t) n1)*((off_t) n2));
    sf_fileflush(out,in);
    sf_setform(in,SF_NATIVE);
    sf_setform(out,SF_NATIVE);

    pos = sf_tell(in);
    for (i2=0; i2<n2; i2++) {
	sf_seek(in,pos+(sorted[i2].pos)*n1,SEEK_SET);
	sf_charread(trace,n1,in);
	sf_charwrite(trace,n1,out);
    }


    exit(0);
}
Esempio n. 3
0
int main(int argc, char* argv[])
{
    int n1, is, ns, nf, esize, pad;
    off_t i2, n2;
    char *trace, *zero;
    sf_file in, shift;

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

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    n2 = sf_leftsize(in,1);

    if (!sf_histint(in,"esize",&esize)) {
	esize=sf_esize(in);
    } else if (0>=esize) {
	sf_error("cannot handle esize=%d",esize);
    }

    if (!sf_getint("ns",&ns)) sf_error("Need ns=");
    /* number of shifts */

    if (!sf_getint("nf",&nf)) nf = 1;
    /* offset of first shift */

    if ((nf+ns-1) >= n1) sf_error("(nf+ns-1)=%d is too large",nf+ns-1);

    sf_putint(shift,"n2",ns);
    sf_shiftdim(in, shift, 2);

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

    n1 *= esize;

    trace = sf_charalloc(n1);
    zero = sf_charalloc(n1);
    memset(zero,0,n1);

    for (i2=0; i2 < n2; i2++) {
	sf_charread(trace,n1,in);
	for (is=nf; is <= (nf+ns-1); is++) {
	    pad = is*esize;
	    sf_charwrite(zero,pad,shift);
	    sf_charwrite(trace,n1-pad,shift);
	}
    }

    exit(0);
}
Esempio n. 4
0
int main(int argc, char* argv[])
{
    char        *mode=NULL;             /* Standard types           */
    int         i1,i2,n1,n2;
    float       *list=NULL;

    sf_file     in=NULL,out=NULL;       /* RSF types                */

    sf_init(argc,argv);                 /* Initialize RSF           */

    in  = sf_input("in");               /* Open files               */
    out = sf_output("out");

    sf_fileflush(out,in);               /* Copy header to output    */

                                        /* Check input data type    */
    if (sf_gettype(in) != SF_FLOAT) sf_error("Input must be float.");

                                        /* Get the file sizes       */
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input.");

    n2 = sf_leftsize(in,1);

                                        /* Check mode               */
    if ((mode = sf_getstring("mode")) == NULL) mode = "min";
    /* 'min' (default) or 'max' */

    if (strcmp(mode,"min")!=0 && strcmp(mode,"max")!=0)
        sf_error ("Unknown mode %s.",mode);

    list = sf_floatalloc(n1);           /* Allocate list            */

    for (i2=0; i2<n2; i2++)             /* Process each list        */
    {
        sf_floatread(list,n1,in);       /* Read a list              */

                                        /* Find the min or max      */
        if (strcmp(mode,"min")==0)
            for (i1=1; i1<n1; i1++) list[i1] = SF_MIN(list[i1],list[i1-1]);

        if (strcmp(mode,"max")==0)
            for (i1=1; i1<n1; i1++) list[i1] = SF_MAX(list[i1],list[i1-1]);

        sf_floatwrite(list,n1,out);     /* Write the output         */
    }



    exit (0);
}
Esempio n. 5
0
int main (int argc, char*argv[]) {

    sf_file in=NULL;
    sf_file out=NULL;

    sf_init(argc, argv);

    in  = sf_input("in");
    out = sf_output("out");

    sf_fileflush(out,in);


    exit(0);
}
Esempio n. 6
0
File: file.c Progetto: liumch/src
void sf_complexwrite (sf_complex* arr, size_t size, sf_file file)
/*< write a complex array arr[size] to file >*/
{
    char* buf;
    size_t i, left, nbuf, bufsiz;
    sf_complex c;
	
    if (NULL != file->dataname) sf_fileflush (file,infiles[0]);
    switch(file->form) {
	case SF_ASCII:
	    for (left = size; left > 0; left-=nbuf) {
		nbuf = (aline < left)? aline: left;
		for (i=size-left; i < size-left+nbuf; i++) {
		    c = arr[i];
		    if (EOF==fprintf(file->stream,
				     (NULL != aformat)? 
				     aformat:"%g %gi ",
				     crealf(c),cimagf(c)))
			sf_error ("%s: trouble writing ascii",__FILE__);
		}
		if (EOF==fprintf(file->stream,"\n"))
		    sf_error ("%s: trouble writing ascii",__FILE__);
	    }
	    break;
	case SF_XDR:
	    size *= sizeof(sf_complex);
	    buf = (char*)arr+size;
	    bufsiz = sf_bufsiz(file);
	    for (left = size; left > 0; left -= nbuf) {
		nbuf = (bufsiz < left)? bufsiz : left;
		(void) xdr_setpos(&(file->xdr),0);
		if (!xdr_vector(&(file->xdr),buf-left,
				nbuf/sizeof(float),sizeof(float),
				(xdrproc_t) xdr_float))
		    sf_error ("sf_file: trouble writing xdr");
		fwrite(file->buf,1,nbuf,file->stream);
	    }
	    break;
	default:
	    fwrite(arr,sizeof(sf_complex),size,file->stream);
	    break;
    }
}
Esempio n. 7
0
File: file.c Progetto: liumch/src
void sf_ucharwrite (unsigned char* arr, size_t size, sf_file file)
/*< write an unsigned char array arr[size] to file >*/
{
    char* buf;
    size_t i, left, nbuf, bufsiz;
	
    if (NULL != file->dataname) sf_fileflush (file,infiles[0]);
	
    switch(file->form) {
	case SF_ASCII:
	    for (left = size; left > 0; left-=nbuf) {
		nbuf = (aline < left)? aline: left;
		for (i=size-left; i < size-left+nbuf; i++) {
		    if (EOF==fputc(arr[i],file->stream))
			sf_error ("%s: trouble writing ascii",__FILE__);
		}
		if (EOF==fprintf(file->stream,"\n"))
		    sf_error ("%s: trouble writing ascii",__FILE__);
	    }
	    break;
	case SF_XDR:
	    buf = (char*)arr+size;
	    bufsiz = sf_bufsiz(file);
	    for (left = size; left > 0; left -= nbuf) {
		nbuf = (bufsiz < left)? bufsiz : left;
		(void) xdr_setpos(&(file->xdr),0);
		if (!xdr_opaque(&(file->xdr),buf-left,nbuf))
		    sf_error ("sf_file: trouble writing xdr");
		fwrite(file->buf,1,nbuf,file->stream);
	    }
	    break;
	default:
	    fwrite(arr,sizeof(unsigned char),size,file->stream);
	    break;
    }
}
Esempio n. 8
0
File: pad.c Progetto: 1014511134/src
int main (int argc, char* argv[])
{
    int i, j, nj, dim, ntr, itr, esize;
    int beg[SF_MAX_DIM], end[SF_MAX_DIM];
    off_t n[SF_MAX_DIM], n2[SF_MAX_DIM];
    size_t n0, n20, beg0, end0;
    sf_file in, out;
    float o, d;
    char key[10], key2[10], *tr, *zero;
    bool inside;

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

    dim = sf_largefiledims(in,n);

    sf_expandpars(out);
    
    ntr=1;
    for (j=0; j < SF_MAX_DIM; j++) {
	i=j+1;
	if (j>=dim) {
	    n[j]=1;
	}
	sprintf(key,"beg%d",i);
	if(!sf_getint(key,beg+j)) {
	    /*( beg#=0 the number of zeros to add before the beginning of #-th axis )*/
	    beg[j]=0;
	} else if (beg[j]<0) {
	    sf_error("negative beg=%d",beg[j]);
	}
	sprintf(key,"end%d",i);
	sprintf(key2,"n%d",i);
	if(!sf_getint(key,end+j)) {
	    /*( end#=0 the number of zeros to add after the end of #-th axis )*/
	    sprintf(key,"n%dout",i);
	    if (sf_getint(key,&nj) || sf_getint(key2,&nj)) {
		/*( n# the output length of #-th axis - padding at the end )*/
		if (0==nj) 
		    for (nj++; nj < n[j]; nj *= 2) ;
			
		end[j]=nj-n[j]-beg[j];
		if (end[j]<0)
		    sf_error("negative end=%d",end[j]);
	    } else {
		end[j]=0;
	    }
	}
	n2[j]=n[j]+beg[j]+end[j];
	if (j>0) ntr *= n2[j];

	if (n2[j] != n[j]) sf_putint(out,key2,n2[j]);

	if (beg[j] > 0) {
	    sprintf(key,"o%d",i);
	    if (sf_histfloat(in,key,&o)) {
		sprintf(key2,"d%d",i);
		if (sf_histfloat(in,key2,&d))
		    sf_putfloat(out,key,o-d*beg[j]);
	    }
	}
	if (n2[j] > 1 && j >= dim) dim=i;
    }

    if (!sf_histint(in,"esize",&esize)) {
	esize=sf_esize(in);
    } else if (0>=esize) {
	sf_error("cannot handle esize=%d",esize);
    }

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

    n[0]   *= esize; n0   = (size_t) n[0];
    n2[0]  *= esize; n20  = (size_t) n2[0];
    beg[0] *= esize; beg0 = (size_t) beg[0];
    end[0] *= esize; end0 = (size_t) end[0];

    tr   = sf_charalloc(n20);
    zero = sf_charalloc(n20);
    memset(zero,0,n20);

    if (beg0>0) memcpy(tr,zero,beg0);
    if (end0>0) memcpy(tr+beg0+n0,zero,end0);

    for (itr=0; itr < ntr; itr++) {
	inside = true;
	for (nj=j=1; j < dim; nj *= n2[j], j++) {
	    i = (itr/nj)%n2[j];
	    if (i < beg[j] || i >= beg[j]+n[j]) {
		inside = false;
		break;
	    }
	}
	if (inside) {
	    sf_charread (tr+beg0,n0,in);
	    sf_charwrite(tr,n20,out);
	} else {
	    sf_charwrite(zero,n20,out);
	}
    }


    exit (0);
}
Esempio n. 9
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);
}
Esempio n. 10
0
int main(int argc, char* argv[])
{

    off_t pos;
    bool sign, half;
    int   nsx, nsy,   nmx, nmy,   nhx, nhy,   nmhx, nmhy,   nt;
    int   isx, isy,   imx, imy,   ihx, ihy;
    float osx, osy, dsx, dsy, omx, omy, dmx, dmy, ohx, ohy, dhx, dhy, dmhx, dmhy, binx, biny;
    float s_xmin, s_ymin, r_xmin, r_ymin, s_xmax, s_ymax, r_xmax, r_ymax, survey_xmin, survey_ymin, survey_xmax, survey_ymax;
    char *trace, *zero;
    sf_file in, out;


    sf_init(argc,argv);
    in  = sf_input ( "in");
    out = sf_output("out");
    sf_warning("WARNING: This 3-D CMP sorting code is not yet ready for use--this message will be removed when it is. ");

    if (!sf_histint  (in,"n1",&nt)) sf_error("No n1= in input");
    /* Number of samples per trace */

    if (!sf_histint  (in,"n2",&nhx)) sf_error("No n2= in input");
    /* Number of offsets per shot gather in x-direction*/
    if (!sf_histfloat(in,"o2",&ohx)) sf_error("No o2= in input");
    /* First offset x-component */
    if (!sf_histfloat(in,"d2",&dhx)) sf_error("No d2= in input");
    /* Offset increment in x-direction */
    if (!sf_histint  (in,"n3",&nhy)) sf_error("No n3= in input");
    /* Number of offsets per shot gather in y-direction*/
    if (!sf_histfloat(in,"o3",&ohy)) sf_error("No o3= in input");
    /* First offset y-component */
    if (!sf_histfloat(in,"d3",&dhy)) sf_error("No d3= in input");
    /* Offset increment in y-direction */
    if (!sf_histint  (in,"n4",&nsx)) sf_error("No n4= in input");
    /* Number of sources along x-direction*/
    if (!sf_histfloat(in,"d4",&dsx)) sf_error("No d4= in input");
    /* Source spacing in x-direction*/
    if (!sf_histfloat(in,"o4",&osx)) sf_error("No o4= in input");
    /* First source x-coordinate*/
    if (!sf_histint  (in,"n5",&nsy)) sf_error("No n5= in input");
    /* Number of sources along y-direction*/
    if (!sf_histfloat(in,"d5",&dsy)) sf_error("No d5= in input");
    /* Source spacing in y-direction*/
    if (!sf_histfloat(in,"o5",&osy)) sf_error("No o5= in input");
    /* First source y-coordinate*/
    
    if (!sf_getfloat("binx",&binx)) sf_error("No x bin size specified. Please set binx=");
    /*Number of bins along x-direction*/
    if (!sf_getfloat("biny",&biny)) sf_error("No y bin size specified. Please set biny=");
    /*Number of bins along y-direction*/

    if (!sf_getbool("positive",&sign)) sign=true;
    /* initial offset orientation:
       yes is generally for off-end surveys, where the first offsets are positive.  
       no is generally for split-spread surveys with first negative then positive offsets. */

    if (!sf_getbool("half",&half)) half=true;
    /* if y, the second axis is half-offset instead of full offset*/

    if (!half) {
	dhx /= 2;
	ohx /= 2;
	dhy /= 2;
	ohy /= 2;
    }

    s_xmin = osx;
    s_ymin = osy;
    s_xmax = osx+(nsx*dsx);
    s_ymax = osy+(nsy*dsy);
    
    r_xmin = s_xmin+2*ohx;
    r_ymin = s_ymin+2*ohy;
    r_xmax = s_xmax+2*(ohx+dhx*nhx);
    r_ymax = s_ymax+2*(ohy+dhy*nhy);

    if (s_xmin <= r_xmin){
      survey_xmin = s_xmin;
    }else{
      survey_xmin = r_xmin;
    }

    if (s_ymin <= r_ymin){
      survey_ymin = s_ymin;
    }else{
      survey_ymin = r_ymin;
    }

    if (s_xmax <= r_xmax){
      survey_xmax = r_xmax;
    }else{
      survey_xmax = s_xmax;
    }

    if (s_ymax <= r_ymax){
      survey_ymax = r_ymax;
    }else{
      survey_ymax = s_ymax;
    }
    
    dmx = (survey_xmax - survey_xmin)/binx;
    dmy = (survey_ymax - survey_ymin)/biny;
    omx = survey_xmin + dmx/2.;
    omy = survey_ymin + dmy/2.;
    nmx = binx;
    nmy = biny;
    nmhx = (int)(nhx*nsx/nmx);
    nmhy = (int)(nhy*nsy/nmy);
    dmhx = dhx;
    dmhy = dhy;

    sf_putint  (out,"n2",nmhx);
    sf_putint  (out,"n3",nmhy);
    sf_putfloat(out,"d2",dmhx);
    sf_putfloat(out,"d3",dmhy);

    sf_putint  (out,"n4",nmx);
    sf_putint  (out,"n5",nmy);
    sf_putfloat(out,"d4",dmx);
    sf_putfloat(out,"d5",dmy);
    sf_putfloat(out,"o4",omx);
    sf_putfloat(out,"o5",omy);

    sf_putstring(out,"label3","hx");
    sf_putstring(out,"label4","hy");
    sf_putstring(out,"label5","mx");
    sf_putstring(out,"label6","my");
    
    nt *= sf_esize(in);

    trace = sf_charalloc(nt);
    zero  = sf_charalloc(nt);
    memset(zero,0,nt);

    sf_fileflush(out,in);
    sf_setform( in,SF_NATIVE);
    sf_setform(out,SF_NATIVE);
    
    sf_unpipe(in,(off_t) nsx*nsy*nhx*nhy*nt);
    pos = sf_tell(in);

    for (isx=0; isx < nsx; isx++) {
      for (isy=0; isy < nsy; isy++) {

	for (ihx=0; ihx < nhx; ihx++) {
	  for (ihy=0; ihy < nhy; ihy++) {

	    imx = (int)((isx + ihx)/2.0);
	    imy = (int)((isy + ihy)/2.0);

	    if (isx >= 0 && isx < nsx && ihx < nhx) {
	      if (isy >= 0 && isy < nsy && ihy < nhy) {
		sf_seek(in,pos+((isx*nhx+ihx)+(isy*nhy+ihy))*nt,SEEK_SET);
		sf_charread(trace,nt,in);
		sf_charwrite(trace,nt,out);
	      }
	    } else {
		sf_charwrite(zero,nt,out);
	    }
	  }
	}
      }
    }


    exit(0);
}
Esempio n. 11
0
int main (int argc, char* argv[])
{
    off_t n1, n2, n[SF_MAX_DIM];
    int axis, ndim, i, i2, *ibuf;
    size_t nsize, nbuf, ntest;
    sf_file in=NULL;
    sf_file out=NULL;
    float *fbuf, *abuf, dscale, pclip;
    sf_complex* cbuf;
    sf_datatype type;

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

    type = sf_gettype(in);
    nbuf = sf_bufsiz(in);
    if (SF_INT == type) sf_settype(out,SF_FLOAT);

    sf_fileflush(out,in);

    if (!sf_getint("axis",&axis)) axis = 0;
    /* Scale by maximum in the dimensions up to this axis. */
    if (!sf_getfloat("rscale",&dscale)) dscale=0.;
    /* Scale by this factor. */
    if (!sf_getfloat("pclip",&pclip)) pclip=100.;
    /* data clip percentile */

    ndim = sf_largefiledims (in, n);

    n1=1;
    n2=1;

    for (i=0; i < ndim; i++) {
	if (i < axis) n1 *= n[i];
	else          n2 *= n[i];
    }

    abuf = sf_floatalloc(n1);

    ntest = SF_MAX(n1*pclip/100. + .5,0);
    if (ntest > n1) ntest = n1;

    if (1 < n1 && 0. == dscale) {
	switch (type) {
	    case SF_FLOAT:
		fbuf = sf_floatalloc(n1);

		for (i2=0; i2 < n2; i2++) {
		    sf_floatread (fbuf,n1,in);
		    for (i=0; i < n1; i++) {
			abuf[i] = fabsf(fbuf[i]);
		    }
		    dscale = getscale(ntest,n1,abuf);		    
		    for (i=0; i < n1; i++) {
			fbuf[i] *= dscale;
		    }
		    sf_floatwrite (fbuf,n1,out);
		}
		break;
	    case SF_COMPLEX:
		cbuf = sf_complexalloc(n1);

		for (i2=0; i2 < n2; i2++) {
		    sf_complexread (cbuf,n1,in);
		    for (i=0; i < n1; i++) {
			abuf[i] = cabsf(cbuf[i]);
		    }
		    dscale = getscale(ntest,n1,abuf);
		    for (i=0; i < n1; i++) {
#ifdef SF_HAS_COMPLEX_H
			cbuf[i] *= dscale;
#else
			cbuf[i] = sf_crmul(cbuf[i],dscale);
#endif
		    }
		    sf_complexwrite (cbuf,n1,out);
		}
		break;
	    default:
		sf_error("Unsupported type %d",type);	
		break;
	}
    } else {
	if (0.==dscale && !sf_getfloat("dscale",&dscale)) dscale=1.;
	/* Scale by this factor (works if rscale=0) */

	nsize = n1*n2;

	switch (type) {
	    case SF_COMPLEX:
		nsize *= 2;
		sf_settype(in,SF_FLOAT);
		sf_settype(out,SF_FLOAT);
	    case SF_FLOAT:
		nbuf /= sizeof(float);
		fbuf = sf_floatalloc(nbuf);
	
		while (nsize > 0) {
		    if (nsize < nbuf) nbuf = nsize;
		    sf_floatread (fbuf,nbuf,in);
		    for (i=0; i < nbuf; i++) {
			fbuf[i] *= dscale;
		    }
		    sf_floatwrite (fbuf,nbuf,out);
		    nsize -= nbuf;
		}	
		break;
	    case SF_INT:
		nbuf /= sizeof(int);
		ibuf = sf_intalloc(nbuf);
		fbuf = sf_floatalloc(nbuf);
		
		while (nsize > 0) {
		    if (nsize < nbuf) nbuf = nsize;
		    sf_intread (ibuf,nbuf,in);
		    for (i=0; i < nbuf; i++) {
			fbuf[i] = (double) ibuf[i]*dscale;
		    }
		    sf_floatwrite (fbuf,nbuf,out);
		    nsize -= nbuf;
		}	
		break;
	    default:
		sf_error("Unsupported type %d",type);

	} 
    }


    exit (0);
}
Esempio n. 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);
}
Esempio n. 13
0
int main(int argc, char* argv[])
{
    int verbose;
    int i, i1, n1_headers, n1_traces, len, tempint, outkeyindx;
    sf_file in, out;
    char *output=NULL, *outputkey=NULL;
    float /* *ftra=NULL, */ **fbuf=NULL, **fst=NULL;
    int /* *itra=NULL, */ **ibuf=NULL, **ist=NULL;
    char* header_format;  
    sf_datatype typehead;

    float* fheader=NULL;
    int* iheader=NULL;
    float* intrace=NULL;
    

    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 */
    /******************************************/


    in = sf_input ("in");
    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");

    /* kls change type to header_format and read from history */
    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);
    iheader = (int*)fheader;

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

    segy_init(n1_headers,in);
 
    for (i=0; i < n1_headers; i++) {
	/* see if the segy keywords are in the input history file.  If they
	   are missing or different than I think they should be add them to
	   the output file history */
	/* no idea why this always has to be added to history, but I get 
	   errors it I remove the forcing condition below (i.e. 1 || ) */
	if(1 || !sf_histint(in,segykeyword(i),&tempint) || tempint!=i){
	    sf_putint(out,segykeyword(i),i);
	}
    }


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

    len = sf_math_parse (output,out,typehead);

    if (NULL==(outputkey=sf_getstring("outputkey")))sf_error("Need outputkey=");
    /* name of the header key to put the results of the output equation */
    if(!sf_histint(out,outputkey,&outkeyindx)){
	sf_error("user parameter outputkey is not an input data header key.");
    }
    if(verbose>0)fprintf(stderr,"outkeyindx=%d\n",outkeyindx);

    /* I do not like these 2d arrays with one of the lengths is 1.
       I have done this so I can continue to use sf_math_parse and 
       sf_math_evaluate.  Perhaps someday these can be refactored and the 
       alloc2 below can become sf_floatalloc (without the 2). Karl S */ 
    if (SF_FLOAT == typehead) { /* float typehead */
	/* ftra = sf_floatalloc(n1_headers); */
	fbuf = sf_floatalloc2(1,n1_headers);
	fst  = sf_floatalloc2(1,len+3);
    } else {               /* int typehead */
	/* itra = sf_intalloc(n1_headers); */
	ibuf = sf_intalloc2(1,n1_headers);
	ist  = sf_intalloc2(1,len+3);
    }

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

    /***************************/
    /* 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 (SF_FLOAT == typehead) { 
	    for (i1=0; i1 < n1_headers; i1++) {
		fbuf[i1][0]=fheader[i1];
	    }
	    sf_math_evaluate (len, 1, fbuf, fst);
	    if(verbose>2){
		fprintf(stderr,"after math_evaluate fst[1][0]=%f\n",fst[1][0]);
	    }
	    fheader[outkeyindx]=fst[1][0];	
	} else {
	    for (i1=0; i1 < n1_headers; i1++) {
		/* iheader point to same place as fheader */
		ibuf[i1][0]=iheader[i1];
		if(verbose>2)fprintf(stderr,"iheader[i1]=%d\n",iheader[i1]);
	    }
	    sf_int_math_evaluate (len, 1, ibuf, ist);
	    if(verbose>2){
		fprintf(stderr,"after int_math_evaluate ist[1][0]=%d\n",ist[1][0]);
	    }
	    iheader[outkeyindx]=ist[1][0];
	}
      
	/***************************/
	/* write trace and headers */
	/***************************/
	put_tah(intrace, fheader, n1_traces, n1_headers, out);
	if(verbose>1)fprintf(stderr,"returned from writing the tah\n");
	
    }

    exit(0);
}
Esempio n. 14
0
int main(int argc, char* argv[])
{
    int esize, shift;
    off_t size;
    size_t i, nleft, nbuf, e_size, len, bufsiz;
    sf_file real, cmplx;
    char *rbuf, *cbuf, *rformat, *cformat, *prog;

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

    if (SF_COMPLEX != sf_gettype(cmplx))
      {
	
	sf_error("wrong input type");
      }
    
    cformat = sf_histstring(cmplx,"data_format");
    len = strlen(cformat)+1;
    rformat = sf_charalloc(len);
    memcpy(rformat,cformat,len);
    strcpy(strstr(rformat,"complex"),"float");
    sf_setformat(real,rformat);

    if (!sf_histint(real,"esize",&esize)) esize=4;
    if (esize <= 0) sf_error("wrong esize=%d",esize);
    e_size = (size_t) esize;

    size = sf_filesize (cmplx);
    
    sf_fileflush(real,cmplx);
    sf_setform(real ,SF_NATIVE);
    sf_setform(cmplx,SF_NATIVE);
    
    prog = sf_getprog();
    if (       NULL != strstr(prog,"real")) {
	shift=0;
    } else if (NULL != strstr(prog,"imag")) {
	shift=esize;
    } else {
	sf_warning("neither real nor imag, assume real");
	shift=0;
    }

    bufsiz = sf_bufsiz(cmplx);
    rbuf = sf_charalloc(bufsiz);
    cbuf = sf_charalloc(2*bufsiz);
    
    for (nleft=size*e_size; nleft > 0; nleft -= nbuf) {
	nbuf = (bufsiz < nleft)? bufsiz: nleft;
	sf_charread(cbuf,2*nbuf,cmplx);
	for (i=0; i < nbuf; i += e_size) {
	    memcpy(rbuf+i,cbuf+2*i+shift,e_size);
	}
	sf_charwrite(rbuf,nbuf,real);
    }
    

    exit (0);
}
Esempio n. 15
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);
}
Esempio n. 16
0
int main(int argc, char* argv[])
{
    bool velocity, verb, escvar;
    int is, n[2], im, nm, order, nshot, ndim;
    int nt, nt1, nr, ir, it, i;
    float t, dt, da=0., a0, amax, v0, deg2rad;
    float x[2], p[2], d[2], o[2], **traj, *vz2, *vx2, *q, **s, *a;
    char *velfile;
    araytrace rt;
    sf_file shots, vz, vx, eta, rays, angles;

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

    /* get 2-D grid parameters */
    if (!sf_histint(vz,"n1",n))     sf_error("No n1= in input");
    if (!sf_histint(vz,"n2",n+1))   sf_error("No n2= in input");
    if (!sf_histfloat(vz,"d1",d))   sf_error("No d1= in input");
    if (!sf_histfloat(vz,"d2",d+1)) sf_error("No d2= in input");
    if (!sf_histfloat(vz,"o1",o))   o[0]=0.;
    if (!sf_histfloat(vz,"o2",o+1)) o[1]=0.;

    /* additional parameters */
    if(!sf_getbool("vel",&velocity)) velocity=true;
    /* If y, input is velocity; if n, slowness */
    if(!sf_getint("order",&order)) order=4;
    /* Interpolation order */

    if (!sf_getint("nt",&nt)) sf_error("Need nt=");
    /* Number of time steps */
    if (!sf_getfloat("dt",&dt)) sf_error("Need dt=");
    /* Sampling in time */

    if(!sf_getbool("verb",&verb)) verb=true;
    /* Verbosity flag */
    if(!sf_getbool("escvar",&escvar)) escvar=false;
    /* If y - output escape values, n - trajectories */

    /* get shot locations */
    if (NULL != sf_getstring("shotfile")) {
        /* file with shot locations */
        shots = sf_input("shotfile");
        if (!sf_histint(shots,"n1",&ndim) || 2 != ndim) 
            sf_error("Must have n1=2 in shotfile");
        if (!sf_histint(shots,"n2",&nshot))
            sf_error("No n2= in shotfile");

        if (sf_histfloat(shots,"o2",&t)) sf_putfloat(rays,"o3",t);
        if (sf_histfloat(shots,"d2",&t)) sf_putfloat(rays,"d3",t);

        s = sf_floatalloc2 (ndim,nshot);
        sf_floatread(s[0],ndim*nshot,shots);
        sf_fileclose (shots);
    } else {
        nshot = 1;
        ndim = 2;

        s = sf_floatalloc2 (ndim,nshot);

        if (!sf_getfloat("zshot",&s[0][0]))   s[0][0]=0.;
        /* shot coordinates (if no shotfile) */
        if (!sf_getfloat("yshot",&s[0][1])) s[0][1]=o[1] + 0.5*(n[1]-1)*d[1];
    }

    deg2rad = SF_PI/180.;

    if (NULL != sf_getstring("anglefile")) {
        /* file with initial angles */
        angles = sf_input("anglefile");
        
        if (!sf_histint(angles,"n1",&nr)) sf_error("No n1= in anglefile");
    } else {
        angles = NULL;

        if (!sf_getint("nr",&nr)) sf_error("Need nr=");
        /* number of angles (if no anglefile) */
        if (!sf_getfloat("a0",&a0)) a0 = 0.;
        /* minimum angle (if no anglefile) */
        if (!sf_getfloat("amax",&amax)) amax=360.;
        /* maximum angle (if no anglefile) */

        /* convert degrees to radians */
        a0   *= deg2rad;
        amax *= deg2rad;

        /* figure out angle spacing */
        da = (nr > 1)? (amax - a0)/(nr-1) : 0.;
    }

    a = sf_floatalloc(nr);

    /* specify output dimensions */
    nt1 = nt+1;
    sf_putint (rays,"n1",escvar ? 4 : nt1);
    sf_putint (rays,"n2",nr);
    if( nshot > 1 ) sf_putint (rays,"n3",nshot);
    sf_putfloat(rays,"o1",0.);
    sf_putfloat(rays,"d1",dt);
    if (NULL == angles) {
        sf_putfloat(rays,"o2",a0/deg2rad);
        sf_putfloat(rays,"d2",da/deg2rad);
    }
    if (!escvar) {
        sf_putstring(rays,"label1","Time");
        sf_putstring(rays,"unit1","s");
        sf_settype (rays,SF_COMPLEX);
    } else {
        sf_putstring(rays,"label1","Escvar");
        sf_putstring(rays,"unit1","");
    }
    sf_putstring(rays,"label2","Angle");
    sf_putstring(rays,"unit2","Degrees");

    sf_fileflush (rays,NULL);
    sf_settype (rays,SF_FLOAT);


    /* get velocities */
    nm = n[0]*n[1];
    vz2 = sf_floatalloc(nm);
    sf_floatread(vz2,nm,vz);

    for(im = 0; im < nm; im++) {
        v0 = vz2[im];
        vz2[im] = velocity? v0*v0: 1./(v0*v0);
    }

    if (NULL != (velfile = sf_getstring("vx"))) {
        /* horizontal velocity or slowness */
        vx = sf_input(velfile);
        vx2 = sf_floatalloc(nm);
        sf_floatread(vx2,nm,vx);

        for(im = 0; im < nm; im++) {
            v0 = vx2[im];
            vx2[im] = velocity? v0*v0: 1./(v0*v0);
        }
        free(velfile);
        sf_fileclose(vx);
    } else {
        vx2 = NULL;
    }

    
    if (NULL != (velfile = sf_getstring("eta"))) {
        /* eta parameter */
        eta = sf_input(velfile);
        q = sf_floatalloc(nm);
        sf_floatread(q,nm,eta);

        /* transform eta to q */
        for(im = 0; im < nm; im++) {
            q[im] = 1./(1.+2.*q[im]);
        }

        free(velfile);
        sf_fileclose(eta);
    } else {
        q = NULL;
    }

    /* initialize ray tracing object */
    rt = araytrace_init (2, nt, dt, n, o, d, vz2, vx2, q, order);    
    free (vz2);
    if (NULL != vx2) free (vx2);
    if (NULL != q) free (q);

    traj = sf_floatalloc2 (2*ndim,nt1);

    for( is = 0; is < nshot; is++) { /* loop over shots */
        /* initialize angles */
        if (NULL != angles) {
            sf_floatread(a,nr,angles);
        } else {
            for (ir = 0; ir < nr; ir++) {
                a[ir] = a0+da*ir;
            }
        }
        if (verb)
            sf_warning ("Shooting from z=%g, x=%g;", s[is][0], s[is][1]);

        for (ir = 0; ir < nr; ir++) { /* loop over rays */
            /* initialize position */
            x[0] = s[is][0]; 
            x[1] = s[is][1];

            /* initialize direction */
            p[0] = -cosf(a[ir]);
            p[1] = sinf(a[ir]);

            it = trace_aray (rt, x, p, traj);
            if (it < 0) it = -it; /* keep side-exiting rays */
            
            if (escvar) {
                /* Write escape variables only */
                sf_floatwrite (traj[it],ndim,rays); /* z, x */
                t = it*dt; /* t */
                sf_floatwrite (&t,1,rays);
                /* a */
                if (it > 0) {
                    i = it >= 2 ? it - 2 : it - 1;
                    /* Escape vector */
                    x[0] = traj[it][0];
                    x[1] = traj[it][1];
                    x[0] -= traj[i][0];
                    x[1] -= traj[i][1];
                    /* Dot product with unit vector pointing upward */
                    t = sqrt(x[0]*x[0] + x[1]*x[1]); /* Length */
                    t = acos(-x[0]/t);
                    if (x[1] < 0) t = -t;
                } else
                    t = a[ir];
                t /= deg2rad;
                sf_floatwrite (&t,1,rays);
            } else {
                /* Write full trajectory */
                for (i=0; i < nt1; i++) {
                    if (0==it || it > i) {
                        sf_floatwrite (traj[i],ndim,rays);
                    } else {
                        sf_floatwrite (traj[it],ndim,rays);
                    }
                }
            }
        }
    }


    exit (0);
}
Esempio n. 17
0
int main(int argc, char* argv[])
{
    int niter, n1, n2, i, nf1, nf2, nf3, nf4, nf5, nf6, n3, n4, i4, nf;
    float *mm, *kk, *filt, eps;
    bool *known, exact, verb;
    sf_file in, out, fil, mask=NULL;

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

    if (!sf_getint("niter",&niter)) niter=100;
    /* Number of iterations */

    if (!sf_getbool("exact",&exact)) exact=true;
    /* If y, preserve the known data values */

    if (!sf_getfloat("eps",&eps)) eps=0.;
    /* regularization parameter */

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

    /* input data, output model */
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    if (!sf_histint(in,"n3",&n3)) sf_error("No n3= in input");
    n4 = sf_leftsize(in,3);

    if (1==n1 && 1==n2 && 1==n3) {
	if (!sf_histint(in,"n4",&n1)) sf_error("No n4= in input");
	if (!sf_histint(in,"n5",&n2)) sf_error("No n5= in input");
	if (!sf_histint(in,"n6",&n3)) sf_error("No n6= in input");
	n4 = sf_leftsize(in,6);
    }

    sf_fileflush(out,in);  /* copy data dimensions */

    /* input filter */
    if (!sf_histint(fil,"n1",&nf1)) sf_error("No n1= in filter");
    if (!sf_histint(fil,"n2",&nf2)) sf_error("No n2= in filter");
    if (!sf_histint(fil,"n3",&nf3)) sf_error("No n3= in filter");
    if (!sf_histint(fil,"n4",&nf4)) sf_error("No n4= in filter");
    if (!sf_histint(fil,"n5",&nf5)) sf_error("No n5= in filter");
    if (!sf_histint(fil,"n6",&nf6)) sf_error("No n6= in filter");
    
    if (nf4!=n1 || nf5!=n2 || nf6!=n3) 
	sf_error("need n1==nf4 && n2==nf5 && n3==nf6");
    
    nf = nf1*nf2*nf3*nf4*nf5*nf6;
    filt = sf_floatalloc(nf);
    mm = sf_floatalloc(n1*n2*n3);
    kk = sf_floatalloc(n1*n2*n3);
    known = sf_boolalloc(n1*n2*n3);

    for (i=0; i < n1*n2*n3; i++) {
	mm[i]=0.;
	kk[i]=0.;
	known[i]=false;
    }

    if (NULL != sf_getstring("mask")) {
	/* optional input mask file for known data */
	mask = sf_input("mask");
    }

    for (i4=0; i4 < n4; i4++) {
	sf_warning("slice %d of %d",i4+1,n4);
	sf_floatread(mm,n1*n2*n3,in);
	sf_floatread(filt,nf,fil);
	
	if (NULL != sf_getstring("mask")) {
	    sf_floatread(kk,n1*n2*n3,mask);
	    
	    for (i=0; i < n1*n2*n3; i++) {
		known[i] = (bool) (kk[i] != 0.);
	    }
	} else {
	    for (i=0; i < n1*n2*n3; i++) {
		known[i] = (bool) (mm[i] != 0.);
	    }
	}
	
	if (exact) {
	    for (i=0; i < n1*n2*n3; i++) {
		if (known[i]) kk[i] = mm[i];
	    }
	}
	
	nmis3(niter, nf1, nf2, nf3, nf4, nf5, nf6, filt, mm, known, eps, verb);
	
	if (exact) {
	    for (i=0; i < n1*n2*n3; i++) {
		if (known[i]) mm[i] = kk[i];
	    }
	}
	
	sf_floatwrite(mm,n1*n2*n3,out);
    }

    exit (0);
}
Esempio n. 18
0
int main(int argc, char* argv[])
{
    sf_file in, out;
    char *buf, *buf2, key[5];
/* Want them to be arbitrary, neither float nor complex */
/* Just pretend they are character pointers so we multiply offsets ourselves.*/
    int i, dim, dim1, dim2, rot[SF_MAX_DIM], esize;
    int mem; /* for avoiding int to off_t typecast warning */
    off_t n[SF_MAX_DIM], pos=0, pos3=0, memsize, *k1 = NULL, *k2 = NULL;
    size_t n1, i1, i2, i3, n2, n3, size;
    bool verb;

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

    dim   = sf_largefiledims(in,n);
    esize = sf_esize(in);

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

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

    dim2=0;
    for (i=0; i < dim; i++) {	
	snprintf(key,5,"rot%d",i+1);
	if (!sf_getint(key,rot+i)) rot[i]=0;
	/*( rot#=(0,0,...) length of #-th axis that is moved to the end )*/
	if (rot[i] < 0) rot[i] += n[i];
	if (rot[i] >= n[i]) 
	    sf_error("rot%d=%d must be smaller than n%d=%d",
		     i,rot[i],i,n[i]);
	if (rot[i]) dim2=i;
    }
    dim2++;
    /* dim2 is the last dimension involved */

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

    n1=n2=n3=1;
    dim1=0;
    for (i=0; i < dim; i++) {
	if (i < dim2) {
	    size = n1*n[i];
	    if (1==n2 && size*esize < memsize) {
		n1=size;
		dim1=i;
	    } else {
		n2*=n[i];
	    }
	} else {
	    n3 *= n[i];
	}
    }
    dim1++;
    /* dim1 is the last dimension that fits in memory,
       n1 is the size up to this dimension
       n2 is the size of other dimensions involved 
       n3 is the size of all other dimensions so that
       n1*n2*n3 is the total data size
    */
    
    buf  = sf_charalloc(n1*esize);
    buf2 = sf_charalloc(n1*esize);

    if (n1>1) {
	k1 = sf_largeintalloc(n1);
	rotate(n1,dim1,n,rot,k1);
    }

    if (n2>1) {
	if (verb) sf_warning("Going out of core...");

	sf_unpipe(in,n1*n2*n3*esize); /* prepare for random access */
	pos = sf_tell(in);

	k2 = sf_largeintalloc(n2);
	rotate(n2,dim2-dim1,n+dim1,rot+dim1,k2);
    } 

    if (verb) sf_warning("n1=%d, n2=%d, n3=%d",
			 (int) n1,(int) n2,(int) n3);

    /* k1 is a table for in-core     reversal 
       k2 is a table for out-of-core reversal */
    
    for (i3=0; i3 < n3; i3++) {
	if (n2 > 1) pos3 = pos+(off_t) i3*n2*n1*esize;
	for (i2=0; i2 < n2; i2++) {
	    if (n2 > 1) /* if out of core */
		sf_seek(in,pos3+k2[i2]*n1*esize,SEEK_SET);

	    sf_charread(buf,n1*esize,in);
	    for (i1=0; i1 < n1; i1++) {
		memcpy(buf2+k1[i1]*esize,buf+i1*esize,esize);
	    }
	    sf_charwrite(buf2,n1*esize,out);
	}
    }
    

    exit (0);
}
Esempio n. 19
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);
}
Esempio n. 20
0
int main(int argc, char* argv[])
{
    int j, n1, n2, i2, ni, axis, esize;
    float f;
    off_t i3, n3;
    size_t n;
    sf_file in, out;
    char key1[7], key2[7], *val, *trace;

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

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"esize",&esize)) sf_error("No esize= in input");

    if (!sf_getint("axis",&axis)) axis=2;
    /* which axis to spray */

    n = (size_t) esize;
    for (j=0; j < axis-1; j++) {
	sprintf(key2,"n%d",j+1);
	if (!sf_histint(in,key2,&ni)) break;
	n *= ni;
    }

    if (!sf_getint("n",&n2)) sf_error("Need n=");
    /* Size of the newly created dimension */    
    sprintf(key1,"n%d",axis);
    sf_putint(out,key1,n2);

    if (sf_getfloat("d",&f)) {
	/* Sampling of the newly created dimension */ 
	sprintf(key1,"d%d",axis);
	sf_putfloat(out,key1,f);
    }
    
    if (sf_getfloat("o",&f)) {
	/* Origin of the newly created dimension */
	sprintf(key1,"o%d",axis);
	sf_putfloat(out,key1,f);
    }

    if (NULL != (val = sf_getstring("label"))) {
	/* Label of the newly created dimension */
	sprintf(key1,"label%d",axis);
	sf_putstring(out,key1,val);
    }

    if (NULL != (val = sf_getstring("unit"))) {
	/* Units of the newly created dimension */
	sprintf(key1,"unit%d",axis);
	sf_putstring(out,key1,val);
    }

    n3 = sf_shiftdim(in, out, axis);
    
    sf_fileflush(out,in);
    sf_setform(in,SF_NATIVE);
    sf_setform(out,SF_NATIVE);

    trace = sf_charalloc (n);
    
    for (i3=0; i3 < n3; i3++) {
	sf_charread (trace, n, in);
	for (i2=0; i2 < n2; i2++) {
	    sf_charwrite(trace, n, out);
	} 
    }


    exit (0);
}
Esempio n. 21
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);
}
Esempio n. 22
0
int main(int argc, char* argv[])
{

    float *data /*input [nk] */; 
    int nk      /* data length */;
    float dk    /* data sampling */; 
    float k0    /* initial location */;
    bool verb   /* verbosity flag */;
    int niter   /* number of iterations */; 

    int ik, iter;
    float x1, x2, x3, dx1, dx2, dx3, x10, x20, x30, x4, x5;
    float k, f, s, l, r, p, q, s2, y1, y2, y3, sk, s1, s3;
    float a11, a12, a13, a22, a23, a33, a2, a3, a4, a5, a6, d;
    float b11, b12, b13, b22, b23, b33, eps, r2; 
    
    sf_file in, out, prm;

    /* Estimate shape (Caution: data gets corrupted) */ 

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


    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&nk)) sf_error("No n1= in input");
    if (!sf_histfloat(in,"d1",&dk)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"o1",&k0)) sf_error("No o1= in input");
 
    if (!sf_getint("niter",&niter)) niter=100;
    /* number of iterations */
    if (!sf_getfloat("x10",&x10)) x10=6.;
    /* initial nonlinear parameter x1 value */
    if (!sf_getfloat("x20",&x20)) x20=-0.5;
    /* initial nonlinear parameter x2 value */
    if (!sf_getfloat("x30",&x30)) x30=200.;
    /* initial nonlinear parameter x3 value */
    if (!sf_getbool("verb",&verb)) verb=false;
    /* verbosity flag */

    sf_putint(prm,"n1",3);
    sf_putint(prm,"nk",nk);
    sf_putfloat(prm,"dk",dk);
    sf_putfloat(prm,"k0",k0);
    sf_fileflush(prm,in);

    data = sf_floatalloc(nk);
    
    sf_floatread(data,nk,in);

    x1 = x10; /* initial x1 */
    x2 = x20; /* initial x2 */
    x3 = x30; /* initial x3 */
    x4 = x3*x3;

    eps = 10.*FLT_EPSILON;
    eps *= eps;
    a11 = (float)nk;

    if (verb) sf_warning("got x10=%g x20=%g x30=%g k0=%g niter=%d nk=%d dk=%g",
			 x10,x20,x30,k0,niter,nk,dk);
	
    /* Gauss iterations */
    for (iter = 0; iter < niter; iter++) {
        x5 = 2.*x2*x3;
        s2 = y1 = y2 = y3 = sk = s1 = s3 = a12 = r2 = 0.;
	for (ik = 0; ik < nk; ik++) {
	    k = ik*dk + k0;
	    k *= k;
            s = 1. + x4*k;
	    l = log(s);
            q = 1./s;
	    p = k*q;
            
            s2 += l*l;
            sk += p;
            a12 += l;

	    f = log(data[ik]);       
	    r = x1 + x2*l - f;

	    y1 += r;
	    y2 += r*l;
	    y3 += r*p;
	    s1 += p*(r + x2*l);
            s3 += p*q*(2.*x2*x4*k + r*(1.-x4*k));

	    r2 += r*r;
	}

        y3 *= x5;

	a13 = x5*sk;
	a22 = s2;
        a23 = 2.*x3*s1;
	a33 = 2.*x2*s3;

	a2 = a12*a12;
	a3 = a12*a13;
	a4 = a13*a13;
        a5 = a23*a23;
        a6 = a22*a33;

        d = 1./(a11*(a6-a5) + 2.*a3*a23 - a33*a2 - a22*a4);

	b11 = a6-a5;
        b12 = a13*a23-a12*a33;
	b13 = a12*a23-a13*a22;
	b22 = a11*a33-a4;
	b23 = a3-a23*a11;
	b33 = a11*a22-a2;

	dx1 = -d*(b11*y1 + b12*y2 + b13*y3);
	dx2 = -d*(b12*y1 + b22*y2 + b23*y3);
	dx3 = -d*(b13*y1 + b23*y2 + b33*y3);

	x1 += dx1;
	x2 += dx2;
	x3 += dx3;
        x4 = x3*x3;
	
        if (verb) sf_warning("iter=%d r2=%g dx1=%g dx2=%g dx3=%g x1=%g x2=%g x3=%g",
			     iter,r2,dx1,dx2,dx3,x1,x2,x3);
	if (r2 < eps || (dx1*dx1+dx2*dx2+dx3*dx3) < eps) break;    
    }
 
    for (ik = 0; ik < nk; ik++) {
	k = ik*dk + k0;
	k *= k;
	data[ik] = exp(x1+x2*log(1.+x4*k));
    }
 
    if (verb) sf_warning ("%d iterations", iter);        
	
    /* Optimized parameters for f = log(data) = log(d0) + a*log(1+b*b*k*k) where a = -(nu/2+1/4) */
    sf_warning ("b=%g nu=%g d0=%g",x3,-2*x2-0.5,exp(x1));

    sf_floatwrite(&x3,1,prm);
    sf_floatwrite(&x2,1,prm);
    sf_floatwrite(&x1,1,prm);

    sf_floatwrite (data,nk,out);

    exit (0);
}
Esempio n. 23
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);
}
Esempio n. 24
0
int main(int argc, char* argv[])
{
    int i, iter, niter;
    sf_complex *buf, *buf2;
    double rn, rnp, alpha, beta;
    off_t nm, nd, msiz, dsiz, pos;
    size_t nbuf, mbuf, dbuf, len, iolen, cmdlen;
    FILE *xfile, *Rfile, *gfile, *Gfile, *sfile, *Sfile;
    char *x, *R, *g, *G, *s, *S, *prog, *cmdline, *iostring, *arg;
    sf_file mod, dat, x0;  /* input */
    sf_file Rrsf, Grsf, grsf, out; /* output */

    extern int fseeko(FILE *stream, off_t offset, int whence);
    extern off_t ftello (FILE *stream);

    sf_init(argc,argv);

    dat = sf_input("--input");
    mod = sf_input("mod");

    out = sf_output("--output");
    sf_fileflush(out,mod);
    sf_settype(out,SF_COMPLEX);

    if (SF_COMPLEX != sf_gettype(mod) ||
	SF_COMPLEX != sf_gettype(dat)) 
	sf_error("Need complex type in mod and dat");

    for (i=0; i < argc-1; i++) {
	argv[i]=argv[i+1];
    }
    for (i=0; i < argc-1; i++) {	
	/* find the program to run */
	if (NULL == strchr(argv[i],'=')) {
	    /* first one without the '=' */
	    prog = argv[0];
	    argv[0] = argv[i];
	    argv[i] = prog;
	    break;
	}
    }

    argv[argc-1] = sf_charalloc(6);
    snprintf(argv[argc-1],6,"adj=X");

    if (!sf_getint("niter",&niter)) niter=1;
    /* number of iterations */

    Rfile = sf_tempfile(&R,"w+"); 
    xfile = sf_tempfile(&x,"w+b"); 
    gfile = sf_tempfile(&g,"w+");
    Gfile = sf_tempfile(&G,"w+");
    sfile = sf_tempfile(&s,"w+b");
    Sfile = sf_tempfile(&S,"w+b");

    fclose(Rfile); Rrsf = sf_output(R); sf_readwrite(Rrsf,true); sf_fileflush(Rrsf,dat);
    fclose(xfile);
    fclose(gfile); 
    fclose(Gfile); 
    fclose(sfile);
    fclose(Sfile);

    nm = sf_filesize(mod);
    nd = sf_filesize(dat);

    /* I/O buffers */
    nbuf = BUFSIZ/sizeof(sf_complex);
    buf  = sf_complexalloc(nbuf);
    buf2 = sf_complexalloc(nbuf);

    cmdline = sf_charalloc(SF_CMDLEN);
    iostring = sf_charalloc(SF_CMDLEN);
    cmdlen = 0;
    for (i=0; i < argc; i++) {
	arg = argv[i];
	len = strlen(arg);
	if (cmdlen+len > SF_CMDLEN-2) sf_error("command line is too long");

	strncpy(cmdline+cmdlen,arg,len);
	cmdline[cmdlen+len]=' ';
	cmdlen += len+1;
    }

    if (NULL != sf_getstring("x0")) {
	x0 = sf_input("x0"); /* initial model */
    } else {
	x0 = NULL;
    }

    for (iter=0; iter < niter; iter++) {
	if (0 == iter) {
	    xfile = fopen(x,"wb");
	    
	    if (NULL == x0) {
		for (i=0; i < nbuf; i++) { buf[i] = sf_cmplx(0.0f,0.0f); }
	    }
	    
	    MLOOP( if (NULL != x0) sf_complexread(buf,mbuf,x0);
		   MWRITE(xfile); );
	    
	    fclose(xfile);
#ifdef SF_HAS_COMPLEX_H 
	    DLOOP( sf_complexread(buf,dbuf,dat); 
		   for (i=0; i < dbuf; i++) { buf[i] = -buf[i]; }
		   sf_complexwrite(buf,dbuf,Rrsf); );
Esempio n. 25
0
int main(int argc, char* argv[])
{
    int niter, n1, n2, i;
    int sf1, sf2, sf3, sf4, nf1, nf2, nf3, nf4, n3, i3, sf, nf;
    float *mm, *dd, *sfilter, *nfilter, eps;
    bool verb;
    sf_file in, out, sfilt, nfilt;

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

    if (!sf_getint("niter",&niter)) niter=100;
    /* Number of iterations */

    if (!sf_getfloat("eps",&eps)) eps=0.;
    /* regularization parameter */

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

    /* read input file parameters */
    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input");
    n3 = sf_leftsize(in,2);

    if (1==n1 && 1==n2) {
	if (!sf_histint(in,"n3",&n1)) sf_error("No n3= in input");
	if (!sf_histint(in,"n4",&n2)) sf_error("No n4= in input");
	n3 = sf_leftsize(in,4);
    }

    sf_fileflush(out,in);  /* copy data dimensions */

    /* input filter */
    if (!sf_histint(sfilt,"n1",&sf1)) sf_error("No n1= in sfilt");
    if (!sf_histint(sfilt,"n2",&sf2)) sf_error("No n2= in sfilt");
    if (!sf_histint(sfilt,"n3",&sf3)) sf_error("No n3= in sfilt");
    if (!sf_histint(sfilt,"n4",&sf4)) sf_error("No n4= in sfilt");
    
    if (!sf_histint(nfilt,"n1",&nf1)) sf_error("No n1= in nfilt");
    if (!sf_histint(nfilt,"n2",&nf2)) sf_error("No n2= in nfilt");
    if (!sf_histint(nfilt,"n3",&nf3)) sf_error("No n3= in nfilt");
    if (!sf_histint(nfilt,"n4",&nf4)) sf_error("No n4= in nfilt");

    if (nf3!=n1 || nf4!=n2 || sf3!=n1 || sf4!=n2) 
	sf_error("need n1==sf3==nf3 && n2==sf4==nf4!");

    sf = sf1*sf2*sf3*sf4;
    nf = nf1*nf2*nf3*nf4;
    sfilter = sf_floatalloc(sf);
    nfilter = sf_floatalloc(nf);
    mm = sf_floatalloc(n1*n2);
    dd = sf_floatalloc(n1*n2);

    for (i=0; i < n1*n2; i++) {
	mm[i] = 0.;
	dd[i] = 0.;
    }

    for (i3=0; i3 < n3; i3++) {
	sf_warning("slice %d of %d",i3+1,n3);
	sf_floatread(dd,n1*n2,in);
	sf_floatread(sfilter,sf,sfilt);
	sf_floatread(nfilter,nf,nfilt);
	
	apefsignoi_lop(niter, sf1, sf2, nf1, nf2, n1, n2, 
		       sfilter, nfilter, mm, dd, eps, verb);
	
	sf_floatwrite(mm,n1*n2,out);
    }

    exit (0);
}
Esempio n. 26
0
int main(int argc, char* argv[])
{
    int n2, i2, nx, ny, niter, nliter;
    float x0, dx, y0, dy, a[3], **data=NULL, **pred=NULL;

    bool verb;
    sf_file in, out, ma;

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

    if (SF_FLOAT != sf_gettype(in)) sf_error("Need float input");
    if (!sf_histint(in,"n1",&nx)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&ny)) sf_error("No n2= in input");
    n2 = sf_leftsize(in,2);

    if (!sf_histfloat(in,"d1",&dx)) sf_error("No d1= in input");
    if (!sf_histfloat(in,"o1",&x0)) sf_error("No o1= in input");
    if (!sf_histfloat(in,"d2",&dy)) sf_error("No d2= in input");
    if (!sf_histfloat(in,"o2",&y0)) sf_error("No o2= in input");

    if (!sf_getfloat("a0",&a[0])) a[0]=1.;
    /* starting sharpness in xx */
    if (!sf_getfloat("b0",&a[1])) a[1]=0.;
    /* starting sharpness in xy */
    if (!sf_getfloat("c0",&a[2])) a[2]=1.;
    /* starting sharpness in yy */

    if (!sf_getint("niter",&niter)) niter=100;
    /* number of iterations */
    if (!sf_getint("nliter",&nliter)) nliter=1;
    /* number of reweighting iterations */
    if (!sf_getbool("verb",&verb)) verb=false;
    /* verbosity flag */

    sf_putint(ma,"n1",3);
    sf_putint(ma,"n2",1);
    sf_putint(ma,"nx",nx);
    sf_putfloat(ma,"dx",dx);
    sf_putfloat(ma,"x0",x0);
    sf_putint(ma,"ny",ny);
    sf_putfloat(ma,"dy",dy);
    sf_putfloat(ma,"y0",y0);
    sf_fileflush(ma,in);

    data = sf_floatalloc2(nx,ny);
    pred = sf_floatalloc2(nx,ny);

    for (i2=0; i2 < n2; i2++) {
	sf_floatread(data[0],nx*ny,in);

	monof2(data,pred,nliter,niter,a,nx,dx,x0,ny,dy,y0,verb);

	sf_floatwrite(a,3,ma);

	sf_floatwrite (pred[0],nx*ny,out);
    }

    exit (0);
}
Esempio n. 27
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);
}
Esempio n. 28
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);
}
Esempio n. 29
0
int main(int argc, char* argv[])
{
    int p[4][2], i, im, id, status;
    unsigned long mseed, dseed;
    off_t nm, nd, msiz, dsiz;
    size_t nbuf, mbuf, dbuf;
    float *buf;
    double dp;
    pid_t pid[6]={1,1,1,1,1,1};
    sf_file mod=NULL;
    sf_file dat=NULL;
    sf_file pip=NULL;

    sf_init(argc,argv);

    mod = sf_input("mod");
    dat = sf_input("dat");

    if (SF_FLOAT != sf_gettype(mod) ||
	SF_FLOAT != sf_gettype(dat))
	sf_error("Need float type in mod and dat");

    nm = sf_filesize(mod);
    nd = sf_filesize(dat);

    nbuf = BUFSIZ/sizeof(float);
    buf = sf_floatalloc(nbuf);

    mseed = (unsigned long) time(NULL);
    init_genrand(mseed);
    mseed = genrand_int32();
    dseed = genrand_int32();

    for (i=0; i < argc-1; i++) {
	argv[i]=argv[i+1];
    }
    argv[argc-1] = sf_charalloc(6);
    snprintf(argv[argc-1],6,"adj=X");

    for (i=0; i < 4; i++) { /* make four pipes */
	if (pipe(p[i]) < 0) sf_error("pipe error:");
    }

    for (i=0; i < 6; i++) { /* fork six children */
	if ((pid[i] = fork()) < 0) sf_error("fork error:");
	if (0 == pid[i]) break;
    }

    if (0 == pid[0]) {	
	/* makes random model and writes it to p[0] */

	close(p[0][0]);
	close(STDOUT_FILENO);
	DUP(p[0][1]);

	pip = sf_output("out");
	sf_fileflush(pip,mod);

	init_genrand(mseed);
	for (msiz=nm, mbuf=nbuf; msiz > 0; msiz -= mbuf) {
	    if (msiz < mbuf) mbuf=msiz;

	    sf_random(mbuf,buf);

	    sf_floatwrite(buf,mbuf,pip);
	}
    } 

    if (0 == pid[1]) {
	/* reads from p[0], runs the program, and writes to p[1] */

	close(p[0][1]);
	close(STDIN_FILENO);
	DUP(p[0][0]);

	close(p[1][0]);
	close(STDOUT_FILENO);
	DUP(p[1][1]);

	argv[argc-1][4]='0';
	execvp(argv[0],argv);

	_exit(1);
    }

    if (0 == pid[2]) {
	/* reads from p[1] and multiplies it with random data */
	
	close(p[1][1]);
	close(STDIN_FILENO);
	DUP(p[1][0]);

	pip = sf_input("in");

	init_genrand(dseed);
	dp = 0.;
	for (dsiz=nd, dbuf=nbuf; dsiz > 0; dsiz -= dbuf) {
	    if (dsiz < dbuf) dbuf=dsiz;

	    sf_floatread(buf,dbuf,pip);
	    for (id=0; id < dbuf; id++) {
		dp += buf[id]*genrand_real1 ();
	    }	
	}
	sf_warning(" L[m]*d=%g",dp);

	_exit(2);
    }

    if (0 == pid[3]) {	
	/* makes random data and writes it to p[2] */

	close(p[2][0]);
	close(STDOUT_FILENO);
	DUP(p[2][1]);

	pip = sf_output("out");
	sf_fileflush(pip,dat);

	init_genrand(dseed);
	for (dsiz=nd, dbuf=nbuf; dsiz > 0; dsiz -= dbuf) {
	    if (dsiz < dbuf) dbuf=dsiz;

	    sf_random(dbuf,buf);

	    sf_floatwrite(buf,dbuf,pip);
	}
    } 

    if (0 == pid[4]) {
	/* reads from p[2], runs the adjoint, and writes to p[3] */

	close(p[2][1]);
	close(STDIN_FILENO);
	DUP(p[2][0]);

	close(p[3][0]);
	close(STDOUT_FILENO);
	DUP(p[3][1]);

	argv[argc-1][4]='1';
	execvp(argv[0],argv);

	_exit(4);
    }

    if (0 == pid[5]) {
	/* reads from p[3] and multiplies it with random model */
	
	close(p[3][1]);
	close(STDIN_FILENO);
	DUP(p[3][0]);

	pip = sf_input("in");

	init_genrand(mseed);
	dp = 0.;
	for (msiz=nm, mbuf=nbuf; msiz > 0; msiz -= mbuf) {
	    if (msiz < mbuf) mbuf=msiz;

	    sf_floatread(buf,mbuf,pip);


	    for (im=0; im < mbuf; im++) {
		dp += buf[im]*genrand_real1 ();
	    }	
	}
	sf_warning("L'[d]*m=%g",dp);
	
	_exit(5);
    }

    for (i=0; i < 6; i++) {
	if (0 == pid[i]) break;
    }

    if (6==i) {
	/* parent waits */
	waitpid(pid[2],&status,0);
	waitpid(pid[5],&status,0);
    
	exit(0);
    }
}
Esempio n. 30
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);
}