Download presentation
Presentation is loading. Please wait.
Published byAbner Merritt Modified over 9 years ago
1
Control Statements I: Loop
2
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
3
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
4
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)
5
Example: 3D array (Loop within loop) ni = 3 nj = 5 nk = 8 d=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 print, sum print, total(d)
6
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
7
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)
8
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)
9
Close a file Close a file: Please remember to close a file after finishing reading/writing close, lun
10
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
11
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
12
Reading ascii files Syntax: readf, lun, arg1, arg2, …, argn readf, lun, arg1, arg2, …, argn, format=‘(format code)’
13
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
14
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
15
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
16
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
17
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
18
In-class assignment III 1. 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.