;+ ; NAME: aprint ; ; PURPOSE: ; print something out formatted as an IDL array (i.e. in IDL source code syntax) ; NOTES: ; ; This is useful if you want to take some variable and embed ; it as a constant into an IDL routine. ; ; CAUTION: Does not work properly on multidimensional arrays yet. ; ; INPUTS: ; KEYWORDS: ; OUTPUTS: ; ; HISTORY: ; Began 2005-07-22 10:12:01 by Marshall Perrin ;- function aprint,thing,_extra=_extra if size(thing,/type) eq 7 then begin ; it's a string, so use quotes. delimiter='", "' delim2 = '"' endif else begin delimiter=', ' delim2 = '' endelse ; handle multidimensional arrays via recursion if (size(thing))[0] gt 1 then begin nd = (size(thing))[0] ; recursion over the last dimension requires some pretty hairy juggling. ; ; first, we need to find out the dimensions dims = size(thing,/dimensions) nz = dims[nd-1] leading_dims = dims[0:nd-2] ; now, we need to swap the last dimension to be first thing2 = transpose(thing,[nd-1,indgen(nd-1)]) ; and squash it into a 2d array thing2 = reform(thing2,[nz,n_elements(thing2)/nz]) retval = "[ " for i = 0,nz-1 do begin if i gt 0 then retval += ", " ;+string(10) ; extract one slice of the 2d array and reflate it to the correct ; dimensions thing3 = reform(thing2[i,*],leading_dims) retval += aprint(thing3) endfor retval += " ]" return,retval endif return,"[ "+delim2+strc(thing,/join,delim=delimiter,_extra=_extra)+delim2+" ]" end