for and calculate the meanYour 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.
In order to fulfill the required task,
for loop summing the raw SST values stored in sst 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]
Out[1]:
In [2]:
## Your solution goes here:
whileOkay, 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.
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,
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 observedprint 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: