210 likes | 300 Views
MET 50. ARRAYS !!!. 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).
E N D
MET 50 ARRAYS !!!
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) MET 50, FALL 2011, CHAPTER 8 PART 1
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 MET 50, FALL 2011, CHAPTER 8 PART 1
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 MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays Three types of arrays are used extensively in MCS: 1-dimensional arrays 2-dimensional arrays 3-dimensional arrays MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays 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 MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays 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) MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays 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!! MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays 4-dimensional arrays? With the 4th dimension being time? CAN be defined BUT very space-consuming NOT preferred in our science MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays 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 MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays We do this via the DIMENSION statement. Example: REAL, DIMENSION(180) :: CORIOLIS Says: I am declaring “CORIOLIS” to be the name of a 1-D array The array is real and has 180 elements. MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays Another example: INTEGER, DIMENSION(360) :: LAT Says: I am declaring “LAT” to be the name of a 1-D array The array is integer and has 360 elements. MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays How to access individual array elements… Suppose we have declared: REAL, DIMENSION (10) :: ATEST Then: ATEST(1) refers to the 1stelement of array ATEST ATEST(2) refers to the 2ndelement of array ATEST ATEST(10) refers to the 10thelement of array ATEST ATEST(11) does not exist error message!! Generally: ATEST(n) refers to the nthelement of array ATEST MET 50, FALL 2011, CHAPTER 8 PART 1
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 location 80 FORMAT (1X, F10.4) PRINT*, TIME(1), TIME(100) ! Print start and end times MET 50, FALL 2011, CHAPTER 8 PART 1
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 MET 50, FALL 2011, CHAPTER 8 PART 1
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! MET 50, FALL 2011, CHAPTER 8 PART 1
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_AUG02 REAL, DIMENSION(1440) :: SUM SUM = TEMP_AUG01 + TEMP_AUG02 This statement is legal as long as each array is of dimension (1440) MET 50, FALL 2011, CHAPTER 8 PART 1
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 MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays Other things… I) With: REAL, DIMENSION(180) :: CORIOLIS • READ (20,10) CORIOLIS • reads in all 180 values…no need to put in a DO loop • from UNIT # 20 • via FORMAT statement # 10 MET 50, FALL 2011, CHAPTER 8 PART 1
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) = -90 DO JJ = 2, 7 LAT(JJ) = LAT(JJ-1) + 30 END DO MET 50, FALL 2011, CHAPTER 8 PART 1
Arrays III) We can access a subset of an array as follows: REAL, DIMENSION(100) :: ARRTST REAL, 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! MET 50, FALL 2011, CHAPTER 8 PART 1