当前位置 博文首页 > 流浪若相惜的专栏:唤起那些年你对IDL的记忆(一)

    流浪若相惜的专栏:唤起那些年你对IDL的记忆(一)

    作者:[db:作者] 时间:2021-07-31 21:11

    仅做复习参考用:本文整理参考了IDL-Help跟董彦卿老师编写的教材

    基础

    常用的命令及快捷键

    输出:print
    查看:help
    续行符:$
    同行符:&
    注释符: ;
    常用快捷键:
    这里写图片描述
    图1 快捷键

    语法

    数据类型


    IDL有17中基本数据类型,如下图所示:
    这里写图片描述
    图2 数据类型

    Size (源:IDL-help)


    **description:**The SIZE function returns size and type information for its argument if no keywords are set. If a keyword is set, SIZE returns the specified quantity.
    Examples
    Print the size information for a 10 by 20 floating-point array by entering:
    PRINT, SIZE(FINDGEN(10, 20))
    IDL prints:
    2 10 20 4 200 (1、维数 2、各位数的大小 3、类型Code 4、元素数)
    This IDL output indicates the array has 2 dimensions, equal to 10 and 20, a type code of 4, and 200 elements total. Similarly, to print only the number of dimensions of the same array:

    PRINT, SIZE(FINDGEN(10, 20), /N_DIMENSIONS)
    IDL prints:
    2
    For more information on using SIZE, see the Additional Examples.
    Syntax
    Result = SIZE( Expression [, /L64] [, /DIMENSIONS | , /FILE_LUN | , /FILE_OFFSET | , /N_DIMENSIONS | , /N_ELEMENTS | , /SNAME, | , /STRUCTURE | , /TNAME | , /TYPE] )
    Return Value
    If no keywords are set, SIZE returns a vector of integer type. The first element is equal to the number of dimensions of Expression. This value is zero if Expression is scalar or undefined. The next elements contain the size of each dimension, one element per dimension (none if Expression is scalar or undefined). After the dimension sizes, the last two elements contain the type code (zero if undefined) and the number of elements in Expression, respectively. The type codes are listed below.
    If a keyword is set, SIZE returns the specified information.
    Note: The SIZE function treats lists and hashes as if they are one-dimensional arrays.

    IDL Type Codes and Names
    The following table lists the IDL type codes and type names returned by the SIZE function:
    这里写图片描述
    图3 Type Codes and Names

    变量与常量

    变量分为:局部变量和系统变量
    1、命名规则
    变量长度:不能超过255个字符
    首位:字母或下划线
    中后部:字母、下划线、$
    2、IDL_Validname (源:IDL-Help)
    检查变量名
    The IDL_VALIDNAME function determines whether a string may be used as a valid IDL variable name or structure tag name. Optionally, the routine can convert non-valid characters into underscores, returning a valid name string.
    Example
    这里写图片描述
    图4 IDL_Validaname

    CONVERT_ALL
    If this keyword is set, then each element of String is converted into a valid IDL variable name using the following rules:
    All non-alphanumeric characters (except “_”, “!” and “$”) are converted to underscores

    If the first character is a number or a “$”,then an underscore is prepended to the string
    If the first character is not a valid character (“_”, “!”, “A”…“Z”) then that character is converted to an underscore
    If the element of String is an empty string or a reserved word (such as “AND”) then an underscore is prepended to the string

    Tip: The CONVERT_ALL keyword guarantees that a valid variable name is returned. It is useful in converting user-supplied strings into valid IDL variable names.

    CONVERT_SPACES
    If this keyword is set, then all spaces within each element of String are converted to underscores. If an element of String contains any other non-alphanumeric characters, then an empty string is returned, indicating that the string cannot be used as a valid variable name.

    Note: CONVERT_SPACES behaves the same as CREATE_STRUCT when checking structure tag
    3、N_Elements函数
    判断变量是否已经被定义,如果定义返回1,否则返回0.
    4、类型转换
    这里写图片描述
    图5 类型转换函数
    5、改变数组部分元素不改变数组数据类型
    6、系统变量
    分为预定义系统变量和自定义系统变量
    预定义系统变量:常数变量、图形变量、系统配置、错误处理

    这里写图片描述
    图6 常数变量
    这里写图片描述
    图7 图像变量
    这里写图片描述
    这里写图片描述
    图8 系统配置
    这里写图片描述
    图9 错误处理
    自定义系统变量格式:Defsysv,变量名,变量值
    注:自定义系统变量创建成功后,只可以修改,但是类型不变,生命周期从初始化成功到IDL进程关闭。

    数组

    数组是IDL中最重要的数据组织形式,支持0~8维,下标顺序先列标、后行标。
    1、数组创建函数
    这里写图片描述
    图10 数组创建函数
    2、创建特定类型或数值的数组
    MAKE_ARRAY
    The MAKE_ARRAY function enables you to dynamically create an array whose characteristics are not known until run time.

    Examples
    To create a 3-element by 4-element integer array with each element set to the value 5, enter:
    M = MAKE_ARRAY(3, 4, /INTEGER, VALUE = 5)
    Syntax
    Result = MAKE_ARRAY ( [D1[, …, D8]] [, DIMENSION=vector] [, INCREMENT=value] [, /INDEX] [, /NOZERO] [, SIZE=vector] [, START=value] [, TYPE=type_code] [, VALUE=value] [, /BYTE | , /COMPLEX | , /DCOMPLEX | , /DOUBLE | , /FLOAT | , /INTEGER | , /L64 | , /LONG | , /OBJ, | , /PTR | , /STRING | , /UINT | , /UL64 | , /ULONG] )
    3、存储数组
    按行存储
    4、使用数组
    @1、下标方式

    IDL> arr=indgen(8)
    IDL> print,arr
           0       1       2       3       4       5       6       7
    IDL> print,arr[4]
           4

    注:8.0之后下标可以为负,-1代表最后一个元素。*

    IDL> print,arr[-1]
           7
    IDL> print,arr[-5:-1]
           3       4       5       6       7

    @2、向量方式
    读取第1、2、4、5元素值

    IDL> print,arr
           0       1       2       3       4       5       6       7
    IDL> index=[0,1,3,4]
    IDL> result=arr[index]
    IDL> print,result
           0       1       3       4

    5、数组运算
    @1、求大求小和求余
    注:求大、求小时会将数组不符合的元素全都改变成比较的数值

    IDL> print,arr
           0       1       2       3       4       5       6       7
    IDL> print,arr>4
           4       4       4       4       4       5       6       7
    IDL> print,arr<6
           0       1       2       3       4       5       6       6
    IDL> print,arr mod 3
           0       1       2       0       1       2       0       1

    @2、数组与数的运算
    每个元素与数进行运算

    IDL> print,arr
           0       1       2       3       4       5       6       7
    IDL> result=arr*2
    IDL> print,result
           0       2       4       6       8      10      12      14
    IDL> print,arr+3
           3       4       5       6       7       8       9      10

    @3、数组与数组运算
    结果元素个数与参与运算数组最少元素个数的数组一致,数组元素运算按照行优先顺序依次进行运算。

    IDL> arr1=indgen(3,2)
    IDL> print,arr1
           0       1       2
           3       4       5
    IDL> arr2=indgen(3,2)*2
    IDL> print,arr2
           0       2       4
           6       8      10
    IDL> arr3=indgen(2,3)
    IDL> print,arr3
           0       1
           2       3
           4       5
    IDL> result=arr1+arr2
    IDL> print,result
           0       3       6
           9      12      15
    IDL> print,arr1+arr3
           0       2       4
           6       8      10
    IDL> arr4=indgen(4)
    IDL> print,arr1+arr4
           0       2       4       6

    @4、数组合并
    数组合并要求:两个数组行数或者列数相同。
    注:列数相同合并写法方式

    行数相同:
    IDL> print,arr1
           0       1       2
           3       4       5
    IDL> print,arr4
           0       1       2       3
    IDL> arr5=[arr1,arr4]
    % Unable to concatenate variables because the dimensions do not agree: ARR4.
    % Execution halted at: FIRST               4 F:\学习资料\IDL\code\Default\first.pro
    %                      $MAIN$          
    IDL> arr6=indgen(4,2)*3
    IDL> print,arr6
           0       3       6       9
          12      15      18      21
    IDL> arry5=[arr1,arr6]
    IDL> print,arry5
           0       1       2       0       3       6       9
           3       4       5      12      15      18      21
    列数相同:
    IDL> print,arr1
           0       1       2
           3       4       5
    IDL> print,arr7
           0       1       2
           3       4       5
           6       7       8
           9      10      11
    IDL> result=[arr1,arr7]
    % Unable to concatenate variables because the dimensions do not agree: ARR7.
    % Execution halted at: FIRST               4 F:\学习资料\IDL\code\Default\first.pro
    %                      $MAIN$          
    IDL> result=[[arr1,arr7]]
    % Unable to concatenate variables because the dimensions do not agree: ARR7.
    % Execution halted at: FIRST               4 F:\学习资料\IDL\code\Default\first.pro
    %                      $MAIN$          
    IDL> result=[[arr1],[arr7]]
    IDL> print,result
           0       1       2
           3       4       5
           0       1       2
           3       4       5
           6       7       8
           9      10      11
    

    6、数组相关函数
    @1、条件查询–WHERE(源IDL-Help)
    The WHERE function returns a vector that contains the one-dimensional subscripts of the nonzero elements of Array_Expression. The length of the resulting vector is equal to the number of nonzero elements in Array_Expression. Frequently the result of WHERE is used as a vector subscript to select elements of an array using given criteria.

    Note: When WHERE Returns –1
    If the NULL keyword is not set, and all the elements of Array_Expression are zero, then WHERE returns a scalar integer with a value of –1. If you use this result as an index into another array without checking for –1 first, then this will return the last element of the array. To avoid this problem, you should either use /NULL or check the Count argument before indexing. For example:

    ; Use /NULL, if no elements match then array will not be modified:
    array[WHERE(array GT 5, /NULL)] = 5
    or,

    ; Use Count to get the number of nonzero elements:
    index = WHERE(array GT 5, count)
    ; Only subscript the array if it is safe:
    IF count NE 0 THEN array[index] = 5
    Syntax
    Result = WHERE( Array_Expression [, Count] [, COMPLEMENT=variable] [, /L64] [, NCOMPLEMENT=variable] [, /NULL] )

    IDL> print,arr
           0       1       2       3       4       5       6       7
    IDL> result=where(arr gt 3)
    IDL> result1=where(arr lt 4)
    IDL> result2=where(arr eq 5)
    IDL> result3=where(arr ge 3)
    IDL> result4=where(arr le 6)
    IDL> print,result,result1,result2,result3,result4
               4           5           6           7
               0           1           2           3
               5
               3           4           5           6           7
               0           1           2           3           4           5           6

    @2、调整大小–Reform()、Rebin()、Congrid()、Interpolate()函数
    #1、Reform()
    The REFORM function changes the dimensions of an array without changing the total number of elements.

    修改维数,数组元素大小不变

    Examples
    REFORM can be used to remove “degenerate” leading dimensions of size one. Such dimensions can appear when a subarray is extracted from an array with more dimensions. For example

    ; a is a 3-dimensional array:
    
    a = INTARR(10,10,10)
    
    ; Extract a "slice" from a:
    b = a[5,*,*]
    
    ; Use HELP to show what REFORM does:
    HELP, b, REFORM(b)
    Executing the above statements produces the output:
    B            INT = Array[1, 10, 10]
    <Expression> INT = Array[10, 10]

    The statements have the identical effect. They create a new array, b, with dimensions of (200, 5), from a.:

    b = REFORM(a,200,5)
    b = REFORM(a,[200,5])

    Syntax
    Result = REFORM( Array, D1[, …, D8] [, /OVERWRITE] )
    OVERWRITE:
    Set this keyword to cause the specified dimensions to overwrite the present dimensions of the Array parameter. No data are copied, only the internal array descriptor is changed. The result of the function, in this case, is the Array parameter with its newly-modified dimensions. For example, to change the dimensions of the variable a, without moving data, enter:
    a = REFORM(a, n1, n2, /OVERWRITE)
    #2、Rebin()
    The REBIN function resizes a vector or array to dimensions given by the parameters Di. The supplied dimensions must be integral multiples or factors of the original dimension. The expansion or compression of each dimension is independent of the others, so that each dimension can be expanded or compressed by a different value.

    If the dimensions of the desired result are not integer multiples of the original dimensions, use the CONGRID function.
    修改数组大小,修改后数组行或列数必须是原数组行列数的整数倍,默认抽样算法是双线性内插

    IDL> print,arr2
           0       2       4
           6       8      10
    IDL> result=rebin(arr2,6,4)
    IDL> print,result
           0       1       2       3       4       4
           3       4       5       6       7       7
           6       7       8       9      10      10
           6       7       8       9      10      10
    IDL> result=rebin(arr2,6,4,/sample)
    IDL>;sample:最近邻抽样算法
    IDL> print,result
           0       0       2       2       4       4
           0       0       2       2       4       4
           6       6       8       8      10      10
           6       6       8       8      10      10

    #3、Congrid()
    The CONGRID function shrinks or expands the size of an array by an arbitrary amount. CONGRID is similar to REBIN in that it can resize a one, two, or three dimensional array, but where REBIN requires that the new array size must be an integer multiple of the original size, CONGRID will resize an array to any arbitrary size. (REBIN is somewhat faster, however.) REBIN averages multiple points when shrinking an array, while CONGRID just resamples the array.
    **可以将数组调整为同维任意大小。
    处理一维、二维数组时,默认算法是最近邻重采样;
    处理三维数组时,默认采用双线性内插算法;
    在对数组进行缩小操作时,采用Rebin()函数进行插值处理,Congrid()函数仅进行重采样。