Arrays. Review of last lecture Overview of computer languages Overview of IDL Strength of IDL: (1)...
-
Upload
abigayle-malone -
Category
Documents
-
view
213 -
download
0
Transcript of Arrays. Review of last lecture Overview of computer languages Overview of IDL Strength of IDL: (1)...
Arrays
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
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
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).
Dimensions of arrays
1D: d(i) 2D: d(i, j)
Dimensions of arrays
3D: d(i, j, k) 4D: d(i, j, k, l)
t
Most datasets in Geosciences can be stored in a 4-D array D(i,
j, k, l)
LongitudeLatitudeHeightTime
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
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]
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
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
Array reordering• Reverse order of array elements (reverse)
d=indgen(10)print, dd2=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 elementsindex=sort(d3)print, indexprint, d3(index)
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
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
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
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’]
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]
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]
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
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
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)
Contour customization• Keywords
Examplenlon=36lon=findgen(nlon)*10nlat=19lat=findgen(nlat)*10-90d=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
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
In-class assignment II
• Assume x=[1, 3, 5, 6, 7], plot y=x2. 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-x2+3, y2=5x-x2+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.