How to Manipulate Arrays and Read Columns of Data From Files

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.



An array may be created by simply typing:
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.



To create and array that will be filled in a loop or filled by reading values from a data file, you must first define the array and declare what type it will be:
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] = i
A quicker way to create this particular array would be:
C = FindGen(50)


IDL makes it particularly easy to access selected parts of an array.  For instance if you have a 2 dimensional array with 20 elements in each dimension, you can access the values of the first dimension and place them into a one dimensional array like this:
D = IntArr(20,20)
k = 0
For i = 0, 19 Do Begin
   For j = 0, 19 Do Begin
      D[i,j] = k
      k = k + 1
   EndFor
EndFor
 To see what type of an array D is type:
help, D
and the output is:
D               INT       = Array[20, 20]
To see the array contents, type:
print, D
The 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, E
You 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)


Reading a file containing several columns of data

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_Lun
Point_Lun, lun, 0
ReadF, lun, adim, bdim, cdim, ddim, num_columns

array = 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
 

The output from this program is:

      45
      48
      47
      49
      47
      43
      44
      48
      47
      45


Functions for Manipulating Arrays

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)
print, E
The result will be:

     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)


Array concatenation

Arrays can be concatenated together in IDL.  For instance, if

G = [2, 4, 6]
print, G
2   4   6
An '8' can be concatentated to the end of the array G, by:
G = [G, 8]
print, G
2   4   6   8
A '0' can be added to the front by:
G=[0,G]
print, G
0   2   4   6   8
Two arrays can be concatenated:
H = [1,2,3]
I = [4,5,6]
J = [H, I]
print, J
1   2   3   4   5   6
To create a multidimensional array of 1 dimensional arrays:
K = [4,6,8]
L = [3,6,9]
M = [8,16,24]
Narray = [[K], [L], [M]]
print, Narray
       4       6       8
       3       6       9
       8      16      24
Note how the content of N changes if the square brackets are not used around the name of each array.  Instead of making a 3 dimensional array, N is now a 1 dimensional array.
Nplain = [K, L, M]
print, Nplain

     4       6       8       3       6       9       8      16      24