On Thursday, June 16, 2016 at 10:59:02 AM UTC-4, Markus Schmassmann wrote:
> is there any way to get IDL to issue a warning or throw an error in for
> mismatched arrays like those below?
>
> indgen(2,2)+indgen(3,3)
I call the following routine, if there's any doubt about the size of the arrays I'm using:
;+
;**************************************************
FUNCTION ArrayTruncation, a, b, c, d
;**************************************************
;~ Detects whether array operations on a set of variables causes silent truncation. [Programming]
;
; IDL silently truncates the results of array operations to the smaller of the
; array operands. This is almost never what I want! It's particularly insidious
; when one of the operands is a one-element array produced by something like where().
;
; This routine detects such array truncation. It may be called with two, three or four arguments.
;
; Examples
; IDL> print, ArrayTruncation( [1,2,3], 4)
; 0
; IDL> print, ArrayTruncation( [1,2,3], [4])
; 1
; IDL> print, ArrayTruncation( [1,2,3], [4,5,6], 7)
; 0
; IDL> print, ArrayTruncation( [1,2,3], [6], 7)
; 1
; IDL> print, ArrayTruncation( [1,2,3], [6], 7, [4,5,6])
; 1
;
; Notes;
; 1) The method is to check whether the sum of the arguments has the same number of elements as the
; argument with the largest number of elements.
;
; See also:
;-
; Modification history:
;
; Oct 30 2007 W. Rigby, original.
; May 23 2008 changed oneliner category.
; Mar 11 2009 changed oneliner.
; Jul 27 2011 added a fourth argument
; Sep 16 2016 tweaked error messages
compile_opt DEFINT32, STRICTARRSUBS, STRICTARR
On_Error, !my_on_error
MsgPrefix = "[ArrayTruncation] "
case n_params() of
2: begin
na = n_elements(a)
nb = n_elements(b)
if ((na EQ 0) || (nb EQ 0)) then Message, /noname, MsgPrefix + "One or more of the arguments is undefined"
result = ( n_elements(a + b) NE max( [na, nb]))
end
3: begin
na = n_elements(a)
nb = n_elements(b)
nc = n_elements(c)
if ((na EQ 0) || (nb EQ 0) || (nc EQ 0)) then Message, /noname, MsgPrefix + "One or more of the arguments is undefined"
result = ( n_elements(a + b + c) NE max( [na, nb, nc]))
end
4: begin
na = n_elements(a)
nb = n_elements(b)
nc = n_elements(c)
nd = n_elements(d)
if ((na EQ 0) || (nb EQ 0) || (nc EQ 0) || (nd EQ 0)) then Message, /noname, MsgPrefix + "One or more of the arguments is undefined"
result = ( n_elements(a + b + c + d) NE max( [na, nb, nc, nd]))
end
else: Message, /noname, "Usage: ArrayTruncation(a, b [c [d]])"
endcase
return, result
end
|