[SciPy-dev] Memory mapped files in scipy core

Joe Harrington jh at oobleck.astro.cornell.edu
Wed Nov 23 13:36:42 CST 2005


> This looks like a good routine to put in scipy_core if you don't mind 
> contributing it.

Concat is an IDL routine.  Now that you've unified the community, I'll
be using scipy for my next project, but I'm not a heavy-duty scipy
user yet, so I haven't started converting my library.  So, I've
attached concat below, in case you or someone else finds it important
enough to convert and include.  It's pretty straightforward, but I
know you're very busy.  The idl2py converter might be able to do it.
I'm happy to answer questions about it.  I'm also happy if you don't
want to deal with it, I'm just sending it since you were curious.

--jh--

;+
; NAME:
;	CONCAT
;
; PURPOSE:
;	This function joins arrays along the specified dimension,
;	expanding the dimensions to fit.  Set kill0 or kill1 to ignore
;	argument a0 or a1, respectively, and return the other.  Unless
;	noreset is set, the kill* keyword is then set to 0 so
;	that it isn't set on future passes in loops.  The idea of kill0
;	and kill1 is that expressions like a = concat(2, a, b) can be
;	used in loops to collect values from b (see examples).  This
;	would be unnecessary if delvar worked reasonably, as one could
;	delvar, a before the start of the loop to ensure the routine
;	was reentrant.  Optional arguments get passed to make_array.
;
; CATEGORY:
;	Array manipulation.
;
; CALLING SEQUENCE:
;
;	Result = CONCAT(A0, A1)
;
; INPUTS:
;	A0:	The first array to join.
;	A1:	The second array to join.  Its contents will start at
;		a non-zero Dim index, unless A0 is undefined.
;	Dim:	[1-7] The dimension in which to join A0 and A1.
;
; KEYWORDS:
;	KILL0:		If set, return A1, but first set KILL0 to 0 if
;			NORESET is not set.
;	KILL1:		If set, return A0, but first set KILL1 to 0 if
;			NORESET is not set.
;	NORESET:	If set, do not set KILL0 or KILL1 to zero.
;	_EXTRA:		Additional arguments (such as /NOZERO or
;			VALUE=) are passed to MAKE_ARRAY.
;
; OUTPUTS:
;	This function returns the concatenated array.
;
; SIDE EFFECTS:
;	The KILL0 and KILL1 keywords are changed to zero in the caller
;	if they are set (only affects one of these per call, KILL0 has
;	priority).
;
; RESTRICTIONS:
;	The concatenation is not done "in place", so it is generally
;	faster and uses less memory to pre-allocate arrays and copy
;	into them rather than using this routine, though computer
;	scientists may consider that less pretty.
;
; EXAMPLE:
;	a0 = [1, 2, 3]
;	a1 = [[4, 5], [6, 7]]
;	print, concat(1, a0, a1)
;	       1       2       3       4       5
;	       0       0       0       6       7
;	print, concat(1, a0, a1, value=17)
;	       1       2       3       4       5
;	      17      17      17       6       7
;	print, concat(3, a0, a1, value=17)
;	       1       2       3
;	      17      17      17
;	
;	       4       5      17
;	       6       7      17
;	help, concat(3, a0, a1, value=17)
;	<Expression>    INT       = Array[3, 2, 2]
;
;	To collect values calculated in a loop:
;	for a1 = 0, 6, 1 do $
;	  a0 = concat(1, a0, a1)
;	print, a0
;	       0       1       2       3       4       5       6
;	To re-use a variable inside a function (since delvar does not
;	work there):
;	a0 = [100, 10000]
;	kill0 = 1
;	for a1 = 0, 6, 1 do $
;	  a0 = concat(1, a0, a1, kill0 = kill0)
;	print, a0
;	       0       1       2       3       4       5       6
;	print, kill0
;	       0
;
; MODIFICATION HISTORY:
; 	Written by:	Joseph Harrington, Cornell.  before 2001 Aug 22
;			jh at oobleck.astro.cornell.edu
;	
;	28oct05	jh	Changed sz from intarr to lonarr, added
;			header, changed array parens to brackets.
;-
function concat, dim, a0, a1, $
	kill0=kill0, kill1=kill1, noreset=noreset, $
	_extra=e

if (not keyword_defined(a0)) or keyword_set(kill0) then begin
  if not keyword_set(noreset) then begin
    kill0 = 0
  endif
  return, a1
endif

if (not keyword_defined(a1)) or keyword_set(kill1) then begin
  if not keyword_set(noreset) then begin
    kill1 = 0
  endif
  return, a0
endif

if dim gt 7 or dim lt 1 then begin
	message, 'dim: '+whiteout(string(dim))+' must be 1-7'
endif

; make array of sizes (don't care about dimensionality or total size)
sz = lonarr(8,2)
sz[*, *] = 1L

for j = 0, 1, 1 do begin
	if j eq 0 then begin
		s = size(a0)
	endif else begin
		s = size(a1)
	endelse
	; deal with scalars
	if s[0] eq 0 then begin
		s = [1, 1, s[1], 1]
	endif
	for i = 1, s[0], 1 do begin
		sz[i-1, j] = s[i]
	endfor
	sz[7, j] = s[i]
endfor

; find size and type of final array, and make it
typlam        = max(sz[7,*])
dimlam        = colfunc(sz[0:6, *], 'max')
dimlam[dim-1] = total(sz[dim-1, *])

if keyword_set(e) then begin
	lam = make_array(dimension=dimlam, type=typlam, _extra=e)
endif else begin
	lam = make_array(dimension=dimlam, type=typlam)
endelse

; insert first array
lam[0, 0, 0, 0, 0, 0, 0] = a0

; find place and insert second array
ld = [0, 0, 0, 0, 0, 0, 0]
ld[dim-1] = sz[dim-1, 0]

; there must be a better way...
lam[ld[0], ld[1], ld[2], ld[3], ld[4], ld[5], ld[6]] = a1

return, lam
end




More information about the Scipy-dev mailing list