#pragma rtGlobals=1 // Use modern global access method. Macro rebin_wave(inwave,nchan,renormalize) String inwave Prompt inwave,"input wave for rebinning",popup, WaveList("*", ";", "") Variable nchan=10 Prompt nchan,"number of points in input wave to sum together" Variable renormalize=2 Prompt renormalize,"renormalize result",popup,"true;false" Silent 1 ; DelayUpdate if (nchan<=1) return endif String outwave = inwave+"_"+num2istr(nchan) Variable xx,ii Variable nin=numpnts($inwave) Variable nout = ceil(nin/nchan) //number of points in outwave wave Variable lo_edge //used for scaling the $outwave Variable i1,i2 //string indicies String Wunits = WaveInfo($inwave, 0) //obtain both units for the SetScale i1 = strsearch(Wunits,"XUNITS:",0) + 7 i2 = strsearch(Wunits, ";",i1) - 1 String W_xunits = Wunits[i1,i2] duplicate/O $inwave $outwave SetScale/P x 0,1,"", $outwave $outwave = sum($outwave,p*nchan,p*nchan+nchan-1) Redimension/N=(nout) $outwave lo_edge = (nchan-1)/2 * deltax($inwave) + leftx($inwave) //find left edge for scaling SetScale/P x, (lo_edge), (deltax($inwave)*nchan), W_xunits, $outwave if (renormalize==1) $outwave /= nchan //renormalize $outwave endif End Macro Macro GetScaleFactorBetweenWaves(i1,i2,cc1,ee1,cc2,ee2) Variable i1, i2 // range in points of (cc1,ee1) to use Prompt i1, "first point to use of Wave1" Prompt i2, "last point to use of Wave1" String ee1,cc1 // first (x,y) pair Prompt cc1,"Wave1 Y values" Prompt ee1,"Wave1 X values" String ee2,cc2 // second (x,y) pair Prompt cc2,"Wave2 Y values" Prompt ee2,"Wave2 X values" Silent 1 if (!monotonic($ee2)) Abort "the wave "+ee2+" is NOT monotonic" endif Variable scale = ScaleFactorBetweenWaves(i1,i2,$cc1,$ee1,$cc2,$ee2) print " the scaling is: "+cc1+" = "+cc2+" *",scale EndMacro Function ScaleFactorBetweenWaves(i1,i2,cc1,ee1,cc2,ee2) Variable i1, i2 // range in points of (cc1,ee1) to use Wave ee1,cc1 // first (x,y) pair Wave ee2,cc2 // second (x,y) pair // // This routine based on min of SUM{ [Yi - a*f(Xi)]^2 } = V // dV/da = SUM{ 2*[Yi - a*f(Xi)] * [-f(Xi) } // dV/da = -2 * SUM{ Yi*f(Xi) - a*f(Xi)^2 } // for dV/da = 0 implies // 0 = -2 * SUM{ Yi*f(Xi) - a*f(Xi)^2 } // 0 = SUM{ Yi*f(Xi) - a*f(Xi)^2 } // 0 = SUM{ Yi*f(Xi) } - a*SUM{ f(Xi)^2 } // SUM{ Yi*f(Xi) } = a*SUM{ f(Xi)^2 } // // a = SUM{ Yi*f(Xi) } / SUM{ f(Xi)^2 } // Variable yf // SUM{ Yi*f(Xi) } Variable f2 // SUM{ f(Xi)^2 } Variable Ncc1 = numpnts(cc1) if (!WaveExists(cc1) || !WaveExists(ee1) || !WaveExists(cc2) || !WaveExists(ee2)) return NaN endif if ((i2<1) || (i2>=Ncc1)) i2 = Ncc1-1 endif Variable fXi // f(Xi) yf = 0 f2 = 0 Variable i=i1 do fXi = interp(ee1[i], ee2, cc2) yf += cc1[i] * fXi f2 += fXi^2 i += 1 while (i<=i2) return yf/f2 End Function/T xxexpandRange(range) String range Variable i1,i2,i String str,out="" Variable N=ItemsInList(range,",") Variable j=0 do str = StringFromList(j, range, ",") Variable m=-1 // remove any leading white space do m += 1 while (char2num(str[m])<=32) str = str[m,strlen(str)-1] // now check str to see if it is a range like "20-23" i1 = str2num(str) i = strsearch(str,"-",strlen(num2str(i1))) // position of "-" after first number if (i>0) i2 = str2num(str[i+1,inf]) i = i1 do out += num2str(i)+";" i += 1 while (i<=i2) else out += num2str(i1)+";" endif j += 1 while (ja[i]) return 0 endif i += 1 while (i0) // true when yErr wave given Variable root_N=(!cmpstr("_root_N_",yErr)) // used sqrt(N) for error if (root_N) err = 0 endif if (exists(yName)!=1 || exists(xName)!=1) Abort "trying to combine waves that do not exist!" endif Wave yw=$yName Wave xw=$xName if (numpnts(yw)!=numpnts(xw)) Abort "'"+yName+"' and '"+xName+"' must be the same length" endif if (err) Wave yew=$yErr if (numpnts(yw)!=numpnts(yew)) Abort "'"+yName+"' and '"+yErr+"' must be the same length" endif endif if (!cmpstr(yName,xName)) Abort "xwave and ywave must be different" endif if (err) sort xw,xw,yw,yew else sort xw,xw,yw endif // remove all NaN's in either xw, yw, or yew Variable i = numpnts(xw)-1 // start checking from the end of the waves if (err) do if (numtype(xw[i]+yw[i]+yew[i])==2)// found a NaN DeletePoints i, 1, yw,xw,yew endif i -= 1 while(i>=0) else do if (numtype(xw[i]+yw[i])==2) // found a NaN DeletePoints i, 1, yw,xw endif i -= 1 while(i>=0) endif Variable sumY,sumX,n,j Variable sumE, sigma2 i=max(0,BinarySearch(xw,firstX)) do if( abs(xw[i+1]-xw[i])0) // true when yErr wave given Variable root_N=(!cmpstr("_root_N_",yErr)) // used sqrt(N) for error if (root_N) err = 0 endif if (exists(yName)!=1 || exists(xName)!=1) Abort "trying to combine waves that do not exist!" endif Wave yw=$yName Wave xw=$xName if (numpnts(yw)!=numpnts(xw)) Abort "'"+yName+"' and '"+xName+"' must be the same length" endif if (err) Wave yew=$yErr if (numpnts(yw)!=numpnts(yew)) Abort "'"+yName+"' and '"+yErr+"' must be the same length" endif endif if (!cmpstr(yName,xName)) Abort "xwave and ywave must be different" endif if (err) sort xw,xw,yw,yew else sort xw,xw,yw endif Variable Ndups=0 // number of duplicate sets found Variable sumY,sumX,n,j,i=BinarySearch(xw,firstX) Variable sumE, sigma2 i = max(0,i) // print "starting at point",i do if( abs(xw[i+1]-xw[i]) //// Concatenate Waves version 1.01 //// Feb. 2, 1997, JW added test for text waves //// Nov. 20, 2000, JZT modified to use StringFromList & NumberByKey, rather than // //// ConcatenateWaves(w1, w2) //// Tacks the contents of w2 on the to end of w1. //// If w1 does not exist, it is created. //// This is designed for 1D waves only. //Function ConcatenateWaves(w1, w2) // String w1, w2 // // Variable numPoints1, numPoints2 // // if (Exists(w1) == 0) // Duplicate $w2, $w1 // else // String wInfo=WaveInfo($w2, 0) // Variable WType=NumberByKey("NUMTYPE", wInfo) // numPoints1 = numpnts($w1) // numPoints2 = numpnts($w2) // Redimension/N=(numPoints1 + numPoints2) $w1 // if (WType) // Numeric wave // Wave/C ww1=$w1 // Wave/C ww2=$w2 // ww1[numPoints1, ] = ww2[p-numPoints1] // else // Text wave // Wave/T tw1=$w1 // Wave/T tw2=$w2 // tw1[numPoints1, ] = tw2[p-numPoints1] // endif // endif //End // //// ConcatenateWavesInList(dest, wl) //// Makes a dest wave that is the concatenation of the source waves. //// Overwrites the dest wave if it already exists. //// wl is assumed to contain at least one wave name. //// This is designed for 1D waves only. //Function ConcatenateWavesInList(dest, wl) // String dest // name of output wave // String wl // semicolon separated list of waves ("w0;w1;w2;") // // Variable i // for walking through wavelist // String theWaveName // Variable destExisted // // destExisted = Exists(dest) // if (destExisted) // Redimension/N=0 $dest // endif // // i = 0 // do // theWaveName = StringFromList(i, wl, ";") // if (strlen(theWaveName) == 0) // break // no more waves // endif // if (cmpstr(theWaveName, dest) != 0) // don't concat dest wave with itself // ConcatenateWaves(dest, theWaveName) // endif // i += 1 // while (1) //End