1. 程式人生 > >IDL(ENVI/IDL) 簡(jian)明(lou)教程:一、IDL基礎

IDL(ENVI/IDL) 簡(jian)明(lou)教程:一、IDL基礎

    IDL是一門簡單易用的(包括但不限於)科學計算和視覺化語言,包含大量的函式,尤其是同ENVI結合集成了該軟體的大量功能,因此在遙感影象批量處理上應用廣泛。關於IDL(結合ENVI)的中文簡明教程並不多見,但英文版卻有不少,本文以幾個英文資源為基礎介紹。如果有一些程式設計基礎又想短時間內(比如半天)瞭解IDL,並掌握簡單的使用IDL呼叫ENVI函式進行批量處理的話,可以參考本系列文章。目前,該系列包括兩篇,第一篇是IDL基礎,第二篇是ENVI/IDL批處理入門。

一、IDL基礎

先推薦兩個簡明IDL入門教程,功法不同,大家可以根據喜好選擇。

一個是偏理論的概述語法(ChrisNorth, 2005):

A (Very) Quick Guide to IDL: Version 1.1

一個是例項引導的教程(UCL 的P. Lewis等, 2007,遙感屆的先驅)

進一步學習可以參考下面這幾本書(googlesearch),其中6000頁+的IDL Reference Guide對IDL語法和原生函式介紹很細緻又有例子,是不錯的查詢參考手冊:

Using IDL

IDL Reference Guide

Learning IDL & IDL Programming

Building Graphical User Interface in IDL

Building IDL Applications

http://fermi.jhuapl.edu/states/states.html

下面參考Chris North的教程簡單展開下IDL的語法

1、結構(過程和函式)

IDL程式的獨立單元是過程(procedure)或函式(function),兩者定義的語法是:

PRO procedure name, parameter1, parameter2,. . . ,parametern,

keyword1=keyword1,. . . , keywordn=keywordn

FUNCTIONfunction name, parameter1,parameter2, . . . ,parametern,

keyword1=keyword1,. . . , keywordn=keywordn

過程的呼叫方式:

procedure name, parameters, keywords

         函式的呼叫方式:

Result=function name(parameters, keywords)

         Result是函式的返回結果,在程式裡要有return。

         IDL的過程和函式引數(arguments)分為兩類:位置引數和關鍵字引數(parameters and keywords),前者呼叫時順序固定,通常用於必選引數,後者位置可變,通常用於可選引數。

PROCEDURE NAME, parameters, …, keyword=value,…

         2、簡單語法

         1)變數

ü  變數名不能以數字開頭;

ü  大小寫不敏感;

ü  !開頭的變數是系統變數;

ü  過程和函式不能使用非傳遞或未定義的變數(有點像廢話)。

當變數名不是float時,可以通過賦近似的值進行定義。比如A=0代表整型,A=0L代表長整型,A=0.代表浮點型,A=0.d0代表雙精度浮點,A=’’代表字串。

2)塊(Blocks)

IF. . .THEN和WHILE在使用時包含多行程式碼就形成典型的程式碼塊,如果程式碼塊中程式碼超過一行,需要使用Begin END包裹。

IF condition THEN Begin

. . . block of code

ENDIF ELSEIF other condition THEN Begin

. . .more code

ENDELSE

         單行的話可以簡單寫為:

IF condition THEN somethingELSE something else

         3)$和&

$是續行符,可以將較長的一句程式碼寫在多行,比如有很多引數的時候。&是斷行附,指示同一行的不同語句。

4)狀態和迴圈語句

FOR integer=low,high DO expression

OR

FOR integer=low,high DO Begin

block of code

ENDFOR

WHILE condition DO expression

OR

WHILE condition DO Begin

block of code

ENDWHILE

REPEAT statement UNTIL condition

OR

REPEAT Begin

block of code

ENDREP UNTIL condition

         5)判斷運算子

判斷運算子包括:

EQ (equal to),

NE (not equal to),

LT (lessthan),

GT (greater than),

LE (less than or equal to),

GE (greater than orequal to),

AND,

OR,

NOT.

         6)矩陣操作

Array[i,j]可以訪問矩陣的i行j列元素。Array[*,*]代表矩陣的所有元素。#是矩陣乘法運算子,不同於一般運算。矩陣操作函式,不指明行列的話代表所有元素。比如TOTAL(Array)對所有元素求和,TOTAL(Array[*,k] )對第k列求和。

7)數學運算子和函式

數學運算子:

= Assignment

+ Addition

- Subtraction

* Multiplication

/ Division

ˆ Exponentiation

++ Increment by1

-- Decrement by1

MOD Modulooperator

< Minimiseoperator

> Maximiseoperator

數學運算函式:

EXP, SQRT, ALOG,ALOG10, SIN,ASIN, SINH等

8)資料型別

9)檔案讀寫

常用的有:

OPENR, unit,filename 以只讀方式開啟檔案

OPENW, unit,filename 以只寫方式開啟檔案

OPENU, unit,filename 以讀寫方式開啟檔案

10)獲取幫助

在命令列使用 ? procedure_name

其它IDL資源:

IDL Newsgroups.There are several Usenetnewsgroups. The official IDL newsgroup is:

comp.lang.idl-pvwave

Ray Sterner of the Johns Hopkins AppliedPhysics Laboratory publishes an IDL Frequently Asked questions (IDL-FAQ) listto this newsgroup. This FAQ can be found at:

http://fermi.jhuapl.edu/s1r/idl/idl_faq/idl_faq.html

Unfortunately, the FAQ has not been updatedfor a while.

Other useful WWW Sites

Research Systems has information on IDL ingeneral at:

http://www.rsinc.com/

Pete Riley's IDL Home Page

http://nis-www.lanl.gov/~uk2/idl/

Coyote's Guide to IDL Programming

http://www.dfanning.com/

Wayne Landsman's IDL Astronomy Library WWWpage is at:

http://idlastro.gsfc.nasa.gov/homepage.html

The JHU/APL/S1R IDL library WWW page is at:

http://fermi.jhuapl.edu/s1r/idl/idl.html

Ray Sterner's Color Shaded Relief Maps madeby IDL are at: