コード例 #1
0
ファイル: headersort.c プロジェクト: krushev36/src
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);
}
コード例 #2
0
ファイル: parallel.c プロジェクト: housian0724/src
static void sizes(sf_file file, int axis, int ndim, 
		  const off_t *n, off_t *size1, off_t *size2)
{
    int i;

    *size1 = sf_esize(file);
    *size2 = 1;
    for (i=0; i < ndim; i++) {
	if      (i < axis) *size1 *= n[i];
	else if (i > axis) *size2 *= n[i];
    }
}
コード例 #3
0
ファイル: Moshift1.c プロジェクト: 1014511134/src
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);
}
コード例 #4
0
ファイル: Mnorm2.c プロジェクト: 1014511134/src
int main(int argc, char* argv[])
{
    sf_file in=NULL;
    char buf[BUFSIZ];
    off_t n[SF_MAX_DIM], nsiz;
    size_t i, nbuf, nleft, dim;
    size_t bufsiz;
    float f;
    double f_double, c_real, c_imag;
    double fsqr;
    sf_complex c;
    sf_datatype type;

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

    dim = (size_t) sf_largefiledims (in,n); /* Vector with cube dimensions */

    /* Total number of elements in cube as product of dimensions */
    for (nsiz=1, i=0; i < dim; i++) {
	nsiz *= n[i];
    }

    bufsiz = BUFSIZ / sf_esize(in); /* Nr of elements in buffer */
    type = sf_gettype (in);
    fsqr = 0; /* Summation result */

    /* A little bit of code duplication in order to avoid testing the type
    for every single value in the dataset. The clean way to do this would be
    to have a function that takes another function as an argument */

    switch (type) {
        case SF_FLOAT:
            for (nleft=nsiz; nleft > 0; nleft -= nbuf) {
                nbuf = (bufsiz < nleft)? bufsiz: nleft;
                /* Read nbuf elements from "in" into buf. */
                sf_floatread((float*) buf,   nbuf, in);
                for (i=0; i < nbuf; i++) {
                    f        = ((float*)buf)[i];
                    f_double = (double) f;
                    fsqr += f_double*f_double;
                }
            }
        break;
        case SF_COMPLEX:
            for (nleft=nsiz; nleft > 0; nleft -= nbuf) {
                nbuf = (bufsiz < nleft)? bufsiz: nleft;
                /* Read nbuf elements from "in" into buf. */
                sf_complexread((sf_complex*) buf,  nbuf,in);
                for (i=0; i < nbuf; i++) {
                    c = ((sf_complex*)buf)[i];
                    c_real = (double) crealf(c);
                    c_imag = (double) cimagf(c);
                    fsqr += c_real*c_real + c_imag*c_imag;
                }
            }
        break;
	default:
	    sf_error("Bad type %d",type);
	    break;
    }

    printf("%1.15E\n", fsqr);

    exit (0);
}
コード例 #5
0
ファイル: Maxplusy.c プロジェクト: 1014511134/src
int main(int argc, char* argv[])
{
    sf_file in=NULL, yfile=NULL; /* Inputs */
    sf_file out=NULL; /* Output */
    sf_file fa=NULL; /*optional input file */
    char buf_x[BUFSIZ], buf_y[BUFSIZ], buf_out[BUFSIZ]; /* I/O memory buffers */
    off_t n[SF_MAX_DIM], nsiz; /* Dims of cube, total nr elems */
    size_t i, nbuf, nleft, dim;
    size_t bufsiz;
    double a;
    float af;
    double x, y, sum; /* For the float case */
    double sum_r, sum_i, x_r, x_i, y_r, y_i; /* For the sf_complex case */
    sf_complex x_c_sp, y_c_sp; /* Single precision */
    sf_datatype type;
    bool verb;

    sf_init (argc,argv);

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

    if (!sf_getdouble("a",&a)) a=1; /* Scaling factor */
    if(a==1) {
      fa = sf_input("afile");
      sf_floatread((float*)&af,1,fa   );
      a = (double)af; 
    }  






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

    dim = (size_t) sf_largefiledims (in,n); /* Vector with cube dimensions */

    /* Total number of elements in cube as product of dimensions */
    for (nsiz=1, i=0; i < dim; i++) {
	nsiz *= n[i];
    }

    bufsiz = BUFSIZ / sf_esize(in); /* Nr of elements in buffer */
    type = sf_gettype (in);

    for (nleft=nsiz; nleft > 0; nleft -= nbuf) {
	nbuf = (bufsiz < nleft)? bufsiz: nleft;

        if (type == SF_FLOAT) {
	    sf_floatread((float*) buf_x,nbuf,in   );
            sf_floatread((float*) buf_y,nbuf,yfile);
            for (i=0; i < nbuf; i++) {
                x = ((float*)buf_x)[i];
                y = ((float*)buf_y)[i];
                sum = x * a + y;
                if(verb) sf_warning("Double precision: %1.15E", sum);
                ((float*)buf_out)[i] = (float)sum;
            }
            sf_floatwrite((float*) buf_out, nbuf, out);
        }

        else if (type == SF_COMPLEX) {
	    sf_complexread((sf_complex*) buf_x,nbuf,in);
            sf_complexread((sf_complex*) buf_y,nbuf,yfile);
            for (i=0; i < nbuf; i++) {
                x_c_sp = ((sf_complex*)buf_x)[i];
                y_c_sp = ((sf_complex*)buf_y)[i];
                x_r = (double)crealf(x_c_sp);
                y_r = (double)crealf(y_c_sp);
                x_i = (double)cimagf(x_c_sp);
                y_i = (double)cimagf(y_c_sp);
                sum_r = a * x_r + y_r;
                sum_i = a * x_i + y_i;
                if(verb) sf_warning("Double precision: %1.15E + %1.15E i", sum_r, sum_i);
                ((float*)buf_out)[2*i  ] = (float)sum_r;
                ((float*)buf_out)[2*i+1] = (float)sum_i;
            }
            sf_floatwrite((float*) buf_out, 2*nbuf, out);
	}
    }

    exit (0);
}
コード例 #6
0
ファイル: rsf_read.c プロジェクト: 1014511134/src
void mexFunction(int nlhs, mxArray *plhs[], 
		 int nrhs, const mxArray *prhs[])
{
    int taglen, status, argc=2, dim, n[SF_MAX_DIM], i, esize, len;
    size_t nbuf = BUFSIZ, nd, j;
    char *tag, *argv[] = {"matlab","-"}, *par;
    double *pr, *pi=NULL;
    char buf[BUFSIZ];
    off_t pos;
    static off_t shift=0;
    bool same;
    sf_datatype type;
    sf_file file;

    /* Check for proper number of arguments. */
    if (nrhs < 2 || nrhs > 3) mexErrMsgTxt("Two or three inputs required.");

    /* Second input must be a string. */
    if (!mxIsChar(prhs[1]))
	mexErrMsgTxt("Second input must be a string.");

    /* Second input must be a row vector. */
    if (mxGetM(prhs[1]) != 1)
	mexErrMsgTxt("Second input must be a row vector.");

    /* Get the length of the input string. */
    taglen = mxGetN(prhs[1]) + 1;

    /* Allocate memory for input string. */
    tag = mxCalloc(taglen, sizeof(char));

    /* Copy the input filename into a C string. */
    status = mxGetString(prhs[1], tag, taglen);
    if (status != 0) 
	mexWarnMsgTxt("Not enough space. String is truncated.");

    if (3 == nrhs) {
        /* Input 3 must be a string. */
	if (!mxIsChar(prhs[2]))
	    mexErrMsgTxt("Input 3 must be a string.");

	/* Input 3 must be a row vector. */
	if (mxGetM(prhs[2]) != 1)
	    mexErrMsgTxt("Input 3 must be a row vector.");

	/* Get the length of the input string. */
	len = mxGetN(prhs[2]) + 1;

	/* Allocate memory for input string. */
	par = mxCalloc(len, sizeof(char));

	/* Copy the string data from prhs[2] into a C string. */
	status = mxGetString(prhs[2], par, len);
	if (status != 0) 
	    mexWarnMsgTxt("Not enough space. String is truncated.");

	same = (0 == (strncmp(par,"same",4)));
    } else {
	same = false;
    }

    sf_init(argc,argv);
    file = sf_input(tag);

    dim = sf_filedims(file,n);
    type = sf_gettype (file);
    esize = sf_esize(file);

    /* data pointers */
    pr = mxGetPr(prhs[0]);

    /* get data size */
    nd = mxGetNumberOfElements(prhs[0]);

    pos = sf_tell(file);
    if (same) sf_seek(file,shift,SEEK_CUR);

    for (j=0, nbuf /= esize; nd > 0; nd -= nbuf) {
	if (nbuf > nd) nbuf=nd;

	switch(type) {
	    case SF_FLOAT:
		if (!mxIsDouble(prhs[0])) mexErrMsgTxt("First input must be double.");

		sf_floatread((float*) buf,nbuf,file);
		for (i=0; i < nbuf; i++, j++) {
		    pr[j] = (double) ((float*) buf)[i];
		}
		break;
	    case SF_INT:
		if (!mxIsDouble(prhs[0])) mexErrMsgTxt("First input must be double.");

		sf_intread((int*) buf,nbuf,file);
		for (i=0; i < nbuf; i++, j++) {
		    pr[j] = (double) ((int*) buf)[i];
		}
		break;
	    case SF_COMPLEX:
		if(!mxIsComplex(prhs[0])) mexErrMsgTxt("First input must be complex.");

		pi = mxGetPi(prhs[0]);

		sf_complexread((sf_complex*) buf,nbuf,file);
		for (i=0; i < nbuf; i++, j++) {
		    pr[j] = (double) crealf(((sf_complex*) buf)[i]);
		    pi[j] = (double) cimagf(((sf_complex*) buf)[i]);
		}
		break;
	    default:
		mexErrMsgTxt("Unsupported file type.");
		break;
	}
    }

    shift = sf_tell(file) - pos;
    sf_close();
}
コード例 #7
0
ファイル: Mshot2cmp3.c プロジェクト: 1014511134/src
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);
}
コード例 #8
0
ファイル: Mboolcmp.c プロジェクト: krushev36/src
int main(int argc, char *argv[])
{
    off_t n[SF_MAX_DIM],n_r[SF_MAX_DIM], nsiz,nsiz_r=0,nleft;
    int qq[BUFSIZ];
    char buf[BUFSIZ],buf_r[BUFSIZ],*right=0,*sign;
    float eps,fl=0,fr;
    size_t bufsiz=BUFSIZ,dim,dim_r,i,nbuf;
    sf_complex c;
    sf_file in,in_r=0,out;
    sf_datatype type;
    bool cmp_num=false;

    sf_init(argc,argv);

    cmp_num = sf_getfloat("right_f",&fr);
    /* compare input (left) to a single float value (right) */

    if (!cmp_num && NULL == (right=sf_getstring("right"))) sf_error("No right or right_f parameter set.");
    /* the rsf file you will be comparing to */

    if (NULL == (sign=sf_getstring("sign"))) sign="eq";
    /* 'eq'(default),'gt','ge','lq','lt','ne'
        sign=   'eq' equal-to ( == )
        sign=   'gt' greater-than ( > )
        sign=   'ge' greater-than or equal-to ( >= )
        sign=   'lq' less-than or equal-to ( <= )
        sign=   'lt' less-than ( < )
        sign=   'ne' not-equal ( != )
    sign=   'and' the values are both non-zero ( && )
    sign=   'or' one value is non-zero ( !! )
    */

    if (!sf_getfloat("eps",&eps)) eps=0;
    /* comparing within this range epsilon */

    in = sf_input("in");
    out = sf_output("out");
    sf_settype(out,SF_INT);

    dim = sf_largefiledims(in,n);
    for (nsiz=1, i=0; i < dim; i++) nsiz *= n[i];

    if (!cmp_num) {
        in_r = sf_input(right);
        dim_r = (size_t) sf_largefiledims(in_r,n_r);
        for (nsiz_r=1, i=0; i < dim_r; i++) nsiz_r *= n_r[i];
    }

    bufsiz /= sf_esize(in);
    type = sf_gettype(in);

    if (!cmp_num && type != sf_gettype(in_r)) sf_error("Type of input and right files do not match.");
    if (!cmp_num && nsiz != nsiz_r) sf_error("Size of input and right files do not match.");


    for (nleft=nsiz; nleft>0; nleft -= nbuf) {
        nbuf = (bufsiz < nleft)? bufsiz: nleft;
        switch (type) {
        case SF_FLOAT:
            sf_floatread((float*) buf,nbuf,in);
            if (!cmp_num) sf_floatread((float*) buf_r,nbuf,in_r);
            break;
        case SF_INT:
            sf_intread((int*) buf,nbuf,in);
            if (!cmp_num) sf_intread((int*) buf_r,nbuf,in_r);
            break;
        case SF_COMPLEX:
            sf_complexread((sf_complex*) buf,nbuf,in);
            if (!cmp_num) sf_complexread((sf_complex*) buf_r,nbuf,in_r);
            break;
        default:
            sf_error("Type not understood.");
            break;
        }
        for (i=0; i<nbuf; i++) {
            switch (type) {
            case SF_FLOAT:
                fl = ((float*)buf)[i];
                if (!cmp_num) fr = ((float*)buf_r)[i];
                break;
            case SF_INT:
                fl = (float) ((int*)buf)[i];
                if (!cmp_num) fr = (float) ((int*)buf_r)[i];
                break;
            case SF_COMPLEX:
                c=((sf_complex*)buf)[i];
                fl=cabsf(c);
                if (!cmp_num) {
                    c=((sf_complex*)buf_r)[i];
                    fr=cabsf(c);
                }
                break;
            default:
                sf_error("Type not understood.");
                break;
            }

            if      (0==strcmp(sign,"ge")) qq[i] = ((fl-fr) >= -eps);
            else if (0==strcmp(sign,"gt")) qq[i] = ((fl-fr) > -eps);
            else if (0==strcmp(sign,"eq")) qq[i] = (fabs(fl-fr) <= eps);
            else if (0==strcmp(sign,"lt")) qq[i] = ((fl-fr) < eps);
            else if (0==strcmp(sign,"lq")) qq[i] = ((fl-fr) <= eps);
            else if (0==strcmp(sign,"ne")) qq[i] = (fabs(fl-fr) > eps);
            else if (0==strcmp(sign,"and")) qq[i] = ((fabs(fl) > eps) && (fabs(fr) > eps));
            else if (0==strcmp(sign,"or")) qq[i] = ((fabs(fl) > eps) || (fabs(fr) > eps));
            else sf_error("Sign not recognized. Please specify: gt,ge,eq,lq,lt,ne,and,or");
        }
        sf_intwrite(qq,nbuf,out);
    }

    exit(0);
}
コード例 #9
0
ファイル: file.c プロジェクト: liumch/src
void sf_settype (sf_file file, sf_datatype type)
/*< set file type >*/
{
    file->type = type;
    if (NULL != file->dataname) sf_putint(file,"esize",(int) sf_esize(file));
}
コード例 #10
0
ファイル: rotate.c プロジェクト: 1014511134/src
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);
}
コード例 #11
0
ファイル: pad.c プロジェクト: 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);
}
コード例 #12
0
ファイル: in.c プロジェクト: krushev36/src
int main (int argc, char* argv[])
{
    int i, j, ncheck, esize, nin, dim=SF_MAX_DIM;
    off_t nj, size, n[SF_MAX_DIM];
    float check, fj;
    char *label, *dataname, key[8], *val;
    const char **filename;
    char buf[BUFSIZ], zero[BUFSIZ];
    sf_file file;
    bool info, trail;
    const char *type[] = {"uchar","char","int","float","complex","short"};
    const char *form[] = {"ascii","xdr","native"};
    char pad[] = "              ", out[25];

    sf_init (argc,argv);

    filename = (const char**) sf_alloc ((size_t) argc,sizeof(char*));

    if (!sf_getbool ("info",&info)) info = true;
    /* If n, only display the name of the data file. */
    if (!sf_getfloat ("check",&check)) check = 2.;
    /* Portion of the data (in Mb) to check for zero values. */
    check *= (1024. * 1024.); /* convert Mb to b */
    ncheck = (int) check;

    if (!sf_getbool("trail",&trail)) trail=true;
    /* If n, skip trailing dimensions of  one */

    memset(zero,0,BUFSIZ);

    if (!sf_stdin()) { /* no input file in stdin */
	nin=0;
    } else {
	filename[0] = "in";
	nin=1;
    }

    for (i = 1; i < argc; i++) {
	if (NULL != strchr (argv[i], '=')) 
	  continue; /* not a file */

	filename[nin] = argv[i];
	nin++;
    }
    if (0==nin) sf_error ("no input");
	
    for (i = 0; i < nin; i++) {
	file = sf_input (filename[i]);
	dataname = sf_histstring(file,"in");

	if (!info) {
	    printf("%s ",dataname);
            sf_fileclose(file);
	    continue;
	}

	printf ("%s:\n", filename[i]);
	printf ("%sin=\"%s\"\n",pad+10,dataname);

	if (sf_histint(file,"esize",&esize)) {
	    printf ("%sesize=%d ",pad+10,esize);
	} else {
	    esize = sf_esize(file);
	    printf ("%sesize=%d? ",pad+10,esize);
	}
	printf("type=%s form=%s ",
	       type[sf_gettype(file)],
	       form[sf_getform(file)]);

	if (NULL != (label = sf_histstring(file,"label"))) {
	    printf("label=\"%s\" ",label);
	    free(label);
	}

	if (NULL != (label = sf_histstring(file,"unit"))) {
	    printf("unit=\"%s\"",label);
	    free(label);
	}

	printf("\n");

	if (!trail) dim = sf_largefiledims(file,n);

	size = 1;
	for (j=0; j < dim; j++) {
	    snprintf(key,8,"n%d",j+1);
	    if (!sf_histlargeint(file,key,&nj)) break;

#if defined(__cplusplus) || defined(c_plusplus)
	    snprintf(out,25,"%s=%lu",key,(long) nj);
#else
	    snprintf(out,25,"%s=%llu",key,(long long) nj);
#endif
	    printf("%s%s%s",pad+10,out,pad+strlen(out));
	    size *= nj;

	    snprintf(key,8,"d%d",j+1);
	    if (sf_histfloat(file,key,&fj)) {
		snprintf(out,15,"%s=%g ",key,fj);
	    } else {
		snprintf(out,15,"%s=? ",key);
	    }
	    printf(" %s%s",out,pad+strlen(out));

	    snprintf(key,8,"o%d",j+1);
	    if (sf_histfloat(file,key,&fj)) {
		snprintf(out,15,"%s=%g ",key,fj);
	    } else {
		snprintf(out,15,"%s=? ",key);
	    }
	    printf(" %s%s",out,pad+strlen(out));

	    snprintf(key,8,"label%d",j+1);
	    if (NULL != (val = sf_histstring(file,key))) {
		printf("%s=\"%s\" ",key,val);
	    }

	    snprintf(key,7,"unit%d",j+1);
	    if (NULL != (val = sf_histstring(file,key))) {
		printf("%s=\"%s\" ",key,val);
	    }

	    printf("\n");
	}

	check_zeros (file, esize, size, ncheck, buf, zero);
	sf_fileclose(file);
    }
    if (!info) printf("\n");

    exit (0);
}
コード例 #13
0
ファイル: graph3.c プロジェクト: 1014511134/src
int main(int argc, char* argv[])
{
    int n1,n2,n3, frame2,frame3, i1,i2,i3, iframe, np=3, orient;
    int n1pix,n2pix, m1pix,m2pix, n1front,n2front, movie, nframe=1; 
    float point1, point2, **front, **side, **top, **topt, *x, *y, o1, d1, o2, d2;    
    float min, max, f, frame1, dframe, oo1, dd1;
    bool nomin, nomax, yreverse;
    char *label1, *label2, *unit1, *unit2;
    off_t esize;
    bool flat;
    vp_contour cnt;
    sf_file in=NULL;

    sf_init(argc,argv);
    in = sf_input("in");
    vp_init();

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

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

    esize = sf_esize(in);
    sf_unpipe(in,n1*n2*n3*esize);

    if (!sf_getint("orient",&orient)) orient=1;
    /* function orientation */

    if (!sf_getbool("yreverse",&yreverse)) yreverse=false;

    front = sf_floatalloc2(n1,1);
    side = sf_floatalloc2(1,n2);
    top = sf_floatalloc2(n1,n2);
    topt = (2==orient)? sf_floatalloc2(n2,n1): NULL;

    nomin = (bool) !sf_getfloat("min",&min);
    /* minimum function value */
    nomax = (bool) !sf_getfloat("max",&max);
    /* maximum function value */

    if (nomin) min = +FLT_MAX;
    if (nomax) max = -FLT_MAX;
    if (nomin || nomax) {
	for (i3=0; i3 < n3; i3++) {
	    sf_floatread(top[0],n1*n2,in);
	    for (i2=0; i2 < n2; i2++) {
		for (i1=0; i1 < n1; i1++) {
		    f = top[i2][i1];
		    if (nomax && f > max) max=f;
		    if (nomin && f < min) min=f;
		}
	    }
	}
    }


    if (min == 0. && max == 0.) {
	min = -1.;
	max = 1.;
    } else if (min == max) {
        max *= 1.04;
        min *= 0.96;
    }

    if (!sf_histfloat(in,"o1",&o1)) o1=0.;
    if (!sf_histfloat(in,"d1",&d1)) d1=1.;
    if (!sf_histfloat(in,"o2",&o2)) o2=0.;
    if (!sf_histfloat(in,"d2",&d2)) d2=1.;

    switch(orient) {
	case 1:
	default:    
	    dd1 = (min-max)/np;
	    oo1 = max+0.5*dd1;

	    sf_putint(in,"n1",np);
	    sf_putfloat(in,"o1",oo1);
	    sf_putfloat(in,"d1",dd1);

	    sf_putint(in,"n2",n1);
	    sf_putfloat(in,"o2",o1);
	    sf_putfloat(in,"d2",d1);

	    sf_putint(in,"n3",n2);
	    sf_putfloat(in,"o3",o2);
	    sf_putfloat(in,"d3",d2);

	    break;
	case 2:
	    dd1 = (max-min)/np;
	    oo1 = min+0.5*dd1;

	    sf_putint(in,"n2",np);
	    sf_putfloat(in,"o2",oo1);
	    sf_putfloat(in,"d2",dd1);

	    sf_putint(in,"n3",n2);
	    sf_putfloat(in,"o3",o2);
	    sf_putfloat(in,"d3",d2);

	    break;
	case 3:
	    dd1 = (max-min)/np;
	    oo1 = min+0.5*dd1;

	    sf_putint(in,"n1",n2);
	    sf_putfloat(in,"o1",o2);
	    sf_putfloat(in,"d1",d2);

	    sf_putint(in,"n2",n1);
	    sf_putfloat(in,"o2",o1);
	    sf_putfloat(in,"d2",d1);

	    sf_putint(in,"n3",np);
	    sf_putfloat(in,"o3",oo1);
	    sf_putfloat(in,"d3",dd1);

	    break;
    }

    label1 = sf_histstring(in,"label1");
    label2 = sf_histstring(in,"label2");

    if (NULL != label1) {
	sf_putstring(in,"label1","");
	sf_putstring(in,"label2",label1);
	free(label1);
    }
    if (NULL != label2) {
	sf_putstring(in,"label3",label2);
	free(label2);
    }

    unit1 = sf_histstring(in,"unit1");
    unit2 = sf_histstring(in,"unit2");

    if (NULL != unit1) {
	sf_putstring(in,"unit1","");
	sf_putstring(in,"unit2",unit1);
	free(unit1);
    }
    if (NULL != unit2) {
	sf_putstring(in,"unit3",unit2);
	free(unit2);
    }

    if (!sf_getfloat("point1",&point1)) point1=0.5;
    /* fraction of the vertical axis for front face */
    if (!sf_getfloat("point2",&point2)) point2=0.5;
    /* fraction of the horizontal axis for front face */

    if (!sf_getfloat("frame1",&frame1)) frame1=0.5*(min+max);
    if (!sf_getint("frame2",&frame2)) frame2=n1-1;
    if (!sf_getint("frame3",&frame3)) frame3=0;
    /* frame numbers for cube faces */

    /* sanity check */
    if (min < max) {
	if (frame1 < min) frame1 = min;
	if (frame1 > max) frame1 = max;
    } else {
	if (frame1 > min) frame1 = min;
	if (frame1 < max) frame1 = max;
    }
    if (frame2 < 0) frame2 = 0;
    if (frame3 < 0) frame3 = 0;
    if (frame2 >= n1) frame2 = n1-1;
    if (frame3 >= n2) frame3 = n2-1;

    if (!sf_getint("movie",&movie)) movie=0;
    /* 0: no movie, 1: movie over axis 1, 2: axis 2, 3: axis 3 */

    if (!sf_getfloat("dframe",&dframe)) dframe=1;
    /* frame increment in a movie */

    switch (movie) {
	case 0:
	    nframe = 1;
	    break;
	case 1:
	    nframe = 0.5+(max-frame1)/dframe;
	    break;
	case 2:
	    nframe = (n1-frame2)/dframe;
	    break;
	case 3:
	    nframe = (n2-frame3)/dframe;
	    break;
	default:
	    sf_error("movie=%d is outside [0,3]",movie);
	    break;
    }

    if (sf_getint("nframe",&iframe) && iframe < nframe) nframe=iframe;
    /* number of frames in a movie */

    if (nframe < 1) nframe=1;

    if (!sf_getint("n1pix",&n1pix)) n1pix = n1/point1+n3/(1.-point1);
    /* number of vertical pixels */
    if (!sf_getint("n2pix",&n2pix)) n2pix = n2/point2+n3/(1.-point2);
    /* number of horizontal pixels */

    m1pix = VP_STANDARD_HEIGHT*72;
    m2pix = VP_STANDARD_HEIGHT*72/VP_SCREEN_RATIO;

    if (n1pix < m1pix) n1pix=m1pix;
    if (n2pix < m2pix) n2pix=m2pix;

    n1front = n1pix*point1;
    n2front = n2pix*point2;

    if (n1front <= 0) n1front=1;
    if (n2front <= 0) n2front=1;
    if (n1front >= n1pix) n1front=n1pix-1;
    if (n2front >= n2pix) n2front=n2pix-1;

    x = sf_floatalloc(n1);
    y = sf_floatalloc(n2);

    for (i1=0; i1 < n1; i1++) {
	x[i1] = o1 + i1*d1;
    }

    for (i2=0; i2 < n2; i2++) {
	y[i2] = o2 + i2*d2;
    }

    if (!sf_getbool("flat",&flat)) flat=true;
    /* if n, display perspective view */

    vp_cubeplot_init (n1pix, n2pix, n1front, n2front, flat, false); 
    vp_frame_init (in,"blt",false);
    vp_plot_init(n3);
    cnt = vp_contour_init(false,
			  n1,o1,d1,0.,
			  n2,o2,d2,0.); 

    for (iframe=0; iframe < nframe; iframe++) {
	if (iframe > 0) vp_erase (); 

	for (i3=0; i3 < n3; i3++) {
	    vp_plot_set (i3);

	    /* front face */

	    switch(orient) {
		case 1:
		default:
		    sf_seek(in,(off_t) (i3*n1*n2+frame3*n1)*esize,SEEK_SET);
		    sf_floatread(front[0],n1,in);
		    
		    vp_cubecoord(3,x[0],x[n1-1],min,max);
		    vp_umove(x[0],front[0][0]);
		    for (i1=1; i1 < n1; i1++) {
			vp_udraw(x[i1],front[0][i1]);
		    }

		    break;
		case 2:
		    sf_seek(in,(off_t) (i3*n1*n2+frame3*n1)*esize,SEEK_SET);
		    sf_floatread(front[0],n1,in);

		    if (yreverse) {
			vp_cubecoord(3,min,max,x[n1-1],x[0]);
			vp_umove(front[0][n1-1],x[n1-1]);
			for (i1=n1-2; i1 >= 0; i1--) {
			    vp_udraw(front[0][i1],x[i1]);
			}
		    } else {
			vp_cubecoord(3,min,max,x[0],x[n1-1]);
			vp_umove(front[0][0],x[0]);
			for (i1=1; i1 < n1; i1++) {
			    vp_udraw(front[0][i1],x[i1]);
			}
		    }
		    
		    break;
		case 3:
		    sf_seek(in,(off_t) (i3*n1*n2*esize),SEEK_SET);
		    sf_floatread(top[0],n1*n2,in);

		    vp_cubecoord(3,x[0],x[n1-1],y[0],y[n2-1]);
		    vp_contour_draw(cnt,false,top,frame1);

		    break;
	    }
		    

	    /* side face */

	    switch(orient) {
		case 1:
		default:	    
		    for (i2=0; i2 < n2; i2++) {
			sf_seek(in,(off_t) (i3*n1*n2+i2*n1+frame2)*esize,SEEK_SET);
			sf_floatread(side[i2],1,in);
		    }
		    
		    vp_cubecoord(2,y[0],y[n2-1],min,max);
		    vp_umove(y[0],side[0][0]);
		    for (i2=1; i2 < n2; i2++) {
			vp_udraw(y[i2],side[i2][0]);
		    }

		    break;
		case 2:
		    sf_seek(in,(off_t) (i3*n1*n2*esize),SEEK_SET);
		    sf_floatread(top[0],n1*n2,in);

		    /* transpose */
		    for (i2=0; i2 < n2; i2++) {
			for (i1=0; i1 < n1; i1++) {
			    topt[i1][i2] = top[i2][i1];
			}
		    }
		    
		    if (yreverse) {
			vp_cubecoord(2,y[0],y[n2-1],x[n1-1],x[0]);
		    } else {
			vp_cubecoord(2,y[0],y[n2-1],x[0],x[n1-1]);
		    }
		    vp_contour_draw(cnt,false,topt,frame1);
		    
		    break;
		case 3:
		    for (i2=0; i2 < n2; i2++) {
			sf_seek(in,(off_t) (i3*n1*n2+i2*n1+frame2)*esize,SEEK_SET);
			sf_floatread(side[i2],1,in);
		    }
		    
		    vp_cubecoord(2,min,max,y[0],y[n2-1]);
		    vp_umove(side[0][0],y[0]);
		    for (i2=1; i2 < n2; i2++) {
			vp_udraw(side[i2][0],y[i2]);
		    }

		    break;
	    }

	    /* top face */

	    switch(orient) {
		case 1:
		default:
		    sf_seek(in,(off_t) (i3*n1*n2*esize),SEEK_SET);
		    sf_floatread(top[0],n1*n2,in);
		    
		    vp_cubecoord(1,x[0],x[n1-1],y[0],y[n2-1]);
		    vp_contour_draw(cnt,false,top,frame1);

		    break;
		case 2:
		    for (i2=0; i2 < n2; i2++) {
			sf_seek(in,(off_t) (i3*n1*n2+i2*n1+frame2)*esize,SEEK_SET);
			sf_floatread(side[i2],1,in);
		    }
		    
		    vp_cubecoord(1,min,max,y[0],y[n2-1]);
		    vp_umove(side[0][0],y[0]);
		    for (i2=1; i2 < n2; i2++) {
			vp_udraw(side[i2][0],y[i2]);
		    }
		    
		    break;
		case 3:
		    sf_seek(in,(off_t) (i3*n1*n2+frame3*n1)*esize,SEEK_SET);
		    sf_floatread(front[0],n1,in);
		    
		    vp_cubecoord(1,x[0],x[n1-1],min,max);
		    vp_umove(x[0],front[0][0]);
		    for (i1=1; i1 < n1; i1++) {
			vp_udraw(x[i1],front[0][i1]);
		    }

		    break;
	    }
	}
	
	vp_plot_unset();
	vp_coordinates();

	switch(orient) {
	    case 1:
	    default:
		vp_cubeframe((frame1-oo1)/dd1,frame2,frame3);
		break;
	    case 2:
		vp_cubeframe(frame2,(frame1-oo1)/dd1,frame3);
		break;
	    case 3:
		vp_cubeframe(frame2,frame3,(frame1-oo1)/dd1);
		break;
	}
	
	switch (movie) {
	    case 2:
		frame2 += (int) dframe;
		break;
	    case 3:
		frame3 += (int) dframe;
		break;
	    case 1:
		frame1 += dframe;
		break;
	    default:
		break;
	}

	vp_purge(); 
    } /* frame loop */


    exit(0);
}
コード例 #14
0
ファイル: cut.c プロジェクト: 1014511134/src
int main (int argc, char *argv[])
{
    off_t *table, nleft, nbuf, n[SF_MAX_DIM], f[SF_MAX_DIM];
    int i, esize, dim, n1, n2, n3, m1, i2, i1, j1, jump;
    int m[SF_MAX_DIM], j[SF_MAX_DIM]; 
    float a, d[SF_MAX_DIM], o[SF_MAX_DIM];
    char key[7], *buf, buf0[BUFSIZ], *zero;
    bool verb;
    sf_file in=NULL;
    sf_file out=NULL;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	if (i2==n2) break;

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


    exit (0);
}