200 likes | 376 Views
IDL Tutorial Day 1 Goals: 1) Introduce IDL basics 2) Describe fundamental IDL structures. Maria Kazachenko (kazachenko@physics.montana.edu). What is IDL?. 1970 – IDL’s predecessor, Laboratory for Atmospheric and Space Physics, CO 1987 – Unix environment 1992 – Widgets added
E N D
IDL Tutorial Day 1Goals:1) Introduce IDL basics2) Describe fundamental IDL structures Maria Kazachenko (kazachenko@physics.montana.edu)
What is IDL? • 1970 – IDL’s predecessor, Laboratory for Atmospheric and Space Physics, CO • 1987 – Unix environment • 1992 – Widgets added Interactive Data Language (IDL) is a programming language that is a popular data analysis language among scientists. It is has all of the program control options of a standard programming language. • Syntax includes many constructs from Fortran and some from C • Many useful functions are available in large library • Used by - NASA - Lockheed-Martin - Medical Imaging Fields http://www.rsinc.com/ http://idlastro.gsfc.nasa.gov/homepage.html http://www.astro.washington.edu/deutsch/idl/htmlhelp/index.html http://www.ugastro.berkeley.edu/tutorials/tutorials.html http://www.dfanning.com/documents/tips.html http://www.lmsal.com/solarsoft
Basic Unix Commands • >man command-name- looks at the manual page for the command • >pwd - shows current working directory path • >cd {dirname} - changes current directory into dirname >cd /home/ • >chmod {options} - changes permission modes of a file • >cp {filename}{path} - copies files from one directory/filename to another >cp f1 f2 - makes a file f2 identical to f1 >cp –r - copies a folder • >emacs {filename} - runs the text editor named EMACS • >find ./ -name ‘’t*’’ –print - searches the named directory and its sub- directories for files starting with ‘t’ • >gv {filename.ps} - X PostScript previewer • >ls {directory} - shows directory listing >ls - prints the names of the files in the current directory >ls –l - shows long directory listing • >mkdir dirname - makes a sub-directory named “dirname” in the current directory • >ssh {machine-name} - secure alternative to telnet.
In this tutorial, IDL> refers to the IDL prompt. And >refers to OS prompt (outside IDL).Once in IDL, you type the stuff that comes after this. For example to enter the print command, I’ll have IDL>print,aand you would type in “print,a” at the command line. Also [brackets]in an IDL description indicate an optional item. Note
Starting IDL • In a terminal window, once an IDL “path” is setup (.idl_startup), type idl, idlde, sswidl, or sswidlde >sswidl • sswidl calls IDL with solar software and specified paths which have useful programs for solar physics work. • sswidlde calls a graphical developer, where sswidl simply works in your terminal window You’ll almost always use sswidl or sswidlde. • To manually set the “paths” needed for your work, talk to your research advisor • To use an OS command, enter $ prior to it (Exceptions: pwd, cd,’/disk…’) IDL>$ls ;lists content of current directory • To exit out of IDL IDL>exit
Journal Procedure • Records everything you enter, plus any error or processing messages output by IDL, into a text file IDL> journal,‘text_file’ ;opens the file & ;starts record IDL> command line IDL> journal ;closes the journal file • To view, open journal file with any text editor > emacs text_file or > nedit text_file Journal is also useful for starting a program you want to write, or for creating batch files
General Strategies • IDL> ? ; IDL help menu • IDL> xdoc ; Sswidl (solar) help • IDL> help, [variable, /str]; Properties of variables IDL> z=fltarr(3,7) ; Defines new array (3x7) IDL> help,z ; Tells about z Z FLOAT = Array[3, 7] • IDL> which,’display.pro’ ; Location of the program ; you’re using /disk/hl2/data/maria/projects/flares/LCT/display.pro • Unix text editors - nedit, emacs, pico, vi > nedit mypro.pro • Don’t memorize all commands • Keep notes on your work and use the IDL journal • Write programs in steps • Learn by doing - don’t be afraid to make mistakes!!!
Some basic syntax • IDL is not case sensitive but Unix is IDL> help, var (=) IDL> HELP, Var IDL> journal, “text_file” (NOT =) journal, “Text_File” • The up arrow key recalls the most recent IDL input lines • Ctrl-e, Ctrl-a – moves cursor to the end, beginning of the line • Ctrl-C – interrupts IDL-command • IDL> ; comments out an IDL line • Use & to enter more than one command on a line IDL> a=[1,2,3,4,5,6] & print,a,total(a) • Commas separate arguments from one another IDL> plot,a,sin(a/5.)/exp(a/50.) ;plots function • Use $ at the end of a line that needs to continue IDL> for i=0,5 do $ IDL> print,i,a[i] • “All” programs end with .pro - masha.pro
Basic Variable Types • Integers - 1 byte – byte data type (0..256) IDL> a=bindgen(1) - 2 bytes – integers data type (0..2562-1) IDL> b=indgen(1) & print,5/3 1 - 4 bytes – long integers (0..2564-1) IDL> c=lindgen(1) - 8 bytes – 64-bit long integers (0..2568-1) • Floating data types - 4 bytes – floats (exponent from-38 to 38) IDL>print,5./3. & y=float(3)/float(2) 1.66667 IDL> a=1.23456789 IDL> print,a,format=‘(f20.10)’ - 8 bytes – double-precision IDL> print,5d/3d 1.6666667 IDL> a=1.23456789d0 & print,a,format=‘(f20.10)’ IDL> xyouts,xloc,yloc,string(a,format=‘f(20.10)’) • Strings (Text) IDL>x=‘Hi there!!!’
Assigning a Variable a Value, Algebra • Variable_name = value - will automatically select a variable type IDL> a=5/3.+4.1 ; float 5.76667 • Variable_name = function_name(argument) IDL> b=exp(a) 148.413 ; float 319.471 • Variable_name = string(value) IDL> str=string(’10’) ; str=’10’ • Variable_name = long(value), converts to long integer type. IDL>x=long(32000.0) ; long integer 32000 • Variable_name = fix(value), convert to integer type. IDL> print,fix(!Pi) 3 ; integer • Variable_name = float(value), convert into single precision float IDL> help,float(2) 2.00000 ;float • Variable_name = double(value), converts into double precision float
Naming Variables • Assign ‘readable’ variable names that make sense • Variable names must start with a letter - NO: 6a=“gamma” OK: a6=“gamma” • Variables should not have the same name as IDL functions -NO: plot=6.0 • Variable names may not have spaces in them - NO: A 6=5 OK: A_6=5 • Some characters are illegal or have special meanings - NO: a@=5, a.b=5 (used for structures), A+=5, A#=5
Organizational structures • Scalars IDL> x=3. • Vectors IDL> a=[0,1,2,3] & print,a[1] 1 • Arrays (see IDL help for matrices) (index from zero) intarr(), fltarr(), dblarr(), strarr().indgen() IDL> q=intarr(2,2) 0 0 0 0 IDL> m=indgen(2,2) 0 1 2 3 IDL> print,m(0,1) 2 IDL> a=findgen(100,100) IDL> print,a[1,0] 1.00000 IDL> b=a[23:25,67:69] IDL> indx=where(a gt 10.) IDL> jndx=where(a[indx] le 100.) IDL> b=a[indx[ jndx]] makes b equal to 1-d array equal to the elements of a that are both larger than 10 and less than or equal to 100
Array operations • Simple math operations (* by scalar, +, -); A 3-column by 2-row array: IDL> A = [ [0, 1, 2],[3, 4, 5] ,[5,6,7]] IDL>B=2*A 0 2 4 IDL>print,B 6 8 10 10 12 14 • More complex math • #, Multiply an array by another IDL> C=A#B • N_elements: Total number of elements in array IDL> n=n_elements(A) • Total(array): computes a sum of all elements • Min(array): outputs the min value; also Max • Minmax(array): outputs the min then max • Rebin: Resize array IDL> C=Rebin(A,6,6) • Eigenvec: finds eigenvectors
Structures • Structures are variables that contain multiple variables (scalars, arrays, or other structures) • Each variable contained within a structure is called a tag IDL> struct={[struct_name], tag_name1: tag_value1,…} Anonymous structure (may change tags) IDL> A={name:’alpha ori’, ra:5.33, dec:-7.6, red:fltarr(12)} IDL> help,/st NAME STRING 'alpha ori' RA FLOAT 5.44000 DEC FLOAT -7.60000 RED FLOAT Array[12] IDL> print,a.dec -7.6 IDL> a.dec=5.5 Named structure.Once a named structure is defined, you cannot change the names of the tags or the number of tags IDL> A={star, name:’alpha ori’, ra:5.33,dec:-7.6,red:fltarr(12)} Array of structures (see Help for replicate function) IDL> cataloga=replicate(a,585)
“Homework” • 0) Start with Journal-file • Make a vector x=(0,1,2,3,4,5,6,7,8,9)*Pi/9 • IDL> x=indgen(10)*!Pi/9 • Make a vector y=sin(x) IDL> y=sin(x) • Plot (x,y) IDL> plot,x,y 3) Find a determinant of the matrix IDL> A = [ [1, 1, 2],[3, 4, 5] ,[5,6,7]] (first manually and then check the result with Determ(A)) 4) Think about an IDL project
Loops • Programs with vector and array expressions run faster than programs with scalars, loops, and IF statements. Consider the problem of adding all positive elements of array B to array A. IDL> FOR i = 0, (N-1) DO IF B[i] GT 0 THEN A[i] = A[i] + B[i] Or IDL> A = A + (B > 0) For…do – FOR statement is used to execute one or more statement repeatedly, while incrementing or decrementing a variable with each repetition. 1 1 IDL>FOR i = 1, 4 DO PRINT, i,i^2 2 4 3 9 4 16 Or if there are many lines in a loop, then write simple program PRO Test FOR i = 1, 4 DO BEGIN PRINT,i,i^2 ENDDO END