Re: keyword issue (top 10 IDL request?) [message #20768] |
Wed, 19 July 2000 00:00 |
promashkin
Messages: 169 Registered: December 1999
|
Senior Member |
|
|
This matter has been discussed here before, and, in fact, is one of the
few that are well covered in the manual. KEYWORD_SET is *only* to be
used to check *status* ot keywords (for "trigger" keywords) that you
either *set* or don't, using "/":
pro temp, plot=plot
if keyword_set(plot) then plot, indgen(10) $
else print, 'No keyword - no plotting!'
end
For KEYWORD_SET, value of zero means "not set", just like /SENSITIVE is
the same as SENSITIVE=1 or SENSITIVE=2, while SENSITIVE=0 means "turn it
off" (for WIDGET_CONTROL procedure).
If your keyword is used to pass (numerical) values, you should, just as
Andy pointed out, use N_ELEMENTS instead of KEYWORD_SET:
pro temp, plot=plot
if n_elements(plot) gt 0 then $
case plot of
0: ; do nothing
1: plot, indgen(10)
2: plot_3dbox, indgen(10), indgen(10), indgen(10)
else: ; do nothing
endcase
end
Cheers,
Pavel
Kenneth Mankoff wrote:
>
> Hi all,
>
> here is another thing with current versions of IDL that causes problems
> and confuses me:
>
> a keyword set to 0 should not be undefined, it should be defined as zero.
> For example:
>
> IDL> create_globe, lat=90, long=0, ...
> I want create_globe to put longitude 0 at the center of the image (i.e.
> pass "0" to map_set). But if i don't use long= at all, then it should be
> undefined, and the following code should run (inside create_globe)
> IF ( NOT KEYWORD_SET( long ) ) THEN long = 277.5 ;;; geomagnetic pole '98
>
> However, the above line of code runs even if i set long to "0". I have
> been using long=0.1 as a work-around, but this is an ugly hack that others
> might not think to use.
>
> any ideas to fix this are welcome, and if no one has any good ideas, then
> please add this to your top ten list, David Fanning.
>
> Thanks,
> ken.
|
|
|
Re: keyword issue (top 10 IDL request?) [message #20771 is a reply to message #20768] |
Wed, 19 July 2000 00:00  |
Andy Loughe
Messages: 174 Registered: November 1995
|
Senior Member |
|
|
Kenneth Mankoff wrote:
>
> Hi all,
>
> here is another thing with current versions of IDL that causes problems
> and confuses me:
>
> a keyword set to 0 should not be undefined, it should be defined as zero.
> For example:
>
> IDL> create_globe, lat=90, long=0, ...
> I want create_globe to put longitude 0 at the center of the image (i.e.
> pass "0" to map_set). But if i don't use long= at all, then it should be
> undefined, and the following code should run (inside create_globe)
> IF ( NOT KEYWORD_SET( long ) ) THEN long = 277.5 ;;; geomagnetic pole '98
>
> However, the above line of code runs even if i set long to "0". I have
> been using long=0.1 as a work-around, but this is an ugly hack that others
> might not think to use.
>
> any ideas to fix this are welcome, and if no one has any good ideas, then
> please add this to your top ten list, David Fanning.
>
> Thanks,
> ken.
if ( N_elements(long) eq 0 ) then long = 277.5
Andrew Loughe **********************************************************
NOAA/FSL/AD Mailstop: R/FS5 * Email: loughe@fsl.noaa.gov
325 Broadway * Web: www-ad.fsl.noaa.gov/users/loughe/
Boulder, CO 80303 * Phn/Fax: (303)497-6211 / (303)497-6301
|
|
|
Re: keyword issue (top 10 IDL request?) [message #20772 is a reply to message #20768] |
Wed, 19 July 2000 00:00  |
Liam E. Gumley
Messages: 378 Registered: January 2000
|
Senior Member |
|
|
Kenneth Mankoff wrote:
> here is another thing with current versions of IDL that causes problems
> and confuses me:
>
> a keyword set to 0 should not be undefined, it should be defined as zero.
> For example:
>
> IDL> create_globe, lat=90, long=0, ...
> I want create_globe to put longitude 0 at the center of the image (i.e.
> pass "0" to map_set). But if i don't use long= at all, then it should be
> undefined, and the following code should run (inside create_globe)
> IF ( NOT KEYWORD_SET( long ) ) THEN long = 277.5 ;;; geomagnetic pole '98
>
> However, the above line of code runs even if i set long to "0". I have
> been using long=0.1 as a work-around, but this is an ugly hack that others
> might not think to use.
>
> any ideas to fix this are welcome, and if no one has any good ideas, then
> please add this to your top ten list, David Fanning.
I like to make a clear distinction between keywords which are used to
supply a value, and keywords which represent a Boolean flag.
To check a keyword which supplies a value (as in your case), and set a
default value if the keyword is undefined:
if (n_elements(key_value) eq 0) then key_value = -999.9
To check a keyword which represents a Boolean flag:
if keyword_set(key_boolean) then print, 'True'
if (keyword_set(key_boolean) eq 1) then print, 'True'
if (keyword_set(key_boolean) eq 0) then print, 'False'
If you wish to pass a Boolean keyword from inside your
procedure/function another procedure or function:
myfunc, arg1, arg2, key=keyword_set(key_boolean)
Cheers,
Liam.
|
|
|