Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

IDL freshup

VIEWS: 24 PAGES: 8

									                                         IDL freshup
                           author: Selma de Mink, September 2007

                                      updated: August 2008




Contents
1 How to use this document?                                                                                                                            2

2 What is IDL?                                                                                                                                         2

3 Getting started                                                                                                                                      2

4 Tips and warnings                                                                                                                                    3
  4.1 How to get help? . . . . . . . . . . . . . . .   .   . .   . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   3
  4.2 Confusing integers and floats . . . . . . . .     .   . .   . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   3
  4.3 How to define an array? . . . . . . . . . . .     .   . .   . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4
  4.4 How to make a simple plot? / How to make         a   for   loop?     .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4
  4.5 How to plot multiple lines? . . . . . . . . .    .   . .   . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   4
  4.6 How to read data from an ASCII file? . . .        .   . .   . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   5
  4.7 How to get the sunsymbol in L ? . . . . . .      .   . .   . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   6
  4.8 How to show multiple plots on one page? .        .   . .   . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   6
  4.9 How to produce a colour plot? . . . . . . .      .   . .   . . . .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   6

5 Useful links                                                                                                                                         6

6 Exercises                                                                                                                                            7




                                                  1
1     How to use this document?
The aim of this document is to help you to get your first IDL program running (or to refresh your
memory of how this is done). Secondly this document will give you tips and warnings to prepare you
for the upcoming computer practica of the stellar evolution course. This document can be used in two
ways.

    • If you have not used IDL before or if you do not remember much of it, I suggest work through this
      document by trying to understand the examples and see if you can get them to work.1 Then do
      exercise 1 − 3 to apply the things you have learned.

    • If you are a more experienced IDL programmer quickly scan this document and start working on
      Exercise 1 − 5, using the document as a reference.

Disclaimer This document has been written in little time and probably contains some errors. We hope
you can contribute to a better version for next years students by giving us comments and suggestions.


2     What is IDL?
IDL (the Interactive Data Language) is a general purpose scientific computing package, which provides a
suite of mathematical functions, data analysis tools, as well as some scientific visualization and animation
tools. Although it is a commercial program it is widely used in the astronomical community.


3     Getting started
On the Windows PCs in the practicum room (BBL 408) the IDL Developer Environment should be
installed. You can find it when you click Start, then go to Programs and look for “Research Systems
IDL”. ⇒ Open IDL
    At the bottom of your screen you will see the command line. You can enter commands here. ⇒ Try
typing print, "Hello World" . You can use the command line for short commands and quick checks
or calculations. ⇒ Enter the lines below on the command line and type enter after each line to see what
they do:

                                                    IDL command line
       print,   5.+2.
       print,   2.^2
       print,   sqrt(4.)
       print,   alog10(10.)
       print,   alog(exp(1.))


    For longer programs the it is more useful to write a program. Let’s assume that your working directory
is G:/stev/pract/ and you are planning to save you programs there.2 ⇒ Tell IDL to go to this directory
typing the following on the command-line3

                                                    IDL command line
       cd, "G:/stev/pract/"


    All your programs can be stored in this directory with the extension .pro. You can use any text-
editor to write your programs, but here we will use the built-in editor of IDL DE. ⇒ Click FILE, then go
   1 If you open the digital version of this document, available on http://www.phys.uu.nl/∼pols/education/stev/practicum/idlfreshup/,

in the Adobe reader you can use copy-paste to save typing. Beware that only copy-pasting will not refresh your IDL
knowledge, try to understand what the examples do and how.
   2 If it does not exist then open My Computer on your desktop, navigate to G, and create a new folder for this course

stev and a a subfolder for the files of this practicum pract
   3 You might want to try single quotes if you experience any problems, although I am not sure about the difference.




                                                            2
to “new editor”. Write the simple program below in the text-editor and save it in your working directory,
for example as G:/stev/pract/myfirstprog.pro.4
                                                IDLcode
         pro myfirstprog
         ; With the semicolon you can add comments to your program
         print, "Test, Test"
         end


   Now we have to compile the code and then run your program by typing the name you gave. ⇒ Type
the following lines on the command-line.

                                          IDL command line
       .r "myfirstprog.pro" ; this compiles your programs
       myfirst              ; this runs your program


   Note that if you push the cursor-key with up-arrow, the commands you entered previously appear.
This might save you a lot of typing. An alternative way is the following set of keys: (1) save your
program with ctr+s, (2) compile it with ctr+F5 and (3) run your program with F5.


4     Tips and warnings
This section lists some commonly made mistakes and tips specifically for the stellar evolution practicum.
For a more general review of IDL functions I can recommend, for example, this website
    http://nstx.pppl.gov/nstx/Software/IDL/idl_intro.html#INTRO.
    The examples in this section are not complete programs. You can try most of them for yourself by
starting with pro myprog and ending with end. See the Getting Started section.

4.1     How to get help?
                                                     IDLcode
         ?                  ; will start on-line help
         ? plot             : opens help about plot function


The official on-line manual of IDL is useful if you remember the name of a function but not the syntax.
However in general the manual is a bit technical and it does not give many simple examples. Trying
google with words as “introduction” or “simple” might be more useful in the beginning. The more
experienced you become with IDL, the more useful the manual.

4.2     Confusing integers and floats
In the following program there is a mistake that you will probably make many times using IDL (I know
from experience). ⇒ Run it and see what it does. Can you fix the error?
                                                            IDLcode
         a = 1
         b = 3
         c = a/b
         print, a, b, c


    In the case a = 1 and b = 3, a and b are integers. Therefore c is also an integer. As an integer
cannot have the value 0.333333 it is rounded off and the value of c will be zero.
        Try to get used to typing a dot behind every number or even better to define your constants
directly as floats: a = 1.0e0, or double precision: a = 1.0d0.
  4 If you use another text editor like notepad, it tends to save your file as a textfile with the extension myfirstprog.pro.txt.

This will give problems.



                                                              3
4.3   How to define an array?
IDL is designed to work well with arrays, lists of numbers. ⇒ Try the following examples.
                                                  IDLcode
       a = [1., 2., 3.]           ;   define a small array by hand
       print, a(0)                ;   print the first(!) element of a
       print, max(a), min(a)      ;   print extreme values
       a = [a, 4., 5.]            ;   add more elements to the array
       print, 5*a


       Remember that the first element of an array a is denoted as a(0). If your array has 5 elements
then a(5) will give an error message. Can you explain why?
   You can also automatically initialize large arrays.
                                                 IDLcode
       a = fltarr(20)             ; float array with 20 elements
       print, a                   ;
       a = dblarr(20)             ; a is a double precision array now,
       print, a


     Try to use double precision number throughout the practicum to avoid errors with very large
numbers.

4.4   How to make a simple plot? / How to make a for loop?
In IDL you can easily plot the elements in one array against the elements in an other array. In the
following example we use a for-loop to initialize an array of x-values between 0.0 and 6.0 and an array
with y-values containing the sin(x). Note the range of the loop variable i and the use of double(),
which converts the value of the integer i into a double precision value.
                                                 IDLcode
       n = 100                    ; length of the array
       x = dblarr(n)              ; initialize the arrays
       y = dblarr(n)              ; with double precision

       for i=0, n-1 do begin
       ; fill x with values between 0. and 6. at equal intervals
          x(i) = 0.d0 + 6.d0 * (double(i) / double(n-1))

       ;  fill y with the sin x
          y(i) = sin ( x(i) )
       endfor

       plot, x, y                  ; plot y versus x


      Try to indent your code if you write a for loop and add many comments. A neat looking code is
much easier to debug.

4.5   How to plot multiple lines?
To overplot more than one line you can use the function oplot. Note that the $ symbol can be used to
continue code on the next line.
                                                 IDLcode
       n    =   100               ; length of the array
       x    =   DBLARR(n)         ; initialize the arrays
       y1   =   DBLARR(n)         ; with double precision
       y2   =   DBLARR(n)
       y3   =   DBLARR(n)



                                                   4
           FOR i=0,   n-1 DO BEGIN
              x(i)    = 0.d0 + 6.d0 * (DOUBLE(i) / DOUBLE(n-1))
              y1(i)   = SIN ( x(i) )
              y2(i)   = COS ( x(i) )
              y3(i)   = 2. * x (i)
           ENDFOR

           ; Set plotoptions for thicker lines
           !P.THICK=3
           !P.CHARTHICK=3
           !X.THICK=3
           !Y.THICK=3

           ; declare filename
           myfilename = "myfig.ps"
           print, "A figure will be saved in the current directory", $
              "with the name:", myfilename


           ; Open plot device
           SET_PLOT, "PS"
           DEVICE, LANDSCAPE=0, FILENAME=myfilename

           ; First only plot the frame and the axes labels
           PLOT,[0, 0], /NODATA, $
              XRANGE=[0.,6.], $
              XTITLE="X-axis", YTITLE="Y-axis"

           ; plot   the lines
           OPLOT,   x, y1, LINESTYLE=1
           OPLOT,   x, y2, LINESTYLE=2
           OPLOT,   x, y3, LINESTYLE=3

           ; plot a text label at coordinate x=2, y=3
           XYOUTS, 2., 3., "Textlabel"

           ; close the plotting device
           DEVICE, /CLOSE



4.6    How to read data from an ASCII file?
In this section we will see how to read data from an ascii file. ⇒ Download the file Sun.mdl 5 and save
it in your working directory. This file contains a model of the solar interior, i.e. the pressure, density,
temperature and abundances at different points inside.
                                                        IDLcode
           ; define string variable
           modfile = "G:\stev\pract\Sun.mdl"
           PRINT, "Reading ", modfile, "..."

           ; read file into a struct and skip the first line
           sundata_struct=READ_ASCII(modfile, DATA_START=1)
           ; convert struct to an array
           sundata=sundata_struct.(0)

           ;This has created a float array
           ;sundata[#cols-1,#rows-1] as written in the file
           print, sundata[0,5] ; print entry in 1st col, 6st line

  5 from   http://www.phys.uu.nl/∼pols/education/stev/practicum/idlfreshup/


                                                         5
        ; Store cols in separate arrays
        sun_mass   = sundata[0,*] ;array with 1st col
        sun_radius = sundata[1,*] ;array with 2nd col



4.7    How to get the sunsymbol in L ?
The website http://idlastro.gsfc.nasa.gov/homepage.html contains a central repository for low-
level astronomy software. In the section “Plotting Tools” you will find the file SUNSYMBOL(). ⇒ Save it
in your working directory. You can use the following code to plot a sunsymbol in a ps file.
                                                  IDLcode
        text_ylabel="log L (L"+sunsymbol()+")"



4.8    How to show multiple plots on one page?
To show 4 plotframes on one page add the following line to your program.
                                                  IDLcode
        !p.multi=[0,2,2]


  Every time you call PLOT, ... TITLE="frametitle" you will start plotting in a new frame. Use
OPLOT to plot multiple things in one frame.

4.9    How to produce a colour plot?
You can modify the example of plotting multiple lines to produce a colour plot. First, load colour table,
add /COLOR when you call the ps device and add COLOR= after the PLOT statements.
                                                  IDLcode
        ; load a color table
        LOADCT, 13

        ; open ps device
        SET_PLOT, "PS"
        DEVICE, LANSCAPE=0, FILENAME="myfig.ps", /COLOR

        ;plot lines
        PLOT, ........, COLOR = 50
        OPLOT, ......., COLOR = 150
        OPLOT, ......., COLOR = 250

        ; close the plotting device
        DEVICE, /CLOSE



5     Useful links
http://idlastro.gsfc.nasa.gov/homepage.html: The IDL Astronomy Users Library is a central
repository for low-level astronomy software. It contains many functions for analyzing specialized files.
In this course we will use the function LEGEND and SUNSYMBOL() from the section “Plotting Tools” and
the function READCOL form the section “Miscellaneous (Non-Astronomy) Procedures”.




                                                   6
6      Exercises
    1. Download the files mass 1.0.plt, mass 10.plt, mass 50.plt6 . They contain the evolution of all
       the variables listed in Table 1 as a function of time.

    2. Write an IDL routine that reads the files into arrays and produces a plot of the HR diagram with
       the evolution tracks. Add axes labels and text labels to make the plot clear. Save your plot as a
       postscript file.

    3. You will use parts of this program again. Make sure that the layout of your IDL program is clear
       and well structured (using white lines and indentation). Choose names for your variables that are
       self-explanatory. Add clear comments

    4. Optional Exercise: Use the manual to find out what the function WHERE does. Use it to determine
       at which model number the central hydrogen abundance is smaller than 10−4 . Plot a symbol for
       each evolution track to indicate the end of the central hydrogen burning phase. Choose a different
       symbol to indicate the onset of helium burning (think of a good definition yourself).

    5. Optional Exercise: Use the !p.multi=[,,] to plot multiple plots on one page. Plot for example
       an HRD in the upper left corner, a plot of the central temperature versus the central density next
       to it, and below the central hydrogen abundance as function of time and other variables that you
       consider interesting. Use consistent line types and colours. Add symbols indicating the special
       points of the previous question.




    6 http://www.phys.uu.nl/∼pols/education/stev/practicum/idlfreshup/evolutiontracks




                                                          7
Variable       Units        Column nr
model number   -            0
age            years        1
mass           M            2
log R          R            3
log L          L            4
log Teff        K            5
log Tc         K            6
log ρc         ?            7
mass He-core   M            8
mass CO-core   M            9
dM/dt          M /year      10
Xs             -            11
Ys             -            12
Cs             -            13
Ns             -            14
Os             -            15
Xc             -            16
Yc             -            17
Cc             -            18
Nc             -            19
Oc             -            20
LH             L            21
LHe            L            22
LC             L            23
Lν             L            24
Lgrav          L            25
MV             -            26
U −B           -            27
B−V            -            28
V −I           -            29

  Table 1: The content of .plt files




                  8

								
To top