Post on 21-Dec-2015
REPETITIVE EXECUTION
MET 50
FABULOUS
“DO LOOPS”
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
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
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
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
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
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
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
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
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
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
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
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
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
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 values
DO “from ground level to tropopause” – again do for ALL values
(fancy meteorological calculations)ENDDO
ENDDOENDDO
16
The “IF” statement
DO I=1,1000 I=1DO J=1, 2000 J=1
DO K=1,50 K=1 TO 50(fancy meteorological calculations)ENDDO
ENDDOENDDO
Then…DO I=1,1000 I=1
DO J=1, 2000 J=2DO K=1,50 K=1 TO 50 (fancy meteorological calculations)ENDDO
ENDDOENDDO
Etc.
17
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
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
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
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
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
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
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 & continue
END 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
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-down
END DO north-southEND DO west-east
“west-east” etc. are names!
Makes it easier to find the end of a loop!!
25
The “IF” statement
DO-LOOPS IN FORTRAN 77:
Next week.
26