Ejemplo n.º 1
0
float alpha_trim_w(float *a,float *w,int n,float p)
/******************************************************************
alpha_trim_w - Weighted Alpha trim of array 
******************************************************************
Notes:
 the value a[irp] is replaced by the mean of the 
 a values, but the largers and smallest p% of a 
 values are left out fo mean computation,array a is destroyed 
******************************************************************
Author: Balasz Nemeth, given to CWP in 2008 by Potash Corporation
******************************************************************/
{
	float mean=0;
	unsigned int ist=NINT(n*(p/100.0));
	unsigned int i,ic;
	int *ind=NULL;
	
	ind = ealloc1(n,sizeof(unsigned int));
	
	for(i=0;i<n;++i) ind[i]=i;
	
	qkisort(n,a,ind);
	
	for(i=ist,ic=0;i<n-ist;i++,ic++) 
		mean += a[ind[i]]*w[ind[i]];
		
	free1(ind);
	
	return(mean/(float)ic);
}
Ejemplo n.º 2
0
void quantFixerror(float **x, float error, void *inconf, void *qstate)
/*******************************************************************************
quantization 
********************************************************************************
x		array[][] for the floats 
error		percent RMS error tolerable 
inconf		configuration info
qstate		quantization status
********************************************************************************
Author:		Tong Chen, 07/20/94
Modifier:	Tong Chen, 07/28/94, for API, without iteration
*******************************************************************************/
{
	wpcCONFIG *config = (wpcCONFIG *) inconf;
	wpcQUANT *quant = (wpcQUANT *) qstate;
	int nblock, lblock, tileszt, tileszx, nsz;
	int i;
	int iblock;
	float *fmax, *dev;
	int **qx, *blockind;
	unsigned char *flowflag, *cblockind;
	float atmp, average; 
	float clip, loading, bound, step_factor, rn, ratio; 


	/* obtain the configuration info */
	nblock = config->nblock;
	lblock = config->lblock;
	tileszt = config->tileszt;
	tileszx = config->tileszx;

	/* quantization status */
	qx = quant->qx;
	flowflag = quant->flowflag;
	cblockind = quant->blockind;

	nsz = tileszt*tileszx;

	/* spaces */
	fmax = alloc1float(nblock);
	dev = alloc1float(nblock);
	blockind = alloc1int(nblock);

	rn = 1./((float) lblock);

        /* for each difference block */
	clip = 0.;
        for(iblock = 0; iblock < nblock-1; iblock ++){

            /* compute the maximum amplitude and deviation */
	    fmax[iblock] = 0.;
	    dev[iblock] = 0.;
            for(i=0; i<lblock; i++){
                atmp = ABS(x[iblock][i]);
                if(fmax[iblock] < atmp) fmax[iblock] = atmp;
                dev[iblock] += atmp*atmp;
            }

            clip += dev[iblock];
            dev[iblock] *= rn;
            dev[iblock] = sqrt(dev[iblock]);
        }

	/* for the average block */
	average = 0.;
        for(i=0;i<lblock;i++){
	    average += x[nblock-1][i]; 
	}

	average *= rn;

	/* save this average for reconstruction */
	quant->ave = average;
	
	fmax[nblock-1] = dev[nblock-1] = 0.;
        for(i=0; i<lblock; i++){
            x[nblock-1][i]  = x[nblock-1][i] - average;
            atmp = ABS(x[nblock-1][i]);
            if(fmax[nblock-1] < atmp) fmax[nblock-1] = atmp;
            dev[nblock-1] += atmp*atmp;
        }
        clip += dev[nblock-1];

        dev[nblock-1] *= rn;
        dev[nblock-1] = sqrt(dev[nblock-1]);

        clip = clip/((float) nsz);
        clip = sqrt(clip);

	/* if all zero, no quantization needed */
	if(clip <= FLT_MIN){
	    quant->zeroflag = QUANTZEROFLAGY; 
	    return;
	}


	/* not all zero */
	quant->zeroflag = QUANTZEROFLAGN;

	error = 100.*error;
	clip *= error;

	/* for underflow */
	if(clip < FLT_MIN) clip = FLT_MIN;

	ratio = QUANTERRATIO;
	bound = ratio*clip;

	step_factor = QUANTBYTEMAXINT/bound;

	/* for overflow */
	if(step_factor > FLT_MAX) step_factor = FLT_MAX;

	quant->step = step_factor;

	/* quantization  */
       	for(iblock = 0; iblock < nblock; iblock ++){
            for(i=0; i<lblock; i++){
	    	atmp = x[iblock][i]*step_factor; 		
	      	qx[iblock][i] = NINT(atmp);
	    }

	    loading = fmax[iblock]*step_factor;
	    if(loading > QUANTBYTEMAXINT) 
		flowflag[iblock] = QUANTFLOWFLAGY;
	    else 
		flowflag[iblock] = QUANTFLOWFLAGN;
	}


        /* sort the blocks using deviation to reduce entropy */
        for(iblock=0; iblock<nblock; iblock++) 
            blockind[iblock] = iblock;

	qkisort(nblock, dev, blockind);

        for(iblock=0; iblock<nblock; iblock++) 
            cblockind[iblock] = blockind[iblock];

	/* free spaces */
	free((void *) fmax);
	free((void *) dev);
	free((void *) blockind);
}
Ejemplo n.º 3
0
main(int argc, char **argv)
{
    int n1, n2, i1, i2;
    float *zpicks, *vpicks, *velotb, *vzx;
    float *ztops, *zbots, *dvdzs, *dvdztb, *dvdzzx;
    float *z, *work, *smx, *smz;
    float *cdpsort, *sorts, *cdps;
    int *sortindex, *cdpsvelo, *cdpsdvdz, *npsvelo, *npsdvdz;
    int ncdpvelo, ncdpdvdz;
    FILE *infp=stdin,*outfp=stdout,*tmpfp;
    int nps, ivcon, indx;
    int cdpmin, dcdp, ncdp, cdpmax;
    float fz, dz, fcdp, res;
    int iz, nz, i, icdp;
    int ismxv,ismzv,nx;
    int ismxg,ismzg,invtyp,otvtyp;
    int incdp, nn, idvdz, indvdz;
    char *dvdzgrid, *dvdzghdr="dvdzg_hdr", *vghdr="vg_hdr";
    char *buf;
    FILE *dvdzfp, *vhfp, *dvdzhfp;

    int n3=1,n4=1,n5=1;
    float o1=0.,o2=0.,o3=0.,o4=0.,o5=0.,d1=0.,d2=0.,d3=0.,d4=0.,d5=0.;
    float scale=1.e-6, ocdp2=0., oline3=0., dcdp2=0., dline3=0.;
    float gmin=0, gmax=0.;
    int dtype=4,ierr=0;
    int orient=0,gtype=0;
    ghed gh;

  

    /* get parameters */
    initargs(argc,argv);
    askdoc(1);

    if (!getparint("nz",&nz)) err("number of depth (nz) must be given \n");
    if (!getparfloat("dz",&dz)) err("depth interval (dz) must be given \n");
    if (!getparint("cdpmin",&cdpmin)) 
	err("minimum output cdp (cdpmin) must be given \n");
    if (!getparint("ncdp",&ncdp)) 
	err("number of output cdp (ncdp) must be given \n");
    if (!getparint("dcdp",&dcdp)) dcdp=1;
    cdpmax = cdpmin + (ncdp-1)*dcdp;
    if (!getparfloat("fz",&fz)) fz=0.;
    if (!getparint("ivcon",&ivcon)) ivcon=1;
    if (!getparint("ismzv",&ismzv)) ismzv=1;
    if (!getparint("ismxv",&ismxv)) ismxv=1;
    if (!getparint("ismzg",&ismzg)) ismzg=1;
    if (!getparint("ismxg",&ismxg)) ismxg=1;
    if (!getparint("invtyp",&invtyp)) invtyp=0;
    if (invtyp==0 && ivcon==1) {
	otvtyp = 1;
    } else if (invtyp==1 && ivcon==1) {
	otvtyp = 0;
    } else if (ivcon==0) {
	otvtyp = invtyp;
    }	 
    getparstring("vghdr",&vghdr);
    getparstring("dvdzghdr",&dvdzghdr);
    nx = ncdp;

    ismzg=(ismzg/2)*2+1;
    ismxg=(ismxg/2)*2+1;
    if(ismzg>nz) ismzg=nz;
    if(ismxg>nx) ismxg=nx;
    ismzv=(ismzv/2)*2+1;
    ismxv=(ismxv/2)*2+1;
    if(ismzv>nz) ismzv=nz;
    if(ismxv>nx) ismxv=nx;
	
/* memory allocation */
    /* at most 128 cards with at most 256 depth-vel pairs each */
    n1 = 128;
    n2 = 256;

    /* cdpsdvdz is used to store all DVDZ card's cdp locations */
    cdpsdvdz = (int*)malloc(n2*sizeof(int));
    npsdvdz = (int*)malloc(n2*sizeof(int));
    ztops = (float*)malloc(n1*n2*sizeof(float));
    zbots = (float*)malloc(n1*n2*sizeof(float));
    dvdzs = (float*)malloc(n1*n2*sizeof(float));

    z = (float*)malloc(nz*sizeof(float));

    dvdztb = (float*)malloc(nz*n2*sizeof(float));
    dvdzzx = (float*)malloc(nz*nx*sizeof(float));
  
    /* arrays for sorting input cards in cdp ascending order */
    sortindex = (int*)malloc(n2*sizeof(int));
    cdpsort = (float*)malloc(n2*sizeof(float));
    cdps = (float*)malloc(n2*sizeof(float));
    sorts = (float*)malloc(nz*n2*sizeof(float));

    if(ismzg>1 || ismxg>1) {
    	work = (float*)malloc(nz*nx*sizeof(float));
    	smz = (float*)malloc(ismzg*sizeof(float));
    	smx = (float*)malloc(ismxg*sizeof(float));
    }

    /* compute depth of output */
    for(iz=0;iz<nz;iz++) z[iz] = fz + iz*dz;

    /* read in DVDZ and VELO cards to a temp file */
    tmpfp = etempfile(NULL);
    buf = (char*) malloc(81*sizeof(char));
    do {
	bzero(buf,81);
	fgets(buf, 81, infp); 
	buf[80] = '\n';
	fwrite(buf, 1, 81, tmpfp);
    } while(!feof(infp));
    free(buf);


    /* read in DVDZ cards */
    dvdzread(tmpfp, cdpsdvdz, ztops, zbots, dvdzs, &ncdpdvdz, npsdvdz, n1, n2);
    /*
    for(i=0;i<ncdpdvdz;i++) {
	for(iz=0;iz<npsdvdz[i];iz++)
		fprintf(stderr,"zt=%6.0f zb=%6.0f dvdz=%6.3f cdp=%d \n",
		ztops[iz+i*n1],zbots[iz+i*n1],dvdzs[iz+i*n1],cdpsdvdz[i]);
    }
    */

    /* compute gradient at z for all DVDZ locations */
    for(i=0;i<ncdpdvdz;i++) {
	indx = i * n1;		
	nps = npsdvdz[i];
    	dvdzint(ztops+indx,zbots+indx,dvdzs+indx,nps,z,dvdztb+i*nz,nz);
    }

    /* sort input dvdz in cdp-ascending order */
    for(i=0;i<ncdpdvdz;i++) {
		sortindex[i] = i;
		cdps[i] = cdpsdvdz[i];
    }
    if(ncdpdvdz>0) qkisort(ncdpdvdz,cdps,sortindex);
    for(i=0;i<ncdpdvdz;i++) {
	cdpsort[i] = cdps[sortindex[i]];
	for(iz=0;iz<nz;iz++) sorts[iz+i*nz] = dvdztb[iz+sortindex[i]*nz];
    }
    for(i=0;i<ncdpdvdz;i++) {
	cdps[i] = cdpsort[i];
	for(iz=0;iz<nz;iz++) dvdztb[iz+i*nz] = sorts[iz+i*nz];
    }

    /* interpolate dvdz to every cdp location */
    if(ncdpdvdz==0) {
	bzero(dvdzzx,nz*nx*sizeof(float));
	indvdz = 0;
    } else {
	indvdz = 1;
	/* main loop over output cdp positions */
        for(icdp=0;icdp<ncdp;icdp++) {
        /* get dvdz at output cdp location from dvdztb */
        	fcdp = cdpmin+icdp*dcdp;
        	if ( ncdpdvdz < 2 ) {
         		i1 = 0; i2=0; res=0.;
        	} else {
           	/* search cdp index */
           		nn = 1;
           		bisear_(&ncdpdvdz,&nn,cdps,&fcdp,&incdp);
           	/* linear interpolation */
           		if (incdp < 1 || fcdp < cdps[0] ) {
              			i1 = 0; i2 = 0; res = 0.;
           		} else if(incdp >= ncdpdvdz) {
              			i1 = ncdpdvdz-1; i2 = ncdpdvdz-1; res = 0.;
           		} else {
              			i1 = incdp-1;
              			i2 = incdp;
              			res =(fcdp-cdps[i1])/(cdps[i2]-cdps[i1]);
           		}
        	}
        	for(iz=0;iz<nz;iz++)
           		dvdzzx[iz+icdp*nz] = dvdztb[i1*nz+iz] +
                        	res*(dvdztb[i2*nz+iz]-dvdztb[i1*nz+iz]);

    	}
    }

    /* smoothing if needed */
    if(ismzg>1 || ismxg >1) {
	smth2d_(dvdzzx,work,smz,smx,&nz,&nx,&ismzg,&ismxg);
    	free(smz);
    	free(smx);
    	free(work);
    }
    free(dvdztb);
    free(dvdzs);
    free(ztops);
    free(zbots);
    free(npsdvdz);
    free(cdpsdvdz);

    if(ismzv>1 || ismxv>1) {
    	work = (float*)malloc(nz*nx*sizeof(float));
    	smz = (float*)malloc(ismzv*sizeof(float));
    	smx = (float*)malloc(ismxv*sizeof(float));
    }
 	
    /* cdpsvelo is used to store all VELO card's cdp locations */
    cdpsvelo = (int*)malloc(n2*sizeof(int));
    npsvelo = (int*)malloc(n2*sizeof(int));
    zpicks = (float*)malloc(n1*n2*sizeof(float));
    vpicks = (float*)malloc(n1*n2*sizeof(float));

    velotb = (float*)malloc(nz*n2*sizeof(float));
    vzx = (float*)malloc(nz*nx*sizeof(float));

    /* read in VELO cards */
    veloread(tmpfp, cdpsvelo, zpicks, vpicks, &ncdpvelo, npsvelo, n1, n2);
    /*
    for(i=0;i<ncdpvelo;i++) {
	for(iz=0;iz<npsvelo[i];iz++)
		fprintf(stderr,"z=%6.0f v=%6.0f cdp=%d \n",
			zpicks[iz+i*n1],vpicks[iz+i*n1],cdpsvelo[i]);
    }
    */
    
    if (ncdpvelo==0) err("No VELO card input ! ");

    /* compute interval velocity at z for all VELO locations */
    for(i=0;i<ncdpvelo;i++) {
 	indx = i * n1;		
	nps = npsvelo[i];
	idvdz = (cdpsvelo[i]-cdpmin)/dcdp;
	if(idvdz<0) {
		idvdz = 0;
	} else if ( idvdz>ncdp-1) {
		idvdz = ncdp-1;
	}
        vconint(zpicks+indx,vpicks+indx,nps,z,velotb+i*nz,nz,
		invtyp,otvtyp,dvdzzx+idvdz*nz); 
    }

    /* sort input velo in cdp-ascending order */
    for(i=0;i<ncdpvelo;i++) {
		sortindex[i] = i;
		cdps[i] = cdpsvelo[i];
    }
    qkisort(ncdpvelo,cdps,sortindex);
    for(i=0;i<ncdpvelo;i++) {
	cdpsort[i] = cdps[sortindex[i]];
	for(iz=0;iz<nz;iz++) sorts[iz+i*nz] = velotb[iz+sortindex[i]*nz];
    }
    for(i=0;i<ncdpvelo;i++) {
	cdps[i] = cdpsort[i];
	for(iz=0;iz<nz;iz++) velotb[iz+i*nz] = sorts[iz+i*nz];
    }

    /* free sort arrays */
    free(sorts);
    free(sortindex);
    free(cdpsort);
    free(cdpsvelo);
    
    /* main loop over output traces */
    for(icdp=0;icdp<ncdp;icdp++) {
		/* get velocity at output cdp location from velotb */
        	fcdp = cdpmin+icdp*dcdp;
		if ( ncdpvelo < 2 ) {
	   		i1 = 0; i2=0; res=0.;
		} else {
           	/* search cdp index */
	   		nn = 1;
	   		bisear_(&ncdpvelo,&nn,cdps,&fcdp,&incdp);
	   	/* linear interpolation */
	   		if (incdp < 1 || fcdp < cdps[0] ) {
	      			i1 = 0; i2 = 0; res = 0.;
	   		} else if(incdp >= ncdpvelo) {
	      			i1 = ncdpvelo-1; i2 = ncdpvelo-1; res = 0.;
           		} else {
	      			i1 = incdp-1; 
	      			i2 = incdp; 
	      			res =(fcdp-cdps[i1])/(cdps[i2]-cdps[i1]);
           		}
		}
/*
	fprintf(stderr,"fcdp=%f incdp=%d i1=%d i2=%d \n",fcdp,incdp,i1,i2);
*/
		for(iz=0;iz<nz;iz++) 
	   		vzx[iz+icdp*nz] = velotb[i1*nz+iz] + 
			    	res*(velotb[i2*nz+iz]-velotb[i1*nz+iz]);
    }
    free(velotb);

    if(ismzv>1 || ismxv >1) smth2d_(vzx,work,smz,smx,&nz,&nx,&ismzv,&ismxv);

    /* prepare grid header */
    bzero( (char*)&gh,GHDRBYTES);
    o1 = fz;
    ocdp2 = cdpmin;
    d1 = dz;
    dcdp2 = dcdp;
    n1 = nz;
    n2 = ncdp;
    toghdr(&gh,&scale,&dtype,&n1,&n2,&n3,&n4,&n5,
		&d1,&d2,&d3,&d4,&d5,&o1,&o2,&o3,&o4,&o5,
                &dcdp2,&dline3,&ocdp2,&oline3,&gmin,&gmax,&orient,&gtype);

    /* output dvdz grid and header if needed */
    if(indvdz==1 && getparstring("dvdzgrid",&dvdzgrid)){
	dvdzfp = fopen(dvdzgrid,"w");
    	fwrite(dvdzzx,sizeof(float),nz*nx,dvdzfp);
	/* find max and min grid values */
	fminmax(dvdzzx,nz*nx,&gmin,&gmax);
	/* update header */
	putgval(&gh,"gmin",gmin);
	putgval(&gh,"gmax",gmax);
        /* add header to dvdzgrid */
        fflush(dvdzfp);        
	fputghdr(dvdzfp,&gh);
    	fclose(dvdzfp);
	dvdzhfp = fopen(dvdzghdr,"w");
	fprintf(dvdzhfp,"header file for dvdz grid \n");
	fprintf(dvdzhfp,"fz=%f dz=%f nz=%d\n",fz,dz,nz);
	fprintf(dvdzhfp,"fcdp=%d dcdp=%d ncdp=%d\n",cdpmin,dcdp,ncdp);
	fprintf(dvdzhfp,"gmin=%g gmax=%g\n",gmin,gmax);
	fclose(dvdzhfp);
    }
    free(dvdzzx);

    /* output velocity grid and header */
    fwrite(vzx,sizeof(float),nz*nx,outfp);
    /* add header to dvdzgrid */
    fflush(outfp);        
    /* find max and min grid values */
    fminmax(vzx,nz*nx,&gmin,&gmax);
    /* update header */
    putgval(&gh,"gmin",gmin);
    putgval(&gh,"gmax",gmax);
    fputghdr(outfp,&gh); 
    fclose(outfp);
    vhfp = fopen(vghdr,"w");
    fprintf(vhfp,"header file for velocity grid \n");
    fprintf(vhfp,"fz=%f dz=%f nz=%d\n",fz,dz,nz);
    fprintf(vhfp,"fcdp=%d dcdp=%d ncdp=%d\n",cdpmin,dcdp,ncdp);
    fprintf(vhfp,"gmin=%g gmax=%g\n",gmin,gmax);
    fclose(vhfp);

    free(vzx);
    if(ismzv>1 || ismxv >1) {
    	free(work);
    	free(smz);
    	free(smx);
    }
    free(vzx);
    free(cdps);

    return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
main(int argc, char **argv)
{
    	int nt,nxi,nxo,ntfft,it,ix,nf,i,extrap;
	int nxipre;
    	float dt,fmin,fmax;
    	float *xi, *xo, *xipre;
    	float *yi, *yo;
    	char *hdrs;
    	float tol;
    	int niter, np;
    	string datain, dataout;
	complex *ccexp, *ccexpo;
	int iccexp=1, iccexpo=1;
	int nxip, ifmin, lftwk, npp, nph;
	float tmp, tmp2;
	float df; 

    	FILE *infp,*outfp;

	segytrace tr;

	String pkey="tracl", ptype, skey="tracr", stype;
     	Value pval, sval;
       	int indxp, indxs;

	int ofill=0, dfill=1, nfill=1;
	int iof=1, inf=1;
	int nsmax, ns; 
	int is, ip, ipre;


	float *sort;
	int *sortindex;
	char *hdrsort;



    	/* get parameters */
    	initargs(argc,argv);
    	askdoc(1);

	getparstring("pkey",&pkey);
	getparstring("skey",&skey);
	if(!getparint("ofill",&ofill)) iof = 0 ;
        if(!getparint("dfill",&dfill)) dfill = 1;
       	if(!getparint("nfill",&nfill))inf = 0;
     	if (!getparint("nsmax",&nsmax)) nsmax = 2000;

	/* open input/output */
    	if (!getparstring("datain",&datain)) {
		infp = stdin;
	} else {
		infp = fopen(datain,"r");
	}
    	if (!getparstring("dataout",&dataout)) {
		outfp = stdout;
	} else {
		outfp = fopen(dataout,"w");
	}
	/* make file size to be able to exceed 2 G on convex */
	file2g(infp);
	file2g(outfp);

	/* read in first trace for nt and dt */
        if (!fgettr(infp,&tr))  err("can't get first trace");
	nt = tr.ns; 
	dt = (float)tr.dt/1000000.;

	/* optional parameters */
    	if (!getparint("ntfft",&ntfft)) ntfft=(nt*3/2)/2*2;
    	if (ntfft < nt) ntfft = nt;
	nf = ntfft;
	radix_(&nf,&ntfft);
    	if (!getparfloat("fmin",&fmin)) fmin = 0.;
    	if (!getparfloat("fmax",&fmax)) fmax = .5 / dt * 2. / 3.;
    	if (!getparint("niter",&niter)) niter = 10; 
    	if (!getparfloat("tol",&tol)) tol = 0.000001;
    	if (!getparint("extrap",&extrap)) extrap = 1; 


        ptype  = hdtype(pkey);
	indxp = getindex(pkey);
	stype  = hdtype(skey);
	indxs = getindex(skey);
	gethval(&tr, indxp, &pval);
	ipre = vtoi(ptype,pval);
	gethval(&tr, indxs, &sval);
	is = vtoi(stype,sval);

	nxo = nfill;
	if(inf==0) nxo = nsmax;

    	xi = (float*)malloc(nsmax*sizeof(float));
    	xipre = (float*)malloc(nsmax*sizeof(float));
    	yi = (float*)malloc(nt*nsmax*sizeof(float));
    	xo = (float*)malloc(nxo*sizeof(float));
    	yo = (float*)malloc(nt*nxo*sizeof(float));
	hdrs = (char*) malloc(nsmax*HDRBYTES*sizeof(char));

	sort = (float*)malloc(nsmax*nt*sizeof(float));
	hdrsort = (char*)malloc(nsmax*HDRBYTES);
	sortindex = (int*)malloc(nsmax*sizeof(int));

	iccexp = 1;
	nxip = nsmax + 10;
    	initpf_(&ntfft,&dt,&fmin,&fmax,&nxip,&ifmin,&df,
		&nf,&lftwk,&npp,&nph);
	tmp = (nph*2+1)*nf*nxip;
	tmp = tmp*sizeof(complex);

	if( tmp > 1600000000. ) {
		fprintf(stderr," --- forward t-p coefficients --- " );
		fprintf(stderr," need memory %f MB \n",tmp/1000000.);
		fprintf(stderr," memory limit exceeded \n");
		fprintf(stderr," no pre-computation of coefficients \n");
		iccexp = -1; 
		ccexp = (complex*) malloc(sizeof(complex));
		tmp = 0.;
	} else {
		ccexp = (complex*) malloc((nph*2+1)*nf*nxip*sizeof(complex));
	}

	iccexpo = 1;
	tmp2 = (nph*2+1)*nf*nxo;
	tmp2 = tmp2*sizeof(complex);
	tmp = tmp + tmp2;
	if( tmp > 1600000000. ) {
		fprintf(stderr," --- inverse t-p coefficients --- " );
		fprintf(stderr," need memory %f MB \n",tmp/1000000.);
		fprintf(stderr," memory limit exceeded \n");
		fprintf(stderr," no pre-computation of coefficients \n");
		iccexpo = -1; 
		ccexpo = (complex*) malloc(sizeof(complex));
	} else {
		ccexpo = (complex*) malloc((nph*2+1)*nf*nxo*sizeof(complex));
	}

   	for(ix=0;ix<nxo;ix++) xo[ix] = ofill + ix*dfill;

	/* loop over input traces */
	ns = 0;
	nxipre = 0;

	do {

	
                gethval(&tr, indxp, &pval);
		ip = vtoi(ptype,pval);
		gethval(&tr, indxs, &sval);
		is = vtoi(stype,sval);

		if(ns>nsmax) 
			err("maximum number traces %d exceed %d \n",ns,nsmax);

		if(ip==ipre) {

			for(it=0;it<nt;it++) yi[it+ns*nt] = tr.data[it];
			xi[ns] = is;
			bcopy((char*)&tr,hdrs+ns*HDRBYTES,HDRBYTES);
			ns = ns + 1;

		} else if(ip!=ipre && ns>0) {

			nxi = ns;
			/* sort xi into ascending order */
			for(i=0;i<nxi;i++) sortindex[i] = i;
			qkisort(nxi,xi,sortindex);
			bcopy(yi,sort,nxi*nt*sizeof(float));

			for(i=0;i<nxi;i++) {
               			bcopy(sort+sortindex[i]*nt,yi+i*nt,
					nt*sizeof(float));
			}

			bcopy(xi,sort,nxi*sizeof(float));
			for(i=0;i<nxi;i++) {
               			xi[i] = sort[sortindex[i]];
			}

			bcopy(hdrs,hdrsort,nxi*HDRBYTES);
			for(i=0;i<nxi;i++) {
			    bcopy(hdrsort+sortindex[i]*HDRBYTES,
				hdrs+i*HDRBYTES,HDRBYTES);
			}

			np = nxi;

			if(inf==0) nxo = nxi;
			if(iof==0) {
				for(ix=0;ix<nxo;ix++) xo[ix] = xi[ix];
			}

			/* tau-p interpolation */
			intps(xi,yi,xo,yo,nxi,nxo,
				nt,dt,np,fmin,fmax,ntfft, 
				niter,tol,xipre,nxipre,ccexp,iccexp,
				ccexpo,iccexpo);
			for(ix=0;ix<nxi;ix++) {
				xipre[ix] = xi[ix];
			}
			nxipre = nxi;


			/* output gather */
			intout(xi,xo,yo,hdrs,nxi,nxo,nt,
				outfp,extrap,stype,indxs);

			fprintf(stderr,
" interpolation done from input %d live traces to output %d traces at %s=%d \n",nxi, nxo, pkey, ipre);

			ns = 0;
			for(it=0;it<nt;it++) yi[it+ns*nt] = tr.data[it];
			xi[ns] = is;
			bcopy((char*)&tr,hdrs+ns*HDRBYTES,HDRBYTES);

			ipre = ip;
			ns = ns + 1;
		}
			
	} while(fgettr(infp,&tr)); 
			

	if (ns>0) {

		nxi = ns;

		for(i=0;i<nxi;i++) sortindex[i] = i;
		qkisort(nxi,xi,sortindex);
		bcopy(yi,sort,nxi*nt*sizeof(float));

		for(i=0;i<nxi;i++) {
       			bcopy(sort+sortindex[i]*nt,yi+i*nt,
			nt*sizeof(float));
		}
		bcopy(xi,sort,nxi*sizeof(float));
		for(i=0;i<nxi;i++) {
               		xi[i] = sort[sortindex[i]];
		}
		bcopy(hdrs,hdrsort,nxi*HDRBYTES);
		for(i=0;i<nxi;i++) {
		    bcopy(hdrsort+sortindex[i]*HDRBYTES,
			hdrs+i*HDRBYTES,HDRBYTES);
		}

		np = nxi;

		if(inf==0) nxo = nxi;
		if(iof==0) {
			for(ix=0;ix<nxo;ix++) xo[ix] = xi[ix];
		}

		/* interpolation */
                intps(xi,yi,xo,yo,nxi,nxo,nt,dt,np,fmin,fmax,ntfft,niter,tol,
			xipre,nxipre,ccexp,iccexp,
			ccexpo,iccexpo);
		/* output gather */
		intout(xi,xo,yo,hdrs,nxi,nxo,nt,outfp,extrap,stype,indxs);

		fprintf(stderr,
" interpolation done from input %d live traces to output %d traces at %s=%d \n",nxi, nxo, pkey, ip);

	}


    	free(xi);
    	free(yi);
    	free(xo);
    	free(yo);
	free(hdrs);
	free(sort);
	free(sortindex);
	free(hdrs);

	return 0;

}