使用IDL的12个小技巧

使用IDL的12个小技巧
使用IDL的12个小技巧

原文来自michaelgallory

译文有不妥之处请大家指正。

tip 1 put code in files. your choices are to write main-level programs, batch files, and normal procedures/functions. the bulk of your code should probably be in procedures and functions, but there are certainly reasons to use both main-level programs and batch files. main-level programs are handy for setting up a situation and allowing you to enter commands accessing the variables in the program after it has run. batch files are good for including code that needs to be typed in multiple locations.

把代码存放在文件中。你可以选择写主程序,批处理文件和常见的过程或函数。大量的代码应该是包含在过程和函数中,但是为什么要用主程序和批处理文件,这肯定是有它的原因的。主程序有利于环境的创建,在程序运行后你还可以输入命令来访问它的变量。批处理文件有利于存储那些需要在多个位置键入的代码。

tip 2 place your main routine last in a file and name the file the same as this routine plus a ".pro" extension. alternatively, you could put each routine in its own file with the same name as the routine plus a ".pro" extension. following this advice will save a big headache someday. when you manually compile your code, this tip doesn't matter. do this if you want idl to automatically find your code (and you will eventually, probably tomorrow).

把你的主程序放在文件的最后,并把此程序名作为文件名,以".pro"为扩展名。或者你可以把每个程序单独放在一个文件里,以程序名作为文件名,以".pro"为扩展名。这样做的好处是以后会给你省很多麻烦。当你手动编译你的代码时,这个方法是没什么用的。如果你想要idl自动找到你的代码时(你最终肯定要的,可能就是明天),就要用这个方法了。

tip 3 be aware of short integers. the default integer in idl is a 16-bit ("short") integer which has a range of -32,768 to 32,767. it is used in situations like: n = 5

where no specific type of integer is specified. if you want to change the default integer, put

compile_opt defint32

at the beginning of every routine where you want to change the behavior. or, always specify the type of integer you want, like

n = 5l

to create a long integer (32-bit).

注意短整型。idl中默认的整型是16位(“短”)整型,它的取值范围是-32,768到32,767。它用于下例情况下:

n = 5

这里没有指定整数的类型。如果你想要改变默认的整型,键入:

compile_opt defint32

在每个你想改变状态的程序的开头,或总是指定你想要的整型类型,如:

n = 5l

创建一个长整型(32位)

tip 4 put compile_opt strictarr at the beginning of every routine you write. the explanation is a bit subtle, but doing this will save you a day's work sometime. it comes down to the fact that both arr(5) and arr[5] can index into the array arr.

but idl can get confused trying to figure out if arr(5) is indexing into an array or a function call. the solution is to always use arr[5] for array indexing and to tell idl that you will be doing this with

compile_opt strictarr

at the beginning of each routine. (this is not a global setting since there is plenty of legacy code, including idl's library, that would not work with it.)

在你写的每个程序的开头键入compile_opt strictarr,这个解译起来有点不太好理解,但这样做有时会省去你一天的工作。这是源于arr(5)和arr[5]都可以索引数组,但idl尝试去识别arr(5)是索引一个数组还是调用一个函数的时会发生误解。解决的办法就是总是用arr[5]作为数组索引,通过程序开头的compile_opt strictarr告诉idl你要这样做。(这不是全球的设置,因为有很多传统代码,包括idl的库是不支持的。)

2 [转贴]使用IDL的12个小技巧

tip 5 learn to use where. any time you want to find the elements of an array that match a given condition (naively, you would have an if statement inside of a for loop), you should try to use where instead. (also, make sure to use the optional count parameter and check to make sure it's greater than zero before you index an array with the returned indices.)

学会用where。任何时候你想要找出符合给定条件的一个数组中的元素(你自然的会想到用for循环里的if表达式),你应该试着用where来代替。

tip 6 use keywords in routines you write. keywords are a useful feature that differentiate idl from many other languages. generally, keywords are optional inputs or outputs, though idl does not enforce it. also, learn how to use n_elements, arg_present, n_params correctly to check parameters.

在你写的程序中应用关键字。关键字是idl不同于其它很多语言的一个有用的特征,一般说来,关键字是可选的输入值或输出值,尽管idl没有作出这样的规定。还要学会正确使用n_elemens, arg_present, n_params来检查参数。

tip 7 use array operations. this is called vectorization and is key to writing efficient idl code. for example, if arr is an array, then

result = arr 1.0

will add 1.0 to each element of arr. almost all of idl's operators will handle array or scalar operands. it is much faster to use them with array operations than to loop over the elements and use the scalar version.

利用数组运算。这称之为向量化,它是有效编写idl代码的关键。例如,如果arr是一个数组,那么

result = arr 1.0

将为arr中的每个元素都加上1.0。几乎所有的idl运算都会处理到数组或标量操作数。利用数组运算要比循环元素和标量的方法快得多。

tip 8 use the online help. all the documentation is now online and nice looking on all platforms. it is accessible with ? from the idl prompt. it is particularly good

at documenting the library api, but it is the first source for everything idl. 使用在线帮助。现在所有的文件都是在线的,并且所有的平台都可以很好的浏览。可以通过在idl提示窗口键入?来进入。它在api库归档方面做的相当好,更是全部idl的第一来源。tip 9 if you're on unix, consider using idlwave. the current idl development environment is "somewhat lacking." if you want more than just a commandline interface, try emacs with idlwave mode installed. emacs traditionally has a steep learning curve, but if you're not really tied to an editor right now, it's probably worth learning. 如里你用的是unix平台,考虑用idlwave。目前的idl开发环境存在一些“罅隙”。如果

你要的不只是一个命令行界面,在idlwave模式安装下试试emacs,emacs据说有陡峭学习曲线,但如果你不是现在就想完全依*编辑器的话,它还是值得学习的。

tip 10 have a style. a style will give you guidance in many of the things that idl doesn't care about: names of variables/routines, capitalization, indentation, comments, etc. unfortunately, rsi ittvis doesn't provide a style since the library routines are written in a myriad of styles. there are some third party style guidelines.

有规范的格式。规范的格式会在很多idl不敏感的事情上成为你的向导。如变量或指令的名字,大小写,缩进,注释等。遗憾的是rsi ittvis没有提供统一格式,因为库程序是用很多不同格式写的。有一些是第三方格式标准。

tip 11 learn to use the debugger. the debugging environment is a main advantage of the interactive nature of idl. it can be used from the command line or the de (although, i must confess that i like to push buttons in the de in this respect of programming). one key point here is that runtime errors drop the command line to the level of the offending statement. you can enter idl statements at the command line as if they were in the original program. this is really handy in examining the situation to find the error.

学会用调试程序。调试环境是idl交互特点中的一个主要优势,它可以通过命令行或开发环境实现(虽然在编程这方面我必须承认我喜欢在开发环境中通过按钮来实现)。这里关键的一点是运行时的错误会使得命令行返回到表达式有问题的地方。你可以在命令行输入idl

表达式,就好像他们原本就在最初的程序中。这对于检测环境找出错误所在非常有用。

tip 12 use single quotes for strings. both single and double quotes are valid for literal strings, but double quotes have some extra odd uses, like specifying integer values in octal. so for instance, in the following

idl> age = "22 years"

age = "22 years"

^

% syntax error.

idl thinks that "22 (octal 22 or decimal 18) is being assigned to age. to bypass this mess, just always use single quotes for strings.

用单引号表示字符串。单引号和双引号都可以表示字符串,但双引号有一些其它不同的用途,如在八进制里指定整型值。所以在有些情况下,如:

idl> age = "22 years"

age = "22 years"

^

% syntax error.

idl认为age的赋值是22(八进制22或十进制18)。为了绕过这种混淆,只要总是用单引号来表示字符串就ok。

相关主题
相关文档
最新文档