Docsity

Arrays - Mechanical Engineering - Lecture Slides, Slides of Mechanical Engineering

Partial preview of the text

Download Arrays - Mechanical Engineering - Lecture Slides and more Slides Mechanical Engineering in PDF only on Docsity!

MET 50

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)

Three types of arrays are used extensively in MCS:

  1. 1-dimensional arrays
  2. 2-dimensional arrays
  3. 3-dimensional 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
  1. 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!!
  1. 4-dimensional arrays?

With the 4 th^ dimension being time?

  • CAN be defined
  • BUT very space-consuming
  • NOT preferred in our science

We do this via the

DIMENSION

statement.

Example:

REAL, DIMENSION(180) :: CORIOLIS

Says:

  1. I am declaring “CORIOLIS” to be the name of a 1-D array
  2. The array is real and has 180 elements.

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.

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

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

Suppose we wish to add two arrays, each with temperatures measurements recorded every minute: TEMP_AUG01 and TEMP_AUG02.

Fortran

REAL, DIMENSION(1440) :: TEMP_AUG01, TEMP_AUG REAL, DIMENSION(1440) :: SUM SUM = TEMP_AUG01 + TEMP_AUG

This statement is legal as long as each array is of dimension (1440)

Fortran77 version…

REAL TEMP_AUG01 (1440), TEMP_AUG02(1440) REAL SUM(1440)

DO 10 JJ=1, SUM(JJ) = TEMP_AUG01(JJ) + TEMP_AUG02(JJ) 10 CONTINUE

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) = - DO JJ = 2, 7 LAT(JJ) = LAT(JJ-1) + 30 END DO

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!