Presentation is loading. Please wait.

Presentation is loading. Please wait.

IDL Tutorial Day 1 Goals: 1) Introduce IDL basics 2) Describe fundamental IDL structures Maria Kazachenko

Similar presentations


Presentation on theme: "IDL Tutorial Day 1 Goals: 1) Introduce IDL basics 2) Describe fundamental IDL structures Maria Kazachenko"— Presentation transcript:

1 IDL Tutorial Day 1 Goals: 1) Introduce IDL basics 2) Describe fundamental IDL structures Maria Kazachenko (kazachenko@physics.montana.edu)

2 What is IDL? 1970 – IDL’s predecessor, Laboratory for Atmospheric and Space Physics, CO 1970 – IDL’s predecessor, Laboratory for Atmospheric and Space Physics, CO 1987 – Unix environment 1987 – Unix environment 1992 – Widgets added 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. It is has all of the program control options of a standard programming language. Syntax includes many constructs from Fortran and some from C Syntax includes many constructs from Fortran and some from C Many useful functions are available in large library Many useful functions are available in large library Used by 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 http://www.lmsal.com/solarsofthttp://www.lmsal.com/solarsoft

3 IDL under Unix

4 Basic Unix Commands >man command-name - looks at the manual page for the command >man command-name - looks at the manual page for the command >pwd - shows current working directory path >pwd - shows current working directory path >cd {dirname} - changes current directory into dirname >cd {dirname} - changes current directory into dirname >cd /home/ >chmod {options} - changes permission modes of a file >chmod {options} - changes permission modes of a file >cp {filename}{path} - copies files from one directory/filename to another >cp {filename}{path} - copies files from one directory/filename to another >cp f1 f2 - makes a file f2 identical to f1 >cp f1 f2 - makes a file f2 identical to f1 >cp –r - copies a folder >emacs {filename} - runs the text editor named EMACS >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’ >find./ -name ‘’t*’’ –print - searches the named directory and its sub- directories for files starting with ‘t’ >gv {filename.ps} - X PostScript previewer >gv {filename.ps} - X PostScript previewer >ls {directory} - shows directory listing >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 >mkdir dirname - makes a sub-directory named “dirname” in the current directory >ssh {machine-name} - secure alternative to telnet. >ssh {machine-name} - secure alternative to telnet.

5 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,a and you would type in “print,a” at the command line. Also [brackets] in an IDL description indicate an optional item. Note

6 Starting IDL

7 In a terminal window, once an IDL “path” is setup (.idl_startup), type idl, idlde, sswidl, or sswidlde 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 useful programs for solar physics work. 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 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 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…’) To use an OS command, enter $ prior to it (Exceptions: pwd, cd,’/disk…’) IDL>$ls ;lists content of current directory IDL>$ls ;lists content of current directory To exit out of IDL To exit out of IDL IDL>exit IDL>exit

8 Journal Procedure Records everything you enter, plus any error or processing messages output by IDL, into a text file 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> journal,‘text_file’ ;opens the file & ;starts record IDL> command line IDL> command line IDL> journal ;closes the journal file IDL> journal ;closes the journal file To view, open journal file with any text editor To view, open journal file with any text editor > emacs text_file > emacs text_file or > nedit text_file Journal is also useful for starting a program you want to write, or for creating batch files

9 General Strategies IDL> ?; IDL help menu IDL> ?; IDL help menu IDL> xdoc; Sswidl (solar) help IDL> xdoc; Sswidl (solar) help IDL> help, [variable, /str]; Properties of variables IDL> help, [variable, /str]; Properties of variables IDL> z=fltarr(3,7) ; Defines new array (3x7) IDL> z=fltarr(3,7) ; Defines new array (3x7) IDL> help,z; Tells about z IDL> help,z; Tells about z Z FLOAT = Array[3, 7] Z FLOAT = Array[3, 7] IDL> which,’display.pro’; Location of the program ; you’re using 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 Unix text editors - nedit, emacs, pico, vi > nedit mypro.pro > nedit mypro.pro Don’t memorize all commands Don’t memorize all commands Keep notes on your work and use the IDL journal Keep notes on your work and use the IDL journal Write programs in steps Write programs in steps Learn by doing - don’t be afraid to make mistakes!!! Learn by doing - don’t be afraid to make mistakes!!!

10 Some basic syntax IDL is not case sensitive but Unix is 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 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-e, Ctrl-a – moves cursor to the end, beginning of the line Ctrl-C – interrupts IDL-command Ctrl-C – interrupts IDL-command IDL> ; comments out an IDL line IDL> ; comments out an IDL line Use & to enter more than one command on a 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 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 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 “All” programs end with.pro - masha.pro

11 Basic Variable Types Integers Integers - 1 byte – byte data type (0..256) - 1 byte – byte data type (0..256) IDL> a=bindgen(1) - 2 bytes – integers data type (0..256 2 -1) - 2 bytes – integers data type (0..256 2 -1) IDL> b=indgen(1) & print,5/3 1 - 4 bytes – long integers (0..256 4 -1) - 4 bytes – long integers (0..256 4 -1) IDL> c=lindgen(1) - 8 bytes – 64-bit long integers (0..256 8 -1) - 8 bytes – 64-bit long integers (0..256 8 -1) Floating data types Floating data types - 4 bytes – floats (exponent from-38 to 38) - 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 - 8 bytes – double-precision IDL> print,5d/3d 1.6666667 IDL> a=1.23456789d0 & print,a,format=‘(f20.10)’ IDL> a=1.23456789d0 & print,a,format=‘(f20.10)’ IDL> xyouts,xloc,yloc,string(a,format=‘f(20.10)’) Strings (Text) Strings (Text) IDL>x=‘Hi there!!!’ IDL>x=‘Hi there!!!’

12 Assigning a Variable a Value, Algebra Variable_name = value Variable_name = value - will automatically select a variable type - will automatically select a variable type IDL> a=5/3.+4.1 ; float 5.76667 IDL> a=5/3.+4.1 ; float 5.76667 Variable_name = function_name(argument) Variable_name = function_name(argument) IDL> b=exp(a) 148.413 ; float 319.471 IDL> b=exp(a) 148.413 ; float 319.471 Variable_name = string(value) Variable_name = string(value) IDL> str=string(’10’) ; str=’10’ IDL> str=string(’10’) ; str=’10’ Variable_name = long(value), converts to long integer type. Variable_name = long(value), converts to long integer type. IDL>x=long(32000.0) ; long integer 32000 IDL>x=long(32000.0) ; long integer 32000 Variable_name = fix(value), convert to integer type. Variable_name = fix(value), convert to integer type. IDL> print,fix(!Pi) 3 ; integer IDL> print,fix(!Pi) 3 ; integer Variable_name = float(value), convert into single precision float Variable_name = float(value), convert into single precision float IDL> help,float(2) 2.00000 ;float IDL> help,float(2) 2.00000 ;float Variable_name = double(value), converts into double precision float Variable_name = double(value), converts into double precision float

13 Naming Variables Assign ‘readable’ variable names that make sense Assign ‘readable’ variable names that make sense Variable names must start with a letter Variable names must start with a letter - NO: 6a=“gamma” OK: a6=“gamma” Variables should not have the same name as IDL functions Variables should not have the same name as IDL functions -NO: plot=6.0 -NO: plot=6.0 Variable names may not have spaces in them Variable names may not have spaces in them - NO: A 6=5 OK: A_6=5 Some characters are illegal or have special meanings Some characters are illegal or have special meanings - NO: a@=5, a.b=5 (used for structures), A+=5, A#=5

14 Organizational structures Scalars Scalars IDL> x=3. Vectors Vectors IDL> a=[0,1,2,3] & print,a[1] 1 Arrays (see IDL help for matrices) (index from zero) Arrays (see IDL help for matrices) (index from zero) intarr(), fltarr(), dblarr(), strarr().indgen() intarr(), fltarr(), dblarr(), strarr().indgen() IDL> q=intarr(2,2) 00 00 IDL> m=indgen(2,2)01 23 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 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

15 Array operations Simple math operations (* by scalar, +, -); A 3-column by 2-row array: Simple math operations (* by scalar, +, -); A 3-column by 2-row array: IDL> A = [ [0, 1, 2],[3, 4, 5],[5,6,7]] IDL> A = [ [0, 1, 2],[3, 4, 5],[5,6,7]] IDL>B=2*A 0 2 4 IDL>B=2*A 0 2 4 IDL>print,B 6 810 IDL>print,B 6 810 101214 101214 More complex math More complex math #, Multiply an array by another IDL> C=A#B#, Multiply an array by another IDL> C=A#B N_elements: Total number of elements in arrayN_elements: Total number of elements in array IDL> n=n_elements(A) Total(array): computes a sum of all elementsTotal(array): computes a sum of all elements Min(array): outputs the min value; also MaxMin(array): outputs the min value; also Max Minmax(array): outputs the min then maxMinmax(array): outputs the min then max Rebin: Resize array IDL> C=Rebin(A,6,6)Rebin: Resize array IDL> C=Rebin(A,6,6) Eigenvec: finds eigenvectorsEigenvec: finds eigenvectors

16 Structures Structures are variables that contain multiple variables (scalars, arrays, or other structures) Structures are variables that contain multiple variables (scalars, arrays, or other structures) Each variable contained within a structure is called a tag 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' NAME STRING 'alpha ori' RA FLOAT 5.44000 RA FLOAT 5.44000 DEC FLOAT -7.60000 DEC FLOAT -7.60000 RED FLOAT Array[12] 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)

17 “Homework” 0) Start with Journal-file 1)Make a vector x=(0,1,2,3,4,5,6,7,8,9)*Pi/9 IDL> x=indgen(10)*!Pi/9 2)Make a vector y=sin(x) IDL> y=sin(x) 3)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

18 Questions

19 Loops Programs with vector and array expressions run faster than programs with scalars, loops, and IF statements. 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.11 IDL>FOR i = 1, 4 DO PRINT, i,i^2 24 39 4 16 Or if there are many lines in a loop, then write simple program PRO Test FOR i = 1, 4 DO BEGIN FOR i = 1, 4 DO BEGINPRINT,i,i^2ENDDOEND


Download ppt "IDL Tutorial Day 1 Goals: 1) Introduce IDL basics 2) Describe fundamental IDL structures Maria Kazachenko"

Similar presentations


Ads by Google