80 likes | 212 Views
IDL Tutorial: Day 2. Goals: 1) Introduce functions and procedures 2) Describe writing the simplest kind of programs 3) Explain saving and restoring. Maria Kazachenko (kazachenko@physics.montana.edu). Functions and Procedures. Two basic types of programs Functions:
E N D
IDL Tutorial: Day 2 Goals:1) Introduce functions and procedures2) Describe writing the simplest kind of programs 3) Explain saving and restoring Maria Kazachenko (kazachenko@physics.montana.edu)
Functions and Procedures • Two basic types of programs • Functions: result=function_name(parameter, Keyword=Set, …) Examples: size, n_elements, sin, cos, reverse, where - Functions can be placed anywhere you can put a value • Procedures: procedure_name, param1, param2, Keyword=Set… Examples: help, print, open, printf, plot, set_plot, device, save - Procedures do stuff, but don’t need to return a value • Exact syntax for particular Procedures or Functions can be found using the help menu, xdoc, or the doc_library procedure
Commonly Used Procedures • print: for printing stuff to screen: IDL>print, sin(x) • plot: plot variables to the current graphics device (usually screen): IDL>plot, x, y,[title=‘blah’,…keywords] • set_plot: tells idl where to send plots to. Screen (Xdisplay) is default - IDL>set_plot,’x’ ;sends output to the x display - IDL>set_plot, ‘ps’ ; sends the output to a ; postscript file with default ; name idl.ps. • device: changes properties of the device currently enabled by set_plot. See help menu. • Read ps files with ghostview (in Unix, not in IDL) unix_prompt > gv idl.ps
Save and Restore • Programs are saved by saving the text file you write them in • Save, the procedure, allows you to save variables to a file IDL>save, variable1, variable1, file=‘file.sav’ • Restore allows you to restore saved variables to a session IDL>restore, ‘file.sav’,/verbose
Commonly Used Functions • Many math operators are functions IDL> y=sin(x) IDL> y=cos(x) IDL>y=exp(x) • Where allows you to pick out the index of an array that meets some criterion IDL> index=where(new_array eq 15) (more on boolean operators later) • Replicate makes copies of variables, arrays, structures, etc... IDL> stars=replicate(stars,15) • dialog_pickfile allows you to graphically pick out file names IDL> file_name=dialog_pickfile()
Macros (Scripts) • Simplest form of Program In the editor window write commands just like you would at command prompt • At the end type END so IDL knows when program is over • Save the program with a unique name (i.e. save the text file) • At the command prompt type either: IDL> .run program_name IDL> .rnew program_name Shortcoming: no loops allowed in macros.
“Homework” • Make a macro in which you plot, on a good scale, with labels, y=ex. Also, have it print to the screen, “You did it right!” Make a post script copy of your plot. • Make a file using emacs (or your favorite editor) which contains a row for each student in this class and a column for each of the following: distance (in miles) of the student’s home from Bozeman, year in college, favorite color (in Angstroms), number of books read in the last year, number of trails hiked in the last year, and number of foods disliked . Use ascii_template and read_ascii to make a structure containing your numbers. Using the plot procedure, find out if there are any correlations among yourselves!