FUNCTION SubArray, L, element=Element, dimension=Dimension, $ lastelement=LastElement, lastdimension=LastDimension, $ add=add, multiply=multiply, replace=replace, clear=clear,$ type=type, value=value ;+ ; NAME: ; SubArray ; PURPOSE: ; Extract/add/multiply subarrays in a multi-dimensional array ; CATEGORY: ; Array strangling ; CALLING SEQUENCE: ; R = SubArray(L) Extract subarray with dimension=0, element=0 ; R = SubArray(L,add=1) Add 1 to subarray with dimension=0, element=0 ; INPUTS: ; L any multi-dimensional array ; OPTIONAL INPUT PARAMETERS: ; dimension=dimension scalar; type: integer ; identifies one of the dimensions, 0,..,(size(L))[0]-1 ; /lastdimension selects the trailing dimension of L, i.e. ; equivalent to dimension=(size(L))[0]-1 ; (/lastdimension takes precedence over the dimension keyword) ; ; If neither 'dimension' nor 'lastdimension' are used, then dimension=0 is assumed ; ; element=element scalar; type: integer ; identifies one of the elements in the selected dimension, ; 0,..,(size(L))[Dimension+1]-1 ; /lastelement selects the last element of the selected dimension, i.e. ; equivalent to element=(size(L))[Dimension+1]-1. ; ; If neither 'element' nor 'lastelement' are used, then element=0 is assumed ; ; add=add scalar or array with same # elements as the subarray identified ; by (dimension, element). 'Add' will be added to the subarray. ; multiply=multiply scalar or array with same # elements as the subarray identified ; by (dimension, element). 'multiply' will be multiplied to ; the subarray.'multiply' is executed prior to 'add' ; replace=Replace replaces elements ; Takes precedence over 'add' and 'multiply' ; /clear sets elements to zero (numerical input) or null-string (string input) ; Take precedence over 'multiply' ; ; type=type makes a subarray of the appropriate type (filled with zeroes) ; ; If neither of 'type','add','multiply','replace' and 'clear' is used, then ; the selected subarray will be extracted. ; OUTPUTS: ; If 'add' or 'multipy' or 'replace', or 'clear' are used: ; Input array with the selected subarray modified. ; If 'type is set then a subarray of the selected type is returned ; Otherwise: ; Array with one dimension less than the input array, corresponding ; to the selected subarray. ; INCLUDE: @compile_opt.pro ; On error, return to caller ; CALLS: ; InitVar, IsType ; EXAMPLES: ; L = indgen(2,3,4) ; R = SubArray( L, dimension=1, element=2) ; returns L[*,2,*] as an 2x4 array. ; R = SubArray( L, dimension=1, element=2, add=1) ; returns L with one added to L[*,2,*]. ; PROCEDURE: ; Trivial ; MODIFICATION HISTORY: ; MAR-1998, Paul Hick (UCSD/CASS) ; OCT-2004, Paul Hick (UCSD/CASS) ; Modifications to allow string input for /clear and /replace ; MAR-200r, Paul Hick (UCSD/CASS, pphick@ucsd.edu) ; Added type=type keyword. ;- InitVar, clear , /key InitVar, LastDimension , /key InitVar, LastElement , /key extract = 1-clear AND $ IsType(replace , /undefined) AND $ IsType(add , /undefined) AND $ IsType(multiply , /undefined) make = IsType(type,/defined) OR IsType(value,/defined) s = size(L) CASE s[0] GT 0 OF 0: BEGIN ; First deal with special case of scalar input L CASE 1 OF make : R = (make_array(dim=[1],type=type,value=value))[0] extract: R = L clear : IF IsType(L,/string) THEN R = '' ELSE R = 0 IsType(replace ,/defined): R = replace IsType(add ,/defined): IF IsType(multiply,/defined) THEN R = L*multiply+add ELSE R = L+add IsType(multiply ,/defined): R = L*multiply ENDCASE END 1: BEGIN CASE IsType(Dimension, /defined) OF 0: IF LastDimension THEN Dim = s[0]-1 ELSE Dim = 0 1: Dim = Dimension[0] ENDCASE CASE IsType(Element, /defined) OF 0: IF LastElement THEN Ele = s[1+Dim]-1 ELSE Ele = 0 1: Ele = Element[0] ENDCASE Dim = (Dim > 0) < (s[0]-1) Ele = (Ele > 0) < (s[1+Dim]-1) CASE Dim OF 0 : i = indgen(s[0]) s[0]-1: i = [s[0]-1,indgen(s[0]-1)] ELSE : i = [Dim,indgen(Dim),Dim+1+indgen(s[0]-Dim-1)] ENDCASE ; Size vector of input array after dimension DIM has been ; to shifted to the front (becoming the first dimension) s = [s[0],(s[1:s[0]])[i],s[s[0]+1],s[s[0]+2]] CASE 1 OF make: BEGIN CASE 1 OF s[0] EQ 1: R = (make_array(dim=[1],type=type,value=value))[0] ELSE : R = reform(make_array(dim=s[2:s[0]],type=type,value=value)) ENDCASE END extract: BEGIN CASE 1 OF s[0] EQ 1: R = L[Ele] Dim EQ 0: R = reform((reform(L,s[1],s[s[0]+2]/s[1]))[Ele,*],s[2:s[0]]) ELSE : R = reform((reform(transpose(L,i),s[1],s[s[0]+2]/s[1]))[Ele,*],s[2:s[0]]) ENDCASE END ELSE: BEGIN IF Dim NE 0 then R = transpose(L,i) ELSE R = L R = reform(R,s[1],s[s[0]+2]/s[1]) CASE 1 OF clear : IF IsType(R,/string) THEN R[Ele,*] = '' ELSE R[Ele,*] = 0 IsTYpe(replace ,/defined): R[Ele,*] = replace IsType(add ,/defined): IF IsType(multiply,/defined) THEN R[Ele,*] = R[Ele,*]*multiply+add ELSE R[Ele,*] = R[Ele,*]+add IsType(multiply ,/defined): R[Ele,*] = R[Ele,*]*multiply ENDCASE R = reform(R,s[1:s[0]]) IF Dim NE 0 THEN R = transpose(R,sort(i)) END ENDCASE END ENDCASE RETURN, R & END