met 50

26
REPETITIVE EXECUTION MET 50

Upload: eavan

Post on 24-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

MET 50. Repetitive EXECUTION. MET 50. fabulous “do loops”. The “IF” statement. Often in Meteorology we need to do calculations many, many times. Example: Read in multiple pieces of data from multiple locations. Hourly temperatures, wind speeds/directions, pressures. The “IF” statement. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MET 50

REPETITIVE EXECUTION

MET 50

Page 2: MET 50

FABULOUS

“DO LOOPS”

MET 50

Page 3: MET 50

The “IF” statement

Often in Meteorology we need to do calculations many, many times.

Example: Read in multiple pieces of data from multiple locations.

Hourly temperatures, wind speeds/directions, pressures.

3

Page 4: MET 50

The “IF” statement

Example: Compute quantities at multiple locations around the globe.

Compute pressure tendency over the last 3 hours.

At the surface in New York. At 1 km elevation over New York. At 2 km elevation over New York. At 3 km elevation over New York. Etc.

4

Page 5: MET 50

The “IF” statement

How can we do these repetitive computations (e.g., a thousand times) in Fortran?

A. Cut and paste the code a thousand times? Nah!

B. Or: Invent Fortran code structure to loop thru our lines of calculation code a thousand times?

Epic idea! This is the DO LOOP

5

Page 6: MET 50

The “IF” statement

Two ways of using the DO LOOP … (1) = COUNTER-CONTROLLED:

DO NUM = 1, 1000 (insert your code here, e.g., calculations) END DO

“1000” is the # times the code below is performed

6

Page 7: MET 50

The “IF” statement

meaning… DO NUM = 1, 4 (insert code you’re here, e.g., calculations) END DO

means:1. Set NUM=1; do the calculations; “go back to start of loop”2. Set NUM=2; do the calculations; “go back to start of loop”3. Set NUM=3; do the calculations; “go back to start of loop”4. Set NUM=4; do the calculations; DO NOT “go back to start of loop”

Once NUM has reached the maximum value (“4” in this example), finish the calculations and EXIT THE LOOP.

7

Page 8: MET 50

The “IF” statement

example… DO NUM = 1, 4 PRINT*, ‘value is’,NUM END DO

Would produce:(since NUM =1) value is 1(since NUM =2) value is 2(since NUM =3) value is 3(since NUM =4) value is 4

8

Page 9: MET 50

The “IF” statement

GENERAL FORM:

DO NUM = start value, end value, step (insert code you’re here, e.g., calculations) END DO

“start value” MUST be integer“end value” MUST be integer“step” MUST be integer

9

Page 10: MET 50

The “IF” statement

DO NUM = start value, end value, step (insert code you’re here, e.g., calculations) END DO

OK to not write “step” compiler will assume “step = 1” if nothing is

written

DO NUM = 1,10 implies DO NUM = 1,10,1

10

Page 11: MET 50

The “IF” statement

RULES:

DO NUM = start value, end value, step

1) “start value”“end value”“step” may NOT be modified once you are in the loop

2) It is OK to jump OUT of a LOOP (see below)3) Cannot jump INTO a loop

11

Page 12: MET 50

The “IF” statement

RULES:4) It is OK to have NESTED DO loops

It is common in Meteorology to have NESTED DO loops

DO “from west coast to east coast”DO “from Mexican border to Canadian border ”DO “from ground level to tropopause (about 10 km elevation)”(fancy meteorological calculations)ENDDOENDDOENDDO

12

Page 13: MET 50

The “IF” statement

Looks dense…instead write like this:

DO “from west coast to east coast”DO “from Mexican border to Canadian border ”

DO “from ground level to tropopause”(fancy meteorological calculations)ENDDO

ENDDOENDDO

13

Page 14: MET 50

The “IF” statement

Which is executed inside out:

DO “from west coast to east coast” – set at 1st valueDO “from Mexican border to Canadian border” – set at 1st

valueDO “from ground level to tropopause” – do for ALL

values(fancy meteorological calculations)ENDDO

ENDDOENDDO

14

Page 15: MET 50

The “IF” statement

Which is executed inside out:

DO “from west coast to east coast” – set at 1st valueDO “from Mexican border to Canadian border” – rest of

valuesDO “from ground level to tropopause” – again do for

ALL values(fancy meteorological calculations)ENDDO

ENDDOENDDO

15

Page 16: MET 50

The “IF” statement

Which is executed inside out:

DO “from west coast to east coast” – do for rest of valuesDO “from Mexican border to Canadian border” – again do for

ALL valuesDO “from ground level to tropopause” – again do for

ALL values(fancy meteorological calculations)ENDDO

ENDDOENDDO

16

Page 17: MET 50

The “IF” statement

DO I=1,1000 I=1DO J=1, 2000 J=1DO K=1,50 K=1 TO 50(fancy meteorological calculations)ENDDOENDDOENDDO

Then…DO I=1,1000 I=1DO J=1, 2000 J=2DO K=1,50 K=1 TO 50 (fancy meteorological calculations)ENDDOENDDOENDDO

Etc.

17

Page 18: MET 50

The “IF” statement

Second way of using the DO LOOP … (2) = CONDITION-CONTROLLED or DO-EXIT

structure:

DO (insert code you’re here, e.g.,

calculations)IF (something) EXIT(insert code you’re here, e.g., calculations)

END DO

18

Page 19: MET 50

The “IF” statement

3 versions - general form on last slide:(B) DO

IF (something) EXIT ! Which means jump out of loop!(insert code you’re here, e.g., calculations)

END DO(C) DO

(insert code you’re here, e.g., calculations)IF (something) EXIT ! Which means jump out of loop!

END DO

19

Page 20: MET 50

The “IF” statement

Example:

IMPLICIT …REAL …!DO READ*, input some MET dataIF (we’re at the end of the data) EXIT ! (calculations e.g., averaging, finding max values etc.)

END DO (next set of statements)

20

Page 21: MET 50

The “IF” statement

The “counter-controlled” structure is useful when we DO know how much data we will get…it is given by the value of “end value” in the statement

DO NUM = start value, end value, step

The “condition-controlled” structure is for when we do NOT know how much data we will get.

(radiosonde data example)

21

Page 22: MET 50

The “IF” statement

How could we tell if we are “at the end”, e.g., at the end of reading in data?

1) We can use an “IOSTAT” structure – see next chapter .

2) We can build something into the data.

22

Page 23: MET 50

The “IF” statement

Example: data could look like this…

Flag Temp Pressure Humidity1 22.4 1010.3 77.1 18.7 1003.8 68.1 15.1 999.0 51.0

We can then code:

INTEGER :: FLAGREAL :: TEMP, PRESS, HUMDOREAD*, FLAG, TEMP, PRESS, HUM !might barf @ end lineIF (FLAG == 0) EXIT(calculations)ENDDOPRINT*

23

Page 24: MET 50

The “IF” statement

THE CYCLE COMMAND:

DO (code)

IF (something#1) EXIT ! jump out of loop(code)IF (something#2) CYCLE ! go back to start & continueEND DO

Example: if you read in a temperature and T < 0, you would ignore that value – but go on and read in the next –NOT stop all together!

24

Page 25: MET 50

The “IF” statement

THE NAMED DO-LOOP:

west-east: DO “from west coast to east coast”north-south: DO “from Mexican border to Canadian border ”up-down: DO “from ground level to tropopause”(fancy meteorological calculations)END DO up-downEND DO north-southEND DO west-east

“west-east” etc. are names!

Makes it easier to find the end of a loop!!

25

Page 26: MET 50

The “IF” statement

DO-LOOPS IN FORTRAN 77:

Next week.

26