control statements i: loop. control statements used to specify the order in which computations will...

18
Control Statements I: Loop

Upload: abner-merritt

Post on 14-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Control Statements I: Loop

Page 2: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Control Statements

• Used to specify the order in which computations will be carried out

• Three types

Loop: for, while, repeat

Branch: if, case, switch

Exit or jump: return, break, continue, goto

Page 3: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Loop I: For … do …• The for statement executes statement(s) for a

specific number of times by incrementing a loop index variable from a start value to an end value

• Syntax:

for i = v1, v2, inc do begin

statement(s)

endfor

• Simplified version

for i = v1, v2, inc do statement

for i = v1, v2 do statement

Page 4: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Example: Calculate the sum of an array

d=indgen(100)

sum=0

for i = 0, 99 do begin

sum=sum+d(i)

endfor

print, sum

print, total(d)

Page 5: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Example: 3D array (Loop within loop)

ni = 3nj = 5nk = 8d=findgen(ni,nj,nk)

for i =0,ni-1 do begin for j =0,nj-1 do begin for k=0,nk-1 do begin sum=sum+d(i,j,k) endfor endforendforprint, sumprint, total(d)

Page 6: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Loop II: While … do …• The while statement executes statement(s) while

a specified condition is true. while condition do statement while condition do begin statement(s) endwhile

Example: sum=0 i=1 while(i le 100) do begin sum=sum+i i=i+1 endwhile

Page 7: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Loop III: Repeat … until …• The repeat statement executes statement(s) until

a specified condition is true. repeat statement until condition repeat begin statement(s) endrep until condition

Example: sum=0 i=1 repeat begin sum=sum+i i=i+1 endrep until(i gt 100)

Page 8: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Open a file• Two types of files: Formatted: ascii format Unformatted: binary format

• Open a file for writing: openw, lun, filename (lun is the logical unit number, could be set as 1-99) openw, 1, ‘test.dat’

• Open a file for reading only: openr, lun, filename • Check for end-of-file: eof(lun) (=1, end; =0 not end)

Page 9: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Close a file

• Close a file: Please remember to close a file after finishing reading/writing

close, lun

Page 10: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Writing to ascii files

• Syntax:

printf, lun, arg1, arg2, …, argn,

printf, lun, arg1, arg2, …, argn, format=‘(format code)’

• Example

n=10

d=findgen(n)*2.0

openw, 1, ‘t.dat’

printf, 1, ‘Data’

for i=0,n-1 do begin

printf, 1, i, d(i)

endfor

close, 1

end

Page 11: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Format code

• Format codes (iN, fN.M, eN.M, aN)

print, !pi, format=‘(i1)’

print, !pi, format=‘(f5.2)’

print, !pi, format=‘(e6.2)’

print, string(!pi, format=‘(a5)’)

• Notes on format codes:

1) Always used from left to right

2) codes and values must match

Page 12: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Reading ascii files• Syntax:

readf, lun, arg1, arg2, …, argn

readf, lun, arg1, arg2, …, argn, format=‘(format code)’

Page 13: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Example: World population

n=2015-1950+1 yr=fltarr(n) d=fltarr(n)

yr1=0.0 d1=0.0 openr, 1, ‘data_population’ c=‘ ’ readf, 1, c for i=0, n-1 do begin readf, 1, yr1, d1 yr(i)=yr1 d(i)=d1 endfor close, 1 plot, yr, d, xtitle=‘Year’, ytitle=‘Population’

end

Page 14: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Example: World GDP n=2014-1960+1 yr=fltarr(n) d=fltarr(n)

yr1=0.0 d1=0.0 openr, 1, ‘data_GDP’ c=‘ ’ readf, 1, c for i=0, n-1 do begin readf, 1, yr1, d1, format=‘(8x,i4,3x,f6.2) yr(i)=yr1 d(i)=d1 endfor close, 1 plot, yr, d, xtitle=‘Year’, ytitle=‘GDP’

end

Page 15: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Example: Global mean temperature nyr=2013-1948+1 n=nyr*12 t=1948+findgen(n)/12.0 d=fltarr(n)

yr1=0.0 d1=fltarr(12) openr, 1, ‘data_temp’ c=‘ ’ readf, 1, c for i=0, nyr-1 do begin readf, 1, yr1, d1 i1=i*12 i2=i1+11 d(i1:i2)=d1 endfor close, 1 plot, t, d, xtitle=‘Time’, ytitle=‘Global Temperature’ end

Page 16: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

Check if you read the data correctly

• Print the value of one variable• Print the maximum, minimum, mean, median

values• Plot the time series• Plot spatial distribution along a longitude or

latitude or height• Plot horizontal distribution using contour map

Page 17: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

More about plots

• Plot multiple panels on one page: !p.multi=[0, ncolumn, nrow] where ncolumn is number of columns,

and nrow is number of rows

Page 18: Control Statements I: Loop. Control Statements Used to specify the order in which computations will be carried out Three types Loop: for, while, repeat

In-class assignment III1. Read and plot the population history of Columbus, OH http://www.biggestuscities.com/city/columbus-ohio and the population history of 5 other cities of your interest. 2. Please go to http://www.esrl.noaa.gov/psd/data/climateindices/list/

Read and plot any four of the climate indices (e.g. the last four: Global Mean Lan/Ocean Temperature, Solar Flux, Northeast Brazil Rainfall Anomaly, SW Monsoon Region rainfall)

3. Please go to University of Wyoming sounding data site:

http://weather.uwyo.edu/upperair/sounding.html

Click on one station of your interest. Read and plot the profile of temperature (temp) versus height for the sounding