;+ ; NAME: ; binedge ; PURPOSE: (one line) ; resample fine (x,y) to coarse (u,v) ; DESCRIPTION: ; resample fine (x,y) to coarse (u,v) ; CATEGORY: ; Spectra ; CALLING SEQUENCE: ; v = resample, x, y, u, DX = dx, DU = du, YERR=yerr, VERR=verr ; INPUTS: ; x: the dependant variable (1-D array) (centers of fine bins) ; y: the unsmoothed independant variable (1-D array) (average of fine bin) ; u: the coarser x's (center of coarse bins) ; OPTIONAL INPUT PARAMETERS: ; none ; KEYWORD INPUT PARAMETERS: ; DX = width of fine bins. If not passed, the bin edges and widths ; are calculated using binedge. ; DU = width of coarse bins. If not passed, the bin edges and widths ; are calculated using binedge. ; Yerr = errors for y (the original fine values) ; KEYWORD OUTPUT PARAMETERS: ; Verr = errors for v (the calculated coarse values) ; OUTPUTS: ; Return v, the y's averaged over bins du wide ; COMMON BLOCKS: ; None ; SIDE EFFECTS: ; RESTRICTIONS: ; x and u have to be monotonically increasing ; PROCEDURE: ; MODIFICATION HISTORY: ; Written Leslie Young, SwRI, Nov 8, 2000 ; Jan 3 2006. Added doc, moved to layoung/spectra, LAY ;- function resample, x, y, u, DX = dx, DU = du, YERR=yerr, VERR=verr debug=0 if keyword_set(dx) then begin xl = x - dx/2. xh = x + dx/2. nx = n_elements(x) end else begin binedge, x, nx, xl, xh, dx end if keyword_set(du) then begin ul = u - du/2. uh = u + du/2. nu = n_elements(u) end else begin binedge, u, nu, ul, uh, du end ; calculate scaling factor s =1/ mean(y * dx) v = fltarr(nu) verr=fltarr(nu) ; begin at the begining iu = 0 & ix = 0 ; skip over initial mismatches while iu le nu-1 and ul(iu) lt xl(ix) do $ iu = iu + 1 while ix le nx-1 and xh(ix) le ul(iu) do $ ix = ix + 1 while iu le nu-1 do begin if debug then $ print, 'new bin ', iu, ' spanning ', [ul[iu],uh[iu]] ; initial partial bin d = xh(ix) - ul(iu) v(iu) = y(ix) * d * s if arg_present(verr) then $ verr(iu) = (yerr(ix) * d * s)^2 if debug then $ print, 'adding ', (xh(ix) - ul(iu))/du(iu), ' of old bin', $ ix, ' spanning ', [xl[ix],xh[ix]], '(initial bin)' ix=ix+1 if ix eq nx then begin v = v / (s*du) if arg_present(verr) then verr=sqrt(verr)/(s*du) return,v end ; interior bins while xh(ix) lt ul(iu) + du(iu) do begin ; test done before 2nd v(iu) = v(iu) + dx(ix) * y(ix) * s if arg_present(verr) then $ verr(iu) = verr(iu) + ( dx(ix) * yerr(ix) * s )^2 if debug then $ print, 'adding ', dx(ix)/du(iu), ' of old bin', $ ix, ' spanning ', [xl[ix],xh[ix]], '(internal bin)' ix = ix + 1 if ix eq nx then begin v = v / (s*du) if arg_present(verr) then verr=sqrt(verr)/(s*du) return,v end end ; final partial bin if ix eq nx then begin v = v / (s*du) if arg_present(verr) then verr=sqrt(verr)/(s*du) return,v end d = uh(iu) - xl(ix) v(iu) = v(iu) + y(ix) * d * s if arg_present(verr) then $ verr(iu) = verr(iu) + (yerr(ix) * d * s)^2 if debug then $ print, 'adding ', d/du[iu], ' of old bin', $ ix, ' spanning ', [xl[ix],xh[ix]], '(final bin)' iu = iu + 1 end v = v / (s*du) if arg_present(verr) then verr=sqrt(verr)/(s*du) return, v end