Task W02-1.1: Sum values using for and calculate the mean

Your first task of the day should rather easily be accomplished:

Using the so-called Oceanic Niño Index (ONI), calculate the long-term mean sea surface temperature (SST) in the east-central equatorial Pacific Ocean (5S to 5N, 170W to 120W), better known as Niño-3.4 region. The fixed-width data set can be retrieved from the Climate Prediction Center of the National Oceanic and Atmospheric Administration (NOAA).

If you have newer encountered any of the Niño regions before – not to mention El Niño Southern Oscillation (ENSO) in general – now is the time! The article on the 2015 El Niño event published on The Weather Network, from which we also "borrowed" the following image, could be a good starting point.


Source: https://www.theweathernetwork.com

In order to fulfill the required task,

  • write a for loop summing the raw SST values stored in sst
  • and afterwards, calculate the long-term mean SST by dividing through the length of sst.

Here is the corresponding formula:

$$\bar{x} = \frac{\sum_{i=1}^{n} x_i}{n}.$$

To create the sum of raw SST values inside the for loop, you will likely need to initialize a numeric (float) target variable beforehand. Considering last week's theoretical input on data types, however, this should not be an issue.

(We are well aware that there are more elegant ways to calculate the mean from a sequence of values in Python. In order for you to practice writing for loops, however, it makes totally sense to stick to this suggested approach.)


In [1]:
import pandas as pd

url = "http://www.cpc.ncep.noaa.gov/data/indices/oni.ascii.txt"

# help(pd.read_fwf)
oni = pd.read_fwf(url, widths = [5, 5, 7, 7])

## Print the head() and tail() of the ONI data set to the console
print(oni.head(), "\n")
print(oni.tail(), "\n")

## This is how to access a particular column in a Pandas DataFrame and transform 
## its contents into a standard list. We'll come back to that in a follow-up session.
sst = oni['TOTAL'].tolist()
sst[:10]


  SEAS    YR  TOTAL  ANOM
0  DJF  1950  24.72 -1.53
1  JFM  1950  25.17 -1.34
2  FMA  1950  25.75 -1.16
3  MAM  1950  26.12 -1.18
4  AMJ  1950  26.32 -1.07 

    SEAS    YR  TOTAL  ANOM
807  MAM  2017  27.88  0.28
808  AMJ  2017  28.12  0.38
809  MJJ  2017  27.95  0.37
810  JJA  2017  27.42  0.15
811  JAS  2017  26.84 -0.15 

Out[1]:
[24.72, 25.17, 25.75, 26.12, 26.32, 26.31, 26.21, 25.96, 25.76, 25.63]

In [2]:
## Your solution goes here:

Task W02-1.2: Identify the first occurrence of very strong ENSO events using while

Okay, so far for the actual long-term mean SST in the Niño-3.4 region. Let us now move on to the corresponding SST anomalies, which are calculated as the 3-month running mean based on centered 30-year base periods.


Source: http://origin.cpc.ncep.noaa.gov/

As you might know, warm (El Niño) and cold (La Niña) ENSO events are traditionally classified according to SST anomalies into

Category SST anomaly threshold
weak 0.5 to 0.9
medium 1.0 to 1.4
strong 1.5 to 1.9
very strong >= 2.0

Your next task is to identify the season (oni['SEAS']) and year (oni['YR']) in which very strong warm and cold ENSO events occurred for the first time since records began. For that purpose,

  • write two while loops (one for El Niño, the other for La Niña) to identify the index at which very strong conditions (i.e. exceeding the warm or falling below the cold anomaly threshold in oni['ANOM']) were first observed
  • and subsequently, print the corresponding season and year to the console.

Since you already know from the previous task how to extract a certain column from a Pandas DataFrame and convert it to an object of type list, we will not explicitly repeat this procedure here. Once you are done, you might want to verify your results on the basis of this figure.


In [3]:
## Your solution goes here: