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); }
void sf_setform (sf_file file, sf_dataform form) /*< set file form >*/ { size_t bufsiz; file->form = form; switch(form) { case SF_ASCII: if (NULL != file->buf) { free (file->buf); file->buf = NULL; } if (NULL != file->dataname) sf_putint(file,"esize",0); /* for compatibility with SEPlib */ break; case SF_XDR: if (NULL == file->buf) { bufsiz = sf_bufsiz(file); file->buf = sf_charalloc(bufsiz); xdrmem_create(&(file->xdr),file->buf,bufsiz,file->op); } break; case SF_NATIVE: default: if (NULL != file->buf) { free (file->buf); file->buf = NULL; } break; } }
/* global variables. need a temp array for sf_get_tah to pass non tah records read from pipe. This means sf_get_tah is not thread safe, but the intended use is to read stdin, so why whould you want to run multiple sf_get_tah on more then one thread? char* sf_get_tah_bytebuffer=NULL; int sf_get_tah_bytebuffer_len=0; */ int counttokens(const char* delimittedtokens, const char* delimiters) /*< return number of delimiter seperated tokens in string>*/ { char* one_token; char* copy_of_delimittedtokens; int numtoken=0; if(NULL==delimittedtokens)return 0; /* allocate and copy delimittedtokens strtok destroys the string as it splits it up */ copy_of_delimittedtokens=sf_charalloc(strlen(delimittedtokens)+1); strcpy (copy_of_delimittedtokens, delimittedtokens); fprintf(stderr,"copy_of_delimittedtokens=%s\n",copy_of_delimittedtokens); one_token=strtok(copy_of_delimittedtokens,delimiters); while (one_token!=NULL){ if(false)fprintf(stderr,"before increment numtoken=%d\n",numtoken); numtoken++; one_token=strtok(NULL,delimiters); } if(false)fprintf(stderr,"free(copy_of_delimittedtokens)\n"); /* did not save and of the pointers returned by strtok, so OK to free! */ free(copy_of_delimittedtokens); if(false)fprintf(stderr,"return from counttokens numtoken=%d\n",numtoken); return numtoken; }
bool sf_simtab_getstrings (sf_simtab table, const char* key, /*@out@*/ char **par,size_t n) /*< extract a string array parameter from the table >*/ { int i, iopen; size_t iclose; char *val, *string, *qopen, *qclose; val = sf_simtab_get(table,key); if (NULL == val) return false; qopen = strchr(val,'\"'); iopen = (NULL == qopen)? 0: (qopen-val)+1; qclose = strrchr(val,'\"'); iclose = (NULL == qclose)? strlen(val): (size_t) (qclose-val); iclose -= iopen; string = sf_charalloc (iclose+1); memcpy(string,val+iopen,iclose); string[iclose]='\0'; par[0] = strsep1(&string,":"); for (i = 1; i < n; i++) { par[i] = strsep1(&string,":"); } return true; }
static char* gettmpdatapath (void) /* Finds temporary datapath. Datapath rules: 1. check tmpdatapath= on the command line 2. check .tmpdatapath file in the current directory 3. check TMPDATAPATH environmental variable 4. check .tmpdatapath in the home directory 5. return NULL */ { char *path, *penv, *home, file[PATH_MAX]; path = sf_getstring ("tmpdatapath"); if (NULL != path) return path; path = sf_charalloc(PATH_MAX+1); if (readpathfile (".tmpdatapath",path)) return path; penv = getenv("TMPDATAPATH"); if (NULL != penv) { strncpy(path,penv,PATH_MAX); return path; } home = getenv("HOME"); if (NULL != home) { (void) snprintf(file,PATH_MAX,"%s/.tmpdatapath",home); if (readpathfile (file,path)) return path; } free(path); return NULL; }
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); }
int main (int argc, char *argv[]) { const bool su = true; bool verb, xdr; char *filename, *trace; int format=0, ns, ntr, itrace[SF_NKEYS]; off_t pos, nsegy; float dt; FILE *file; extern int fseeko (FILE * stream, off_t offset, int whence); /**** Start common code with segy2rsf (16 lines) ****/ sf_init (argc, argv); if (!sf_getbool ("verb", &verb)) verb = false; /* Verbosity flag */ if (!sf_getbool ("endian", &xdr)) xdr = true; /* Whether to automatically estimate endianness or not */ if (xdr) endian (); if (NULL == (filename = sf_getstring ("tape"))) /* input data */ sf_error ("Need to specify tape="); if (NULL == (file = fopen (filename, "rb"))) sf_error ("Cannot open \"%s\" for reading:", filename); pos = readfilesize (file); /**** End common code with segy2rsf ****/ /* Figure out ns and ntr */ trace = sf_charalloc (SF_HDRBYTES); if (SF_HDRBYTES != fread (trace, 1, SF_HDRBYTES, file)) sf_error ("Error reading first trace header"); fseeko (file, 0, SEEK_SET); segy2head (trace, itrace, SF_NKEYS); ns = itrace[segykey ("ns")]; dt = itrace[segykey ("dt")] / 1000000.; free (trace); nsegy = SF_HDRBYTES + ns * 4; ntr = pos / nsegy; su_or_segy_to_rsf (verb, su, ntr, format, ns, itrace, nsegy, file, dt); exit (0); }
bool sf_simtab_getints (sf_simtab table, const char* key, /*@out@*/ int *par,size_t n) /*< extract an int array parameter from the table >*/ { size_t i, clen; long num, j=0; char *val, *cnum, *fval, *fvali; val = sf_simtab_get(table,key); if (NULL == val) return false; cnum = sf_charalloc(strlen(val)); for (i = 0; i < n; i++) { fval = (0==i)? strtok(val,","):strtok(NULL,","); if (NULL==fval) { if (0==i) return false; } else { fvali = strpbrk(fval,"x*"); if (NULL != fvali) { clen = fvali-fval; strncpy(cnum,fval,clen); cnum[clen]='\0'; num = strtol(cnum,NULL,10); if (ERANGE == errno) sf_error ("%s: Wrong counter in %s='%s':", __FILE__,key,fval); fvali++; j = strtol(fvali,NULL,10); if (ERANGE == errno || j < INT_MIN || j > INT_MAX) sf_error ("%s: Wrong value in %s='%s':",__FILE__,key,fval); for (; i < n && i < (size_t) num; i++) { par[i] = (int) j; } if (i == n) { free(cnum); return true; } } else { j = strtol(fval,NULL,10); if (ERANGE == errno || j < INT_MIN || j > INT_MAX) sf_error ("%s: Wrong value in %s='%s':",__FILE__,key,fval); } } par[i] = (int) j; } free(cnum); return true; }
void sf_simtab_enter(sf_simtab table, const char *key, const char* val) /*< Add an entry key=val to the table >*/ { int h; struct entry *e; size_t len; h = hash(key,table->size); /* check if exists */ for (e = table->pars[h]; e != NULL; e = e->next) { if (strcmp(key,e->key) == 0) { if (strcmp(val,e->val) != 0) { len = strlen(val)+1; e->val = (char*) sf_realloc(e->val,len,1); memcpy(e->val,val,len); } return; } } /* create new */ e = (struct entry*) sf_alloc (1,sizeof(*e)); len = strlen(key)+1; e->key = sf_charalloc(len); memcpy(e->key,key,len); len = strlen(val)+1; e->val = sf_charalloc(len); memcpy(e->val,val,len); e->next = table->pars[h]; table->pars[h] = e; }
void sf_setaformat (const char* format /* number format (.i.e "%5g") */, int line /* numbers in line */ ) /*< Set format for ascii output >*/ { size_t len; if (NULL != aformat) free (aformat); if (NULL != format) { len = strlen(format)+1; aformat = sf_charalloc(len); memcpy(aformat,format,len); } else { aformat = NULL; } aline = (size_t) line; }
int sf_try_charread(const char* test, sf_file file) /*< check if you can read test word >*/ { int size, got, cmp; char* arr; size = strlen(test); arr = sf_charalloc(size+1); got = fread(arr,sizeof(char),size+1,file->stream); if (got != size) return 1; arr[size] = '\0'; cmp = strncmp(arr,test,size); free(arr); return cmp; }
bool sf_simtab_getbools (sf_simtab table, const char* key, /*@out@*/bool *par,size_t n) /*< extract a bool array parameter from the table >*/ { bool test=false; size_t i, clen; long num; char *val, *cnum, *fval, *fvali; val = sf_simtab_get(table,key); if (NULL == val) return false; cnum = sf_charalloc(strlen(val)); for (i = 0; i < n; i++) { fval = (0==i)? strtok(val,","):strtok(NULL,","); fvali = strpbrk(fval,"x*"); if (NULL != fvali) { clen = fvali-fval; strncpy(cnum,fval,clen); cnum[clen]='\0'; num = strtol(cnum,NULL,10); if (ERANGE == errno) sf_error ("%s: Wrong counter in %s='%s':",__FILE__,key,fval); fvali++; test = (bool) (('y' == fvali[0]) || ('Y' == fvali[0]) || ('1' == fvali[0])); for (; i < n && i < (size_t) num; i++) { par[i] = test; } if (i == n) return true; } else { if (NULL==fval) { if (0==i) return false; } else { test = (bool) (('y' == fval[0]) || ('Y' == fval[0]) || ('1' == fval[0])); } } par[i] = test; } free(cnum); return true; }
void sf_simtab_put (sf_simtab table, const char *keyval) /*< put a key=val string to the table >*/ { char *eq, *key; size_t keylen; eq = strchr(keyval,'='); if (NULL == eq) return; eq++; keylen = (size_t) (eq-keyval); key = sf_charalloc(keylen); memcpy(key,keyval,keylen); key[keylen-1]='\0'; sf_simtab_enter(table,key,eq); free(key); }
void fastmarch_init(int nx_init, int nz_init, int nt_init, float hx_init, float hz_init, float ht_init, float *x0_init, float *t0_init, float *v_init, float *s_init) /*< initialize >*/ { int i,j,ind, nxz; nx = nx_init; nz = nz_init; nt = nt_init; nt1 = nt-1; nx1 = nx-1; hx = hx_init; hz = hz_init; ht = ht_init; x0 = x0_init; t0 = t0_init; v = v_init; s = s_init; nxz = nx*nz; pup= sf_intalloc(nxz); ms= sf_charalloc(nxz); r= sf_intalloc(nxz); p= sf_intalloc(nxz); count=0; for( i=0;i<nx;i++ ) { *(pup+i)=2; *(ms+i)='a'; ind=i; for( j=1;j<nz;j++ ) { ind+=nx; *(pup+ind)=0; *(ms+ind)='u'; } } }
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); }
char* sf_simtab_getstring (sf_simtab table, const char* key) /*< extract a string parameter from the table >*/ { char *val, *string, *qopen, *qclose; int iopen; size_t iclose; val = sf_simtab_get(table,key); if (NULL == val) return NULL; qopen = strchr(val,'\"'); iopen = (NULL == qopen)? 0: (qopen-val)+1; qclose = strrchr(val,'\"'); iclose = (NULL == qclose)? strlen(val): (size_t) (qclose-val); iclose -= iopen; string = sf_charalloc (iclose+1); memcpy(string,val+iopen,iclose); string[iclose]='\0'; return string; }
static char* getdatapath (void) /* Finds datapath. Datapath rules: 1. check datapath= on the command line 2. check .datapath file in the current directory 3. check DATAPATH environmental variable 4. check .datapath in the home directory 5. use '.' (not a SEPlib behavior) Note that option 5 results in: if you move the header to another directory then you must also move the data to the same directory. Confusing consequence. */ { char *path, *penv, *home, file[PATH_MAX]; path = sf_getstring ("datapath"); if (NULL != path) return path; path = sf_charalloc(PATH_MAX+1); if (readpathfile (".datapath",path)) return path; penv = getenv("DATAPATH"); if (NULL != penv) { strncpy(path,penv,PATH_MAX); return path; } home = getenv("HOME"); if (NULL != home) { (void) snprintf(file,PATH_MAX,"%s/.datapath",home); if (readpathfile (file,path)) return path; } strncpy(path,"./",3); return path; }
void sf_fft_unit(int axis /* axis number */, const char* unit /* input unit */, sf_file out /* output file */) /*< Change unit to 1/unit or vice versa >*/ { size_t len; char *unit2, varname[7]; snprintf(varname,7,"unit%d",axis); if (NULL != unit) { if (0==strcmp(unit,"s")) { sf_putstring(out,varname,"Hz"); } else if (0==strcmp(unit,"Hz")) { sf_putstring(out,varname,"s"); } else if (0==strncmp(unit,"1/",2)) { sf_putstring(out,varname,unit+2); } else { len=strlen(unit)+3; unit2 = sf_charalloc(len); snprintf(unit2,len,"1/%s",unit); sf_putstring(out,varname,unit2); } } }
FILE *sf_tempfile(char** dataname, const char* mode) /*< Create a temporary file with a unique name >*/ { FILE *tmp; char *path; int stemp; extern FILE * fdopen (int fd, const char *mode); extern int mkstemp (char *tmpl); path = gettmpdatapath(); if (NULL == path) path = getdatapath(); *dataname = sf_charalloc (NAME_MAX+1); snprintf(*dataname,NAME_MAX,"%s%sXXXXXX",path,sf_getprog()); free(path); stemp = mkstemp(*dataname); if (stemp < 0) sf_error ("%s: cannot create %s:",__FILE__,*dataname); tmp = fdopen(stemp,mode); if (NULL == tmp) sf_error ("%s: cannot open %s:",__FILE__,*dataname); return tmp; }
int main(int argc, char* argv[]) { int p[4][2], i, im, id, status; unsigned long mseed, dseed; off_t nm, nd, msiz, dsiz; size_t nbuf, mbuf, dbuf; float *buf; double dp; pid_t pid[6]={1,1,1,1,1,1}; sf_file mod=NULL; sf_file dat=NULL; sf_file pip=NULL; sf_init(argc,argv); mod = sf_input("mod"); dat = sf_input("dat"); if (SF_FLOAT != sf_gettype(mod) || SF_FLOAT != sf_gettype(dat)) sf_error("Need float type in mod and dat"); nm = sf_filesize(mod); nd = sf_filesize(dat); nbuf = BUFSIZ/sizeof(float); buf = sf_floatalloc(nbuf); mseed = (unsigned long) time(NULL); init_genrand(mseed); mseed = genrand_int32(); dseed = genrand_int32(); for (i=0; i < argc-1; i++) { argv[i]=argv[i+1]; } argv[argc-1] = sf_charalloc(6); snprintf(argv[argc-1],6,"adj=X"); for (i=0; i < 4; i++) { /* make four pipes */ if (pipe(p[i]) < 0) sf_error("pipe error:"); } 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]) { /* makes random model and writes it to p[0] */ close(p[0][0]); close(STDOUT_FILENO); DUP(p[0][1]); pip = sf_output("out"); sf_fileflush(pip,mod); init_genrand(mseed); for (msiz=nm, mbuf=nbuf; msiz > 0; msiz -= mbuf) { if (msiz < mbuf) mbuf=msiz; sf_random(mbuf,buf); sf_floatwrite(buf,mbuf,pip); } } if (0 == pid[1]) { /* reads from p[0], runs the program, and writes to p[1] */ close(p[0][1]); close(STDIN_FILENO); DUP(p[0][0]); close(p[1][0]); close(STDOUT_FILENO); DUP(p[1][1]); argv[argc-1][4]='0'; execvp(argv[0],argv); _exit(1); } if (0 == pid[2]) { /* reads from p[1] and multiplies it with random data */ close(p[1][1]); close(STDIN_FILENO); DUP(p[1][0]); pip = sf_input("in"); init_genrand(dseed); dp = 0.; for (dsiz=nd, dbuf=nbuf; dsiz > 0; dsiz -= dbuf) { if (dsiz < dbuf) dbuf=dsiz; sf_floatread(buf,dbuf,pip); for (id=0; id < dbuf; id++) { dp += buf[id]*genrand_real1 (); } } sf_warning(" L[m]*d=%g",dp); _exit(2); } if (0 == pid[3]) { /* makes random data and writes it to p[2] */ close(p[2][0]); close(STDOUT_FILENO); DUP(p[2][1]); pip = sf_output("out"); sf_fileflush(pip,dat); init_genrand(dseed); for (dsiz=nd, dbuf=nbuf; dsiz > 0; dsiz -= dbuf) { if (dsiz < dbuf) dbuf=dsiz; sf_random(dbuf,buf); sf_floatwrite(buf,dbuf,pip); } } if (0 == pid[4]) { /* reads from p[2], runs the adjoint, and writes to p[3] */ close(p[2][1]); close(STDIN_FILENO); DUP(p[2][0]); close(p[3][0]); close(STDOUT_FILENO); DUP(p[3][1]); argv[argc-1][4]='1'; execvp(argv[0],argv); _exit(4); } if (0 == pid[5]) { /* reads from p[3] and multiplies it with random model */ close(p[3][1]); close(STDIN_FILENO); DUP(p[3][0]); pip = sf_input("in"); init_genrand(mseed); dp = 0.; for (msiz=nm, mbuf=nbuf; msiz > 0; msiz -= mbuf) { if (msiz < mbuf) mbuf=msiz; sf_floatread(buf,mbuf,pip); for (im=0; im < mbuf; im++) { dp += buf[im]*genrand_real1 (); } } sf_warning("L'[d]*m=%g",dp); _exit(5); } for (i=0; i < 6; i++) { if (0 == pid[i]) break; } if (6==i) { /* parent waits */ waitpid(pid[2],&status,0); waitpid(pid[5],&status,0); exit(0); } }
int main(int argc, char* argv[]) { fint1 str, istr; int i1,i2, n1,n2,n3, ix,iv,ih, ib, ie, nb, nx,nv,nh, nw, next; float d1,o1,d2,o2, eps, w,x,k, v0,v2,v,v1,dv, dx, h0,dh,h, num, den, t, dw; float *trace=NULL, *strace=NULL, ***stack=NULL, ***stack2=NULL, ***cont=NULL, **image=NULL; sf_complex *ctrace=NULL, *ctrace0=NULL, shift; char *time=NULL, *space=NULL, *unit=NULL; size_t len; static kiss_fftr_cfg forw, invs; sf_file in=NULL, out=NULL; bool sembl; sf_init (argc,argv); in = sf_input("in"); out = sf_output("out"); if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input"); if (!sf_histint(in,"n2",&nx)) sf_error("No n2= in input"); if (!sf_histint(in,"n3",&nh)) sf_error("No n3= in input"); if (!sf_getint("nb",&nb)) nb=2; if (!sf_getfloat("eps",&eps)) eps=0.01; if (!sf_getint("pad",&n2)) n2=n1; if (!sf_getint("pad2",&n3)) n3=2*kiss_fft_next_fast_size((n2+1)/2); nw = n3/2+1; forw = kiss_fftr_alloc(n3,0,NULL,NULL); invs = kiss_fftr_alloc(n3,1,NULL,NULL); if (NULL == forw || NULL == invs) sf_error("KISS FFT allocation error"); if (!sf_histfloat(in,"o1",&o1)) o1=0.; o2 = o1*o1; if(!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input"); d2 = o1+(n1-1)*d1; d2 = (d2*d2 - o2)/(n2-1); if (!sf_getint("nv",&nv)) sf_error("Need nv="); if (!sf_getfloat("dv",&dv)) sf_error("Need dv="); if (!sf_getfloat("v0",&v0) && !sf_histfloat(in,"v0",&v0)) sf_error("Need v0="); if (!sf_getbool("semblance",&sembl)) sembl=true; /* if y, compute semblance; if n, stack */ if(!sf_histfloat(in,"o3",&h0)) sf_error("No o2= in input"); if(!sf_histfloat(in,"d3",&dh)) sf_error("No d2= in input"); if(!sf_histfloat(in,"d2",&dx)) sf_error("No d3= in input"); sf_putfloat(out,"o3",v0+dv); sf_putfloat(out,"d3",dv); sf_putint(out,"n3",nv); sf_putstring(out,"label3","Velocity"); if (NULL != (time = sf_histstring(in,"label1")) && NULL != (space = sf_histstring(in,"label2"))) { len = strlen(time)+strlen(space)+2; unit = sf_charalloc(len); snprintf(unit,len,"%s/%s",space,time); sf_putstring(out,"unit3",unit); free(time); free(space); } dx = 2*SF_PI/(2*kiss_fft_next_fast_size(nx-1)*dx); dw = 16*SF_PI/(d2*n3); /* 2pi * 8 */ stack = sf_floatalloc3(n1,nx,nv); stack2 = sf_floatalloc3(n1,nx,nv); cont = sf_floatalloc3(n1,nx,nv); image = sf_floatalloc2(n1,nx); trace = sf_floatalloc(n1); strace = sf_floatalloc(n3); ctrace = sf_complexalloc(nw); ctrace0 = sf_complexalloc(nw); if (!sf_getint("extend",&next)) next=4; /* trace extension */ str = fint1_init(next,n1,0); istr = fint1_init(next,n2,0); for (i1=0; i1 < n1*nx*nv; i1++) { stack[0][0][i1] = 0.; stack2[0][0][i1] = 0.; } sf_cosft_init(nx); for (ih=0; ih < nh; ih++) { sf_warning("offset %d of %d;",ih+1,nh); h = h0 + ih*dh; h *= h * 0.5; sf_floatread(image[0],n1*nx,in); for (i1=0; i1 < n1; i1++) { sf_cosft_frw(image[0],i1,n1); } for (ix=0; ix < nx; ix++) { x = ix*dx; x *= x; k = x * 0.5; fint1_set(str,image[ix]); for (i2=0; i2 < n2; i2++) { t = o2+i2*d2; t = sqrtf(t); t = (t-o1)/d1; i1 = t; if (i1 >= 0 && i1 < n1) { strace[i2] = fint1_apply(str,i1,t-i1,false); } else { strace[i2] = 0.; } } for (i2=n2; i2 < n3; i2++) { strace[i2] = 0.; } kiss_fftr(forw,strace, (kiss_fft_cpx *) ctrace0); for (iv=0; iv < nv; iv++) { v = v0 + (iv+1)* dv; v1 = h * (1./(v*v) - 1./(v0*v0)); v2 = k * ((v0*v0) - (v*v)); ctrace[0]=sf_cmplx(0.,0.); /* dc */ for (i2=1; i2 < nw; i2++) { w = i2*dw; w = v2/w+(v1-0.125*o2)*w; shift = sf_cmplx(cosf(w),sinf(w)); #ifdef SF_HAS_COMPLEX_H ctrace[i2] = ctrace0[i2] * shift; #else ctrace[i2] = sf_cmul(ctrace0[i2],shift); #endif } /* w */ kiss_fftri(invs,(const kiss_fft_cpx *) ctrace, strace); fint1_set(istr,strace); for (i1=0; i1 < n1; i1++) { t = o1+i1*d1; t = t*t; t = (t-o2)/d2; i2 = t; if (i2 >= 0 && i2 < n2) { cont[iv][ix][i1] = fint1_apply(istr,i2,t-i2,false); } else { cont[iv][ix][i1] = 0.; } } } /* v */ } /* x */ for (iv=0; iv < nv; iv++) { for (i1=0; i1 < n1; i1++) { sf_cosft_inv(cont[0][0],i1+iv*nx*n1,n1); } } for (iv=0; iv < nv; iv++) { for (ix=0; ix < nx; ix++) { for (i1=0; i1 < n1; i1++) { t = cont[iv][ix][i1]; stack[iv][ix][i1] += t; stack2[iv][ix][i1] += t*t; } /* i1 */ } /* x */ } /* v */ } /* h */ sf_warning("."); for (iv=0; iv < nv; iv++) { for (ix=0; ix < nx; ix++) { for (i1=0; i1 < n1; i1++) { ib = i1-nb > 0? i1-nb: 0; ie = i1+nb+1 < n1? i1+nb+1: n1; num = 0.; den = 0.; if (sembl) { for (i2=ib; i2 < ie; i2++) { t = stack[iv][ix][i2]; num += t*t; den += stack2[iv][ix][i2]; } den *= nh; trace[i1] = den > 0.? num/den: 0.; } else { for (i2=ib; i2 < ie; i2++) { t = stack[iv][ix][i2]; num += t; } den = nh; trace[i1] = num/(den+ FLT_EPSILON); } } sf_floatwrite (trace,n1,out); } } exit(0); }
int main(int argc, char* argv[]) { bool inv; int nt, nx, nv, it, ix, iv, nw, n3, i3, ntr, ntm, im; float *trace=NULL, *modl=NULL, *r=NULL; float vmin, vmax, dv, dx, x0, t0, t, dt, tp, xp; char *unit=NULL, *space=NULL, *time=NULL, *intp=NULL; size_t len; sf_bands spl=NULL; sf_file in, out; const float tau=0.21; sf_init (argc,argv); in = sf_input("in"); out = sf_output("out"); if (!sf_getbool("inv",&inv)) inv=false; /* if y, do inverse transform */ if (!sf_getint("nw",&nw)) nw=2; /* accuracy level */ if (!sf_histint(in,"n2",&nt)) sf_error("No n2= in input"); if (!sf_histfloat(in,"d2",&dt)) sf_error("No d2= in input"); if (!sf_histfloat(in,"o2",&t0)) sf_error("No o2= in input"); if (!sf_getfloat("tp",&tp)) tp=t0; if (!sf_getfloat("xp",&xp)) xp=0.; intp = sf_getstring("interp"); if (NULL == intp) intp[0]='s' ; if (inv) { if (!sf_histint(in,"n1",&nv)) sf_error("No n1= in input"); if (!sf_histfloat(in,"d1",&dv)) sf_error("No d1= in input"); if (!sf_histfloat(in,"o1",&vmin)) sf_error("No o1= in input"); if (!sf_getint("nx",&nx) && !sf_histint(in,"nx",&nx)) sf_error("Need nx="); /* number of offsets (if inv=y) */ if (!sf_getfloat("x0",&x0) && !sf_histfloat(in,"x0",&x0)) sf_error("Need x0="); /* offset origin (if inv=y) */ if (!sf_getfloat("dx",&dx) && !sf_histfloat(in,"dx",&dx)) sf_error("Need dx="); /* offset sampling (if inv=y) */ sf_putint(out,"n1",nx); sf_putfloat(out,"d1",dx); sf_putfloat(out,"o1",x0); sf_putstring(out,"label1","Offset"); if (NULL != (unit=sf_histstring(in,"unit1"))) { space=strchr(unit,'/'); if (*space == '\0') sf_putstring(out,"unit1",unit); } if (intp[0] == 's') { if (nw > 2) spl = sf_spline_init (nw, nv); } ntr = nv; ntm = nx; } else { if (!sf_histint(in,"n1",&nx)) sf_error("No n1= in input"); if (!sf_histfloat(in,"d1",&dx)) sf_error("No d1= in input"); if (!sf_histfloat(in,"o1",&x0)) sf_error("No o1= in input"); if (!sf_getint("nv",&nv)) sf_error("Need nv="); /* number of velocities (if inv=n) */ if (!sf_getfloat("vmin",&vmin)) sf_error("Need vmin="); /* minimum velocity (if inv=n) */ if (!sf_getfloat("vmax",&vmax)) sf_error("Need vmax="); /* maximum velocity (if inv=n) */ dv = (vmax - vmin)/(nv-1.); sf_putint(out,"n1",nv); sf_putfloat(out,"d1",dv); sf_putfloat(out,"o1",vmin); sf_putstring(out,"label1","Velocity"); if (NULL != (time = sf_histstring(in,"unit2")) && NULL != (space = sf_histstring(in,"unit1"))) { len = strlen(time)+strlen(space)+2; unit = sf_charalloc(len); snprintf(unit,len,"%s/%s",space,time); sf_putstring(out,"unit1",unit); free(time); free(space); } sf_putint(out,"nx",nx); sf_putfloat(out,"x0",x0); sf_putfloat(out,"dx",dx); if (intp[0] == 's') { if (nw > 2) spl = sf_spline_init (nw, nx); } ntr = nx; ntm = nv; } n3 = sf_leftsize(in,2); trace = sf_floatalloc(ntr); modl = sf_floatalloc(ntm); r = sf_floatalloc(ntm); for (i3=0; i3 < n3; i3++) { for (it=0; it < nt; it++) { t = t0 + it*dt; sf_floatread (trace,ntr,in); if (intp[0] == 's') { if (nw > 2) sf_banded_solve (spl,trace); } if (t > tp) { if (inv) { for (ix=0; ix < nx; ix++) { r[ix] = (x0-xp+ix*dx)/(t-tp); } if (intp[0] == 's') { sf_int1_init (r, vmin, dv, nv, sf_spline_int, nw, nx, 0.0); } else { sf_int1_init (r, vmin, dv, nv, sf_lin_int, 2, nx, tau); shprefilter(ntr,trace); } } else { for (iv=0; iv < nv; iv++) { r[iv] = xp+(vmin+iv*dv)*(t-tp); } if (intp[0] == 's') { sf_int1_init (r, x0, dx, nx, sf_spline_int, nw, nv, 0.0); } else { sf_int1_init (r, x0, dx, nx, sf_lin_int, 2, nv, tau); shprefilter(ntr,trace); } } sf_int1_lop (false,false,ntr,ntm,trace,modl); } else { for (im=0; im < ntm; im++) { modl[im] = 0.; } } sf_floatwrite (modl,ntm,out); } } exit(0); }
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[]) { 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); );
/* initialize the length of axis and the number of tics */ void vp_axis_init (const sf_file in) { float ftmp, num; bool want; char *stmp, *label, *unit; size_t len; struct Axis axis; if (!sf_getint ("axisfat",&axisfat)) axisfat=0; if (!sf_getint ("axiscol",&axiscol)) axiscol=7; if (!sf_getint ("labelfat",&labelfat)) labelfat=0; if (!sf_getfloat ("labelsz",&labelsz)) labelsz=8.; if ((NULL == (label=sf_getstring("label1"))) && (NULL == (label=sf_histstring(in,"label1")))) { axis1.label = blank; } else if ((NULL == (unit=sf_getstring("unit1"))) && (NULL == (unit=sf_histstring(in,"unit1")))) { axis1.label = label; } else { len = strlen(label)+strlen(unit)+4; axis1.label = sf_charalloc(len); snprintf(axis1.label,len,"%s (%s)",label,unit); free(label); free(unit); } if ((NULL == (label=sf_getstring("label2"))) && (NULL == (label=sf_histstring(in,"label2")))) { axis2.label = blank; } else if ((NULL == (unit=sf_getstring("unit2"))) && (NULL == (unit=sf_histstring(in,"unit2")))) { axis2.label = label; } else { len = strlen(label)+strlen(unit)+4; axis2.label = sf_charalloc(len); snprintf(axis2.label,len,"%s (%s)",label,unit); free(label); free(unit); } where1 = (NULL != (stmp = sf_getstring ("wherexlabel")) && 'b' == *stmp); where2 = (NULL != (stmp = sf_getstring ("whereylabel")) && 'r' == *stmp); /* checking to see if wantaxis is fetched */ if (!sf_getbool ("wantaxis", &want)) { /* setting the default to put the axes on the plot */ want = true; axis1.want = true; axis2.want = true; } else if (!want) { axis1.want = false; axis2.want = false; } else { if (!sf_getbool ("wantaxis1",&(axis1.want))) axis1.want = true; if (!sf_getbool ("wantaxis2",&(axis2.want))) axis2.want = true; } /* frame or axis */ wheretics = NULL != (stmp = sf_getstring ("wheretics")) && 'a' == *stmp; if (!sf_getfloat ("axisor1",&(axis1.or))) axis1.or = where1? min2: max2; if (!sf_getfloat ("axisor2",&(axis2.or))) axis2.or = where2? min1: max1; if (!sf_getint ("n1tic",&(axis1.ntic))) axis1.ntic = 1; if (!sf_getint ("n2tic",&(axis2.ntic))) axis2.ntic = 1; if (!sf_getfloat ("d1num", &(axis1.dnum))) getscl (&axis1); if (!sf_getfloat ("d2num", &(axis2.dnum))) getscl (&axis2); if (0. == axis1.dnum) sf_error("%s: zero d1num",__FILE__); if (0. == axis2.dnum) sf_error("%s: zero d2num",__FILE__); if (!sf_getfloat("o1num",&(axis1.num0))) { ftmp = (min1 < max1)? min1: max1; for (num = floorf(ftmp / axis1.dnum) * axis1.dnum - axis1.dnum; num < ftmp; num += axis1.dnum) ; axis1.num0 = num; } if (!sf_getfloat("o2num",&(axis2.num0))) { ftmp = (min2 < max2)? min2: max2; for (num = floorf(ftmp / axis2.dnum) * axis2.dnum - axis2.dnum; num < ftmp; num += axis2.dnum) ; axis2.num0 = num; } axis1.dtic = axis1.dnum / axis1.ntic ; ftmp = (min1 < max1)? min1: max1; for (num = axis1.num0 - axis1.ntic * axis1.dtic; num < ftmp; num += axis1.dtic); axis1.tic0 = num; axis2.dtic = axis2.dnum / axis2.ntic ; ftmp = (min2 < max2)? min2: max2; for (num = axis2.num0 - axis2.ntic * axis2.dtic; num < ftmp; num += axis2.dtic); axis2.tic0 = num; if (transp) { /* swap */ axis = axis1; axis1 = axis2; axis2 = axis; } if(yreverse) axis1.or = (min2+max2)-axis1.or; if(xreverse) axis2.or = (min1+max1)-axis2.or; }
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); }
int main(int argc, char *argv[]) { bool wantwf, verb; int ix, iz, is, it, wfit, im, ik, i, j, itau; int ns, nx, nz, nt, wfnt, rnx, rnz, nzx, rnzx, vnx, ntau, htau, nds; int scalet, snap, snapshot, fnx, fnz, fnzx, nk, nb; int rectx, rectz, repeat, gpz, n, m, pad1, trunc, spx, spz; float dt, t0, z0, dz, x0, dx, s0, ds, wfdt, srctrunc; float dtau, tau0, tau; int nr, ndr, nr0; char *path1, *path2, number[5], *left, *right; double tstart, tend; struct timeval tim; /*wavenumber domain tapering*/ int taper; float *ktp; float ktmp,kx_trs,kz_trs,thresh; float dkx,dkz,kx0,kz0; float kx,kz; int nkz; sf_complex c, **lt, **rt; sf_complex *ww, **dd, ***dd3; float ***img1, **img2, ***mig1, **mig2; float *rr, **ccr, **sill, ***fwf, ***bwf; sf_complex *cwave, *cwavem, **wave, *curr; sf_axis at, ax, az, atau; sf_file Fdat, Fsrc, Fimg1, Fimg2; sf_file Ffwf, Fbwf, Fvel; sf_file Fleft, Fright; int cpuid, numprocs, nth, nspad, iturn; float *sendbuf, *recvbuf; sf_complex *sendbufc, *recvbufc; MPI_Comm comm=MPI_COMM_WORLD; MPI_Init(&argc, &argv); MPI_Comm_rank(comm, &cpuid); MPI_Comm_size(comm, &numprocs); sf_init(argc, argv); #ifdef _OPENMP #pragma omp parallel { nth=omp_get_num_threads(); } sf_warning(">>> Using %d threads <<<", nth); #endif gettimeofday(&tim, NULL); tstart=tim.tv_sec+(tim.tv_usec/1000000.0); if (!sf_getint("taper",&taper)) taper=0; /* tapering in the frequency domain */ if (!sf_getfloat("thresh",&thresh)) thresh=0.92; /* tapering threshold */ if(!sf_getbool("wantwf", &wantwf)) wantwf=false; if(!sf_getbool("verb", &verb)) verb=false; if(!sf_getint("pad1", &pad1)) pad1=1; /* padding factor on the first axis */ if(!sf_getint("nb", &nb)) sf_error("Need nb= "); if(!sf_getfloat("srctrunc", &srctrunc)) srctrunc=0.4; if(!sf_getint("rectx", &rectx)) rectx=2; if(!sf_getint("rectz", &rectz)) rectz=2; if(!sf_getint("repeat", &repeat)) repeat=2; if(!sf_getint("scalet", &scalet)) scalet=1; if(!sf_getint("snap", &snap)) snap=100; /* interval of the output wavefield */ if(!sf_getint("snapshot", &snapshot)) snapshot=0; /* print out the wavefield snapshots of this shot */ if(!sf_getint("nds", &nds)) sf_error("Need nds=!"); /* source and receiver positions */ if(!sf_getint("gpz", &gpz)) sf_error("Need gpz="); if(!sf_getint("spx", &spx)) sf_error("Need spx="); if(!sf_getint("spz", &spz)) sf_error("Need spz="); /* tau parameters */ if(!sf_getint("ntau", &ntau)) sf_error("Need ntau="); if(!sf_getfloat("dtau", &dtau)) sf_error("Need dtau="); if(!sf_getfloat("tau0", &tau0)) sf_error("Need tau0="); /* geometry parameters */ if(!sf_getint("rnx", &rnx)) sf_error("Need rnx="); if(!sf_getint("ndr", &ndr)) ndr=1; if(!sf_getint("nr0", &nr0)) nr0=0; /* input/output files */ Fdat=sf_input("--input"); Fimg1=sf_output("--output"); Fimg2=sf_output("Fimg2"); Fsrc=sf_input("Fsrc"); Fvel=sf_input("Fpadvel"); if(wantwf){ Ffwf=sf_output("Ffwf"); Fbwf=sf_output("Fbwf"); } at=sf_iaxa(Fsrc, 1); nt=sf_n(at); dt=sf_d(at); t0=sf_o(at); ax=sf_iaxa(Fvel, 2); vnx=sf_n(ax); dx=sf_d(ax); x0=sf_o(ax); az=sf_iaxa(Fvel, 1); rnz=sf_n(az); dz=sf_d(az); z0=sf_o(az); if(!sf_histint(Fdat, "n2", &nr)) sf_error("Need n2= in input!"); if(!sf_histint(Fdat, "n3", &ns)) sf_error("Need n3= in input!"); if(!sf_histfloat(Fdat, "d3", &ds)) sf_error("Need d3= in input!"); if(!sf_histfloat(Fdat, "o3", &s0)) sf_error("Need o3= in input!"); wfnt=(nt-1)/scalet+1; wfdt=dt*scalet; /* double check the geometry parameters */ if(nds != (int)(ds/dx)) sf_error("Need ds/dx= %d", nds); //sf_warning("s0=%g, x0+(rnx-1)*dx/2=%g", s0, x0+(rnx-1)*dx/2); //if(s0 != x0+(rnx-1)*dx/2) sf_error("Wrong origin information!"); if(vnx != nds*(ns-1)+rnx) sf_error("Wrong dimension in x axis!"); /* set up the output files */ atau=sf_iaxa(Fsrc, 1); sf_setn(atau, ntau); sf_setd(atau, dtau); sf_seto(atau, tau0); sf_setlabel(atau, "Tau"); sf_setunit(atau, "s"); sf_oaxa(Fimg1, az, 1); sf_oaxa(Fimg1, ax, 2); sf_oaxa(Fimg1, atau, 3); sf_oaxa(Fimg2, az, 1); sf_oaxa(Fimg2, ax, 2); sf_putint(Fimg2, "n3", 1); sf_settype(Fimg1, SF_FLOAT); sf_settype(Fimg2, SF_FLOAT); if(wantwf){ sf_setn(ax, rnx); sf_seto(ax, -(rnx-1)*dx/2.0); sf_oaxa(Ffwf, az, 1); sf_oaxa(Ffwf, ax, 2); sf_putint(Ffwf, "n3", (wfnt-1)/snap+1); sf_putfloat(Ffwf, "d3", snap*wfdt); sf_putfloat(Ffwf, "o3", t0); sf_putstring(Ffwf, "label3", "Time"); sf_putstring(Ffwf, "unit3", "s"); sf_settype(Ffwf, SF_FLOAT); sf_oaxa(Fbwf, az, 1); sf_oaxa(Fbwf, ax, 2); sf_putint(Fbwf, "n3", (wfnt-1)/snap+1); sf_putfloat(Fbwf, "d3", -snap*wfdt); sf_putfloat(Fbwf, "o3", (wfnt-1)*wfdt); sf_putstring(Fbwf, "label3", "Time"); sf_putstring(Fbwf, "unit3", "s"); sf_settype(Fbwf, SF_FLOAT); } nx=rnx+2*nb; nz=rnz+2*nb; nzx=nx*nz; rnzx=rnz*rnx; nk=cfft2_init(pad1, nz, nx, &fnz, &fnx); fnzx=fnz*fnx; if(ns%numprocs==0) nspad=ns; else nspad=(ns/numprocs+1)*numprocs; /* print axies parameters for double check */ sf_warning("cpuid=%d, numprocs=%d, nspad=%d", cpuid, numprocs, nspad); sf_warning("nt=%d, dt=%g, scalet=%d, wfnt=%d, wfdt=%g",nt, dt, scalet, wfnt, wfdt); sf_warning("vnx=%d, nx=%d, dx=%g, nb=%d, rnx=%d", vnx, nx, dx, nb, rnx); sf_warning("nr=%d, ndr=%d, nr0=%g", nr, ndr, nr0); sf_warning("nz=%d, rnz=%d, dz=%g, z0=%g", nz, rnz, dz, z0); sf_warning("spx=%d, spz=%d, gpz=%d", spx, spz, gpz); sf_warning("ns=%d, ds=%g, s0=%g", ns, ds, s0); sf_warning("ntau=%d, dtau=%g, tau0=%g", ntau, dtau, tau0); sf_warning("nzx=%d, fnzx=%d, nk=%d", nzx, fnzx, nk); /* allocate storage and read data */ ww=sf_complexalloc(nt); sf_complexread(ww, nt, Fsrc); sf_fileclose(Fsrc); gpz=gpz+nb; spz=spz+nb; spx=spx+nb; nr0=nr0+nb; trunc=srctrunc/dt+0.5; dd=sf_complexalloc2(nt, nr); if(cpuid==0) dd3=sf_complexalloc3(nt, nr, numprocs); rr=sf_floatalloc(nzx); reflgen(nz, nx, spz, spx, rectz, rectx, repeat, rr); fwf=sf_floatalloc3(rnz, rnx, wfnt); bwf=sf_floatalloc3(rnz, rnx, wfnt); img1=sf_floatalloc3(rnz, vnx, ntau); img2=sf_floatalloc2(rnz, vnx); mig1=sf_floatalloc3(rnz, rnx, ntau); mig2=sf_floatalloc2(rnz, rnx); ccr=sf_floatalloc2(rnz, rnx); sill=sf_floatalloc2(rnz, rnx); curr=sf_complexalloc(fnzx); cwave=sf_complexalloc(nk); cwavem=sf_complexalloc(nk); icfft2_allocate(cwavem); if (taper!=0) { dkz = 1./(fnz*dz); kz0 = -0.5/dz; dkx = 1./(fnx*dx); kx0 = -0.5/dx; nkz = fnz; sf_warning("dkz=%f,dkx=%f,kz0=%f,kx0=%f",dkz,dkx,kz0,kx0); sf_warning("nk=%d,nkz=%d,nkx=%d",nk,nkz,fnx); kx_trs = thresh*fabs(0.5/dx); kz_trs = thresh*fabs(0.5/dz); sf_warning("Applying kz tapering below %f",kz_trs); sf_warning("Applying kx tapering below %f",kx_trs); ktp = sf_floatalloc(nk); /* constructing the tapering op */ for (ix=0; ix < fnx; ix++) { kx = kx0+ix*dkx; for (iz=0; iz < nkz; iz++) { kz = kz0+iz*dkz; ktmp = 1.; if (fabs(kx) > kx_trs) ktmp *= powf((2*kx_trs - fabs(kx))/(kx_trs),2); if (fabs(kz) > kz_trs) ktmp *= powf((2*kz_trs - fabs(kz))/(kz_trs),2); ktp[iz+ix*nkz] = ktmp; } } } /* initialize image tables that would be used for summing images */ #ifdef _OPENMP #pragma omp parallel for private(ix, iz, itau) #endif for(ix=0; ix<vnx; ix++){ for(iz=0; iz<rnz; iz++){ img2[ix][iz]=0.; for(itau=0; itau<ntau; itau++){ img1[itau][ix][iz]=0.; } } } path1=sf_getstring("path1"); path2=sf_getstring("path2"); if(path1==NULL) path1="./mat/left"; if(path2==NULL) path2="./mat/right"; /* shot loop */ for (iturn=0; iturn*numprocs<nspad; iturn++){ is=iturn*numprocs+cpuid; /* read data */ if(cpuid==0){ sf_seek(Fdat, ((off_t) is)*((off_t) nr)*((off_t) nt)*sizeof(float complex), SEEK_SET); if((iturn+1)*numprocs<=ns){ sf_complexread(dd3[0][0], nr*nt*numprocs, Fdat); }else{ sf_complexread(dd3[0][0], nr*nt*(ns-iturn*numprocs), Fdat); for(is=ns; is<nspad; is++) for(ix=0; ix<nr; ix++) for(it=0; it<nt; it++) dd3[is-iturn*numprocs][ix][it]=sf_cmplx(0.,0.); is=iturn*numprocs; } sendbufc=dd3[0][0]; recvbufc=dd[0]; }else{ sendbufc=NULL; recvbufc=dd[0]; } MPI_Scatter(sendbufc, nt*nr, MPI_COMPLEX, recvbufc, nt*nr, MPI_COMPLEX, 0, comm); if(is<ns){ /* effective shot loop */ /* construct the names of left and right matrices */ left=sf_charalloc(strlen(path1)); right=sf_charalloc(strlen(path2)); strcpy(left, path1); strcpy(right, path2); sprintf(number, "%d", is+1); strcat(left, number); strcat(right, number); Fleft=sf_input(left); Fright=sf_input(right); if(!sf_histint(Fleft, "n1", &n) || n != nzx) sf_error("Need n1=%d in Fleft", nzx); if(!sf_histint(Fleft, "n2", &m)) sf_error("No n2 in Fleft"); if(!sf_histint(Fright, "n1", &n) || n != m) sf_error("Need n1=%d in Fright", m); if(!sf_histint(Fright, "n2", &n) || n != nk) sf_error("Need n2=%d in Fright", nk); /* allocate storage for each shot migration */ lt=sf_complexalloc2(nzx, m); rt=sf_complexalloc2(m, nk); sf_complexread(lt[0], nzx*m, Fleft); sf_complexread(rt[0], m*nk, Fright); sf_fileclose(Fleft); sf_fileclose(Fright); /* initialize curr and imaging variables */ #ifdef _OPENMP #pragma omp parallel for private(iz) #endif for(iz=0; iz<fnzx; iz++){ curr[iz]=sf_cmplx(0.,0.); } #ifdef _OPENMP #pragma omp parallel for private(ix, iz, itau) #endif for(ix=0; ix<rnx; ix++){ for(iz=0; iz<rnz; iz++){ mig2[ix][iz]=0.; ccr[ix][iz]=0.; sill[ix][iz]=0.; for(itau=0; itau<ntau; itau++){ mig1[itau][ix][iz]=0.; } } } /* wave */ wave=sf_complexalloc2(fnzx, m); /* snapshot */ if(wantwf && is==snapshot) wantwf=true; else wantwf=false; /* forward propagation */ wfit=0; for(it=0; it<nt; it++){ if(verb) sf_warning("Forward propagation it=%d/%d",it+1, nt); cfft2(curr, cwave); for(im=0; im<m; im++){ #ifdef _OPENMP #pragma omp parallel for private(ik) #endif for(ik=0; ik<nk; ik++){ #ifdef SF_HAS_COMPLEX_H cwavem[ik]=cwave[ik]*rt[ik][im]; #else cwavem[ik]=sf_cmul(cwave[ik],rt[ik][im]); #endif } icfft2(wave[im],cwavem); } #ifdef _OPENMP #pragma omp parallel for private(ix, iz, i, j, im, c) shared(curr, it) #endif for(ix=0; ix<nx; ix++){ for(iz=0; iz<nz; iz++){ i=iz+ix*nz; j=iz+ix*fnz; if(it<trunc){ #ifdef SF_HAS_COMPLEX_H c=ww[it]*rr[i]; #else c=sf_crmul(ww[it],rr[i]); #endif }else{ c=sf_cmplx(0.,0.); } // c += curr[j]; for(im=0; im<m; im++){ #ifdef SF_HAS_COMPLEX_H c += lt[im][i]*wave[im][j]; #else c += sf_cmul(lt[im][i], wave[im][j]); #endif } curr[j]=c; } } if (taper!=0) { if (it%taper == 0) { cfft2(curr,cwave); for (ik = 0; ik < nk; ik++) { #ifdef SF_HAS_COMPLEX_H cwavem[ik] = cwave[ik]*ktp[ik]; #else cwavem[ik] = sf_crmul(cwave[ik],ktp[ik]); #endif } icfft2(curr,cwavem); } } if(it%scalet==0){ #ifdef _OPENMP #pragma omp parallel for private(ix, iz) #endif for(ix=0; ix<rnx; ix++){ for(iz=0; iz<rnz; iz++){ fwf[wfit][ix][iz]=crealf(curr[(ix+nb)*fnz+(iz+nb)]); } } wfit++; } } //end of it /* check wfnt */ if(wfit != wfnt) sf_error("At this point, wfit should be equal to wfnt"); /* backward propagation starts from here... */ #ifdef _OPENMP #pragma omp parallel for private(iz) #endif for(iz=0; iz<fnzx; iz++){ curr[iz]=sf_cmplx(0.,0.); } wfit=wfnt-1; for(it=nt-1; it>=0; it--){ if(verb) sf_warning("Backward propagation it=%d/%d",it+1, nt); #ifdef _OPENMP #pragma omp parallel for private(ix) #endif for(ix=0; ix<nr; ix++){ curr[(nr0+ix*ndr)*fnz+gpz]+=dd[ix][it]; } cfft2(curr, cwave); for(im=0; im<m; im++){ #ifdef _OPENMP #pragma omp parallel for private(ik) #endif for(ik=0; ik<nk; ik++){ #ifdef SF_HAS_COMPLEX_H cwavem[ik]=cwave[ik]*conjf(rt[ik][im]); #else cwavem[ik]=sf_cmul(cwave[ik],conjf(rt[ik][im])); #endif } icfft2(wave[im],cwavem); } #ifdef _OPENMP #pragma omp parallel for private(ix, iz, i, j, im, c) shared(curr, it) #endif for(ix=0; ix<nx; ix++){ for(iz=0; iz<nz; iz++){ i=iz+ix*nz; j=iz+ix*fnz; // c=curr[j]; c=sf_cmplx(0.,0.); for(im=0; im<m; im++){ #ifdef SF_HAS_COMPLEX_H c += conjf(lt[im][i])*wave[im][j]; #else c += sf_cmul(conjf(lt[im][i]), wave[im][j]); #endif } curr[j]=c; } } if (taper!=0) { if (it%taper == 0) { cfft2(curr,cwave); for (ik = 0; ik < nk; ik++) { #ifdef SF_HAS_COMPLEX_H cwavem[ik] = cwave[ik]*ktp[ik]; #else cwavem[ik] = sf_crmul(cwave[ik],ktp[ik]); #endif } icfft2(curr,cwavem); } } if(it%scalet==0){ #ifdef _OPENMP #pragma omp parallel for private(ix, iz) #endif for(ix=0; ix<rnx; ix++){ for(iz=0; iz<rnz; iz++){ bwf[wfit][ix][iz]=crealf(curr[(ix+nb)*fnz+(iz+nb)]); ccr[ix][iz] += fwf[wfit][ix][iz]*bwf[wfit][ix][iz]; sill[ix][iz] += fwf[wfit][ix][iz]*fwf[wfit][ix][iz]; } } wfit--; } } //end of it if(wfit != -1) sf_error("Check program! The final wfit should be -1!"); /* free storage */ free(*rt); free(rt); free(*lt); free(lt); free(*wave); free(wave); free(left); free(right); /* normalized image */ #ifdef _OPENMP #pragma omp parallel for private(ix, iz) #endif for (ix=0; ix<rnx; ix++){ for(iz=0; iz<rnz; iz++){ mig2[ix][iz]=ccr[ix][iz]/(sill[ix][iz]+SF_EPS); // sill[ix][iz]=0.; } } /* time-shift imaging condition */ for(itau=0; itau<ntau; itau++){ //sf_warning("itau/ntau=%d/%d", itau+1, ntau); tau=itau*dtau+tau0; htau=tau/wfdt; for(it=abs(htau); it<wfnt-abs(htau); it++){ #ifdef _OPENMP #pragma omp parallel for private(ix, iz) #endif for(ix=0; ix<rnx; ix++){ for(iz=0; iz<rnz; iz++){ mig1[itau][ix][iz]+=fwf[it+htau][ix][iz]*bwf[it-htau][ix][iz]; // sill[ix][iz]+=fwf[it+htau][ix][iz]*fwf[it+htau][ix][iz]; } // end of iz } // end of ix } // end of it //#ifdef _OPENMP //#pragma omp parallel for private(ix, iz) //#endif /* source illumination */ // for(ix=0; ix<rnx; ix++){ // for(iz=0; iz<rnz; iz++){ // mig1[itau][ix][iz] = mig1[itau][ix][iz]/(sill[ix][iz]+SF_EPS); // } // } } //end of itau /* output wavefield snapshot */ if(wantwf){ for(it=0; it<wfnt; it++){ if(it%snap==0){ sf_floatwrite(fwf[it][0], rnzx, Ffwf); sf_floatwrite(bwf[wfnt-1-it][0], rnzx, Fbwf); } } sf_fileclose(Ffwf); sf_fileclose(Fbwf); } /* add all the shot images that are on the same node */ #ifdef _OPENMP #pragma omp parallel for private(itau, ix, iz) #endif for(itau=0; itau<ntau; itau++){ for(ix=0; ix<rnx; ix++){ for(iz=0; iz<rnz; iz++){ img1[itau][ix+is*nds][iz] += mig1[itau][ix][iz]; } } } #ifdef _OPENMP #pragma omp parallel for private(ix, iz) #endif for(ix=0; ix<rnx; ix++){ for(iz=0; iz<rnz; iz++){ img2[ix+is*nds][iz] += mig2[ix][iz]; } } } // end of is<ns } // end of iturn ////////////////end of ishot MPI_Barrier(comm); cfft2_finalize(); sf_fileclose(Fdat); free(ww); free(rr); free(*dd); free(dd); if(cpuid==0) {free(**dd3); free(*dd3); free(dd3);} free(cwave); free(cwavem); free(curr); free(*ccr); free(ccr); free(*sill); free(sill); free(**fwf); free(*fwf); free(fwf); free(**bwf); free(*bwf); free(bwf); free(**mig1); free(*mig1); free(mig1); free(*mig2); free(mig2); /* sum image */ if(cpuid==0){ sendbuf=(float *)MPI_IN_PLACE; recvbuf=img1[0][0]; }else{ sendbuf=img1[0][0]; recvbuf=NULL; } MPI_Reduce(sendbuf, recvbuf, ntau*vnx*rnz, MPI_FLOAT, MPI_SUM, 0, comm); if(cpuid==0){ sendbuf=MPI_IN_PLACE; recvbuf=img2[0]; }else{ sendbuf=img2[0]; recvbuf=NULL; } MPI_Reduce(sendbuf, recvbuf, vnx*rnz, MPI_FLOAT, MPI_SUM, 0, comm); /* output image */ if(cpuid==0){ sf_floatwrite(img1[0][0], ntau*vnx*rnz, Fimg1); sf_floatwrite(img2[0], vnx*rnz, Fimg2); } MPI_Barrier(comm); sf_fileclose(Fimg1); sf_fileclose(Fimg2); free(**img1); free(*img1); free(img1); free(*img2); free(img2); gettimeofday(&tim, NULL); tend=tim.tv_sec+(tim.tv_usec/1000000.0); sf_warning(">> The computing time is %.3lf minutes <<", (tend-tstart)/60.); MPI_Finalize(); exit(0); }
int main (int argc, char* argv[]) { bool verb; float o1,o2,d1,d2; int n1, n2,n3, shift; int i2,i3; char *unit1,*label1; float *column=NULL,*max=NULL; int *index=NULL; sf_file in=NULL,out=NULL,max_val=NULL; sf_init (argc,argv); in=sf_input("in"); out=sf_output("out"); sf_settype (out, SF_INT); if (NULL != sf_getstring("max_val")) max_val=sf_output("max_val"); if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input"); if (!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input"); if (!sf_histfloat(in,"o1",&o1)) sf_error("No o1= in input"); label1=sf_charalloc(100); unit1=sf_charalloc(100); label1=sf_histstring(in,"label2"); unit1=sf_histstring(in,"unit2"); if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input"); if (!sf_histfloat(in,"d2",&d2)) sf_error("No d2= in input"); if (!sf_histfloat(in,"o2",&o2)) sf_error("No o2= in input"); if (!sf_getint("shift",&shift)) shift=0; /* shift */ if (!sf_getbool("verb",&verb)) verb=false; column = sf_floatalloc(n1); max = sf_floatalloc(n2); index = sf_intalloc(n2); sf_putint(out,"n1",n2); sf_putfloat(out,"o1",o2); sf_putfloat(out,"d1",d2); if (!(label1==NULL)) sf_putstring(out,"label1",label1); if (!(unit1==NULL)) sf_putstring(out,"unit1",unit1); //sf_warning("Son qua d2=%s",label1); sf_putint(out,"n2",1); sf_putstring(out,"o2",""); sf_putstring(out,"d2",""); sf_putstring(out,"label2",""); sf_putstring(out,"unit2",""); if (!(max_val==NULL)) { sf_putint(out,"n1",n2); sf_putfloat(out,"o1",o2); sf_putint(max_val,"n2",1); sf_putstring(max_val,"o2",""); sf_putstring(max_val,"d2",""); sf_putstring(max_val,"label2",""); sf_putstring(max_val,"unit2",""); } /* reading the number of gahters in data*/ n3 = sf_leftsize(in,2); for (i3=0;i3<n3;i3++) { /*gahters loop */ sf_warning("Gather %d/%d",i3+1,n3); for (i2=0;i2<n2;i2++) { sf_floatread(column,n1,in); max[i2]=find_max (column, n1, index+i2); if (d1<0) index[i2]=n1-index[i2]+shift; else index[i2]-=shift; //sf_warning("Son qua, max=%f index=%d",max[i2],index[i2]); } if (!(max_val==NULL)) { sf_floatwrite(max,n2,max_val); } sf_intwrite(index,n2,out); } /* END gahters loop */ exit (0); }
int main(int argc, char* argv[]) { bool verb, save, load; int npml, pad1, pad2, n1, n2; int ih, nh, is, ns, iw, nw, i, j; SuiteSparse_long n, nz, *Ti, *Tj; float d1, d2, **vel, ****image, ****timage, dw, ow; double omega, *Tx, *Tz; SuiteSparse_long *Ap, *Ai, *Map; double *Ax, *Az, **Xx, **Xz, **Bx, **Bz; void *Symbolic, **Numeric; double Control[UMFPACK_CONTROL]; sf_complex ***srce, ***recv; char *datapath, *insert, *append; size_t srclen, inslen; sf_file in, out, source, data, us, ur, timg; int uts, its, mts; sf_timer timer; char *order; sf_init(argc,argv); in = sf_input("in"); out = sf_output("out"); if (!sf_getbool("verb",&verb)) verb=false; /* verbosity flag */ if (verb) timer = sf_timer_init(); else timer = NULL; if (!sf_getbool("save",&save)) save=false; /* save LU */ if (!sf_getbool("load",&load)) load=false; /* load LU */ if (save || load) { datapath = sf_histstring(in,"in"); srclen = strlen(datapath); insert = sf_charalloc(6); } else { datapath = NULL; srclen = 0; insert = NULL; append = NULL; } if (!sf_getint("uts",&uts)) uts=0; /* number of OMP threads */ #ifdef _OPENMP mts = omp_get_max_threads(); #else mts = 1; #endif uts = (uts < 1)? mts: uts; if (verb) sf_warning("Using %d out of %d threads.",uts,mts); if (!sf_getint("nh",&nh)) nh=0; /* horizontal space-lag */ if (!sf_getint("npml",&npml)) npml=10; /* PML width */ if (NULL == (order = sf_getstring("order"))) order="j"; /* discretization scheme (default optimal 9-point) */ fdprep_order(order); /* read model */ if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input."); if (!sf_histint(in,"n2",&n2)) sf_error("No n2= in input."); if (!sf_histfloat(in,"d1",&d1)) sf_error("No d1= in input."); if (!sf_histfloat(in,"d2",&d2)) sf_error("No d2= in input."); vel = sf_floatalloc2(n1,n2); sf_floatread(vel[0],n1*n2,in); /* read source */ if (NULL == sf_getstring("source")) sf_error("Need source="); source = sf_input("source"); if (!sf_histint(source,"n3",&ns)) sf_error("No ns=."); if (!sf_histint(source,"n4",&nw)) sf_error("No nw=."); if (!sf_histfloat(source,"d4",&dw)) sf_error("No dw=."); if (!sf_histfloat(source,"o4",&ow)) sf_error("No ow=."); srce = sf_complexalloc3(n1,n2,ns); /* read receiver */ if (NULL == sf_getstring("data")) sf_error("Need data="); data = sf_input("data"); recv = sf_complexalloc3(n1,n2,ns); /* write output header */ sf_putint(out,"n3",2*nh+1); sf_putfloat(out,"d3",d2); sf_putfloat(out,"o3",(float) -nh*d2); /* output source wavefield */ if (NULL != sf_getstring("us")) { us = sf_output("us"); sf_settype(us,SF_COMPLEX); sf_putint(us,"n3",ns); sf_putstring(us,"label3","Shot"); sf_putstring(us,"unit3",""); sf_putint(us,"n4",nw); sf_putfloat(us,"d4",dw); sf_putfloat(us,"o4",ow); sf_putstring(us,"label4","Frequency"); sf_putstring(us,"unit4","Hz"); } else { us = NULL; } /* output receiver wavefield */ if (NULL != sf_getstring("ur")) { ur = sf_output("ur"); sf_settype(ur,SF_COMPLEX); sf_putint(ur,"n3",ns); sf_putstring(ur,"label3","Shot"); sf_putstring(ur,"unit3",""); sf_putint(ur,"n4",nw); sf_putfloat(ur,"d4",dw); sf_putfloat(ur,"o4",ow); sf_putstring(ur,"label4","Frequency"); sf_putstring(ur,"unit4","Hz"); } else { ur = NULL; } /* output time-shift image derivative */ if (NULL != sf_getstring("timg")) { timg = sf_output("timg"); sf_putint(timg,"n3",2*nh+1); sf_putfloat(timg,"d3",d2); sf_putfloat(timg,"o3",(float) -nh*d2); timage = (float****) sf_alloc(uts,sizeof(float***)); for (its=0; its < uts; its++) { timage[its] = sf_floatalloc3(n1,n2,2*nh+1); } } else { timg = NULL; timage = NULL; } /* allocate temporary memory */ if (load) { Ti = NULL; Tj = NULL; Tx = NULL; Tz = NULL; Ap = NULL; Ai = NULL; Map = NULL; Ax = NULL; Az = NULL; } Bx = (double**) sf_alloc(uts,sizeof(double*)); Bz = (double**) sf_alloc(uts,sizeof(double*)); Xx = (double**) sf_alloc(uts,sizeof(double*)); Xz = (double**) sf_alloc(uts,sizeof(double*)); image = (float****) sf_alloc(uts,sizeof(float***)); for (its=0; its < uts; its++) { image[its] = sf_floatalloc3(n1,n2,2*nh+1); } Numeric = (void**) sf_alloc(uts,sizeof(void*)); /* LU control */ umfpack_zl_defaults (Control); Control [UMFPACK_IRSTEP] = 0; /* loop over frequency */ for (iw=0; iw < nw; iw++) { omega = (double) 2.*SF_PI*(ow+iw*dw); /* PML padding */ pad1 = n1+2*npml; pad2 = n2+2*npml; n = fdprep_n (pad1,pad2); nz = fdprep_nz(pad1,pad2); if (!load) { Ti = (SuiteSparse_long*) sf_alloc(nz,sizeof(SuiteSparse_long)); Tj = (SuiteSparse_long*) sf_alloc(nz,sizeof(SuiteSparse_long)); Tx = (double*) sf_alloc(nz,sizeof(double)); Tz = (double*) sf_alloc(nz,sizeof(double)); Ap = (SuiteSparse_long*) sf_alloc(n+1,sizeof(SuiteSparse_long)); Ai = (SuiteSparse_long*) sf_alloc(nz,sizeof(SuiteSparse_long)); Map = (SuiteSparse_long*) sf_alloc(nz,sizeof(SuiteSparse_long)); Ax = (double*) sf_alloc(nz,sizeof(double)); Az = (double*) sf_alloc(nz,sizeof(double)); } for (its=0; its < uts; its++) { Bx[its] = (double*) sf_alloc(n,sizeof(double)); Bz[its] = (double*) sf_alloc(n,sizeof(double)); Xx[its] = (double*) sf_alloc(n,sizeof(double)); Xz[its] = (double*) sf_alloc(n,sizeof(double)); } if (verb) { sf_warning("Frequency %d of %d.",iw+1,nw); sf_timer_start(timer); } /* LU file (append _lu* after velocity file) */ if (save || load) { sprintf(insert,"_lu%d",iw); inslen = strlen(insert); append = malloc(srclen+inslen+1); memcpy(append,datapath,srclen-5); memcpy(append+srclen-5,insert,inslen); memcpy(append+srclen-5+inslen,datapath+srclen-5,5+1); } if (!load) { /* assemble matrix */ fdprep(omega, n1, n2, d1, d2, vel, npml, pad1, pad2, Ti, Tj, Tx, Tz); (void) umfpack_zl_triplet_to_col (n, n, nz, Ti, Tj, Tx, Tz, Ap, Ai, Ax, Az, Map); /* LU */ (void) umfpack_zl_symbolic (n, n, Ap, Ai, Ax, Az, &Symbolic, Control, NULL); (void) umfpack_zl_numeric (Ap, Ai, Ax, Az, Symbolic, &Numeric[0], Control, NULL); /* save Numeric */ #ifdef _OPENMP (void) umfpack_zl_save_numeric (Numeric[0], append); for (its=1; its < uts; its++) { (void) umfpack_zl_load_numeric (&Numeric[its], append); } if (!save) { (void) remove (append); (void) remove ("numeric.umf"); } #else if (save) (void) umfpack_zl_save_numeric (Numeric[0], append); #endif } else { /* load Numeric */ for (its=0; its < uts; its++) { (void) umfpack_zl_load_numeric (&Numeric[its], append); } } if (save || load) free(append); /* read source and data */ sf_complexread(srce[0][0],n1*n2*ns,source); sf_complexread(recv[0][0],n1*n2*ns,data); /* loop over shots */ #ifdef _OPENMP #pragma omp parallel for num_threads(uts) private(its,ih,j,i) #endif for (is=0; is < ns; is++) { #ifdef _OPENMP its = omp_get_thread_num(); #else its = 0; #endif /* source wavefield */ fdpad(npml,pad1,pad2, srce[is],Bx[its],Bz[its]); (void) umfpack_zl_solve (UMFPACK_A, NULL, NULL, NULL, NULL, Xx[its], Xz[its], Bx[its], Bz[its], Numeric[its], Control, NULL); fdcut(npml,pad1,pad2, srce[is],Xx[its],Xz[its]); /* receiver wavefield */ fdpad(npml,pad1,pad2, recv[is],Bx[its],Bz[its]); (void) umfpack_zl_solve (UMFPACK_At, NULL, NULL, NULL, NULL, Xx[its], Xz[its], Bx[its], Bz[its], Numeric[its], Control, NULL); fdcut(npml,pad1,pad2, recv[is],Xx[its],Xz[its]); /* imaging condition */ for (ih=-nh; ih < nh+1; ih++) { for (j=0; j < n2; j++) { for (i=0; i < n1; i++) { if (j-abs(ih) >= 0 && j+abs(ih) < n2) { image[its][ih+nh][j][i] += crealf(conjf(srce[is][j-ih][i])*recv[is][j+ih][i]); if (timg != NULL) timage[its][ih+nh][j][i] += crealf(2.*I*omega*conjf(srce[is][j-ih][i])*recv[is][j+ih][i]); } } } } } if (verb) { sf_timer_stop (timer); sf_warning("Finished in %g seconds.",sf_timer_get_diff_time(timer)/1.e3); } if (!load) (void) umfpack_zl_free_symbolic (&Symbolic); for (its=0; its < uts; its++) { (void) umfpack_zl_free_numeric (&Numeric[its]); } if (!load) { free(Ti); free(Tj); free(Tx); free(Tz); free(Ap); free(Ai); free(Map); free(Ax); free(Az); } for (its=0; its < uts; its++) { free(Bx[its]); free(Bz[its]); free(Xx[its]); free(Xz[its]); } if (us != NULL) sf_complexwrite(srce[0][0],n1*n2*ns,us); if (ur != NULL) sf_complexwrite(recv[0][0],n1*n2*ns,ur); } #ifdef _OPENMP #pragma omp parallel for num_threads(uts) private(j,i,its) for (ih=-nh; ih < nh+1; ih++) { for (j=0; j < n2; j++) { for (i=0; i < n1; i++) { for (its=1; its < uts; its++) { image[0][ih+nh][j][i] += image[its][ih+nh][j][i]; if (timg != NULL) timage[0][ih+nh][j][i] += timage[its][ih+nh][j][i]; } } } } #endif sf_floatwrite(image[0][0][0],n1*n2*(2*nh+1),out); if (timg != NULL) sf_floatwrite(timage[0][0][0],n1*n2*(2*nh+1),timg); exit(0); }
sf_file sf_output (/*@null@*/ const char* tag) /*< Create an output file structure. --- Should do output after the first call to sf_input. >*/ { sf_file file; char *headname, *dataname, *path, *name, *format; size_t namelen; extern int mkstemp (char *tmpl); extern off_t ftello (FILE *stream); file = (sf_file) sf_alloc(1,sizeof(*file)); if (NULL == tag || 0 == strcmp(tag,"out")) { file->stream = stdout; headname = NULL; } else { headname = sf_getstring (tag); if (NULL == headname) { namelen = strlen(tag)+1; headname = sf_charalloc (namelen); memcpy(headname,tag,namelen); } file->stream = fopen(headname,"w"); if (NULL == file->stream) { free(file); sf_error ("%s: Cannot write to header file %s:",__FILE__,headname); } } file->buf = NULL; /* setbuf(file->stream,file->buf); */ file->pars = sf_simtab_init (tabsize); file->head = NULL; file->headname = NULL; file->pipe = (bool) (-1 == ftello(file->stream)); if (file->pipe && ESPIPE != errno) { free(file); sf_error ("%s: pipe problem:",__FILE__); } dataname = sf_getstring("out"); if (NULL == dataname) dataname = sf_getstring("--out"); if (file->pipe) { file->dataname = sf_charalloc (7); memcpy(file->dataname,"stdout",7); } else if (NULL == dataname) { path = getdatapath(); file->dataname = sf_charalloc (PATH_MAX+NAME_MAX+1); strcpy (file->dataname,path); name = file->dataname+strlen(path); free (path); if (getfilename (file->stream,name)) { if(0==strcmp(name,"/dev/null")){ file->dataname = sf_charalloc (7); memcpy(file->dataname,"stdout",7); } else { namelen = strlen(file->dataname); file->dataname[namelen]='@'; file->dataname[namelen+1]='\0'; } } else { /* Invent a name */ /* stdout is not a pipe, not /dev/null, not a file in this directory. One way to get here is to redirect io to a file not in this directory. For example >../myfile.rsf. In this case getfilename cannot find the file from file->stream by looking in the current directory. The function mkstemp is used to create a unique name to contain the binary data. */ sprintf(name,"%sXXXXXX",sf_getprog()); (void) close(mkstemp(file->dataname)); /* (void) unlink(file->dataname); */ /* old code for named pipes below */ /* if (NULL == headname && -1L == fseek(file->stream,0L,SEEK_CUR) && ESPIPE == errno && 0 != mkfifo (file->dataname, S_IRUSR | S_IWUSR)) sf_error ("%s: Cannot make a pipe %s:", __FILE__,file->dataname); */ } } else { namelen = strlen(dataname)+1; file->dataname = sf_charalloc (namelen); memcpy(file->dataname,dataname,namelen); free (dataname); } sf_putstring(file,"in",file->dataname); file->op = XDR_ENCODE; if (NULL == infiles) { infiles = (sf_file *) sf_alloc(1,sizeof(sf_file)); infiles[0] = NULL; nfile=1; } if (NULL != infiles[0]) { if (NULL == infiles[0]->pars) sf_error("The input file was closed prematurely."); if (NULL != (format = sf_histstring(infiles[0],"data_format"))) { sf_setformat(file,format); free (format); } } else { sf_setformat(file,"native_float"); } if (NULL != headname) free(headname); if (!sf_getbool("--readwrite",&(file->rw))) file->rw=false; if (!sf_getbool("--dryrun",&(file->dryrun))) file->dryrun=false; return file; }