Re: IDL-way to use WHERE result as subscripting indices without having to do IF count [message #76180] |
Fri, 20 May 2011 08:22 |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
On Fri, 20 May 2011 14:06:58 +0100, Robin Wilson wrote:
> Hi all,
>
> I'm sure this has been discussed before, but I can't find the
> discussion. I have a lot of code that looks like the following:
>
> Array[WHERE(Input LT 5)] = 2
> Array[WHERE(Input GT 7 AND OtherArray LT 9) = 6
> etc...
>
> I know I shouldn't be using WHERE like that without checking the count
> of items returned, as it will crash if there are no values in Input less
> than 5. My question is: is there an IDL-way to do this, without putting
> in loads of IF count NE 0 statements above each of the assignemnts?
> That'll make the code a lot longer, a lot messier etc.
>
> Any ideas?
>
> Cheers,
>
> Robin
Hi Robin,
for IDL versions previous 8.0 you can use a wrapper function for
WHERE:
function anywhere,array,subscripts,count=count
compile_opt idl2
subscripts=where(array,count)
return,count ge 1
end
With this function you still need the IF statement, however you don't
need more than one line:
if anywhere(Input LT 5,ii) then Array[ii] = 2
if anywhere(Input GT 7 AND OtherArray LT 9,ii) then Array[ii] = 6
Cheers, Heinz
|
|
|
Re: IDL-way to use WHERE result as subscripting indices without having to do IF count [message #76183 is a reply to message #76180] |
Fri, 20 May 2011 06:27  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> Or, if in IDL 8, set the NULL keyword to the Where
> function:
>
> IDL> a=[1,2]
> IDL> help, where(a lt 0)
> <Expression> LONG = -1
> IDL> help, where(a lt 0, /null)
> <Expression> UNDEFINED = !NULL
> IDL> c=a[where(a lt 0, /NULL)]
By the way, if you are writing code that there
is any chance someone else might use, you have
to wait at least two years or two major releases
of IDL, whichever is longer, before you can
use any new feature in a program.
If you do it too soon, your life will become
a nightmare of support issues. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: IDL-way to use WHERE result as subscripting indices without having to do IF count [message #76184 is a reply to message #76183] |
Fri, 20 May 2011 06:19  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Robin Wilson writes:
> I'm sure this has been discussed before, but I can't find the
> discussion. I have a lot of code that looks like the following:
>
> Array[WHERE(Input LT 5)] = 2
> Array[WHERE(Input GT 7 AND OtherArray LT 9) = 6
> etc...
>
> I know I shouldn't be using WHERE like that without checking the count
> of items returned, as it will crash if there are no values in Input less
> than 5. My question is: is there an IDL-way to do this, without putting
> in loads of IF count NE 0 statements above each of the assignemnts?
> That'll make the code a lot longer, a lot messier etc.
>
> Any ideas?
Bite the bullet. You'll be glad you did. :-)
Or, if in IDL 8, set the NULL keyword to the Where
function:
IDL> a=[1,2]
IDL> help, where(a lt 0)
<Expression> LONG = -1
IDL> help, where(a lt 0, /null)
<Expression> UNDEFINED = !NULL
IDL> c=a[where(a lt 0, /NULL)]
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
|
Re: IDL-way to use WHERE result as subscripting indices without having to do IF count [message #76186 is a reply to message #76185] |
Fri, 20 May 2011 06:16  |
penteado
Messages: 866 Registered: February 2018
|
Senior Member Administrator |
|
|
On May 20, 10:06 am, Robin Wilson <ro...@rtwilson.com> wrote:
> Array = 2
> Array
> etc...
>
> I know I shouldn't be using WHERE like that without checking the count
> of items returned, as it will crash if there are no values in Input less
> than 5. My question is: is there an IDL-way to do this, without putting
> in loads of IF count NE 0 statements above each of the assignemnts?
> That'll make the code a lot longer, a lot messier etc.
Array[WHERE(Input LT 5,/null)] = 2
Array[WHERE(Input GT 7 AND OtherArray LT 9,/null) = 6
|
|
|