IDL 5220 Week 2 - Arrays, bubble sorts, and the WHERE function

  1. More Maths
  2. Definition of an Array
    An array is a matrix variable. It can be used to store whole rows, columns, tables, or even cubes of data. Arrays are made up of many individual elements, which are the same as variables. For instance, if A is an array of length 10, it would look like:
    Where 0 - 9 are the indicies of the individual array elements. The first element in an array always has the index of 0. Array elements are accessed with [] square brackets. Each element can be treated just as any variable would. For instance, A[1] = 5*3. Arrays can be very useful for instance when you want to store the prices of 50 different items or when you want to store the flux of an object at thousands of different wavelengths.
  3. Arrays in IDL
  4. Creating Arrays in IDL:
    Arrays can be created for every data type in IDL (integer, float, etc.). There are two ways to make an array for each data type. One of them will create an empty array i.e. every element will have its value set to 0 or null. The other will create a prenumbered array in which every element will have its value set to its index i.e. A[0]=0, A[1]=1, ..., A[20]=20.
    Data TypeCreate Empty ArrayCreate Numbered Array
    Again, since the first element in an array has an index 0, if you create a 10-element array, its highest element is 9, so you can access X[0], X[1], ..., and X[9].
    A third way of creating arrays is to enter the data manually. For example:
    A = [5, 3, 7, 9, 0, 1]
    B = [[3,2],[4,7],[6,3]]
    Creates an array A of length 6 and a 2-dimensional array B with 2 columns and 3 rows.
  5. Arrays and Maths
    IDL is unique in that it is array based. That means that any arithmatic operation can be used on an entire array and not just individual elements.
    A = FINDGEN(10)                A=[0,1,2,3,4,5,6,7,8,9]
    B = FINDGEN(10)*10+100       B=[100,110,120,130,140,150,160,170,180,190]
    A = A+2    every element in A is incremented by 2 so A=[2,3,...,11]
    B = B/20.  every element in B is divided by 20. so B=[5,5.5,6,...,9.5]
    A = A*B  every element in A is multiplied by the corresponding element in B so A=[0,110,240,...,1710]
    print,A > B   takes the greater value of each element i.e.A[0] > B[0], A[1] > B[1],...
    Matrix Multiplication:
              25          40
               6          10
              16           7
              42          19
              16           7
              42          19
  6. Array subscripts
  7. Array operations
  8. Bubble Sort
    Many times, you'll want to write your own sort routine, based on your own criteria. One of the simplest sorting routines is the bubble sort. The bubble sort has a good efficiency, with time complexity of the order n^2. Quick sorts, heaps, and winner trees can sort with better time complexity, of order n*log(n), but are much more complicated to design. In its simplest version, say you have an array A of numbers (or Strings) that you want to sort from lowest to highest (or alphabetically):
    FOR j=0,n_elements(A)-2 do begin
       FOR l=j+1,n_elements(A)-1 do begin
          IF A[l] lt A[j] then begin 
    The first loop goes from the first element to the n-1 element. The second loop goes from the current position of the first loop and checks the rest of the array. If it finds any values less than that at the current position of the first loop, it swaps the two positions. Example:
    A = [3, 6, 0, 4]
    j=0, l=2: A[l]=0  < A[j]=3 so they are swapped 
    A = [0, 6, 3, 4]
    j=1, l=2: A[l]=3  < A[j]=6 so they are swapped 
    A = [0, 3, 6, 4]
    j=2, l=3: A[l]=4  < A[j]=6 so they are swapped 
    A = [0, 3, 4, 6]
    This would work the same way with strings for instance if you had A = ["Moss","Walsh","Drejer","Roberson"] since Drejer  < Moss   < Roberson   < Walsh
    Say you want to sort these 4 basketball players by their scoring averages and the basketball players are in an array names, while their averages are in an array points:
    FOR j=0,n_elements(points)-2 do begin
       FOR l=j+1,n_elements(points)-1 do begin
          IF points[l] gt points[j] then begin
    The names are now sorted by how many points per game they score, highest listed first (Roberson, Walsh, Moss, Drejer if anyone wants to know).
  9. WHERE function
    The WHERE function is one of the most powerful features of IDL. It allows you to sort, find, and manipulate data within arrays in numerous ways. The structure is B = WHERE(condition involving A) where A and B are arrays.

Homework 2: Peak flux program. Write a program that reads a .fits file of a quasar spectrum, generates the x-axis wavelength scale, and finds 1) the peak flux of the entire spectrum, 2) the wavelenth corresponding to the peak flux, 3) the peak flux of C IV 1549 (find the peak flux between 1500 and 1600 angstroms), and 4) the wavelength corresponding to the peak of C IV.
Notes: Before writing this program, copy the file /astro/data/homer0/warner/MBH/IRAF/M7.fits to your local directory. To read the .fits file, use the following line:
The array flux will be a 13992 element array containing the flux values for the spectra of a quasar. You have to generate your own x-axis wavelength scale when reading in .fits files. The information to do this is located in the header (see next week's File I/O). For this example, you of course need another 13992 element array. The first wavelength is 500 angstroms and there is an interval of 0.5 angstroms between every point.