comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » get IDL to issue warning or throw error for mismatched arrays
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: get IDL to issue warning or throw error for mismatched arrays [message #94047 is a reply to message #93331] Thu, 05 January 2017 15:12 Go to previous messageGo to previous message
Bill Nel is currently offline  Bill Nel
Messages: 31
Registered: October 2010
Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: IDL 8,2 cannot call ENVI function
Next Topic: Mismatch between video frame dimensions and stream using Coyote graphics

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:48:59 PDT 2025

Total time taken to generate the page: 0.00476 seconds