Functions and a Bit about Procedures or something more useful - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Functions and a Bit about Procedures or something more useful

Description:

Distinguishable by the parentheses following function name with argument(s) enclosed ... Common Functions. Math functions you're used to seeing: ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 11
Provided by: solarPhys
Category:

less

Transcript and Presenter's Notes

Title: Functions and a Bit about Procedures or something more useful


1
Functions and a Bit about Procedures(or
something more useful)
IDL Tutorial Day 2 Nick Hill hillnich_at_grinnell.ed
u
2
Functions
  • They perform calculations and assign the result
    of the calculation to a variable
  • IDLgt x function_name(y)
  • Distinguishable by the parentheses following
    function name with argument(s) enclosed
  • IDL comes with several, several functions, and
    they are used all the time in procedures/programs
  • Examples sin(x), cos(x), exp(x), min(x),
    max(x), etc.

3
Common Functions
  • Math functions youre used to seeing
  • IDLgt y cos(x) IDLgt y exp(x) IDLgt y tan(x)
  • Sizing arrays for programs
  • IDLgt x intarr(15) IDLgt x fltarr(13, 12)
  • Converting variables to different types
  • IDLgt x fix(15.2) converts the argument 15.2
    to an integer such that x 15
  • Information about array indices
  • IDLgt x where (y eq min(y)) more about Boolean
    later, but what is going on here?

4
Writing Functions
  • Sometimes youll want to write a function for a
    series of calculations you may use multiple times
    in a program
  • Youll treat it just like a program (save as .pro
    text file), but with slightly different syntax
  • FUNCTION name_of_function, input
  • commands
  • RETURN, output
  • END
  • To use the function, compile it, and call it as
    you would any function
  • IDLgt .compile name_of_function.pro
  • IDLgt x name_of_function (input)

5
Procedures/Programs
  • Procedures can perform calculations, create
    plots, etc, and are at the core of what youll be
    doing
  • Arguments are input, but does not have to output
    values. You HAVE TO TELL IT you want output
  • IDLgt prog_name, arg1, arg2, output1, output2
  • To write a program, open a text file, and use the
    syntax
  • PRO prog_name, input, output
  • commands
  • END
  • You must then save the text file with ending
    .pro
  • my_program.pro

6
Programs contd
  • Any program you write must be compiled and syntax
    error free before it can be used
  • IDLgt .compile prog_name.pro
  • Programs are written in text editors (ie emacs,
    nedit), and must merely have that .pro ending
  • IDL has a lot of programs for you to use. Try
    looking for one to do what you want before trying
    to write a new one
  • Well talk more about writing programs in the
    next 2 days

7
Common Programs
  • print it prints variable values to a screen, or
    to a text file if specified
  • IDLgt print, y
  • plot makes plots given values
  • IDLgt plot, x, sin(x)
  • set_plot tells IDL where to output plots
  • IDLgt set_plot, ps sends plot to a
    postscript file. More
  • on this in the exercises
  • x2jpeg takes whatever image is being displayed
    in a window (ie a plot) and saves it as a jpeg
    in your current working directory (really useful
    for your log)
  • IDLgt x2jpeg, file_name.jpg

8
Save Files and Restoration
  • You often want to save the results you obtain
    from programs, and this is easily done with a
    save file
  • Saves files simply contain variables you want to
    keep and end in .sav
  • To create one
  • IDLgt save, x, y, z, filewhatever.sav
  • To restore the save file, so you can access
    those variables in IDL again
  • IDLgt restore, whatever.sav, /ver /ver just
    allows you see what variables are being
    restored

9
Batch Files/Macros
  • These are text files with a series of commands to
    be executed in a sequential order
  • Useful when you have a large sequence of programs
    that need to be run, but require a lot of time to
    run
  • Only requires that at end of text file, you place
    end
  • Executed in IDL like so
  • IDLgt .r batch_file

10
Practice
Write a Comment
User Comments (0)
About PowerShow.com