Earth & Environmental Computing · Week 03 · Course session (mini-lecture, then Lab 2 Part B) · 18 slides
Download HTML
Earth & Environmental Computing | Computational Methods for Earth & Environmental Science | 16-Week Course

Conditionals, Loops, and Environmental Events

An event is a threshold decision a scientist must defend. Today the code learns to decide.

if / elif / else · for loops · Counters · Thresholds
Week 03·Course session (mini-lecture, then Lab 2 Part B)·Open Course Materials | Adapt for your classroom·Week 3 | Course session | 20-minute mini-lecture, then Lab 2 Part B
Learning Objectives

By the end of this mini-lecture, you will be able to...

  • Write if/elif/else chains and predict which branch fires
  • Maintain counters across loop iterations
  • Explain why an environmental event is a threshold choice
Contents

Today's Mini-Lecture

01Branches: one decision
03Thresholds as science
02Loops that remember
Recap

Last Week in Sixty Seconds

  • Names carry units: temp_c, elevation_ft
  • Lists index by position; dictionaries label by name
  • f"{x:.1f}" prints a value at the precision you actually have
  • Today: those values become decisions about events
Data

Today's Datasets

FileSourceCoverageUsed for
iowa_city_weather_daily.csvIowa Environmental Mesonet, station IOWDaily, 2019–2025, 2,549 rowsWet, hot and cold day labels; dry spells
iowa_streamflow_daily.csvUSGS NWIS, site 05454500, Iowa River at Iowa CityDaily, 2015–2025, 4,018 rowsCounting high-flow days by month
01

Branches: One Decision

An if/elif/else chain runs exactly one branch, and the order decides which.

01 · Branches

One Decision, Three Branches

Python

rain_mm = 12.0

if rain_mm >= 10:
    label = "wet"
elif rain_mm > 0:
    label = "damp"
else:
    label = "dry"

print(label)     # wet
  • Exactly one branch runs
  • Change rain_mm, watch the branch change
01 · Branches

Branch Order Decides the Label

Python

# A day with 15 mm rain AND a 33 deg C maximum:
if row["precipitation_mm"] >= 10:
    label = "wet"            # <- evaluation stops here
elif row["temp_max_c"] >= 32:
    label = "hot"            # never reached for this day
else:
    label = "ordinary"
  • elif chains stop at the first true branch
  • Wet-before-hot is a decision; the lab asks you to defend it
02

Loops That Remember

A counter carries state from one iteration to the next.

02 · Loops that remember

Counters Remember Across Iterations

Python

longest = 0
current = 0
for precip in precip_values:
    if precip < 1.0:
        current = current + 1
        longest = max(longest, current)
    else:
        current = 0     # forget this line and see the next slide
  • current tracks the run in progress; longest keeps the record
  • The reset in the else branch is where the logic lives
Pause and think

?

Without the reset, the code reports a 900-day dry spell in a 7-year record of an Iowa town. What single check exposes the answer as absurd?

Plausibility: can a dry spell exceed the record itself, or an Iowa summer?
Wrong answers that look like answers are the dangerous kind
02 · Loops that remember

Two Implementations, One Answer

Loop version

for each row: if hot and humid: count = count + 1

Vectorized version

((temp_max >= 30) & (humidity >= 70)).sum()

The counts must match. Agreement between independent implementations is the lab's built-in test.

03

Thresholds Are Science

The number you pick is a scientific claim you have to defend.

03 · Thresholds are science

Thresholds Are Choices

Farmer

5 mm wets the topsoil: a useful rain.

Flood manager

25 mm in a day starts mattering downstream.

Climatologist

1 mm separates a wet day from a dry one in many indices.

Every event count in this lab depends on a threshold somebody chose.

03 · This week's lab

Lab 2, Part B: Event Detection

Today

  • Label every day: wet, hot, cold, ordinary
  • Count high-flow days by month
  • Find the longest dry spell

Studio session

  • Muggy-day agreement check
  • Season function and wet-day table
  • Freeze-thaw months, threshold defense

Part B continues the Lab 2 notebook you started in Week 2. Restart the kernel and run from the top.

Bridge

Connection to Next Week

This weekNext weekBridge idea
Decisions inside loopsFunctions and debuggingPackage the decision so it can be named, reused, and tested
Summary

Key Takeaways

  • One branch fires; order decides which
  • Counters need a reset path
  • Check plausibility, not just execution
  • Defend your thresholds with a reason
Discussion · Next week: functions and debugging

?

Name a threshold from your own field and the reason behind the number. Who chose it, and what would change if it moved?

A threshold with no stated reason is a guess wearing a number
In the lab you write one sentence defending each of yours