1 / 26

MET 50

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.

eavan
Download Presentation

MET 50

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MET 50 Repetitive EXECUTION

  2. MET 50 fabulous “do loops”

  3. 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.

  4. 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.

  5. The “IF” statement How can we do these repetitive computations (e.g., a thousand times) in Fortran? • Cut and paste the code a thousand times? • Nah! • Or: Invent Fortran code structure to loop thru our lines of calculation code a thousand times? • Epic idea! • This is the DO LOOP

  6. 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

  7. The “IF” statement meaning… DO NUM = 1, 4 (insert code you’re here, e.g., calculations) END DO means: • Set NUM=1; do the calculations; “go back to start of loop” • Set NUM=2; do the calculations; “go back to start of loop” • Set NUM=3; do the calculations; “go back to start of loop” • 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.

  8. 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

  9. 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

  10. 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

  11. The “IF” statement RULES: DO NUM = start value, end value, step • “start value”“end value”“step” may NOT be modified once you are in the loop • It is OK to jump OUT of a LOOP (see below) • Cannot jump INTO a loop

  12. The “IF” statement RULES: • 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) ENDDO ENDDO ENDDO

  13. 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 ENDDO ENDDO

  14. The “IF” statement Which is executed inside out: DO “from west coast to east coast” – set at 1st value DO “from Mexican border to Canadian border” – set at 1st value DO “from ground level to tropopause” – do for ALL values (fancy meteorological calculations) ENDDO ENDDO ENDDO

  15. The “IF” statement Which is executed inside out: DO “from west coast to east coast” – set at 1st value DO “from Mexican border to Canadian border” – rest of values DO “from ground level to tropopause” – again do for ALL values (fancy meteorological calculations) ENDDO ENDDO ENDDO

  16. The “IF” statement Which is executed inside out: DO “from west coast to east coast” – do for rest of values DO “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 ENDDO ENDDO

  17. The “IF” statement DO I=1,1000 I=1 DO J=1, 2000 J=1 DO K=1,50 K=1 TO 50 (fancy meteorological calculations) ENDDO ENDDO ENDDO Then… DO I=1,1000 I=1 DO J=1, 2000 J=2 DO K=1,50 K=1 TO 50 (fancy meteorological calculations) ENDDO ENDDO ENDDO Etc.

  18. 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

  19. 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

  20. The “IF” statement Example: IMPLICIT … REAL … ! DO READ*, input some MET data IF (we’re at the end of the data) EXIT ! (calculations e.g., averaging, finding max values etc.) END DO (next set of statements)

  21. 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)

  22. The “IF” statement How could we tell if we are “at the end”, e.g., at the end of reading in data? • We can use an “IOSTAT” structure – see next chapter . • We can build something into the data.

  23. The “IF” statement Example: data could look like this… Flag Temp Pressure Humidity 1 22.4 1010.3 77. 1 18.7 1003.8 68. 1 15.1 999.0 51. 0 We can then code: INTEGER :: FLAG REAL :: TEMP, PRESS, HUM DO READ*, FLAG, TEMP, PRESS, HUM !might barf @ end line IF (FLAG == 0) EXIT (calculations) ENDDO PRINT*

  24. 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!

  25. 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-south END DO west-east “west-east” etc. are names! Makes it easier to find the end of a loop!!

  26. The “IF” statement DO-LOOPS IN FORTRAN 77: Next week.

More Related