Esempio n. 1
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. 2
0
int main(int argc, char* argv[])
{
    bool segy;
    int i, i1, i2, n1, n2, n3, n, nt, len, nkey, row;
    sf_file in, out;
    int mem; /* for avoiding int to off_t typecast warning */
    off_t memsize;
    char *eq, *output, *key, *arg;
    float **ftra=NULL, **fbuf=NULL, **fst=NULL, d2, o2;
    int **itra=NULL, **ibuf=NULL, **ist=NULL;
    sf_datatype type;

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

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

    type = sf_gettype(in);

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

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

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

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

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


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

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

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

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

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

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

    len = sf_math_parse (output,out,type);

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

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

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

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

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

    exit(0);
}