bool initauxtbl(char *aux) { char *filename; FILE * fp ; int pflen, nread, pfargc, targc, i, j ; char *argstr, **targv ; if( !getnpar(0,aux,"s",&filename) ) { fprintf(stderr,"Can't get auxiliary input %s definition\n",aux) ; return 0 ; } fp = fopen(filename, "r") ; if( fp == NULL ) { fprintf(stderr,"Can't open auxiliary input file %s=%s\n",aux,filename); return 0 ; } /* Get the length */ efseek(fp, 0, SEEK_END); pflen = eftell(fp); rewind(fp); argstr = (char *) ealloc1(1+pflen+1, 1); /* Read the parfile */ nread = efread(argstr+1, 1, pflen, fp); if (nread != pflen) { err("%s: fread only %d bytes out of %d from %s", __FILE__,nread,pflen,filename); } efclose(fp); /* Zap whites in parfile to help in parsing */ argstr[0] = '\0' ; pfargc = white2null(argstr, pflen); targc = pfargc ; /* Allocate space for total arg pointers */ targv = (char **) ealloc1(targc, sizeof(char*)); /* Parse the parfile. Skip over multiple NULLs */ for( j=1, i=0; j < pflen; j++) { if( argstr[j] && !argstr[j-1] ) { targv[i++] = argstr + j; } } /* Allocate space for the pointer table */ argtbl = (ArgStruct*) ealloc1(targc, sizeof(ArgStruct)); /* Tabulate targv */ tabulate(targc, targv); return 1 ; }
int main(int argc, char **argv) { char *plotcmd; /* build pswigb command for popen */ float *trbuf; /* trace buffer */ FILE *plotfp; /* fp for plot data */ int nt; /* number of samples on trace */ int ntr; /* number of traces */ int verbose; /* verbose flag */ float d1; /* time/depth sample rate */ float d2; /* trace/dx sample rate */ float f1; /* tmin/zmin */ float f2; /* tracemin/xmin */ cwp_Bool seismic; /* is this seismic data? */ cwp_Bool have_ntr=cwp_false;/* is ntr known from header or user? */ char *tmpdir; /* directory path for tmp files */ cwp_Bool istmpdir=cwp_false;/* true for user given path */ char *cwproot; /* value of CWPROOT environment variable*/ char *bindir; /* directory path for tmp files */ /* Support for irregularly spaced data */ cwp_String key; /* header key word with x2 information */ cwp_String type1=NULL; /* ... its type */ int index1=0; /* ... its index */ Value val; /* value of key */ Value scale; /* Value of scaler */ cwp_String type2=NULL; /* ... its type */ int index2=0; /* ... its index */ cwp_Bool isDepth=cwp_false; /* Is this key a type of depth? */ cwp_Bool isCoord=cwp_false; /* Is this key a type of coordinate? */ cwp_Bool irregular=cwp_false; /* if true, reading x2 from header */ cwp_String x2string; /* string of x2 values */ off_t x2len; /* ... its length */ cwp_String style; /* style parameter */ /* Initialize */ initargs(argc, argv); requestdoc(1); /* Get info from first trace */ if (!gettr(&tr)) err("can't get first trace"); seismic = ISSEISMIC(tr.trid); nt = tr.ns; ntr = tr.ntr; if (ntr) have_ntr = cwp_true; if (!getparint("verbose", &verbose)) verbose=0; if (!getparfloat("d1", &d1)) { if (tr.d1) d1 = tr.d1; else if (tr.dt) d1 = ((double) tr.dt)/1000000.0; else { if (seismic) { d1 = 0.004; warn("tr.dt not set, assuming dt=0.004"); } else { /* non-seismic data */ d1 = 1.0; warn("tr.d1 not set, assuming d1=1.0"); } } } if (!getparfloat("f1", &f1)) { if (tr.f1) f1 = tr.f1; else if (tr.delrt) f1 = (float) tr.delrt/1000.0; else f1 = 0.0; } /* Get or set ntr */ if (getparint("n2", &ntr) || getparint("ntr", &ntr)) have_ntr = cwp_true; if (!getparfloat("d2", &d2)) d2 = (tr.d2) ? tr.d2 : 1.0; if (!getparfloat("f2", &f2)) { if (tr.f2) f2 = tr.f2; else if (tr.tracr) f2 = (float) tr.tracr; else if (tr.tracl) f2 = (float) tr.tracl; else if (seismic) f2 = 1.0; else f2 = 0.0; } if (!getparstring("style", &style)) style = "seismic"; if (getparstring("key", &key)) { type1 = hdtype(key); if ( (index1 = getindex(key)) == -1 ) err("%s: keyword not in segy.h: '%s'", __FILE__, key); irregular = cwp_true; isDepth = IS_DEPTH(key); isCoord = IS_COORD(key); if (isDepth) { index2 = getindex("scalel"); type2 = hdtype("scalel"); } else if (isCoord) { index2 = getindex("scalco"); type2 = hdtype("scalco"); } } /* Look for user-supplied tmpdir */ if (!getparstring("tmpdir",&tmpdir) && !(tmpdir = getenv("CWP_TMPDIR"))) tmpdir=""; if (!STREQ(tmpdir, "") && access(tmpdir, WRITE_OK)) err("you can't write in %s (or it doesn't exist)", tmpdir); /* See if CWPBIN environment variable is not set */ if (!(bindir = getenv("CWPBIN"))) { /* construct bindir from CWPROOT */ bindir = (char *) emalloc(BUFSIZ); /* Get value of CWPROOT environment variable */ if (!(cwproot = getenv("CWPROOT"))) cwproot ="" ; if (STREQ(cwproot, "")) { warn("CWPROOT environment variable is not set! "); err("Set CWPROOT in shell environment as per instructions in CWP/SU Installation README files"); } /* then bindir = $CWPROOT/bin */ sprintf(bindir, "%s/bin", cwproot); } strcat(bindir,"/"); /* put / at the end of bindir */ /* Allocate trace buffer */ trbuf = ealloc1float(nt); if (!have_ntr || irregular ) { /* count traces */ if (verbose) { if (irregular) { warn("trace spacing from header field %s",key); warn("... getting positions"); } else { warn("n2 not getparred and " "ntr header field not set"); warn(".... counting traces"); } } /* Create temporary "file" to hold data */ if (STREQ(tmpdir,"")) { datafp = etmpfile(); if (irregular) x2fp = etmpfile(); if (verbose) warn("using tmpfile() call"); } else { /* user-supplied tmpdir */ char directory[BUFSIZ]; strcpy(directory, tmpdir); strcpy(datafile, temporary_filename(directory)); strcpy(x2file, temporary_filename(directory)); /* Handle user interrupts */ signal(SIGINT, (void (*) (int)) closefiles); signal(SIGQUIT, (void (*) (int)) closefiles); signal(SIGHUP, (void (*) (int)) closefiles); signal(SIGTERM, (void (*) (int)) closefiles); datafp = efopen(datafile, "w+"); if (irregular) x2fp = efopen(x2file, "w+"); istmpdir=cwp_true; if (verbose) warn("putting temporary files in %s", directory); } /* Loop over input data and read to temporary file */ ntr = 0; if(irregular ) { float x,xmin=FLT_MAX,xmax=-FLT_MAX; fprintf(x2fp,"x2="); do { if(ntr) fprintf(x2fp,","); ++ntr; gethval(&tr,index1,&val); if (isDepth || isCoord) { gethval(&tr,index2,&scale); x = (float) (vtod(type1,val) * pow(10.0,vtod(type2,scale))); } else x = vtof(type1,val); fprintf(x2fp,"%g",x); xmin = MIN(xmin,x); xmax = MAX(xmax,x); if (isDepth && STREQ(style,"vsp")) { int i; for (i = 0; i < nt; ++i) tr.data[i] *= -1.0; } efwrite(tr.data, FSIZE, nt, datafp); } while (gettr(&tr)); /* Flip vertical axis if style = vsp */ if (isDepth && STREQ(style,"vsp")) { fprintf(x2fp," x2beg=%g x2end=%g",xmax,xmin); style = "normal"; } if(xmin==xmax) { warn("values in header %s all equal,",key); warn("using f2=%f d2=%f",f2,d2); irregular=cwp_false; have_ntr=cwp_false; efclose(x2fp); if (istmpdir) eremove(x2file); } } else { do { ++ntr; efwrite(tr.data, FSIZE, nt, datafp); } while (gettr(&tr)); /* Save naive user */ if (STREQ(style,"vsp")) { style = "normal"; warn("style=vsp requires key= to be set"); } } } /* Set up pswigb command line */ if (irregular ) { x2len = (off_t) eftell( x2fp ); x2string = (char *) emalloc( ++x2len ); rewind(x2fp); fread(x2string,sizeof(char),x2len,x2fp); plotcmd = (char *) emalloc(x2len+BUFSIZ); if (STREQ(style,"vsp")) { style = "normal"; } sprintf(plotcmd, "%spswigb n1=%d d1=%f f1=%f %s style=%s", bindir, nt, d1, f1, x2string, style); free(x2string); } else { if (STREQ(style,"vsp")) { style = "normal"; } plotcmd = (char *) emalloc(BUFSIZ); sprintf(plotcmd, "%spswigb n1=%d n2=%d d1=%f d2=%f f1=%f f2=%f style=%s", bindir, nt, ntr, d1, d2, f1, f2, style); } for (--argc, ++argv; argc; --argc, ++argv) { if (strncmp(*argv, "d1=", 3) && /* skip those already set */ strncmp(*argv, "d2=", 3) && strncmp(*argv, "f1=", 3) && strncmp(*argv, "f2=", 3) && strncmp(*argv, "style=", 6)){ strcat(plotcmd, " "); /* put a space between args */ strcat(plotcmd, "\""); /* user quotes are stripped */ strcat(plotcmd, *argv); /* add the arg */ strcat(plotcmd, "\""); /* user quotes are stripped */ } } /* Open pipe to pswigb and send the traces */ plotfp = epopen(plotcmd, "w"); free(plotcmd); if (!have_ntr || irregular) { /* send out stored traces one by one */ rewind(datafp); { register int itr; for (itr = 0; itr < ntr; ++itr) { efread (trbuf, FSIZE, nt, datafp); efwrite(trbuf, FSIZE, nt, plotfp); } } } else { /* just pump out traces and let pswigb do the work */ do { efwrite(tr.data, FSIZE, nt, plotfp); } while (gettr(&tr)); } /* Clean up */ epclose(plotfp); if (!have_ntr) { efclose(datafp); if (istmpdir) eremove(datafile); } if (irregular) { efclose(x2fp); if (istmpdir) eremove(x2file); } return EXIT_SUCCESS; }
/* Initialize getpar */ static int getparinit (void) { static int targc; /* total number of args */ static char **targv; /* pointer to arg strings */ static char *pfname; /* name of parameter file */ FILE *pffd; /* file id of parameter file */ int pflen; /* length of parameter file in bytes */ static int pfargc; /* arg count from parameter file */ bool parfile; /* parfile existence flag */ int argstrlen; char *argstr, *pargstr; /* storage for command line and parameter file args */ int nread; /* bytes fread */ int i, j; /* counters */ char *getpfname(); /* return name of parameter file */ int white2null(); /* deliminate arg strings from parameter file with (possibly multiple) NULLs and return a count of the strings */ int tabulate(); /* install symbol table */ tabled = true; /* remember table is built */ /* Check if xargc was initiated */ if(!xargc) err("%s: xargc=%d -- not initiated in main", __FILE__, xargc); /* Space needed for command lines */ for (i = 1, argstrlen = 0; i < xargc; i++) { argstrlen += strlen(xargv[i]) + 1; } /* Get parfile name if there is one */ /* parfile = (pfname = getpfname()) ? true : false; */ if (pfname = getpfname()) { parfile = true; } else { parfile = false; } if (parfile) { pffd = efopen(pfname, "r"); /* Get the length */ efseek(pffd, 0, SEEK_END); pflen = eftell(pffd); rewind(pffd); argstrlen += pflen; } else { pflen = 0; } /* Allocate space for command line and parameter file plus nulls at the ends to help with parsing. */ /* argstr = (char *) calloc((size_t) (1+argstrlen+1), 1); */ argstr = (char *) ealloc1(1+argstrlen+1, 1); if (parfile) { /* Read the parfile */ nread = efread(argstr + 1, 1, pflen, pffd); if (nread != pflen) { err("%s: fread only %d bytes out of %d from %s", __FILE__, nread, pflen, pfname); } efclose(pffd); /* Zap whites in parfile to help in parsing */ argstr[0] = '\0' ; pfargc = white2null(argstr, pflen); } else { pfargc = 0; } /* Total arg count */ targc = pfargc + xargc - 1; /* Allocate space for total arg pointers */ targv = (char **) ealloc1(targc, sizeof(char*)); if (parfile) { /* Parse the parfile. Skip over multiple NULLs */ for (j = 1, i = 0; j < pflen; j++) { if (argstr[j] && !argstr[j-1]) { targv[i++] = argstr + j; } } } else { i = 0; } /* Copy command line arguments */ for (j = 1, pargstr = argstr + pflen + 2; j < xargc; j++) { strcpy(pargstr,xargv[j]); targv[i++] = pargstr; pargstr += strlen(xargv[j]) + 1; } /* Allocate space for the pointer table */ argtbl = (ArgStruct*) ealloc1(targc, sizeof(ArgStruct)); /* Tabulate targv */ tabulate(targc, targv); }
main (int argc, char **argv) { int n1,n2,n3, n1s,n2s,n3s, id1s,id2s,id3s, if1s,if2s,if3s, *ix1s,*ix2s,*ix3s, i1s,i2s,i3s, i1,i2,i3, offset; float *p,*ps; FILE *infp=stdin,*outfp=stdout; /* hook up getpar to handle the parameters */ initargs(argc,argv); askdoc(0); /* get optional parameters */ if (!getparint("n1",&n1)) { if (fseek(infp,0L,2)==-1) err("input file size unknown; specify n1\n"); n1 = eftell(infp)/sizeof(float); } if (!getparint("n2",&n2)) { if (fseek(infp,0L,2)==-1) err("input file size unknown; specify n2\n"); n2 = eftell(infp)/(n1*sizeof(float)); } if (!getparint("n3",&n3)) { if (fseek(infp,0L,2)==-1) err("input file size unknown; specify n3\n"); n3 = eftell(infp)/(n2*n1*sizeof(float)); } ix1s = alloc1int(countparval("ix1s")); if ((n1s=getparint("ix1s",ix1s))==0) { free1int(ix1s); if (!getparint("id1s",&id1s)) id1s = 1; if (!getparint("if1s",&if1s)) if1s = 0; if (!getparint("n1s",&n1s)) n1s = 1+(n1-if1s-1)/id1s; ix1s = alloc1int(n1s); for (i1s=0,i1=if1s; i1s<n1s; i1s++,i1+=id1s) ix1s[i1s] = i1; } ix2s = alloc1int(countparval("ix2s")); if ((n2s=getparint("ix2s",ix2s))==0) { free1int(ix2s); if (!getparint("id2s",&id2s)) id2s = 1; if (!getparint("if2s",&if2s)) if2s = 0; if (!getparint("n2s",&n2s)) n2s = 1+(n2-if2s-1)/id2s; ix2s = alloc1int(n2s); for (i2s=0,i2=if2s; i2s<n2s; i2s++,i2+=id2s) ix2s[i2s] = i2; } ix3s = alloc1int(countparval("ix3s")); if ((n3s=getparint("ix3s",ix3s))==0) { free1int(ix3s); if (!getparint("id3s",&id3s)) id3s = 1; if (!getparint("if3s",&if3s)) if3s = 0; if (!getparint("n3s",&n3s)) n3s = 1+(n3-if3s-1)/id3s; ix3s = alloc1int(n3s); for (i3s=0,i3=if3s; i3s<n3s; i3s++,i3+=id3s) ix3s[i3s] = i3; } /* check parameters */ for (i1s=0; i1s<n1s; i1s++) if (ix1s[i1s]<0 || ix1s[i1s]>n1-1) err("ix1s[%d]=%d is out of bounds!\n",i1s,ix1s[i1s]); for (i2s=0; i2s<n2s; i2s++) if (ix2s[i2s]<0 || ix2s[i2s]>n2-1) err("ix2s[%d]=%d is out of bounds!\n",i2s,ix2s[i2s]); for (i3s=0; i3s<n3s; i3s++) if (ix3s[i3s]<0 || ix3s[i3s]>n3-1) err("ix3s[%d]=%d is out of bounds!\n",i3s,ix3s[i3s]); /* allocate space for input and output arrays */ p = ealloc1float(n1); ps = ealloc1float(n1s); /* loop over 3rd dimension */ for (i3s=0; i3s<n3s; i3s++) { /* loop over 2nd dimension */ for (i2s=0; i2s<n2s; i2s++) { /* find beginning of input array */ offset = (ix2s[i2s]+ix3s[i3s]*n2)*n1*sizeof(float); efseek(infp,offset,0); /* read input array, if it exists */ if (fread(p,sizeof(float),n1,infp)==n1) { /* loop over 1st dimension */ for (i1s=0; i1s<n1s; i1s++) { ps[i1s] = p[ix1s[i1s]]; } /* if input does not exist */ } else { err("no input for ix2s[%d]=%d ix3s[%d]=%d!\n", i2s,ix2s[i2s], i3s,ix3s[i3s]); } /* write trace to output file */ efwrite(ps,sizeof(float),n1s,outfp); } } }
int kirmigs(char *datain, char *dataout, int lseekin, int lseekout, char *timefile, char *ampfile, int iamp, int nt, int nx, int nz, int lt, float itmin, float iix0, float iz0, float idt, float idx, float idz, float ismint, float ixmint, float izmint, float idst, float idxt, float idzt, int nst, int nxt, int nzt, float iofimin, float iofimax, float iofomin, float idofo, int nofo, int *mtrace, int i2p5, int intps, int intpx, int intpz,int intype, float idcdp, int amptype,int mlimit, int lagc, float itpow, float iaper, float iapanl, float iapanr, float iv0, int cdpx0, int dcdpx, char *dipfile, float idxdip, float idxtol) { /* subroutine kmigs */ int ix, iz, it, i; float tminl; float *trace, *trt, *migs, *fold, *tr, *ts, *ar, *as, *sigxt, *sigzt; int *inxt, *inzt; short *ttbl, *atbl; float ascale, tscale; long sx, gx; int ntrace, iofo; int ix0, iof, latbl; float xs, xr, tmp, *trz, *trr,dldt,dl,offout,dlm; float *twk1,*twk2,*awk1,*awk2,*trwk; FILE *tfp, *afp; FILE *infp, *outfp; int mute; int chkcdp; long sy, gy, icdp1; float offset; char *afile, *tfile; int lttbl,llimit; int lmem ; int lwin, mt, notrace, itmp; float rmso, zw; float *dips; int idip; int isize; /* convert double to float */ float tmin = itmin , x0 = iix0 , z0 = iz0 ; float dt = idt , dx = idx , dz =idz ; float smint = ismint , xmint = ixmint , zmint = izmint ; float dst = idst , dxt = idxt , dzt = idzt ; float ofimin = iofimin , ofimax = iofimax ; float ofomin = iofomin , dofo = idofo ; float dcdp = idcdp; float tpow = itpow; float v0 = iv0; float aper = iaper; float apanl = iapanl; float apanr = iapanr; float dxdip = idxdip; float dxtol = idxtol; char chost[MAXHOSTNAMELEN] ; int nxdip; gethostname(chost,MAXHOSTNAMELEN) ; fprintf(stderr,"starting migration at %s for\t%f < offsets <= %f \n", chost, ofomin-dofo*.5,ofomin+(nofo-.5)*dofo); tmp = nx * dx/dxdip; nxdip = (int)tmp; if(nxdip<1) nxdip = 1; if(nxdip>nx) nxdip = nx; dxdip = dx * nx/nxdip; lwin = lagc/(int)(dt*1000.); /* in case the dp starts the migration again, do not remigrate */ isize = 0; if((outfp = fopen(dataout,"r"))!=NULL) { fseek(outfp,0L,SEEK_END); isize= (int) ftell(outfp); fclose(outfp); } if( isize==(nz*sizeof(float)+HDRBYTES)*nx*nofo ) { fprintf(stderr,"output %s already migrated \n",dataout); return 0 ; } /* set mtrace to be -1 in case of error return to kirmig */ *mtrace = -1; infp = fopen(datain,"r"); if( infp == NULL ) { fprintf(stderr,"kirmigs datain=%s not found\n",datain); return 1 ; } outfp = fopen(dataout,"w"); if( outfp == NULL ) { fprintf(stderr,"kirmigs dataout=%s not found\n",dataout); return 1 ; } /* check to see if there is any input data */ efseek(infp,0L,2); notrace = 0; if((eftell(infp)-lseekin)<=0) { warn("no trace in input %s \n",datain); notrace = 1; *mtrace = 0; } if (notrace==1) goto skipmig; /* turn off buffering for outfp */ /* setbuf(outfp,NULL); */ /* fprintf(stderr,"within kirmigs mlimit=%d \n",mlimit); fprintf(stderr,"x0=%f z0=%f nx=%d nz=%d \n",x0,z0,nx,nz); fprintf(stderr,"dx=%f dz=%f \n",dx,dz); fprintf(stderr,"timefile=%s \n",timefile); fprintf(stderr,"ampfile=%s \n",ampfile); fprintf(stderr,"nt=%d dt=%f tmin=%f \n",nt,dt,tmin); fprintf(stderr,"ofomin=%f dofo=%f nofo=%d \n",ofomin,dofo,nofo); fprintf(stderr,"xmint=%f zmint=%f smint=%f \n",xmint,zmint,smint); fprintf(stderr,"dxt=%f dzt=%f dst=%f \n",dxt,dzt,dst); fprintf(stderr,"nxt=%d nzt=%d nst=%d \n",nxt,nzt,nst); fprintf(stderr,"lt=%d \n",lt); fprintf(stderr,"dcdp=%f dxtol=%f \n",dcdp,dxtol); fprintf(stderr,"lseekin=%d lseekout=%d \n",lseekin,lseekout); */ idip = 1; if(dipfile[0]=='N' && dipfile[1]=='U', dipfile[2]=='L' && dipfile[3]=='L') idip=0; /* memory allocations */ lmem = (2*nzt*nxt + nofo*nx*nz + nofo + 4*nx*nz + nx + 2*nz + nzt + 2*lt + nt)*sizeof(float) +(nx + nz) * sizeof(int) + idip*2*nxdip*nz*sizeof(float); llimit = mlimit * 1024 * 1024; if ( lmem > llimit ) { fprintf(stderr,"Need at least memory size mlimit=%d (Bytes)\n", lmem); return 1; } twk1 = (float*) malloc(nzt*nxt*sizeof(float)); if( twk1 == 0 ) return 1 ; twk2 = (float*) malloc(nzt*nxt*sizeof(float)); if( twk2 == 0 ) return 1 ; migs = (float*) malloc(nofo*nx*nz*sizeof(float)); if( migs == 0 ) return 1 ; fold = (float*) malloc(nofo*sizeof(float)); if( fold == 0 ) return 1 ; ts = (float*) malloc(nx*nz*sizeof(float)); if( ts == 0 ) return 1 ; tr = (float*) malloc(nx*nz*sizeof(float)); if( tr == 0 ) return 1 ; as = (float*) malloc(nx*nz*sizeof(float)); if( as == 0 ) return 1 ; ar = (float*) malloc(nx*nz*sizeof(float)); if( ar == 0 ) return 1 ; sigxt = (float*) malloc(nx*sizeof(float)); if( sigxt == 0 ) return 1 ; sigzt = (float*) malloc(nz*sizeof(float)); if( sigzt == 0 ) return 1 ; inxt = (int*) malloc(nx*sizeof(int)); if( inxt == 0 ) return 1 ; inzt = (int*) malloc(nz*sizeof(int)); if( inzt == 0 ) return 1 ; trt = (float*) malloc(nzt*sizeof(float)); if( trt == 0 ) return 1 ; trz = (float*) malloc(nz*sizeof(float)); if( trz == 0 ) return 1 ; trace = (float*) malloc(lt*sizeof(float)); if( trace == 0 ) return 1 ; trwk = (float*) malloc(lt*sizeof(float)); if( trwk == 0 ) return 1 ; trr = (float*) malloc(nt*sizeof(float)); if( trr == 0 ) return 1 ; dips = (float*) malloc(idip*2*nxdip*nz*sizeof(float)); if( dips == 0 ) return 1 ; lttbl = nst*nxt*nzt*sizeof(short); if ( lmem + lttbl > llimit ) { lttbl = 1 * sizeof(short); tfile = (char*) malloc(strlen(timefile)+1); sprintf(tfile,"%s\0",timefile); } else { tfile = "null"; } lmem +=lttbl; ttbl = (short*) malloc(lttbl); if( ttbl == 0 ) return 1 ; if ( iamp == 1 ) { latbl = nst*nxt*nzt*sizeof(short); if ( lmem + latbl > llimit ) { latbl = 1 * sizeof(short); afile = (char*) malloc(strlen(ampfile)+1); sprintf(afile,"%s\0",ampfile); } else { afile = "null"; } awk2 = (float*) malloc(nzt*nxt*sizeof(float)); lmem += nzt*nxt*sizeof(float); if( awk2 == 0 ) return 1 ; awk1 = (float*) malloc(nzt*sizeof(float)); lmem += nzt*sizeof(float); if( awk1 == 0 ) return 1 ; } else { latbl = 1 * sizeof(short); afile = "null"; awk2 = (float*) malloc(latbl); lmem += latbl; if( awk2 == 0 ) return 1 ; awk1 = (float*) malloc(latbl); lmem += latbl; if( awk1 == 0 ) return 1 ; } atbl = (short*) malloc(latbl); lmem += latbl; if( atbl == 0 ) return 1 ; fprintf(stderr,"total memory used (Byte) =%d \n",lmem); if(lttbl==1) fprintf(stderr,"travel time table disk i/o used \n"); if(iamp==1 && latbl==1) fprintf(stderr,"amplitude table disk i/o used \n"); /* read in travel time table and amplitude table */ tfp = fopen(timefile,"r"); if( tfp == NULL ) { fprintf(stderr,"kirmigs: timefile=%s not found\n",timefile); return 1 ; } if ( iamp == 1 && latbl > 1 ) { afp = fopen(ampfile,"r"); if( afp == NULL ) { fprintf(stderr,"kirmigs: ampfile=%s not found\n",ampfile); return 1 ; } /* find scale to scale amplitudes */ ascale = 0.; for(ix=0;ix<nxt*nst;ix++) { fread((char *)trt,sizeof(float),nzt,afp); for(iz=0;iz<nzt;iz++) { if(fabs(trt[iz]) > ascale) ascale=fabs(trt[iz]); } } /* read in amplitudes, scale and store in short *atbl */ fseek(afp,0,0); if(ascale>0.) ascale = 32000./ascale; for(ix=0;ix<nxt*nst;ix++) { fread((char *)trt,sizeof(float),nzt,afp); ix0 = ix*nzt; for(iz=0;iz<nzt;iz++) { tmp = trt[iz]*ascale; atbl[ix0+iz] = (short)tmp; } } } else { ascale = 1.; } if(lttbl > 1 ) { /* read in times, scale and store in short *ttbl */ /* times in ms */ if (dt>=1.) { tscale = 32000./(nt*dt); } else { tscale = 1000. * 32000. / (nt*dt*1000.); } for(ix=0;ix<nxt*nst;ix++) { fread((char *)trt,sizeof(float),nzt,tfp); ix0 = ix*nzt; for(iz=0;iz<nzt;iz++) { tmp = trt[iz]*tscale; if ( tmp < 32000 ) { ttbl[ix0+iz] = (short)tmp; } else { ttbl[ix0+iz] = 32500; } } } if (dt>=1.) { tmin = tmin * 1000.; dl = dt * nt / lt; dlm = dl; } else { tscale = tscale/1000.; dt = dt * 1000.; tmin = tmin * 1000.; dl = dt * nt / lt; dlm = dl; } } else { dl = dt*nt/lt; dlm = dl * 1000.; tscale = 1.; } /* Main loop over traces */ ix = 0; dldt = dl/dt; tminl = tmin / dt; ntrace =0; /* skip lseekin bytes in infp */ fseek(infp,lseekin,0); /* see if cdp and offset are used in migration, instead of sx and gx */ /* read first trace */ fgettr(infp,&tra); sy = tra.sy; gy = tra.gy; if ( sy == gy && dcdp==0. ) { chkcdp = 0; } else { chkcdp = 1; if( dcdp == 0. ) { fprintf(stderr,"dcdp must be specified ! \n"); return 1; } icdp1 = cdpx0; } /* read in dips grid */ if(idip==1) { tmp = dcdpx*nx/nxdip; itmp = (int) tmp; dipsgrid_cpp(dipfile,dips,dips+nz*nxdip,z0,dz,nz, cdpx0,itmp,nxdip); for(iz=0;iz<nxdip*nz*2;iz++) { tmp=dips[iz]; if(tmp>=90.) { tmp = 89.9; } else if (tmp<=-90.) { tmp = -89.9; } dips[iz]=tan(tmp*3.141592654/180.); } } /* initialize mtrace */ *mtrace = 0; do { int nonzerotrace ; /* Load trace into trace (zero-padded) */ memcpy(trr, tra.data, nt*sizeof(float)); /* check if this is a zero trace */ nonzerotrace = 0 ; for(it=0;it<nt;it++) { if(trr[it] != 0.0) { nonzerotrace = 1; break ; } } if( nonzerotrace == 0 ) tra.trid = 0; if( tra.trid != 0 ) { /* apply 2.5-D filter */ if(i2p5==1) f2p5_(trr,&nt); /* apply agc */ if(lwin>0) { mute = (tra.mute-tra.delrt)/tra.dt; if(mute<0) mute=0; if(mute>nt) mute=nt; mt = nt - mute; rmso = 2000.; agc_(trr+mute,&mt,&lwin,trwk,&rmso); } /* apply tpow */ if(fabs(tpow)>0.0001) tp_(trr,&nt,&tpow,&tmin,&dt); /* linearly interpolate input trace */ for(it=0;it<lt;it++) { tmp = tminl+it*dldt; i = (int)tmp; tmp = tmp - i; if(i>=0 && i<nt-1) { trace[it] = (1.-tmp)*trr[i]+tmp*trr[i+1]; } else { trace[it] = 0.; } } /* obtain source and receiver x's from trace hader */ if ( chkcdp == 0 ) { sx = tra.sx; gx = tra.gx; xs = sx; xr = gx; if(tra.scalco>1) { xs = xs*tra.scalco; xr = xr*tra.scalco; } else if (tra.scalco<0) { xs = xs/(-tra.scalco); xr = xr/(-tra.scalco); } } else { xs = (tra.cdp-icdp1)*dcdp + x0 - tra.offset/2.; xr = (tra.cdp-icdp1)*dcdp + x0 + tra.offset/2.; } tmp = (tra.mute-tra.delrt)/dlm + 1.; mute = (int)tmp ; tmp = tra.mute * 0.5 * v0 * 0.001; tmp = tmp*tmp - 0.25 * fabs(xs-xr) * fabs(xs-xr); if ( tmp > 0. ) { zw = sqrt(tmp); } else { zw = 0.; } ix = ix+ 1; /* migration */ tmp = fabs(xs-xr); offset = (tmp-ofomin)/dofo+1.5 ; iofo = (int)offset; if ( (iofo<1 || iofo>nofo) && intype==1 ) break; /* fprintf(stderr,"xr=%f xs=%f at cdp=%d dcdp=%f icdp1=%d x0=%f offset=%d\n", xr,xs,tra.cdp,dcdp,icdp1,x0,tra.offset); */ } ntrace = ntrace + 1; if ( ntrace%1000 == 0 ) fprintf(stderr,"input %d traces processed at %s \n",ntrace,chost); if(tmp>=ofimin && tmp<=ofimax && tra.trid==1 && mute<lt) { kirmig_(trace,&xs,&xr,&tmin,<,&dl, migs,&x0,&z0,&dx,&dz,&nx,&nz, &nofo,&ofomin,&dofo,fold, ttbl,atbl,&xmint,&zmint,&dxt,&dzt,&nxt,&nzt, &nst,&smint,&dst,ts,tr,as,ar,&dxtol, sigxt,sigzt,inxt,inzt,&iamp,mtrace, twk1,twk2,awk1,awk2,&tscale,trwk, &intps,&intpx,&intpz,&mute, &ascale,&type, tfile,afile,&aper,&apanl,&apanr,&zw, dips,&idip,&nxdip,&dxdip); } } while (fgettr(infp,&tra)); fprintf(stderr,"input %d traces processed at %s \n",ntrace,chost); /* free spaces */ free((char *)trt); free((char *)trr); free((char *)ttbl); free((char *)atbl); free((char *)twk1); free((char *)twk2); free((char *)awk1); free((char *)awk2); free((char *)trwk); free((char *)tr); free((char *)ts); free((char *)ar); free((char *)as); free((char *)sigxt); free((char *)sigzt); free((char *)inxt); free((char *)inzt); free((char *)trace); free((char*)dips); if(fabs(tpow)>0.0001) { trace = (float *) malloc(nz*sizeof(float)); for(i=0;i<nz;i++) { tmp = (z0+i*dz)/(z0+nz*0.5*dz); tmp = fabs(tmp); if(tmp==0.) { trace[i] = 0.; } else { trace[i] = pow(tmp,-tpow); } } } ascale=1./ascale; if ( amptype == 0 ) { ascale = ascale/2.; } else { ascale=ascale*ascale; } if ( iamp == 0 ) ascale = 1.; skipmig: /* output traces */ /* skip lseekout bytes in outfp */ fseek(outfp,lseekout,0); /* if no input trace, zero output */ if(notrace==1) { bzero((char*)&tra,(240+nz*sizeof(float))); } /* update trace headers */ tra.ns = nz; tra.trid = 1; if ( dz < 30. ) { tmp = dz * 1000.; tra.dt = (unsigned short) tmp; tmp = z0 * 1000.; tra.delrt = (unsigned short) tmp; } else if (dz <300.) { tmp = dz * 100.; tra.dt = (unsigned short) tmp; tmp = z0 * 100.; tra.delrt = (unsigned short) tmp; } else { tra.dt = (unsigned short) dz; tra.delrt = (unsigned short) z0; } tra.sy = 0; tra.gy = 0; /* scale x coordinate if needed */ itmp = (int) dx; tmp = dx - itmp; if(fabs(tmp)>0.01) { tra.scalco = -100; } else { tra.scalco = 1; } for(iof=0;iof<nofo;iof++) { offout = ofomin + iof*dofo; if(notrace==0) { if(fold[iof]>1.) { tmp = ascale / fold[iof]; } else { tmp = ascale; } } for(ix=0;ix<nx;ix++) { /* update trace headers */ tra.offset = offout; tra.cdp = ix+1 ; tra.tracf = ix+1 ; xs = x0 + ix*dx - offout/2; xr = x0 + ix*dx + offout/2; if (tra.scalco==-100) { xs = xs * 100.; xr = xr * 100.; } tra.sx = xs; tra.gx = xr; tra.ep = tra.sx; tra.fldr = tra.sx; if(notrace==0) { ix0 = ix*nz+iof*nx*nz; for (i=0;i<nz;i++) { trz[i] = migs[ix0+i]*tmp; } if(fabs(tpow)>0.0001) for(i=0;i<nz;i++) trz[i] = trz[i]*trace[i]; memcpy(tra.data, trz, nz*sizeof(float)); } fputtr(outfp,&tra); /* fflush(outfp); */ } } if(notrace==0) { free((char *)migs); free((char *)fold); free((char *)trz); if(fabs(tpow)>0.0001) free((char *)trace); } fclose(infp); fclose(outfp); fprintf(stderr, "kirmig done at %s for\t%f < offsets <= %f for %d live traces\n", chost,ofomin-0.5*dofo,ofomin+(nofo-0.5)*dofo,*mtrace); return 0; }
main (int argc, char **argv) { int nt,nz,nin,nout,nx,ix; float dt,ft,dz,fz,*din,*dout,*zt,*tz; char *intype="",*outtype=""; FILE *infp=stdin,*outfp=stdout; /* hook up getpar to handle the parameters */ initargs(argc,argv); askdoc(2); /* get required parameters */ if (!getparstring("intype",&intype)) err("Must specify intype!\n"); if (!getparstring("outtype",&outtype)) err("Must specify outtype!\n"); /* get optional parameters */ if (!getparint("nt",&nt)) { if (getparint("nz",&nz)) { nt = nz; } else { if (fseek(infp,0L,2)==-1) err("input file size unknown; specify nt or nz\n"); nt = eftell(infp)/sizeof(float); } } if (!getparfloat("dt",&dt)) dt = 1.0; if (!getparfloat("ft",&ft)) ft = 0.0; if (!getparint("nz",&nz)) { if (getparint("nt",&nt)) { nz = nt; } else { if (fseek(infp,0L,2)==-1) err("input file size unknown; specify nt or nz\n"); nz = eftell(infp)/sizeof(float); } } if (!getparfloat("dz",&dz)) dz = 1.0; if (!getparfloat("fz",&fz)) fz = 0.0; /* determine number of samples per input and output trace */ if ( STREQ(intype,"vintt") || STREQ(intype,"vrmst") || STREQ(intype,"zt") ) { nin = nt; } else if ( STREQ(intype,"vintz") || STREQ(intype,"tz") ) { nin = nz; } else { err("invalid intype=%s!\n",intype); } if ( STREQ(outtype,"vintt") || STREQ(outtype,"vrmst") || STREQ(outtype,"zt") ) { nout = nt; } else if ( STREQ(outtype,"vintz") || STREQ(outtype,"tz") ) { nout = nz; } else { err("invalid outtype=%s!\n",outtype); } /* determine number of traces to process */ if (!getparint("nx",&nx)) nx = -1; /* allocate space */ tz = ealloc1float(nz); zt = ealloc1float(nt); din = ealloc1float(nin); dout = ealloc1float(nout); /* set input file pointer to beginning of file */ efseek(infp,0L,0); /* loop over traces */ for (ix=0; ix<nx || nx<0; ix++) { /* read input data */ if (efread(din,sizeof(float),nin,infp)!=nin) break; /* convert input data to zt and tz */ if (STREQ(intype,"vintt")) in_vintt(nt,dt,ft,nz,dz,fz,din,zt,tz); else if (STREQ(intype,"vrmst")) in_vrmst(nt,dt,ft,nz,dz,fz,din,zt,tz); else if (STREQ(intype,"zt")) in_zt(nt,dt,ft,nz,dz,fz,din,zt,tz); else if (STREQ(intype,"vintz")) in_vintz(nt,dt,ft,nz,dz,fz,din,zt,tz); else if (STREQ(intype,"tz")) in_tz(nt,dt,ft,nz,dz,fz,din,zt,tz); else err("invalid intype=%s!\n",intype); /* convert zt and tz to output data */ if (STREQ(outtype,"vintt")) out_vintt(nt,dt,ft,nz,dz,fz,zt,tz,dout); else if (STREQ(outtype,"vrmst")) out_vrmst(nt,dt,ft,nz,dz,fz,zt,tz,dout); else if (STREQ(outtype,"zt")) out_zt(nt,dt,ft,nz,dz,fz,zt,tz,dout); else if (STREQ(outtype,"vintz")) out_vintz(nt,dt,ft,nz,dz,fz,zt,tz,dout); else if (STREQ(outtype,"tz")) out_tz(nt,dt,ft,nz,dz,fz,zt,tz,dout); else err("invalid outtype=%s!\n",outtype); /* write output data */ efwrite(dout,sizeof(float),nout,outfp); } }
int fgettra(FILE *fp, segy *tp, int itr) { static int nsfirst; /* samples on first trace */ static int nsegy; /* size of trace in bytes */ static int ntr; /* number of traces in file */ static bool first=true; /* flag for first entry */ int nread; /* bytes read */ static char card[EBCBYTES]; static char bhdr[BNYBYTES]; static int hdbytes; if (first) { /* first entry; get number of traces */ unsigned short bytesper;/* bytes per float (packed?) */ long length; /* length of file in bytes */ filetype ftype = filestat(fileno(fp)); first = false; switch(ftype) { case TTY: warn("stdin not redirected"); selfdoc(); break; case DISK: /* correct */ break; default: err("%s: input must be disk file", __FILE__); break; } efread(card, 1, 10, fp); if (strncmp(card, "C 1 CLIENT",10)==0 ) { efseek(fp, EBCBYTES+BNYBYTES, 0); hdbytes = EBCBYTES + BNYBYTES; } else { efseek(fp, 0, 0); hdbytes = 0; } if (HDRBYTES != (nread = efread(tp, 1, HDRBYTES, fp))) { err("%s: read only %d bytes of header", __FILE__, nread); } nsfirst = tp->ns; if (nsfirst > SU_NFLTS) { err("%s: trace too long: nsfirst=%d>SU_NFLTS=%d", __FILE__, nsfirst, SU_NFLTS); } if (tp->trid == CHARPACK) bytesper = sizeof(char); else if (tp->trid == SHORTPACK) bytesper = sizeof(short); else bytesper = sizeof(float); nsegy = HDRBYTES + nsfirst * bytesper; efseek(fp, 0, SEEK_END); length = eftell(fp); ntr = (length-hdbytes) / nsegy; } /* end first entry */ /* Check on requested trace number */ if (itr >= ntr) err("%s, trying to read off end of file", __FILE__); /* Position file pointer at start of requested trace */ efseek(fp, itr*nsegy+hdbytes, SEEK_SET); nread = efread(tp, 1, nsegy, fp); if (nread != nsegy) err("%s: read only %d of %d bytes in trace", __FILE__, nread, nsegy); if (tp->ns != nsfirst) warn("%s: header ns field = %d differs from first trace = %d", __FILE__, tp->ns, nsfirst); return ntr; }
bool lds_load(SDL_RWops *f, unsigned int music_offset, unsigned int music_size) { SoundBank *sb; efseek(f, music_offset, SEEK_SET); /* load header */ mode = efgetc(f); if (mode > 2) { fprintf(stderr, "error: failed to load music\n"); return false; } efread(f, &speed, 2, 1); tempo = efgetc(f); pattlen = efgetc(f); for (unsigned int i = 0; i < 9; i++) chandelay[i] = efgetc(f); regbd = efgetc(f); /* load patches */ efread(f, &numpatch, 2, 1); free(soundbank); soundbank = (SoundBank *)malloc(sizeof(SoundBank) * numpatch); for (unsigned int i = 0; i < numpatch; i++) { sb = &soundbank[i]; sb->mod_misc = efgetc(f); sb->mod_vol = efgetc(f); sb->mod_ad = efgetc(f); sb->mod_sr = efgetc(f); sb->mod_wave = efgetc(f); sb->car_misc = efgetc(f); sb->car_vol = efgetc(f); sb->car_ad = efgetc(f); sb->car_sr = efgetc(f); sb->car_wave = efgetc(f); sb->feedback = efgetc(f); sb->keyoff = efgetc(f); sb->portamento = efgetc(f); sb->glide = efgetc(f); sb->finetune = efgetc(f); sb->vibrato = efgetc(f); sb->vibdelay = efgetc(f); sb->mod_trem = efgetc(f); sb->car_trem = efgetc(f); sb->tremwait = efgetc(f); sb->arpeggio = efgetc(f); for (unsigned int j = 0; j < 12; j++) sb->arp_tab[j] = efgetc(f); efread(f, &sb->start, 2, 1); efread(f, &sb->size, 2, 1); sb->fms = efgetc(f); efread(f, &sb->transp, 2, 1); sb->midinst = efgetc(f); sb->midvelo = efgetc(f); sb->midkey = efgetc(f); sb->midtrans = efgetc(f); sb->middum1 = efgetc(f); sb->middum2 = efgetc(f); } /* load positions */ efread(f, &numposi, 2, 1); free(positions); positions = (Position *)malloc(sizeof(Position) * 9 * numposi); for (unsigned int i = 0; i < numposi; i++) { for (unsigned int j = 0; j < 9; j++) { /* * patnum is a pointer inside the pattern space, but patterns are 16bit * word fields anyway, so it ought to be an even number (hopefully) and * we can just divide it by 2 to get our array index of 16bit words. */ Uint16 temp; efread(f, &temp, 2, 1); positions[i * 9 + j].patnum = temp / 2; positions[i * 9 + j].transpose = efgetc(f); } } /* load patterns */ efseek(f, 2, SEEK_CUR); /* ignore # of digital sounds (dunno what this is for) */ unsigned int remaining = music_size - (eftell(f) - music_offset); free(patterns); patterns = (Uint16 *)malloc(sizeof(Uint16) * (remaining / 2)); for (unsigned int i = 0; i < remaining / 2; i++) efread(f, &patterns[i], 2, 1); lds_rewind(); return true; }