// PassDataThroughBox(), written by Mike Blanpied, USGS, 2/6/95. // Routine scans a pair (X,Y) of input waves and performs intelligent decimation. Writes out a pair of points // only when X or Y changes by at least the user-specified amounts. Think of those amounts, Xbox and Ybox, // as forming the dimensions of a "box" through which the data passes. The bulk of the routine ("Preliminary // stuff") is a useful template on which to build other data-processing routines. The actual work is done by // Function QuikChop(). This routine does not average over the points which were not written, though such // a feature could easily be added to QuikChop(). Macro PassDataThroughBox(Xwavein,Ywavein,Xwaveout,Ywaveout,Xbox,Ybox,ConservePnt,Qoverwrite) string Xwavein,Ywavein,Xwaveout,Ywaveout variable Xbox,Ybox,ConservePnt,Qoverwrite prompt Xwavein,"X input wave:",popup,WaveList("*", ";", "") prompt Ywavein,"Y input wave:",popup,WaveList("*", ";", "") prompt Xwaveout,"Name of X output wave:" prompt Ywaveout,"Name of Y output wave:" prompt Xbox,"X box size (0=keep all, big=ignore X):" prompt Ybox,"Y box size (0=keep all, big=ignore Y):" // Option to write out the point immediately preceeding the point of interest. // (Usually it's better to "conserve prior points" unless the input data is very smooth.) prompt ConservePnt,"Mode:",popup,"Conserve prior points;Don't conserve prior points" prompt Qoverwrite,"Can overwrite existing waves?",popup,"Yes;No" variable Xwavelength,Ywavelength,Extest,Lout // local variables // Preliminary stuff: PauseUpdate; Silent 1 If (ConservePnt==2) // Recode variable: 1=conserve prior pnt. 0=don't. ConservePnt=0 endif Xwavelength=numpnts($Xwavein) // Get input wave lengths. Ywavelength=numpnts($Ywavein) if (Xwavelength!=Ywavelength) // Check for user errors. Abort "** Waves not of equal length **" endif if((cmpstr(Xwavein,Xwaveout)==0)||(cmpstr(Ywavein,Ywaveout)==0)) Abort " Old and new waves must have different names." endif print "length of input waves = ",Xwavelength if(Qoverwrite==1) Make/o/d/n=(Xwavelength) $Xwaveout,$Ywaveout // Make output waves, overwrite allowed else Extest=exists(Xwaveout) // Oops: tried to overwrite. if(Extest!=0) Abort "** X output wavename in use. Extest = "+num2str(Extest)+" **" endif Extest=exists(Ywaveout) if(Extest!=0) Abort "** Y output wavename in use. Extest = "+num2str(Extest)+" **" endif Make/d/n=(Xwavelength) $Xwaveout,$Ywaveout // Make output waves endif print "New waves made." // Doing the real work: print " Calling function to process waves." Lout=QuikChop($Xwavein,$Ywavein,$Xwaveout,$Ywaveout,Xbox,Ybox,Xwavelength,ConservePnt) // Cleaning up: print "Reduced length of new waves = ",Lout+1 DeletePoints (Lout+1), (Xwavelength-Lout), $Xwaveout,$Ywaveout End Function QuikChop(Xwavein,Ywavein,Xwaveout,Ywaveout,Xbox,Ybox,Xwavelength,ConservePnt) Wave Xwavein,Ywavein,Xwaveout,Ywaveout variable Xbox,Ybox,Xwavelength,ConservePnt variable iin=0,iout=0,Lskip=0,dx,dy Xwaveout[0]=Xwavein[0] // copy first point. Ywaveout[0]=Ywavein[0] do // top of do/while loop iin+=1 dx=abs(Xwavein[iin]-Xwaveout[iout]) // find change since last output point dy=abs(Ywavein[iin]-Ywaveout[iout]) if(dx >= Xbox) // if X has changed enough... if(ConservePnt) // Output last skipped point if being conservative. if(Lskip>0) // ...but only if any are skipped. Xwaveout[iout+1]=Xwavein[iin-1] Ywaveout[iout+1]=Ywavein[iin-1] iout+=1 // Increment skipped point counter endif endif Xwaveout[iout+1]=Xwavein[iin] // Output the point that fell outside of the box. Ywaveout[iout+1]=Ywavein[iin] iout+=1 Lskip=0 // Reset skipped point counter else if(dy >= Ybox) // if Y has changed enough... if(ConservePnt) // Output last skipped point if being conservative. if(Lskip>0) // ...but only if at least one point was skipped. Xwaveout[iout+1]=Xwavein[iin-1] // Output the point that fell outside of the box. Ywaveout[iout+1]=Ywavein[iin-1] iout+=1 endif endif Xwaveout[iout+1]=Xwavein[iin] Ywaveout[iout+1]=Ywavein[iin] iout+=1 Lskip=0 else Lskip+=1 endif endif while (iin<=(Xwavelength-2)) // as long as condition is true, loop if(Lskip>1) // End of input file: make sure last point got copied. Xwaveout[iout+1]=Xwavein[Xwavelength-1] Ywaveout[iout+1]=Ywavein[Xwavelength-1] iout+=1 endif return iout // return (length-1) of new waves. End