Example #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);
}
Example #2
0
File: mpi.c Project: 1014511134/src
int main(int argc, char* argv[])
{
    int rank, nodes, ndim, job, axis, axis2, jobs;
    off_t n[SF_MAX_DIM];
    char **commands, cmdline[SF_CMDLEN], *iname;
    sf_file inp=NULL, out=NULL;
    MPI_Status stat;

    MPI_Init(&argc,&argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &nodes);
    if (nodes < 2) {
	fprintf(stderr,"Need at least two nodes!\n");
	MPI_Finalize();
	exit(1);
    }

    if (!rank) { /* master node */
	sf_init(argc,argv);

	inp = sf_input("--input");
	out = sf_output("--output");

	ndim = sf_largefiledims (inp,n);

	if (!sf_getint("split",&axis)) axis=ndim;
	/* axis to split */
	
	commands = sf_split(inp,axis,nodes,&jobs,ndim,n,argc,argv);  

	for (job=0; job < jobs; job++) {
	    strncpy(cmdline,commands[job],SF_CMDLEN);
	    MPI_Send(cmdline, SF_CMDLEN, MPI_CHAR, job+1, 0, MPI_COMM_WORLD);
	}

	iname = sf_getstring("--input");

	if (!sf_getint("join",&axis2)) axis2=axis;
	/* axis to join (0 means add) */

	sf_out(out,axis2,iname);
	
	for (job=0; job < jobs; job++) {
	    MPI_Recv(&rank,1, MPI_INT, job+1, 1, MPI_COMM_WORLD,&stat);
	    if (axis2 > 0) sf_join(out,job);
	}
	if (0==axis2) sf_add(out,jobs);

	sf_fileclose(inp);
    } else { /* slave nodes */
	MPI_Recv(cmdline, SF_CMDLEN, MPI_CHAR, 0, 0, MPI_COMM_WORLD,&stat);
	fprintf(stderr,"CPU %d: %s\n",rank,cmdline); 
        sf_system(cmdline);
	MPI_Send(&rank,1,MPI_INT,0,1,MPI_COMM_WORLD);
    }
    
    MPI_Finalize();
}
Example #3
0
int
main (int argc, char *argv[])
{
    int i;
    int ndims;
    off_t nlarge[SF_MAX_DIM];
    int n[SF_MAX_DIM];
    bool parform, large;
    sf_file in=NULL;

    sf_init(argc,argv);

    in = sf_input("in");

    if (!sf_getbool("large", &large))
        large = false;
    /* if y, file with large dimensions. */

    if (large) {
	ndims = sf_largefiledims(in, nlarge);
    } else {
	ndims = sf_filedims(in, n);
    }

    if (!sf_getbool("parform", &parform))
        parform = true;
    /* If y, print out parameter=value. If n, print out value. */

    if (parform) printf( "ndims=" );

    printf( "%d", ndims );

    if (parform)
        printf( "\nn=" );
    else
        printf( ":" );

    for (i=0; i<ndims; i++) {
	if (large) {
#if defined(__cplusplus) || defined(c_plusplus)
	    printf( "%ld", (long) n[i] );
#else
	    printf( "%lld", (long long) n[i] );
#endif
	} else {
	    printf( "%d", n[i] );
	}
        if (i<ndims-1) printf(",");
    }

    printf( "\n" );


    exit(0);
}
Example #4
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);
}
Example #5
0
File: omp.c Project: 1014511134/src
int main(int argc, char* argv[])
{
    int axis, axis2, rank, nodes, node, ndim, jobs;
    off_t n[SF_MAX_DIM];
    char *iname=NULL, **cmdline;
    FILE *tmp;
    sf_file inp, out, inp2;

#pragma omp parallel
    {
	nodes = omp_get_num_threads();
	if (1 >= nodes) 
	    nodes = omp_get_num_procs(); 
    }

    /* master node */
    sf_init(argc,argv);

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

    ndim = sf_largefiledims (inp,n);
    if (!sf_getint("split",&axis)) axis=ndim;
    /* axis to split */

    tmp = sf_tempfile(&iname,"w+b");
    fclose(tmp);

    inp2 = sf_output(iname);
    sf_cp(inp,inp2);
    sf_fileclose(inp2);

    inp2 = sf_input(iname);

    cmdline = sf_split(inp2,axis,nodes+1,&jobs,ndim,n,argc,argv);  
    sf_warning("Running %d threads",jobs);

#pragma omp parallel
    {
	omp_set_num_threads(jobs);
    }

#pragma omp parallel private(rank) shared(cmdline)
    {
	rank = omp_get_thread_num();
	if (rank < jobs) {
	    fprintf(stderr,"CPU %d: %s\n",rank,cmdline[rank]); 
	    sf_system(cmdline[rank]);
	}
    }
    
    if (!sf_getint("join",&axis2)) axis2=axis;
    /* axis to join (0 means add) */
    
    sf_out(out,axis2,iname);
    sf_rm(iname,true,false,false);

    if (axis2 > 0) {
	for (node=0; node < jobs; node++) {
	    sf_join(out,node);
	}
    } else {
	sf_add(out,jobs);
    }

    exit(0);
}
Example #6
0
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);
}
Example #7
0
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);
}
Example #8
0
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);
}
Example #9
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);
}
Example #10
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);
}
Example #11
0
File: pad.c Project: 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);
}
Example #12
0
File: in.c Project: 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);
}
Example #13
0
File: cut.c Project: 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);
}