idl tutorial: day 2

7
IDL Tutorial: Day IDL Tutorial: Day 2 2 Maria Kazachenko Maria Kazachenko ([email protected] ([email protected] ) ) Goals: Goals: 1) Introduce functions and procedures 1) Introduce functions and procedures Describe writing the simplest kind of progr Describe writing the simplest kind of progr 3) Explain saving and restoring 3) Explain saving and restoring

Upload: trella

Post on 08-Jan-2016

25 views

Category:

Documents


0 download

DESCRIPTION

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 ([email protected]). Functions and Procedures. Two basic types of programs Functions: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: IDL Tutorial: Day 2

IDL Tutorial: Day 2IDL Tutorial: Day 2

Maria KazachenkoMaria Kazachenko

([email protected])([email protected])

Goals:Goals:1) Introduce functions and procedures1) Introduce functions and procedures

2) Describe writing the simplest kind of programs2) Describe writing the simplest kind of programs3) Explain saving and restoring3) Explain saving and restoring

Page 2: IDL Tutorial: Day 2

Functions and ProceduresFunctions and Procedures• Two basic types of programsTwo basic types of programs• Functions: Functions:

result=function_name(parameter, Keyword=Set, …)result=function_name(parameter, Keyword=Set, …)

Examples: size, n_elements, sin, cos, reverse, whereExamples: size, n_elements, sin, cos, reverse, where

- Functions can be placed anywhere you can put a value- Functions can be placed anywhere you can put a value• Procedures:Procedures:

procedure_name, param1, param2, Keyword=Set…procedure_name, param1, param2, Keyword=Set…

Examples: help, print, open, printf, plot, set_plot, device, saveExamples: help, print, open, printf, plot, set_plot, device, save

- Procedures do stuff, but don’t need to return a value- Procedures do stuff, but don’t need to return a value

• Exact syntax for particular Procedures or Functions can be Exact syntax for particular Procedures or Functions can be found using the help menu, xdoc, or the doc_library procedurefound using the help menu, xdoc, or the doc_library procedure

Page 3: IDL Tutorial: Day 2

Commonly Used ProceduresCommonly Used Procedures

• print:print: for printing stuff to screen: for printing stuff to screen: IDL>print, sin(x)IDL>print, sin(x)

• plot:plot: plot variables to the current graphics device (usually plot variables to the current graphics device (usually screen):screen):

IDL>plot, x, y,[title=‘blah’,…IDL>plot, x, y,[title=‘blah’,…keywordskeywords]]• set_plot:set_plot: tells idl where to send plots to. Screen tells idl where to send plots to. Screen

(Xdisplay) is default(Xdisplay) is default- - IDL>set_plot,’x’IDL>set_plot,’x’ ;sends output to the x display;sends output to the x display- - IDL>set_plot, ‘ps’IDL>set_plot, ‘ps’ ;; sends the output to a sends the output to a

; postscript file with default ; postscript file with default ; name idl.ps. ; name idl.ps.

• device:device: changes properties of the device currently changes properties of the device currently enabled by set_plot. See help menu. enabled by set_plot. See help menu.

• Read ps files with ghostview (in Unix, not in IDL)Read ps files with ghostview (in Unix, not in IDL)unix_prompt > unix_prompt > gv idl.psgv idl.ps

Page 4: IDL Tutorial: Day 2

Save and RestoreSave and Restore

• Programs are saved by saving the text file Programs are saved by saving the text file you write them inyou write them in

• SaveSave, the procedure, allows you to save , the procedure, allows you to save variables to a filevariables to a fileIDL>save, variable1, variable1, file=‘file.sav’IDL>save, variable1, variable1, file=‘file.sav’

• Restore allows you to restore saved variables Restore allows you to restore saved variables to a sessionto a session

IDL>restore, ‘file.sav’,/verboseIDL>restore, ‘file.sav’,/verbose

Page 5: IDL Tutorial: Day 2

Commonly Used FunctionsCommonly Used Functions• Many math operators are functionsMany math operators are functions

IDL> y=sin(x) IDL> y=cos(x) IDL>y=exp(x)IDL> y=sin(x) IDL> y=cos(x) IDL>y=exp(x)• Where Where allows you to pick out the index of an allows you to pick out the index of an

array that meets some criterionarray that meets some criterion

IDL> index=where(new_array eq 15)IDL> index=where(new_array eq 15)(more on boolean operators later)(more on boolean operators later)

• Replicate makes copies of variables, arrays, Replicate makes copies of variables, arrays, structures, etc...structures, etc...IDL> stars=replicate(stars,15)IDL> stars=replicate(stars,15)

• dialog_pickfile allows you to graphically pick dialog_pickfile allows you to graphically pick out file namesout file namesIDL> file_name=dialog_pickfile()IDL> file_name=dialog_pickfile()

Page 6: IDL Tutorial: Day 2

Macros (Scripts)Macros (Scripts)• Simplest form of ProgramSimplest form of ProgramIn the editor window write commands just like you In the editor window write commands just like you

would at command promptwould at command prompt

• At the end typeAt the end type END END so IDL knows when program is so IDL knows when program is overover

• Save the program with a unique name (i.e. save the Save the program with a unique name (i.e. save the text file)text file)

• At the command prompt type either: At the command prompt type either: IDL> .run program_nameIDL> .run program_nameIDL> .rnew program_nameIDL> .rnew program_name

Shortcoming: no loops allowed in macros.Shortcoming: no loops allowed in macros.

Page 7: IDL Tutorial: Day 2

““Homework”Homework”

• Make a macro in which you plot, on a good scale, with Make a macro in which you plot, on a good scale, with labels, y=elabels, y=exx. Also, have it print to the screen, “You did it . Also, have it print to the screen, “You did it right!” Make a post script copy of your plot.right!” Make a post script copy of your plot.

• Make a file using emacs (or your favorite editor) which Make a file using emacs (or your favorite editor) which contains a row for each student in this class and a contains a row for each student in this class and a column for each of the following: distance (in miles) of column for each of the following: distance (in miles) of the student’s home from Bozeman, year in college, the student’s home from Bozeman, year in college, favorite color (in Angstroms), number of books read in favorite color (in Angstroms), number of books read in the last year, number of trails hiked in the last year, and the last year, number of trails hiked in the last year, and number of foods disliked . Use ascii_template and number of foods disliked . Use ascii_template and read_ascii to make a structure containing your numbers. read_ascii to make a structure containing your numbers. Using the plot procedure, find out if there are any Using the plot procedure, find out if there are any correlations among yourselves!correlations among yourselves!