Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL...

17
Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax

Transcript of Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL...

Page 1: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Geog 820.03 Basic Skills in Scientific Programming

Syllabus, Introduction, Fundamentals of IDL Syntax

Page 2: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Syllabus

Page 3: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Overview of IDL• The Interactive Data Language (IDL) is an

array-oriented data analysis and visualization environment developed and marketed by Research Systems, Inc. of Boulder, Colorado (first version released in 1981)

• It is available for Windows, MacOS, UNIX (including Linux). Requires no (or little) change of code when transfer to a new platform

• Now widely used in research, educational, and commercial settings (e.g. astronomy, space, physics, earth, medical, and engineering)

Page 4: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Strength of IDL comparing with other languages (e.g. C and

Fortran)• IDL offers both interactive and compiled

programming modes (C and Fortran offer only compiled mode)

• IDL is optimized to perform array operations (C and Fortran is is optimized to perform scalar operations)

• IDL variables may be created or redefined with a new type, size, or value at any time (in C and Fortran the variable type is fixed at run-time)

• IDL includes many built-in and user-developed routines for visualization and numerical analysis (C and Fortran require external libraries)

Page 5: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Open and run IDL

• Open IDL• Simple code print, ‘This is an IDL program’ end Note: Every program should end with an “end”

statement

• Run IDL Interactive mode Compiled mode .r filename.pro

• Online help: >?

Page 6: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Variables and Data types

• String type: x=‘a’, y=‘This is a string’• To check about data type: help, variable_name

Page 7: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Type conversions

• Note: Conversion from float to integer will loose all the decimals

fix(5.001)=5

fix(5.999)=5

Page 8: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Variable names

• You can use any names other than the reserved words

Page 9: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Operators and operator precedence

• ( ) has the highest level of precedence!

• Arithmetical operators have higher level of precedence than relational operators

• Integer divided by integer returns an integer :

3/4 = 0

• Relational operators return “1” if true or “0” if false

3 le 5 returns 1

8 eq 11 returns 0

Page 10: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Simplest arrays

Integer x = [1, 2, 3, 5, 6]

Floating point y = [1.2, 3.5, 8.8, 11.3, 20.5]

String Month = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’]

Page 11: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Creating arrays• Functions for creating arrays (1-8 dimensions)

zeroed arrays: intarr( ), fltarr( ), strarr( )

index arrays: indgen( ), findgen( ), sindgen( )

Note in IDL, index starts from 0 instead of 1

• Use index array to create a evenly-spaced array

n=10

x0=5

dx=10

arr=x0+findgen(n)*dx

print, arr

Please try to create all the arrays at the beginning of your program

Page 12: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Plotting overview• Plotting procedures: Line plots: plot, oplot, plots, axis Map plots: contour 3-D plots: surface, shade_surf (Putting labels over the plot: xyouts )• When a plotting procedure is called, the default

behavior is as follows:

1. Erase the contents of the current window (a new window is created if none exits)

2. Establish appropriate data coordinates for the input data

3. Draw axes (including labels and tick marks) 4. Draw the data lines

Page 13: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Line plots — Plot • Syntax: plot, x, y plot, y (the data points are plotted against

corresponding array indices) plot, x, y, max=dmax, min=dmin (limit the

maximum and minimum data values to be plotted)

• Overplotting oplot, x, y• Scatter plots plot, x, y, psym=symbol_code

Page 14: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Line plot customization• Keywords

General: title, charsize, charthick

For data lines: linestyle, thick, psym, symsize, color,

/nodata, /noerase, /noclip

For axis: /ynozero, /xlog, /ylog,

[xyz]title, range, style, thick

[xyz]ticks, tickv, tickname, ticklen

Page 15: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Example x=findgen(10) y=x^2.0 plot, x, y, title=‘Google stock value’,

xtitle=‘Time (month)’, ytitle=‘Dollar’, charsize=2.0, $

thick=4, $ xrange=[0,9], xstyle=1, yrange=[0,85],

ystyle=1, $ xticklen=0.01,yticklen=0.01, $ xticks=9, yticks=4,

ytickv=[0,10,20,40,60], ytickname=[‘0’,’10’,’20’,’40’,’60’]

Page 16: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

Summary

• Overview of IDL• Strength of IDL: (1) Interactive/compiled

modes; (2) Array-oriented; (3) Variables easily redefined; (4) Many built-in subroutines

• Open and run IDL• Variables and data types• Operators• Line plots: basics and cosmetics

Page 17: Geog 820.03 Basic Skills in Scientific Programming Syllabus, Introduction, Fundamentals of IDL Syntax.

In-class assignment I

• Design a picture using characters on the keyboard. Write an IDL program to print it out using multiple lines of ‘print’ statement

• Assume x=3, y=4, z=8, write an IDL program to calculate z2-x2+y2.8 and print out the result

• Assume x=[1, 3, 5, 6, 7], plot (1) y=x2, (2) y=ex. Please adjust the titles and axes to make the plots look good.

• Assume x=findgen(101)/100.0*2*!pi, plot (1) y=sin(x), (2) y=cos(5x), and (3) y=5sin(3x)+2cos(9x). Please adjust the titles and axes to make the plots look good. (In IDL, !pi gives the value of )