function Cv2Grid, ZP, nX, nY, dist=Dist, zflag=Zflag, noflag=NoFlag, xp=XP, yp=YP, position=position ;XB,YB,XE,YE @compile_opt.pro ; On error, return to caller ;+ ; NAME: ; Cv2Grid ; PURPOSE: ; Generate function values in a regular grid of NX by NY points. ; The input function values are specified on a set random points XP,YP ; with function values ZP, or as a 2-D array ZP ; CALLING SEQUENCE: ; Z = Cv2Grid (ZP, nX,nY, dist=Dist, xp=XP, yp=YP, pos=pos, zflag=Zflag, noflag=NoFlag) ; Z = Cv2Grid (ZP, nX,nY, dist=Dist, zflag=Zflag, noflag=NoFlag) ; INPUTS: ; ZP array[*] or array[*,*]; type:float ; if array[*] : function values for a 'random' set of data points ; if array[*,*]: any 2-dim array to be rebinned ; nX scalar; type: integer ; nY scalar: type: integer ; size of regular output array ; OPTIONAL INPUTS: ; dist=Dist ; scalar; type: float ; data points closer than abs(Dist) grid ; spacings from an output grid point are included ; in the averaging ; xp=XP array[*]; type: float ; yp=YP array[*]; type: float ; X/Y-coordinates of points in the random set in user-specified units ; position=[XB,YB,XE,YE] ; array[4]; type: float ; XB,YB) X/Y-coordinates of grid point (0,0) in user units ; XE,YE X/Y-coordinates of grid point (nX-1,nY-1) in user units ; zflag=ZFlag ; scalar; type: float; default: !values.f_nan ; value used to identify invalid fnc-values in in- and output Z ; /noflag if set, no fnc-values are flagged as 'bad' ; OUTPUTS: ; Z array[nX,nY]; type: float ; grid function values. ; If no function value was calculated for a particular grid point ; the value Zflag is returned ; RESTRICTIONS: ; The user units for XB,XE,YB,YE should be the same as for XP and YP ; PROCEDURE: ; > If both keywords xp and yp are present, then zp is assumed to be 1-dim ; array of 'random' points. Otherwise zp is assumed to be a 2-dim array. ; > The output grid defines a regular grid of nX by nY squares. ; > The function values Z are calculated by averaging over points ZP ; inside a grid square. ; MODIFICATION HISTORY: ; JAN-1995, Paul Hick (UCSD); based on Fortran routine CONSTRUCT_GRID ;- if not keyword_set(XP) and not keyword_set(YP) then begin a = size(Z) if a[0] ne 2 then message, 'input must be a 2-D array mX = a[1] & mY = a[2] ; Intercept special case: no averaging, no flagging if nX eq mX and nY eq mY and Dist eq 1. and NoFlag then $ return, Z $ else $ return, Cv2Grid(Z, nX,nY, dist=Dist, $ xp = indgen(mX)#replicate(1,mY), $ yp = replicate(1,mX)#indgen(mY), $ pos = [0,0,mX-1,mY-1], zflag=ZFlag, noflag=NoFlag) endif Noflag = keyword_set(NoFlag) if n_elements(ZFlag) eq 0 then ZFlag = !values.f_nan else ZFlag = float(ZFlag[0]) dX = (position[2]-position[0])/(nX-1.0) ; Grid spacings in user units dY = (position[3]-position[1])/(nY-1.0) Dist = Dist > 0. xDis = abs(dX*Dist) yDis = abs(dY*Dist) Z = replicate(ZFlag,nX,nY) ; Clear arrays associated with new grid for i=0,nX-1 do begin X = XB+i*dX for j=0,nY-1 do begin Y = YB+j*dY n = where( (NoFlag or ZP ne ZFlag) and abs(XP-X) lt xDis and abs(YP-Y) lt yDis ) if n[0] ne -1 then Z[i,j] = total(ZP[n])/n_elements(n) endfor endfor return, Z & end