anthonyfang的个人博客分享 http://blog.sciencenet.cn/u/anthonyfang

博文

[转载]转载-IDL基础

已有 2766 次阅读 2019-5-7 09:33 |系统分类:科研笔记|文章来源:转载

转自http://blog.csdn.net/zhanglei0107/article/details/8897797

 

pro文件名与

pro文件中的程序名称要一致,否则无法执行


pro firstIDL
print,'first IDL'
tmp = dialog_Message('first idl',/information)
end

 

IDL如何注释

使用分号(;)  分号右边的任何字符都会被注释


IDL不区分大小写

$符号为续行符


!感叹号后面加变量名称,为系统预定义的变量

比如圆周率为!pi


&为multiple command character(from IDL programming techniques)

&中文翻译成了同行符号(引用自ESRI-2010 idl培训资料)

使用了同行符相当于两行代码写在一行之中

IDL> a = 3 & b =5
;相当于
IDL> a = 3
IDL> b =5

跟其他语言比如C不一样,IDL下的变量无需事先申明,也就是可以通过赋值语句来进行变量、数组定义的

IDL在运行过程中能随时修改变量的数据类型和组织结构

创建2行3列的数组

数组的索引从0开始

 array=[[1,2,3],[4,5,6]]

比如访问第2行第1列的元素可用下面方式

array[0,1]

这个索引方式与fortran相同,先列的下标,再行的下标,与c,c++,java等不同

下标先列号,后行号(参见IDL programming techniques,2nd edition)

column-row下标方式最初是为了处理天文图像数据的需要。

 多维数组可以用一维下标的形式访问

访问数组元素既可以用方括号[],也可以用小括号(),方括号在IDL 5.0及以上版本支持。

比如a(2)与a[2]是等价的

使用方括号可以更好地区别是访问数组还是函数。

IDL读取文件:

文件的路径可以通过FILE_PATH指定,比如访问D:/file/output/2.txt

filename = Filepath('2.txt',ROOT_DIR='D:',SUBDIRECTORY=['file','output'])

比如读取d盘下的test.txt文件

  print,file_lines('D:/test.txt')

file_lines返回该文件中的行数

OPENR (OPEN Read) opens an existing file for input only.

OPENW (OPEN Write) opens a new file for input and output. If the file exists, it is truncated and its old contents are destroyed. 
OPENU (OPEN Update) opens an existing file for input and output.

GET_LUN(来自idl帮助文件)
Set this keyword to use the GET_LUN procedure to set the value of Unit before the file is opened. Instead of using the two statements:

使用GET_LUN procedure在文件打开前设置单元号

GET_LUN, Unit

OPENR, Unit, 'data.dat'

 

GET_LUN, Unit
OPENR, Unit, 'data.dat'

 

you can use the single statement:

 

OPENR, Unit, 'data.dat', /GET_LUN

 

READ, [Prompt,] Var1, ..., Varn
READF, [Prompt,] Unit, Var1, ..., Varn

readf从指定的unit中读取内容,比read多了一个参数unit

比如从D盘的test1.txt下读取文件,并存到一个3行5列的数组中

 openr,lun,'D:/test1.txt',/get_lun
  arr=fltarr(5,3)
  readf,lun,arr
  print,arr
  close,lun

 

Reading and Writing FORTRAN Data

参见http://www.exelisvis.com/docs/Reading_and_Writing_FORT.html


若是fortran写的是real类型的,这种方法是有效的。若fortran文件写入的文件是integer类型,在IDL中读取的时候似乎原来每个integer之后都要用0填充??这点保留疑问,有待查证,至少目前测试结果是这样的,难道是IDL一个整数是2个byte,而fortran中是4个byte??

fortran中integer如果用kind=2,方法就不会出现问题


READU函数

The READU procedure reads unformatted binary data from a file into IDL variables. READU transfers data directly with no processing of any kind performed on the data.



如何移动到指定的位置进行读写:

比如你期望读取输入文件的第100个到第200个数,可以使用point_lun procedure。(这样避免读取整个文件,然后再truncate处理)

IDL提供了point_lun  procedure

以下内容摘自IDL help文件

Syntax
POINT_LUN, Unit, Position

Position

If Unit is positive, Position gives the byte offset into the file at which the file pointer should be set. For example, to rewind the file to the beginning, specify 0.

If Unit is negative, Position must be a named variable into which the current file position will be stored. The returned type will be a longword signed integer if the position is small enough to fit, and a 64-bit longword integer otherwise.


Position指定文件指针在文件中的字节偏移量


IDL运算符

运算符的优先级可以参考

http://www.physics.nyu.edu/grierlab/idl_html_help/expressions5.html

>号与<在IDL中的功能与其他编程语言不一样,可不是关系/比较运算符。>(maximum operator)返回两个操作数的较大值,<(minimum operator)返回两个操作数的较小值。所以刚开始不清楚功能的情况下可能会比较奇怪。

IDL中的关系运算符与fortran类似。提供了LT,LE,EQ,GE,GT,NE6种relational operator。

以下选自http://www.physics.nyu.edu/grierlab/idl_html_help/expressions4.html

Minimum and Maximum Operators

The IDL minimum and maximum operators return the smaller or larger of their operands, as described below. Note that negated values must be enclosed in parentheses in order for IDL to interpret them correctly.

The Minimum Operator

The "less than" sign (<) is the IDL minimum operator. The value of "A < B" is equal to the smaller of A or B. For example:

;Set A equal to 3. A = 5 < 3 ;Set A equal to -6. A = 5 < (-6) ;Syntax Error. IDL attempts to perform a subtraction operation if ;the "-6" is not enclosed in parentheses. A = 5 < -6 ;Set all points in array ARR that are larger than 100 to 100. ARR = ARR < 100 ;Set X to the smallest of the three operands. X = X0 < X1 < X2

For complex numbers the absolute value (or modulus) is used to determine which value is smaller. If both values have the same magnitude then the first value is returned.

For example:

; Set A equal to 1+2i, since ABS(1+2i) is less than ABS(2-4i) A = COMPLEX(1,2) < COMPLEX(2,-4) ; Set A equal to 1-2i, since ABS(1-2i) equals ABS(-2+i) A = COMPLEX(1,-2) < COMPLEX(-2,1)

The Maximum Operator

The "greater than" sign (>) is the IDL maximum operator. "A > B" is equal to the larger of A or B. For example:

;'>' is used to avoid taking the log of zero or negative numbers. C = ALOG(D > 1E - 6) ;Plot positive points only. Negative points are plotted as zero. PLOT, ARR > 0

For complex numbers the absolute value (or modulus) is used to determine which value is larger. If both values have the same magnitude then the first value is returned. For example:

; Set A equal to 2-4i, since ABS(2-4i) is greater than ABS(1+2i) A = COMPLEX(1,2) > COMPLEX(2,-4) ; Set A equal to 1-2i, since ABS(1-2i) equals ABS(-2+i) A = COMPLEX(1,-2) > COMPLEX(-2,1)



IDL绘图

set plot之后的绘图操作均在set _plot procedure指定的device上进行,除非是set_plot,windows,否则不会显示在屏幕上

当调用device,/close之后,若需之后的绘制显示在屏幕上,需调用set_plot,windows

[cpp] view plaincopyprint?

  1. set_plot,'ps'  

  2. device,xsize=8,ysize=6,/inches  

  3. device,color=1,Bits_Per_Pixel=8,FILENAME='D:\test.ps'  

  4. plot,[1,3],[4,6]  

  5. oplot,[1,2],[4,7]  

  6. device,/close  

若要在device绘制的图中添加注释,可以使用xyouts procedure

比如XYOUTS, 100, 100, 'This is text', /DEVICE 

100,100为相应的设备中的坐标位置,若不指定/device选项,则为plot绘制的图中相应的坐标位置

Print an array of strings with each element of the array printed at a different location. Use larger text than in the previous example:

XYOUTS, [0, 200, 250], [200, 50, 100], $
   ['This', 'is', 'text'], CHARSIZE = 3, /DEVICE

再比如在图中的(1,4),(3,5)处输出hello与hi

xyouts,[1,3],[4,5],['hello','hi']



绘制轮廓图/等值线

可以使用contour procedure

以下来自IDL help file

下面中文部分仅供参考,感觉可能理解存在偏差。

A one- or two-dimensional array containing the values that make up the contour surface. If arguments X and Y are provided, the contour is plotted as a function of the (X, Y) locations specified by their contents. Otherwise, the contour is generated as a function of the two-dimensional array index of each element of Z.

If the IRREGULAR keyword is set, X, Y, Z are all required, and are treated as vectors. Each point has a value of Z[i] and a location of (X[i], Y[i]).

如果x与y均提供的话,contour被绘制成(x,y)处所存储的数(即数组z中相应元素)的函数

。否则contour绘制成二维数组索引的函数,该函数的自变量/输入变量为z中的元素。即该函数是z中每个元素的映射


Using CONTOUR

The basic call to CONTOUR is as follows:

CONTOUR, Z

where Z is a two-dimensional array. This call labels the x- andy-axes with the subscript along each dimension. For example, when contouring a 10 ∞ 20 array, thex-axis ranges from 0 to 9, and they-axis ranges from 0 to 19.

You can explicitly specify the x and y locations of each cell as follows:

CONTOUR, Z, X, Y

where the X and Y arrays can be either vectors or two-dimensional arrays of the same size asZ. If they are vectors, the elementzi,j has a coordinate location of (xi,yj). Otherwise, if thex andy arrays are two-dimensional, the elementzi,j has the location (xi,j,yi,j). Thus, vectors should be used if thex location of zi,j does not depend uponj and they location ofzi,j does not depend uponi.

Dimensions must be compatible. In the one-dimensional case, X must have a dimension equal to the number of columns inZ, andY must have a dimension equal to the number of rows inZ. In the two- dimensional case, all three arrays must have the same dimensions.

IDL uses linear interpolation to determine the x and y locations of the contour lines that pass between grid elements. The cells must be regular in that thexandy arrays must be monotonic over rows and columns, respectively. The lines describing the quadrilateral enclosing each cell and whose vertices are (xi,j,yi,j), (xi+1,j,yi+1,j), (xi+1,j+1,yi+1,j+1), and (xi,j+1,yi,j+1) must intersect only at the four corners and the quadrilateral must not contain other nodes


IDL格式化输出

关于此的内容可以参考IDL help file中的Formatting IO(搜索formatting IO即可)下的format code about中的章节

比如浮点数是如下格式

[n]F[+][-][w][.d]

[n]D[+][-][w][.d]

[n]E[+][-][w][.d][Ee]

[n]G[+][-][w][.d][Ee]

例子:

s= 56.89755
format='f5.2'
print,s,format='(f5.2)'

 

w指定输出数字的总长度(含小数点,比如要输出56.90即指定f5.2),d小数点后输出的位数,当宽度不足以表示该数时,即w<(d+1+小数点前的位数)则输出星号,跟fortran一样的

strpos函数

摘自IDL help file

Syntax

Result = STRPOS( ExpressionSearch_String[, Pos] [, /REVERSE_OFFSET] [, /REVERSE_SEARCH] )

Return Value

If Search_String occurs in Expression, STRPOS returns the character position of the match, otherwise it returns -1.


需要注意的是指定了/REVERSE_SEARCH选项后从字符串的最末尾开始找第一个与search string匹配的字符串,但是函数返回的位置仍然是从字符串起始位置开始算起的位置。

提供的例子中

sentence = 'IDL is fun.'
sentence = STRUPCASE(sentence)
lasti = STRPOS(sentence, 'I', /REVERSE_SEARCH)
PRINT, lasti

This results in:

4


Converting variables to string type often results in undesirable leading blanks

IDL把整数转换为字符串时,该字符串的最前面会有空格字符

比如

s =23.0587
s = string(s)
help,s

输出的是

S               STRING    = '      23.0587'

出现了6个空格,可以用strtrim函数除去,比如

s =23.0587
s = string(s)
help,s
s = strtrim(s,1)
help,s

这样字符串前面便没有空格了


IDL嵌套式if语句

选取字IDL help file

Nesting IF Statements

IF statements can be nested in the following manner:

IF P1 THEN S1 ELSE $

IF P2 THEN S2 ELSE $

   ...

IF PN THEN SN ELSE SX

If condition P1 is true, only statement S1 is executed; if condition P2 is true, only statement S2 is executed, etc. If none of the conditions are true, statement SX will be executed. Conditions are tested in the order they are written. The construction above is similar to the CASE statement except that the conditions are not necessarily related.



需要注意的地方:

问题1

数据类型不当导致结果不对,当时申请了一个很大的数组,结果一直不对,后来发现是表示精度不够导致表达式结果不正确。

输入一个数值很大的表达式:比如120*120*120*5,直接打印的话

比如print,120*120*120*5,打印输出结果为0

可以在数字后面加L或者LL等,L代表该数字为32位有符号整数(比如6LL),LL代表该数字为64位有符号整数(比如128LL)。这样表示的精度就足够了

print,120L*120L*120L*5L,就会输出想要的结果。这一点需要谨慎啊



问题2

当前pro文件调用其他pro文件中的procedure或者函数时,若被调用procedure或函数所在的pro文件未编译,则会提示如下错误

Attempt to call undefined procedure/function: '*****************'.,*星号指代的是调用的procedure或函数名




https://blog.sciencenet.cn/blog-3321945-1177553.html

上一篇:第一篇博文
下一篇:[转载]转载-IDL基础
收藏 IP: 159.226.110.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-29 14:52

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部