arrays !!! met 50. arrays arrays allow us to store large amounts of data. often in meteorology and...
Post on 21-Dec-2015
219 views
TRANSCRIPT
ARRAYS !!!
MET 50
MET 50, FALL 2011, CHAPTER 8 PART 1
2
Arrays
ARRAYS allow us to store large amounts of data.
Often in Meteorology and Climate Science we need to:
Read in large amounts of data Perform calculations, statistical analyses etc.
using that data. Save the results (often large numbers of
results)
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
3
Arrays
Also we often need to:
Perform calculations relative to a grid of imaginary points superimposed on a region:
http://en.wikipedia.org/wiki/Numerical_Weather_Prediction
http://lightning.met.sjsu.edu/%7Eclifford/www/wrf/wrf.html
ARRAYS allow us to store and work with this gridded information
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
4
Arrays
Finally – many problems have spatial dimensions!Examples…
Information about elevation as we move west to east (1-D problem)
Information about surface pressure as we move west to east AND south to north (2-D problem)
Information about ozone concentration as we move west to east, south to north AND in the vertical (3-D problem)
ARRAYS allow us to store and work with this information
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
5
Arrays
Three types of arrays are used extensively in MCS:
1. 1-dimensional arrays2. 2-dimensional arrays3. 3-dimensional arrays
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
6
Arrays
1. 1-dimensional arrays
Same as vectors in your math classes!
Example: We might compute and store values of the Coriolis
parameter ( f = 2 sin(latitude)) at every one degree of latitude in a1-D array (180 values!)
Faster than re-computing the value every time we need it
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
7
Arrays
2. 2-dimensional arrays
Same as matrices in your math classes!
Example: We might store and forecast values of surface
pressure at every one degree of latitude and every one degree of longitude across the planet in 2-D array (180 values north-south, 360 values west-east)
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
8
Arrays
3. 3-dimensional arrays
Example: We might store and forecast values of
temperature at every one degree of latitude and every one degree of longitude across the planet and every 1 km in elevation from the surface to 100 km in a 3-D array (180 values north-south, 360 values west-east, 100 values in the vertical)
The atmosphere is 3-dimensional!!
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
9
Arrays
4. 4-dimensional arrays?
With the 4th dimension being time?
CAN be defined
BUT very space-consuming
NOT preferred in our science
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
10
Arrays
1. 1-dimensional arrays
A 1-D array is just a string of numbers.Example…values of Coriolis parameter
At the start of code using arrays, we must set aside or declare 2 things:
Names for the arrays Space (memory) for the arrays
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
11
Arrays
We do this via the DIMENSION
statement.
Example: REAL, DIMENSION(180) :: CORIOLISSays:1. I am declaring “CORIOLIS” to be the name of
a 1-D array 2. The array is real and has 180 elements.
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
12
Arrays
Another example:
INTEGER, DIMENSION(360) :: LAT
Says:1. I am declaring “LAT” to be the name of a 1-D
array 2. The array is integer and has 360 elements.
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
13
Arrays
How to access individual array elements…
Suppose we have declared:
REAL, DIMENSION (10) :: ATEST
Then: ATEST(1) refers to the 1st element of array ATEST ATEST(2) refers to the 2nd element of array ATEST ATEST(10) refers to the 10th element of array ATEST ATEST(11) does not exist error message!!
Generally: ATEST(n) refers to the nth element of array ATEST
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
14
Arrays
Appearance inside code ??? Given: REAL, DIMENSION (100) :: TIME
We might have:
OPEN (18, FILE=‘TIMES.DAT’, STATUS=‘OLD’) DO K = 1, 100 READ (18, 80) TIME(K) ! Reads in 100 values of TIME ENDDO ! Assigns each to an array location80 FORMAT (1X, F10.4)PRINT*, TIME(1), TIME(100) ! Print start and end times
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
15
Arrays
Another example:
SUM = 0.AVG = 0. DO J = 1, 100 SUM = SUM + TIME(J) ! Adds together values from
the ENDDO ! Array TIME inside the DO loop AVG = SUM / (100.)PRINT*, SUM, AVG
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
16
Arrays
So a 1-D array is a subscripted variable.
In Fortran77, we always needed to type the subscript form.
e.g., TEMP(I) Could never write just “TEMP” inside the
code In Fortran90, we can either use the subscript
form - TEMP(I) Or just writing TEMP alone is OK!
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
17
Arrays
Suppose we wish to add two arrays, each with temperatures measurements recorded every minute: TEMP_AUG01 and TEMP_AUG02.
Fortran90
REAL, DIMENSION(1440) :: TEMP_AUG01, TEMP_AUG02REAL, DIMENSION(1440) :: SUMSUM = TEMP_AUG01 + TEMP_AUG02
This statement is legal as long as each array is of dimension (1440)
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
18
Arrays
Fortran77 version…
REAL TEMP_AUG01 (1440), TEMP_AUG02(1440) REAL SUM(1440) DO 10 JJ=1,1440 SUM(JJ) = TEMP_AUG01(JJ) + TEMP_AUG02(JJ) 10 CONTINUE
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
19
Arrays
Other things…
I) With:
REAL, DIMENSION(180) :: CORIOLIS
1. READ (20,10) CORIOLIS1. reads in all 180 values…no need to put in a DO loop 2. from UNIT # 20 3. via FORMAT statement # 10
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
20
Arrays
II) For an array with unchanging values (such as Coriolis values), we can set values as follows:
Example: latitudes every 30 degrees from pole-to-pole
INTEGER, DIMENSION(7) :: LAT
LAT = (/ -90, -60, -30, 0, 30, 60, 90 /) note format using ‘/’or:LAT(1) = -90DO JJ = 2, 7LAT(JJ) = LAT(JJ-1) + 30END DO
10/27/2011
MET 50, FALL 2011, CHAPTER 8 PART 1
21
Arrays
III) We can access a subset of an array as follows:
REAL, DIMENSION(100) :: ARRTSTREAL, DIMENSION(11) :: BSUB
BSUB = ARRTST(50:60:1)
Takes values from ARRTEST elements # 50 – 60 (11 in all) And puts them into array “BSUB”.
BSUB = ARRTST(50:60) is enough to do this!
10/27/2011