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); }
int main (int argc, char* argv[]) { int dim, i, j, n[SF_MAX_DIM], w[SF_MAX_DIM], k[SF_MAX_DIM], np, nw, n12; float *data, *wind; char *temp; FILE *tmp; sf_file in, out; sf_init (argc, argv); in = sf_input("in"); out = sf_output("out"); dim = sf_filedims(in,n); if (!sf_getints("w",w,dim)) sf_error("Need w="); if (!sf_getints("k",k,dim)) sf_error("Need k="); n12 = 1; np = 1; nw = 1; for(j=0; j < dim; j++) { n12 *= n[j]; np *= k[j]; nw *= w[j]; } nw *= np; data = sf_floatalloc (n12); wind = sf_floatalloc (nw); sf_floatread(data,n12,in); tmp = sf_tempfile(&temp,"w+b"); ocparcel_init (dim, k, n, w); if (n12 != fwrite(data,sizeof(float),n12,tmp)) sf_error("writing error:"); ocparcel_lop (false, n12, nw, tmp, wind); oc_zero(n12*sizeof(float),tmp); ocparcel_lop ( true, n12, nw, tmp, wind); rewind(tmp); if (n12 != fread(data,sizeof(float),n12,tmp)) sf_error("reading error"); for (i=0; i < n12; i++) { sf_line2cart(dim, n, i, k); if (k[0] <= 1 || k[0] >= n[0] - 2) data[i] = 0.; } sf_floatwrite (data,n12,out); unlink(temp); exit (0); }
int main(int argc, char* argv[]) { sf_file out; FILE *tiffin; TIFF *tiffout; char *tiffname, buf[BUFSIZ]; unsigned char *grey; int n1, i2, n2, nbuf; short nc; sf_init(argc,argv); out = sf_output("out"); sf_settype(out,SF_UCHAR); fclose(sf_tempfile(&tiffname,"w")); tiffin = fopen(tiffname,"wb"); while (1) { nbuf = fread(buf,1,BUFSIZ,stdin); if (nbuf <= 0) break; fwrite(buf,1,nbuf,tiffin); } fclose(tiffin); tiffout = TIFFOpen(tiffname,"rb"); /* Find the width and height of the image */ TIFFGetField(tiffout, TIFFTAG_IMAGEWIDTH, &n1); TIFFGetField(tiffout, TIFFTAG_IMAGELENGTH, &n2); TIFFGetField(tiffout,TIFFTAG_SAMPLESPERPIXEL,&nc); if (1==nc) { sf_putint(out,"n1",n1); sf_putint(out,"n2",n2); } else { sf_putint(out,"n1",nc); sf_putint(out,"n2",n1); sf_putint(out,"n3",n2); } grey = sf_ucharalloc(n1*nc); for (i2 = 0; i2 < n2; i2++) { if (TIFFReadScanline(tiffout, grey, i2, 0) < 0) sf_error("Trouble reading TIFF file"); sf_ucharwrite(grey,n1*nc,out); } TIFFClose(tiffout); unlink(tiffname); exit(0); }
void sf_unpipe (sf_file file, off_t size) /*< Redirect a pipe input to a direct access file >*/ { off_t nbuf, len, bufsiz; char *dataname=NULL; FILE* tmp; char *buf; if (!(file->pipe)) return; tmp = sf_tempfile(&dataname,"wb"); bufsiz = sf_bufsiz(file); buf = sf_charalloc(SF_MIN(bufsiz,size)); while (size > 0) { nbuf = (bufsiz < size)? bufsiz : size; if (nbuf != fread(buf,1,nbuf,file->stream) || nbuf != fwrite(buf,1,nbuf,tmp)) sf_error ("%s: trouble unpiping:",__FILE__); size -= nbuf; } free(buf); if (NULL != file->dataname ) { len = strlen(dataname)+1; file->dataname = (char*) sf_realloc(file->dataname,len,sizeof(char)); memcpy(file->dataname,dataname,len); } /* if (unlink(file->dataname)) sf_warning ("%s: trouble removing %s:",__FILE__,file->dataname); */ (void) fclose(file->stream); file->stream = freopen(dataname,"rb",tmp); if (NULL == file->stream) sf_error ("%s: Trouble reading data file %s:",__FILE__,dataname); }
int main(int argc, char* argv[]) { bool verb; /* verbosity flag */ float clip; /* threshold (clip value) */ sf_file Fc; /* cube file */ sf_file Fl; /* list file */ extern int fseeko(FILE *stream, off_t offset, int whence); sf_axis ax,ay,az; sf_axis aa; int ix,iy,iz; int nx,ny,nz,nj,na; int nk=0,jk; float **cube; float dx,dy,dz; float x0,y0,z0; FILE* tfile; char* tname; float t2[3],t3[4]; /*------------------------------------------------------------*/ /* init RSF */ sf_init(argc,argv); if(! sf_getbool("verb",&verb)) verb=false; if(! sf_getfloat("clip",&clip)) clip=0; Fc = sf_input ( "in"); /* input cube */ Fl = sf_output("out"); /* output list */ /* read axes*/ az=sf_iaxa(Fc,1); sf_setlabel(az,"z"); ax=sf_iaxa(Fc,2); sf_setlabel(ax,"x"); ay=sf_iaxa(Fc,3); sf_setlabel(ay,"y"); nz=sf_n(az); z0=sf_o(az); dz=sf_d(az); nx=sf_n(ax); x0=sf_o(ax); dx=sf_d(ax); ny=sf_n(ay); y0=sf_o(ay); dy=sf_d(ay); na=0; if(ny>1) { if(verb) sf_warning("initiating 3D points"); nj=4; } else { if(verb) sf_warning("initiating 2D points"); nj=3; } /*------------------------------------------------------------*/ cube = sf_floatalloc2(nz,nx); tfile = sf_tempfile(&(tname), "w+b"); for (iy=0; iy<ny; iy++) { /* if(verb) sf_warning("iy=%d",iy);*/ sf_floatread(cube[0],nz*nx,Fc); nk=0; for (ix=0; ix<nx; ix++) { for (iz=0; iz<nz; iz++) { if( fabs(cube[ix][iz]) > clip) { nk++; } } } if(ny>1) { jk=0; for (ix=0; ix<nx; ix++) { for (iz=0; iz<nz; iz++) { if( fabs(cube[ix][iz]) > clip) { t3[0] = x0 + ix * dx; t3[1] = y0 + iy * dy; t3[2] = z0 + iz * dz; t3[3] = cube[ix][iz]; fseeko(tfile,jk*4*sizeof(float),SEEK_SET); fwrite( t3, sizeof(float),4,tfile); jk++; } } } } else { jk=0; for (ix=0; ix<nx; ix++) { for (iz=0; iz<nz; iz++) { if( fabs(cube[ix][iz]) > clip) { t2[0] = x0 + ix * dx; t2[1] = z0 + iz * dz; t2[2] = cube[ix][iz]; fseeko(tfile,jk*3*sizeof(float),SEEK_SET); fwrite( t2, sizeof(float),3,tfile); jk++; } } } } /* else ny=1 */ na += nk; } /* iy */ /* output axes */ aa = sf_maxa(nj,0,1); sf_oaxa(Fl,aa,1); if(verb) sf_raxa(aa); free(aa); aa = sf_maxa(na,0,1); sf_oaxa(Fl,aa,2); if(verb) sf_raxa(aa); free(aa); if( ny>1) { for( jk=0; jk<nk; jk++) { fseeko(tfile,jk*4*sizeof(float),SEEK_SET); fread( t3, sizeof(float),4,tfile); /* if(verb) sf_warning("%d, %g %g %g %g",jk,t3[0],t3[1],t3[2],t3[3]);*/ sf_floatwrite(t3,4,Fl); } } else { for( jk=0; jk<nk; jk++) { fseeko(tfile,jk*3*sizeof(float),SEEK_SET); fread( t2, sizeof(float),3,tfile); /* if(verb) sf_warning("%d, %g %g %g",jk,t2[0],t2[1],t2[2]);*/ sf_floatwrite(t2,3,Fl); } } free(cube); unlink(tname); exit (0); }
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); }
int main(int argc, char* argv[]) { int i, iter, niter, p[6][2], status, *mask; float *buf, *buf2, *wht; double rn, rnp, alpha, beta; pid_t pid[6]={1,1,1,1,1,1}; off_t nm, nd, msiz, dsiz, pos; size_t nbuf, mbuf, dbuf; FILE *xfile, *Rfile, *gfile, *sfile, *Sfile; char *x, *R, *g, *s, *S, *prog; sf_file mod, dat, from, mwt, x0, known; /* input */ sf_file to, out; /* output */ extern int fseeko(FILE *stream, off_t offset, int whence); extern off_t ftello (FILE *stream); sf_init(argc,argv); dat = sf_input("in"); mod = sf_input("mod"); if (SF_FLOAT != sf_gettype(mod) || SF_FLOAT != sf_gettype(dat)) sf_error("Need float type in mod and dat"); for (i=0; i < argc-1; i++) { argv[i]=argv[i+1]; } for (i=0; i < argc-1; i++) { /* find the program to run */ if (NULL == strchr(argv[i],'=')) { /* first one without the '=' */ prog = argv[0]; argv[0] = argv[i]; argv[i] = prog; break; } } argv[argc-1] = sf_charalloc(6); snprintf(argv[argc-1],6,"adj=X"); if (!sf_getint("niter",&niter)) niter=1; /* number of iterations */ Rfile = sf_tempfile(&R,"w+b"); xfile = sf_tempfile(&x,"w+b"); gfile = sf_tempfile(&g,"w+b"); sfile = sf_tempfile(&s,"w+b"); Sfile = sf_tempfile(&S,"w+b"); fclose(Rfile); fclose(xfile); fclose(gfile); fclose(sfile); fclose(Sfile); nm = sf_filesize(mod); nd = sf_filesize(dat); /* I/O buffers */ nbuf = BUFSIZ/sizeof(float); buf = sf_floatalloc(nbuf); buf2 = sf_floatalloc(nbuf); if (NULL != sf_getstring("mwt")) { mwt = sf_input("mwt"); /* model weight */ wht = sf_floatalloc(nbuf); } else { mwt = NULL; wht = NULL; } if (NULL != sf_getstring("known")) { known = sf_input("known"); /* known model mask */ if (SF_INT != sf_gettype(known)) sf_error("Need int type in known"); mask = sf_intalloc(nbuf); } else { known = NULL; mask = NULL; } if (NULL != sf_getstring("x0")) { x0 = sf_input("x0"); /* initial model */ } else { x0 = NULL; } for (i=0; i < 6; i++) { /* make six pipes */ if (pipe(p[i]) < 0) sf_error("pipe error:"); } for (iter=0; iter < niter; iter++) { for (i=0; i < 6; i++) { /* fork six children */ if ((pid[i] = fork()) < 0) sf_error("fork error:"); if (0 == pid[i]) break; } if (0 == pid[0]) { /* feeds rr to p[0] */ close(p[0][0]); close(STDOUT_FILENO); DUP(p[0][1]); to = sf_output("out"); if (0 == iter) { xfile = fopen(x,"wb"); if (NULL == x0) { for (i=0; i < nbuf; i++) { buf[i] = 0.0f; } } MLOOP( if (NULL != x0) sf_floatread(buf,mbuf,x0); MWRITE(xfile); ); fclose(xfile); Rfile = fopen(R,"wb"); DLOOP( sf_floatread(buf,dbuf,dat); for (i=0; i < dbuf; i++) { buf[i] = -buf[i]; } sf_floatwrite(buf,dbuf,to); DWRITE (Rfile); );
sf_file sf_input (/*@null@*/ const char* tag) /*< Create an input file structure >*/ { int esize; sf_file file; char *filename, *format; size_t len; extern off_t ftello (FILE *stream); file = (sf_file) sf_alloc(1,sizeof(*file)); file->dataname = NULL; if (NULL == tag || 0 == strcmp(tag,"in")) { file->stream = stdin; filename = NULL; } else { filename = sf_getstring (tag); if (NULL == filename) { /* this option allows you to call function with sf_input("mydir/myinput.rsf"); Karl */ len = strlen(tag)+1; filename = sf_charalloc(len); /* should change next line to strcpy or strncpy Karl */ memcpy(filename,tag,len); } file->stream = fopen(filename,"r"); if (NULL == file->stream) { sf_input_error(file,"Cannot read input (header) file",filename); return NULL; } } file->buf = NULL; /* setbuf(file->stream,file->buf); */ /* create a parameter table */ file->pars = sf_simtab_init (tabsize); file->head = sf_tempfile(&file->headname,"w+"); /* read the parameter table from the file */ sf_simtab_input (file->pars,file->stream,file->head); if (NULL == infiles) { infiles = (sf_file *) sf_alloc(1,sizeof(sf_file)); infiles[0] = NULL; nfile=1; } if (NULL == filename) { infiles[0] = file; } else { free (filename); ifile++; if (ifile >= nfile) { /* grow array */ nfile *= 2; infiles = (sf_file *) realloc(infiles, nfile * sizeof(sf_file)); if (NULL == infiles) sf_error("%s: reallocation error",__FILE__); } infiles[ifile] = file; } filename = sf_histstring(file,"in"); if (NULL == filename) { sf_input_error (file,"No in= in file",tag); return NULL; } len = strlen(filename)+1; file->dataname = sf_charalloc(len); memcpy(file->dataname,filename,len); if (0 != strcmp(filename,"stdin")) { file->stream = freopen(filename,"rb",file->stream); if (NULL == file->stream) { sf_input_error(file,"Cannot read data file",filename); return NULL; } } free (filename); file->pipe = (bool) (-1 == ftello(file->stream)); if (file->pipe && ESPIPE != errno) sf_error ("%s: pipe problem:",__FILE__); file->op = XDR_DECODE; format = sf_histstring(file,"data_format"); if (NULL == format) { if (!sf_histint(file,"esize",&esize) || 0 != esize) { sf_input_error (file,"Unknown format in",tag); return NULL; } sf_setformat(file,"ascii_float"); } else { sf_setformat(file,format); free (format); } return file; }
int main(int argc, char* argv[]) { bool color; int n1, i2, n2, nc, nbuf; unsigned char *grey=NULL; sf_file in=NULL; TIFF *tiffout=NULL; FILE *tiffin=NULL; char *tiffname=NULL, buf[BUFSIZ]; sf_init(argc,argv); in = sf_input("in"); fclose(sf_tempfile(&tiffname,"w")); tiffout = TIFFOpen(tiffname,"wb"); if (tiffout == NULL) sf_error("can't open file %s\n", tiffname); if (SF_UCHAR != sf_gettype(in)) sf_error("Need unsigned char in input"); if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input"); if (!sf_getbool("color",&color)) color=(bool)(3==n1); if (color) { nc = n1; if (!sf_histint(in,"n2",&n1)) sf_error("No n2= in input"); if (!sf_histint(in,"n3",&n2)) sf_error("No n3= in input"); } else { nc = 1; if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input"); } grey = sf_ucharalloc (n1*n2*nc); sf_ucharread(grey,n1*n2*nc,in); TIFFSetField(tiffout,TIFFTAG_IMAGEWIDTH,n1); TIFFSetField(tiffout,TIFFTAG_IMAGELENGTH,n2); TIFFSetField(tiffout,TIFFTAG_SAMPLESPERPIXEL,nc); TIFFSetField(tiffout,TIFFTAG_BITSPERSAMPLE,8); TIFFSetField(tiffout,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT); TIFFSetField(tiffout,TIFFTAG_PHOTOMETRIC, (3==nc)? PHOTOMETRIC_RGB: PHOTOMETRIC_MINISBLACK); TIFFSetField(tiffout,TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(tiffout, TIFFTAG_ROWSPERSTRIP, 1); for (i2 = 0; i2 < n2; i2++) { if (TIFFWriteScanline(tiffout, grey+i2*n1*nc, i2, 0) < 0) sf_error("Trouble writing TIFF file"); } TIFFClose(tiffout); tiffin = fopen(tiffname,"rb"); while (1) { nbuf = fread(buf,1,BUFSIZ,tiffin); if (nbuf <= 0) break; fwrite(buf,1,nbuf,stdout); } fclose(tiffin); unlink(tiffname); exit(0); }
int main(int argc, char* argv[]) { int i, iter, niter; sf_complex *buf, *buf2; double rn, rnp, alpha, beta; off_t nm, nd, msiz, dsiz, pos; size_t nbuf, mbuf, dbuf, len, iolen, cmdlen; FILE *xfile, *Rfile, *gfile, *Gfile, *sfile, *Sfile; char *x, *R, *g, *G, *s, *S, *prog, *cmdline, *iostring, *arg; sf_file mod, dat, x0; /* input */ sf_file Rrsf, Grsf, grsf, out; /* output */ extern int fseeko(FILE *stream, off_t offset, int whence); extern off_t ftello (FILE *stream); sf_init(argc,argv); dat = sf_input("--input"); mod = sf_input("mod"); out = sf_output("--output"); sf_fileflush(out,mod); sf_settype(out,SF_COMPLEX); if (SF_COMPLEX != sf_gettype(mod) || SF_COMPLEX != sf_gettype(dat)) sf_error("Need complex type in mod and dat"); for (i=0; i < argc-1; i++) { argv[i]=argv[i+1]; } for (i=0; i < argc-1; i++) { /* find the program to run */ if (NULL == strchr(argv[i],'=')) { /* first one without the '=' */ prog = argv[0]; argv[0] = argv[i]; argv[i] = prog; break; } } argv[argc-1] = sf_charalloc(6); snprintf(argv[argc-1],6,"adj=X"); if (!sf_getint("niter",&niter)) niter=1; /* number of iterations */ Rfile = sf_tempfile(&R,"w+"); xfile = sf_tempfile(&x,"w+b"); gfile = sf_tempfile(&g,"w+"); Gfile = sf_tempfile(&G,"w+"); sfile = sf_tempfile(&s,"w+b"); Sfile = sf_tempfile(&S,"w+b"); fclose(Rfile); Rrsf = sf_output(R); sf_readwrite(Rrsf,true); sf_fileflush(Rrsf,dat); fclose(xfile); fclose(gfile); fclose(Gfile); fclose(sfile); fclose(Sfile); nm = sf_filesize(mod); nd = sf_filesize(dat); /* I/O buffers */ nbuf = BUFSIZ/sizeof(sf_complex); buf = sf_complexalloc(nbuf); buf2 = sf_complexalloc(nbuf); cmdline = sf_charalloc(SF_CMDLEN); iostring = sf_charalloc(SF_CMDLEN); cmdlen = 0; for (i=0; i < argc; i++) { arg = argv[i]; len = strlen(arg); if (cmdlen+len > SF_CMDLEN-2) sf_error("command line is too long"); strncpy(cmdline+cmdlen,arg,len); cmdline[cmdlen+len]=' '; cmdlen += len+1; } if (NULL != sf_getstring("x0")) { x0 = sf_input("x0"); /* initial model */ } else { x0 = NULL; } for (iter=0; iter < niter; iter++) { if (0 == iter) { xfile = fopen(x,"wb"); if (NULL == x0) { for (i=0; i < nbuf; i++) { buf[i] = sf_cmplx(0.0f,0.0f); } } MLOOP( if (NULL != x0) sf_complexread(buf,mbuf,x0); MWRITE(xfile); ); fclose(xfile); #ifdef SF_HAS_COMPLEX_H DLOOP( sf_complexread(buf,dbuf,dat); for (i=0; i < dbuf; i++) { buf[i] = -buf[i]; } sf_complexwrite(buf,dbuf,Rrsf); );