Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays. Review of last lecture Overview of computer languages Overview of IDL Strength of IDL: (1) Interactive/compiled modes; (2) Array-oriented; (3)

Similar presentations


Presentation on theme: "Arrays. Review of last lecture Overview of computer languages Overview of IDL Strength of IDL: (1) Interactive/compiled modes; (2) Array-oriented; (3)"— Presentation transcript:

1 Arrays

2 Review of last lecture Overview of computer languages Overview of IDL Strength of IDL: (1) Interactive/compiled modes; (2) Array-oriented; (3) Variables easily redefined; (4) Many built-in subroutines Open, edit, and run IDL Variables and data types Operators Simple data I/O

3 Structure of a computer program 1.Define variables, arrays, constants 2.Read in data 3.Pre-processing: quality control, filling missing data, interpolation, re-gridding 4.Data analysis or other calculation 5.Plot the results 6.Output the results

4 Arrays: Introduction An array is a systematic arrangement of similar objects, usually in rows, columns, layers, and/or time series In geosciences, we often use arrays to store and process datasets Most high-level computer languages (e.g. IDL) are array-oriented. Array syntax is more compact and runs faster than a corresponding loop construct (i.e. handle a whole array by one sentence rather than handling each element one by one).

5 Dimensions of arrays 1D: d(i)2D: d(i, j)

6 Dimensions of arrays 3D: d(i, j, k)4D: d(i, j, k, l) t

7 Most datasets in Geosciences can be stored in a 4-D array D(i, j, k, l) Longitude Latitude Height Time

8 Creating arrays Functions for creating arrays zeroed arrays: x=intarr(n), x=fltarr(n) index arrays: x=indgen(n), x=findgen(n) Note in IDL, index starts from 0, not 1 Use index array to create an evenly-spaced array: n=10 x0=5 dx=10 x=x0+findgen(n)*dx

9 Extract a sub-array using index Scalar index: print, x[6] Index range: print, x[0:3] All indices: print, x[*] All indices from a specific index to the last index: print, x[5:*] Indices given by a variable expression: I=3 print, x[I-1:I+1]

10 Initialize an array or sub-array with certain values All elements: x[*] = -9999.0 One element: x[0] = 3.0 Subset range: x[5:*] = 2.0

11 Array properties moment(array1) returns another array2, with the first element being the mean, second being the variance, third being the skew, … another useful function: median

12 Array reordering Reverse order of array elements (reverse) d=indgen(10) print, d d2=reverse(d) print, d2 Shifting array elements (shift) Syntax: shift(array, shift_value) d=indgen(5) print, d d2=shift(d, 1) print, d2 d3=shift(d, -2) print, d3 Sorting array elements index=sort(d3) print, index print, d3(index)

13 Array calculation All the arithmetical operators can be directly applied to an array: +, -, *, /, ^ x=findgen(10) print, x y=x*2 print, y y=x^2.0 print, y y=2*x^2-5*x+3 print, y

14 Line plots 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

15 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

16 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’]

17 Creating muli-dimensional arrays For multi-dimensional arrays, zeroed arrays and index arrays can be created using similar functions as those used for 1-D arrays d = intarr(3, 4) d = findgen(3, 4) Multi-dimensional arrays in IDL are stored in column-major format (same as Fortran), which means that consecutive array elements are stored sequentially in memory, with the first array dimension (the column) varying the fastest. a[0,0], a[1,0], a[2,0], a[0,1], a[1,1], a[2,1]

18 Extract a sub-array using index Multi-dimensional One element: print, d(2, 3) All elements in a row: print, d[*,0] All elements in a column: print, d[0,*] Index range over two dimensions: print, d[0:3,1:2]

19 Array properties moment(array1) returns another array2, with the first element being the mean, second being the variance, third being the skew, … another useful function: median

20 Array reordering Changing array dimensions (reform) automatically remove dimensions with a size of 1 d=findgen(3,5) help, d d2=d(0,*) help, d2 d3=reform(d2) help, arr3

21 Contour Syntax: contour, d, x, y x(nx) x ordinate y(ny) y ordinate d(nx, ny) data array contour, d (the data points are plotted against corresponding array indices)

22 Contour customization Keywords

23 Example nlon=36 lon=findgen(nlon)*10 nlat=19 lat=findgen(nlat)*10-90 d=findgen(nlon,nlat) Contour, d, lon, lat, levels=[100,200,300,400,500], c_labels=[1,1,1,1,1], charsize=2.0, $ xtitle=‘Longitude’, xrange=[0,360], $ Ytitle=‘Latitude’, yrange=[-90,90] end

24 Summary Creating arrays Extract a sub-array using index Initialize an array Array properties: n_elements, size, max, min, moment, total Array reordering: reform, reverse, rotate, transpose, shift, sort, unique Simple line plot and contour plot

25 In-class assignment II Assume x=[1, 3, 5, 6, 7], plot y=x 2. Calculate the mean, maximum, minimum, variance and standard deviation of y. Shift x to the left by 2. Shift x to the right by 3. Assume x=findgen(10), y1=6x-x 2 +3, y2=5x-x 2 +3. Plot y1 vs x. Overplot y2 vs x with different linestyle. Sort y1 and y2. Assume nlon=72, nlat=37, d=findgen(nlon, nlat)/4.0. Plot a contour map of d versus longitude and latitude.


Download ppt "Arrays. Review of last lecture Overview of computer languages Overview of IDL Strength of IDL: (1) Interactive/compiled modes; (2) Array-oriented; (3)"

Similar presentations


Ads by Google