This page has only a brief introduction to arrays in IDL. Its
main purpose is to provide useful information on arrays, but which is not
necessarily obvious to find in the IDL manual and on-line help.
A = [1, 2, 3, 4, 5]In this case, IDL accepted the integers as typed and formed an integer array.
Typing:
B = [1., 2, 3, 4, 5]Creates a float array because the decimal point was used for one of the array values.
Note that the values that an array contains are placed within square
brackets--[ ]. This was done to avoid confusion between arrays and
functions (with their arguments). Parentheses were used in
earlier versions of IDL, but a transition to square brackets in preferred.
C = FltArr(50)Note, in this case parentheses are used instead of the square brackets since only the number of elements are declared, not the actual values of the elements.
A way to populate the array might be:
For i = 0,49, Do C[i] = iA quicker way to create this particular array would be:
C = FindGen(50)
D = IntArr(20,20)To see what type of an array D is type:
k = 0
For i = 0, 19 Do Begin
For j = 0, 19 Do Begin
D[i,j] = k
k = k + 1
EndFor
EndFor
help, Dand the output is:
D INT = Array[20, 20]To see the array contents, type:
print, DThe entire 400 elements of the array will be printed to the screen.
To obtain the values from the first dimension of the array and place the contents in a new one dimensional array, simply type:
E=(0,*)The zero in the parentheses indicates you are using the first dimension (remember IDL begins counting at 0). The * in the parentheses indicates that the entire set of values are to be used. If you type
print, EYou would get the entire array of E which is formed of the entire set of values in the first dimension of D. Typing
print, D(0,*)would produce the same values.
If you would like to access (in this case, print) a portion of the array E, such as the 18th to the 24th elements, type:
print, E(17:23)
Suppose you have a data file named 'data_in' containing the following:
10 10 10 10 4
12 25 45 100
15 29 48 110
13 28 47 109
11 26 49 112
14 22 47 108
17 27 43 105
19 26 44 109
18 28 48 108
13 23 47 111
15 25 45 113
Where the first four numbers on the first line are the number of elements in each column, and the final number is the number of columns. Then each of the 4 columns of data are given.
Here's some code that would open the data file and read the columns of numbers and place them in useable arrays.
OpenR, lun, 'data_in', /Get_LunThe output from this program is:
Point_Lun, lun, 0
ReadF, lun, adim, bdim, cdim, ddim, num_columnsarray = intarr(4, 10)
a = intarr(10)
b = intarr(10)
c = intarr(10)
d = intarr(10)readF, lun, array
a = array(0,*)
b = array(1,*)
c = array(2,*)
d = array(3,*)Free_Lun, lun
print, c
45
48
47
49
47
43
44
48
47
45
Reform:
Reform is a function that manipulates an array to change its dimenstions.
For instance, if you have an array, C which contains 10 elements and prints
as this:
IDL> print, C
IDL> 45
48
47
49
47
43
44
48
47
45
And you then type:
E = reform(C)The result will be:
print, E
45 48 47 49 47 43 44 48 47 45
Yet another way to use Reform would be to create a 2 dimensional array with 5 elements in each array:
F = reform(C, 2, 5)
print, F
45
48
47
49
47
43
44
48
47
45
Rebin:
Rebin is a function that decimates data by interpolation. For
instance, consider a data file with a single column of numbers that were
written to the file sequentially that represent values from a 2 dimensional
array of 400 elements, total. Once your IDL code has read
the column into an array, e.g., data_array, it can then be reformed back
into a 2D array and decimated in one step to a 2 D array with 10 elements
each instead of 20 x 20. The final array will be 1/4 the size of
the original.
data_array = fltarr(400)
decimated_data = rebin(data_array, 10, 10)
Arrays can be concatenated together in IDL. For instance, if
G = [2, 4, 6]An '8' can be concatentated to the end of the array G, by:
print, G
2 4 6
G = [G, 8]A '0' can be added to the front by:
print, G
2 4 6 8
G=[0,G]Two arrays can be concatenated:
print, G
0 2 4 6 8
H = [1,2,3]To create a multidimensional array of 1 dimensional arrays:
I = [4,5,6]
J = [H, I]
print, J
1 2 3 4 5 6
K = [4,6,8]4 6 8
L = [3,6,9]
M = [8,16,24]
Narray = [[K], [L], [M]]
print, Narray
Nplain = [K, L, M]
print, Nplain4 6 8 3 6 9 8 16 24