# IDL遥感应用入门（1）：基本语法练习

;尹全超-程序1
;2013-09-29

PRO yinquanchao_1

;-----------------------------------------------------
;                 PRACTICE WITH SCALARS
;-----------------------------------------------------

x=99
y=100
print ,"x+y=",(x+y)
print ,"x-y=",(x-y)

print ,'USE & character'
x=99&y=100& print ,'x+y=',(x+y)&  print ,'x-y=',(x-y)

;the result of adding 3 times X to 2 times Y

print ,'3x+2y=',(3*x+2*y)

;the product of (X-Y) and (X+Y)

print ,'(x+y)*(x-y)=',(x+y)*(x-y)

;the common logarithm of the absolute value of X+Y

print ,ALOG10(ABS(x+y))

;difference between X = 5/2 and Y = 5./2

print , 'x=5/2' , (5/2)    ;输出结果是2.       整数
print , 'x=5./2' , (5./2)  ;输出结果是2.50000  浮点数

;difference between  X = FLOAT(5/2) and Y = FLOAT(5)/2

print , 'X = FLOAT(5/2)' , FLOAT(5/2) ;把结果转换成一个浮点数
print , 'Y = FLOAT(5)/2' , FLOAT(5)/2 ;先把5转换成浮点数，然后进行运算

;define a string variable for each word in the sentence:
;"This is a concatenation"; then concatenate them and print the result

str1='This' & str2='is' & str3='a' & str4='concatenation'
print , str1 + ' ' + str2 + ' ' + str3 + ' ' + str4

;-----------------------------------------------------
;                 PRACTICE WITH VECTORS
;-----------------------------------------------------

x = INDGEN(100)

;How many elements does X contain

print , 'The elements of X is' , N_ELEMENTS(x)

;What is the minimum value in X

print , 'The minimum value in X is ' , MIN(x)

;What is the maximum value in X

print , 'The maximum value in X is ' , MAX(x)

;What is the sum of all the elements in X

print , 'The sum of all the elements in X is' , TOTAL(x)

;Print X to your terminal window

print ,x

;How are values in X related to the corresponding subscripts?

;Is X a floating-point array?

;Print the fifteenth entry in X to your terminal

print , 'The fifteenth entry in X is' , x[14]

;print the entry containing the number 15

print , 'the entry containing the number 15' , x[15]

;Is X a row-vector or a column-vector?

;PRINT,TRANSPOSE(X) 转换列向量

print ,TRANSPOSE(x)

;Try the following one-line command and inspect its output:

FOR I=0,99 DO PRINT,I,X(I)
FOR I=0,99 DO PRINT,I,X[I]  ;测试发现()和[]都可以

; Using the WHERE function:
; Define Q=2*X, then type FIND = WHERE(Q LE 40, COUNT)
; Examine the contents of FIND and COUNT so that you understand how the WHERE function operates.

Q=2*X                            ; 复制了一个数组，每个值都*2
FIND = WHERE(Q LE 40, COUNT)    ; 遍历数组Q,把遍历结果赋值给FIND变量，同时用COUNT变量记录符合条件都个数
print , 'FIND=' , FIND
print , 'COUNT=' , COUNT

;PRINT,Q(FIND)

PRINT , 'Q(FIND):' , Q(FIND)

;Print to your terminal the values of the vector X/10

print , 'The values of the vector X/10 ' , x/10     ;结果说明是一个整形数组

;print the values of X/10.0 and compare the results

print , 'The values of X/10.0 ' , x/10.0            ;一个浮点型数组

;Do the same for FLOAT(X)/10  Do the same for FIX(X/10.0)

print , 'FLOAT(X)/10' , FLOAT(X)/10
print , 'FIX(X/10.0)' , FIX(X/10.0)                ;表示把小数截去小数部分，只保留整数部分

;What is the difference between Z = X*0.0 and Z = 0?

print , 'Z = X*0.0 is ' , (x*0.0)
print , 'Z = 0 is ' , 0                             ;一个是数组，一个是整数

;Print the 11 elements centered on X = 10
;First do this using a FOR loop (on a single line)
;Then do this using the standard IDL subscript range notation, e.g. X(2:6).  No FOR loop is needed.

for i=x[5],x[15],1 do print , 'For Loop =>' , i

print , 'IDL subscript => ' , X(5:15)

;Compare to the following: K=5 & PRINT,X(10-K:10+K)

K=5 & PRINT,X(10-K:10+K)

;Define Y to be the subarray consisting of those 11 elements, using subscript range notation; print Y to your window as a check.

k=5
y=x(10-k:10+k)
print ,' Check y => ', y

;Verify lengths of X,Y using the N_ELEMENTS utility

print , 'The lengths of x =>' , N_ELEMENTS(x)
print , 'The lengths of y =>' , N_ELEMENTS(y)

;Use the SIZE utility to find the sizes of X and Y; what other info does it supply?

print , 'The sizes of x =>' , SIZE(x) ;1 100 2 100 表示一维   一维上有100个元素   整型  100个元素
print , 'The sizes of y =>' , SIZE(y)

;What information does the command HELP,X,Y provide?

HELP , x , y

;Define Q = Y+3 --  Note the values that Q contains
;Define Q = Y*3 --  Note the values that Q contains
;Define Q = Y^3 --  Note the values that Q contains
;Define Q = Y^4 --  Note the values that Q contains  why are they not monotonic?

Q = Y + 3 & print , 'Y+3 =>' , Q
Q = Y - 3 & print , 'Y-3 =>' , Q
Q = Y * 3 & print , 'Y*3 =>' , Q
Q = Y ^ 4 & print , 'Y^4 =>' , Q            ;int型最大到32767,产生溢出
Q = (long(Y) ^ 4) & print , 'Y^4 =>' , Q    ;正确写法

;Define Q = FLOAT(Y)^4 --  Note the change.

Q = FLOAT(Y)^4 & print , 'FLOAT(Y)^4 =>' , Q ;浮点数范围正负10^38

;Print the vector which results from Q = X*Y

Q = x * y
print , 'Q = x * y =>' , Q

;What did IDL actually do to arrive at this result?

;Using the built-in functions TOTAL and N_ELEMENTS:
;Find and print the mean values of X and Y;

print , 'The mean values of X is' , TOTAL(x)/N_ELEMENTS(x)
print , 'The mean values of Y is' , TOTAL(y)/N_ELEMENTS(y)

;Find and print the variances of the two arrays

print , 'The variances of X is' , TOTAL((x-(TOTAL(x)/N_ELEMENTS(x)))^2)/(N_ELEMENTS(x)-1)
print , 'The variances of Y is' , TOTAL((y-(TOTAL(y)/N_ELEMENTS(y)))^2)/(N_ELEMENTS(y)-1)

;Do the same using the built-in functions MEAN and VARIANCE.

print , 'The mean values of X is' , MEAN(x)
print , 'The mean values of Y is' , MEAN(y)

print , 'The variances of X is' , VARIANCE(x)
print , 'The variances of Y is' , VARIANCE(y)

print , 'Test Moment x' , MOMENT(x)
print , 'Test Moment y' , MOMENT(y)    ;计算平均值、方差、斜度、峰度

;write Q = X & Q[2] = Y.  What will happen if you write Q[98] = Y?  Try it.

Q = X
print , 'Q = x :' , Q
Q[2] = Y
print , 'Q[2] = y :' , Q
;Q[98] = Y
;print , 'Q[98] = y :' , Q ;溢出报错

;Q = [Y,Y] Q = [Q,Y]

Q = [Y,Y]
print , 'Q = [Y,Y]:' , Q
Q = [Q,Y]
print , 'Q = [Q,Y]:' , Q

;Create a 16-element vector [1.01, 1.02, 1.03...]
;using a simple one-line command employing FINDGEN.  Verify.

x16 = (1 + FINDGEN(16))/100 + 1
print , 'x16=>' , x16

;Create a 16-element vector: [1,2,4,8,16....] using a simple
;one-line command employing FINDGEN.  Verify.

y16 = 1 + FINDGEN(16)
print , 'y16=>' , y16

;Create the 100-element vector Z = 10*X - 0.1*X^2, where
;X contains the integers between 0 and 99.

x = INDGEN(100)
z = 10 * x - 0.1 * x ^ 2
print , 'z=>' , z

;Use MAX to determine the maximum value of Z.

print , 'The maximum value of Z' , MAX(Z)

;Use WHERE to locate the X value for which this maximum occurs.

FIND = WHERE(Z EQ MAX(Z))
print , ' The X value for which this maximum occurs is ' , FIND

;X was an integer vector but Z is not.  Why?

;Using the SHIFT function, shift the elements of the array you
;just created three entries to the left.  Verify that the
;maximum is now in the expected location.

z = SHIFT(z,-3)
print , ' The X value for which this maximum occurs is ' , WHERE(Z EQ MAX(Z))

END


### 已有 5 条评论

1. 1#
冬冬  · 2016-08-07 13:33

你好，请问代码
OPENR,11,'D:\TEST.TXT'+'ORBIT_1.DAT'
里面的“+”代表什么？

1. Sailor  · 2016-08-07 21:39

字符串连接

2. 2#
jester  · 2016-10-12 19:25

&全报错，可能是因为;后面被视为注释的原因？？全部改成&后就没问题了

1. Sailor  · 2016-10-13 09:06

是的，代码中&被转义了。

2. Sailor  · 2016-10-13 09:11

我把代码修正了一下，现在是没问题的了。