IDL语言的几个要点回顾

最近因为一个简单任务,重新使用了IDL这门在读研究生期间使用得比较多的语言,很久不用,对其中的一些语法规则和特性略显陌生,现将一些关键要点总结如下。

IDL是一种数据分析和图像化应用程序及编程语言,先由美国ITT公司所有,后由美国ExelisVis公司所有。IDL属于第四代科学计算可视化语言,集开放性、高维分析能力、科学计算能力、实用性和可视化分析为一体,可在多种硬件平台上运行,方便地与C、C++连接,还支持数据库的ODBC接口标准。IDL语言内置的数学库函数可以大大地减少图象处理算法开发的工作量,用IDL语言写的程序可以不加修改地在其他运行了IDL平台上运行,具有很强的平台间可移植性。由于IDL语言是解释性语言,其运行速度受到影响,对于速度要求较高的功能可以直接用标准C语言编写,利用IDL与C的接口在IDL语言中调用C模块实现高速度。

使用IDL目前业界已经开发出了ENVI、IMAGIS、RiverTools、医学等成熟产品。

语法规则

IDL语法规则

过程与函数

用关键字pro/function表示,与其它语言的区别是一样的,pro没有返回值,function有返回值,不过要得到过程计算返回结果,通过其它手段也可以实现,如使用关键字或者指针参数传递。

Procedures
IDL procedures use the following general syntax:
PROCEDURE_NAME, Argument [, Optional_Argument]
where PROCEDURE_NAME is the name of the procedure, Argument is a required parameter, and Optional_Argument is an optional parameter to the procedure.

Functions
IDL functions use the following general syntax:
Result = FUNCTION_NAME( Argument [, Optional_Argument] )
where Result is the returned value of the function, FUNCTION_NAME is the name of the function, Argument is a required parameter, and Optional_Argument is an optional parameter. Note that all arguments and keyword arguments to functions should be supplied within the parentheses that follow the function’s name.
Functions do not always have to be used in assignment statements (i.e., A=SIN(10.2)), they can be used just like any other IDL expression. For example, you could print the result of SIN(10.2) by entering the command:
PRINT, SIN(10.2)

参数传递方法

1.参数

参数主要用于程序之间相互调用过程中值的传递。
pr/function 过程名/函数名 参数1,参数2…….

参数使用过程中要注意顺序,参数传递可以是值或者地址。
Note that these arguments are positional parameters that must be supplied in the order indicated by the routine’s syntax.
Named Variables

Often, arguments that contain values upon return from the function or procedure (“output arguments”) are described as accepting “named variables”. A named variable is simply a valid IDL variable name. This variable does not need to be defined before being used as an output argument. Note, however that when an argument calls for a named variable, only a named variable can be used—sending an expression causes an error.

2.关键字

关键字是另一种在不同程序之间传递数据的方式。
pro/function 过程名/函数名 关键字1=关键字变量1,…….

等号左边是关键字,仅仅是一个标识作用,右边是关键字变量才是过程/函数运行时真正使用的变量。

关键字没有顺序限制,可以调换顺序,参数的顺序不能调换。

布尔型关键字。只能有1和0两个不同的值。

有些关键字是只能用其中的几个关键选项,如PLOT, [X,] Y [, /DATA | , /DEVICE | , /NORMAL] ,只能用DATA,DEVICE,NORMAL中的三个之一的参数。如:PLOT, SIN(A), /DEVICE

关键字也能作为输出参数,此时不需要提前定义。如: WIDGET_CONTROL, mywidget, GET_UVALUE = userval ; userval并不需要提前定义。

3.参数值传递和地址传递

函数间传递参数的方式与C++的传递方法类似,可以用值传递和指针传递两种,分别对应内存空间的中栈空间,堆空间。值传递传递的是数据的拷贝,在函数中对其修改不会影响到调用函数中;而指针传递的数据的地址,函数中对数据的修改会影响到调用函数中。关于指针的使用原则与C++的一些原则是类似的,比如谁分配空间谁就负责释放空间;要注意防止内存泄露。

;分配一个堆变量指针
C = PTR_NEW(2.0d)
;释放一个指针
PTR_FREE, A

A = PTR_NEW(FINDGEN(10))
B = A
HELP, A, B
PRINT, *B

;给堆变量指针数组赋值,ptarr实际上是一个指针数组
ptarr = PTRARR(3, /ALLOCATE_HEAP)
FOR I = 0,2 DO *ptarr[I] = I
PRINT, ptarr ;
print,*ptarr[3] ;输出 2

关于IDL中堆变量官方文档叙述如下:
Heap variables are a special class of IDL variables that have global scope and explicit user control over their lifetime. They can be basic IDL variables, accessible via pointers, or objects, accessible via object references.

IDL documentation of pointers and objects, heap variables accessible via pointers are called pointer heap variables, and heap variables accessible via object references are called object heap variables.

Pointers and object references have many similarities, the strongest of which is that both point at heap variables. It is important to understand that they are not the same type, and cannot be used interchangeably. Pointers and object references are used to solve different sorts of problems. Pointers are useful for building dynamic data structures, and for passing large data around using a lightweight token (the pointer itself) instead of copying data. Objects are used to apply object oriented design techniques and organization to a system. It is, of course, often useful to use both in a given program.

Heap variables are global in scope, but do not suffer from the limitations of COMMON blocks. That is, heap variables are available to all program units at all times. (Remember, however, that IDL variables containing pointers to heap variables are not global in scope and must be declared in a COMMON block if you want to share them between program units.)

进度条的使用

在envi的进度条函数使用过程中,注意三个函数的执行顺序,其中ENVI_REPORT_INIT必须放在循环外面,ENVI_REPORT_INC相当于C#中的Application.DoEvent,执行一次刷新一次界面,如果在多重for循环中如果不执行,可能会造成“假死”的现象,ENVI_REPORT_STAT用于设置实际的进度,放在相应的更新进度的位置即可。
ENVI_REPORT_INIT
ENVI_REPORT_INC
ENVI_REPORT_STAT

prog = specNums*ns*nl
envi_report_init, rstr, title='光谱匹配分类', base=base,/INTERRUPT
for i=0L, nl-1, 1L do begin
    envi_report_inc, base, prog
    for j=0L, ns-1, 1L do begin

        for k=0L, specNums-1, 1L do begin ;波普库中共有谱线数目
            envi_report_stat,base, statusN++, prog,, CANCEL=cancelvar
        endfor
    endfor
endfor
envi_report_init, base=base, /finish

;判断是否点击取消
IF cancelVar EQ 1 THEN BEGIN
    tmp = DIALOG_MESSAGE('点击了取消'+STRING(i)+'%',/info)
    ENVI_REPORT_INIT, base=base, /finish
    BREAK
ENDIF

IDL中分块读取数据

ENVI_GET_DATA是一次读取所有的数据,当图像很大的时候而内存不够的时候,这种方法可能会影响到效率。此时可以利用分块读取的方法,每次读取一块数据进行处理,此时就需要用到ENVI_GET_TILE、ENVI_INIT_TILE、ENVI_TILE_DONE三个函数进行处理。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值