FUNCTION ArrayLocation, Pos, onedim=OneDim, $ sizearr=sizearr, dimension=dimension, zero2one=zero2one ;+ ; NAME: ; ArrayLocation ; PURPOSE: ; Convert array indices from one-dimensional form (as returned by e.g. ; the IDL 'where' function) to a multi-dimensional index ; CATEGORY: ; Toolbox: generic ; CALLING SEQUENCE: ; Loc = ArrayLocation(Pos [, size=size, dim=dim, onedim=OneDim]) ; INPUTS: ; Pos array[*], array[n,*], type integer ; if /OneDim not set: 1D array of one-dimensional array indices ; if /OneDim set: 2D array of multi-dimensional indices ; each row array[*,i] specifies the values in each of the n ; dimensions for a specific array element ; sizearr=SizeArr ; array; type: long integer ; a size vector, i.e. the output of the IDL 'size' function ; using the target array as input. ; dimension=dimension ; array; type: long integer ; the dimensions of the target array ; This can be specified instead of the full size vector ; OPTIONAL INPUT PARAMETERS: ; /onedim if set then Pos is interpreted as a multi-dimensional index ; to be converted to a one-dimensional index. ; if not set Pos is a one-dimensional index to be converted ; to a multi-dimensional index ; /zero2one (only if /onedim NOT set) ; the output is scaled to the range [0,1] in each dimension ; OUTPUTS: ; Loc array, type long integer ; the converted array indices ; INCLUDE: @compile_opt.pro ; On error, return to caller ; CALLS: ; InitVar ; EXAMPLE: ; A = indgen(5,5,5) ; L = ArrayLocation([5,6,7],size=size(A)) ; ; results in L = [ [0,1,0],[1,1,0],[2,1,0] ]. ; ; L = ArrayLocation([ [0,1,0],[1,1,0],[2,1,0] ], size=size(A), /onedim) ; ; results in L = [5,6,7] ; PROCEDURE: ; For a given array of dimension (N1,N2,..,Nn) each element can be addressed ; by specifying a 'multi-dimensional' index [i1,i2,..,in] or by specifying ; a one-dimensional index (as returned by several IDL functions such as where, ; min, max, etc.). ; The information about the target array is specified by providing the output ; of size(Array) as keyword SizeArr (SizeArr[1:n] = [N1,N2,..,Nn]) ; MODIFICATION HISTORY: ; NOV-1998, Paul Hick (UCSD/CASS; pphick@ucsd.edu) ;- InitVar, OneDim, /key n = n_elements(dimension) CASE n EQ 0 OF 0: BEGIN p = 1L*dimension[0] FOR i=1,n-1 DO p = p*dimension[i] sza = [n, dimension, 2, p] END 1: sza = SizeArr ENDCASE szp = size(Pos) CASE OneDim OF 0: BEGIN ; 1-dim ---> multi-dim IF sza[0] EQ 0 THEN RETURN, -1 InitVar, Zero2One, /key CASE Zero2One OF 0: BEGIN szn = replicate(1,sza[0]) Loc = lonarr(sza[0],n_elements(Pos)) END 1: BEGIN szn = float(sza[1:sza[0]]-1) Loc = fltarr(sza[0],n_elements(Pos)) END ENDCASE n = 1 FOR i=1,sza[0] DO BEGIN Loc[i-1,*] = (Pos/n mod sza[i])/szn[i-1] n = n*sza[i] ENDFOR IF szp[0] GE 1 THEN Loc = reform(Loc,[sza[0],szp[1:szp[0]]],/overwrite) END 1: BEGIN ; Multi-dim ---> 1-dim IF sza[0] LT szp[1]-1 THEN RETURN, -1 Pos = reform(Pos,szp[1],szp[szp[0]+2]/szp[1],/overwrite) Loc = reform(Pos[0,*]) n = 1 FOR i=1,szp[1]-1 DO BEGIN n = n*sza[i] Loc = Loc+Pos[i,*]*n ENDFOR Pos = reform(Pos,szp[1:szp[0]],/overwrite) IF szp[0] GE 2 THEN Loc = reform(Loc,szp[2:szp[0]],/overwrite) END ENDCASE RETURN, reform(Loc) & END