Assignment: Xarray Fundamentals with Atmospheric Radiation Data

In this assignment, we will use Xarray to analyze top-of-atmosphere radiation data from NASA’s CERES project.

Public domain, by NASA, from Wikimedia Commons

A pre-downloaded and subsetted a portion of the CERES dataset is available here: The size of the data file is 702.53 MB. It may take a few minutes to download.

Please review the CERES FAQs before getting started.

Start by importing Numpy, Matplotlib, and Xarray. Set the default figure size to (12, 6).

Next, download the NetCDF file using pooch.

import pooch
fname = pooch.retrieve(

1) Opening data and examining metadata

1.1) Open the dataset and display its contents

2) Basic reductions, arithmetic, and plotting

2.1) Calculate the time-mean of the entire dataset

2.2) From this, make a 2D plot of the the time-mean Top of Atmosphere Longwave, Shortwave, and Incoming Solar Radiation

(Use “All-Sky” conditions)

Note the sign conventions on each variable.

2.3) Add up the three variables above and verify (visually) that they are equivalent to the TOA net flux

You have to pay attention to and think carefully about the sign conventions for each variable in order to get this to work.

3) Mean and weighted mean

3.1) Calculate the global (unweighted) mean of TOA net radiation

Since the Earth is approximately in radiative balance, the net TOA radiation should be zero. But taking the naive mean from this dataset, you should find a number far from zero. Why?

The answer is that each “pixel” or “grid point” of this dataset does not represent an equal area of Earth’s surface. So naively taking the mean, i.e. giving equal weight to each point, gives the wrong answer.

On a lat / lon grid, the relative area of each grid point is proportional to \(\cos(\lambda)\). (\(\lambda\) is latitude)

3.2) Create a weight array proportional to \(\cos(\lambda)\)

Think carefully a about radians vs. degrees

3.3) Redo your global mean TOA net radiation calculation with this weight factor

Use xarray’s weighted array reductions to compute the weighted mean.

This time around, you should have found something much closer to zero. Ask a climate scientist what the net energy imbalance of Earth due to global warming is estimated to be. Do you think our calculation is precise enough to detect this?

3.4) Now that you have a weight factor, verify that the TOA incoming solar, outgoing longwave, and outgoing shortwave approximately match up with infographic shown in the first cell of this assignment

4) Meridional Heat Transport Calculation

We can go beyond a weight factor and actually calculate the area of each pixel of the dataset, using the formula

\[ dA = R^2 \cos(\lambda) d\lambda d \varphi \]

where \(d\lambda\) and \(d\varphi\) are the spacing of the points in latitude and longitude (measured in radians). We can approximate Earth’s radius as \(R = 6,371\) km.

4.1) calculate the pixel area using this formula and create a 2D (lon, lat) DataArray for it

(Xarray’s ones_like function can help you easily create and broadcast DataArrays.) Verify that the sum of all the pixels equals the Earth’s true surface area as evaluated using the formula for the area of a sphere (yes, the Earth is not a sphere … it’s just a homework problem).

Multiplying the pixel area (m\(^2\)) from above with the radiative flux (W m\(^{-2}\)) gives you the total amount of radiation absorbed in each pixel in W.

4.2) Calculate and plot the total amount of net radiation in each 1-degree latitude band

Label with correct units

4.3) Plot the cumulatuve sum in latitude of that quantity

Label with correct units. (Hint: check out xarray’s cumsum function.)

This curve tells you how much energy must be transported meridionally by the ocean and atmosphere in order to account for the radiative imbalance at the top of the atmosphere.

You should get a curve that looks something like this:

5) Selecting and Merging Data

For the next problem, use the following approximate locations of four different cities.





74 W

40 N

Nome, Alaska

165 W

64 N

Columbo, Sri Lanka

80 E

7 N

Hobart, Tasmania

147 E

43 S

5.1) Create a Dataset for each point from the global dataset

5.2) Merge these four datasets into a new dataset with the new dimension city

Create a new dimension coordinate to hold the city name. Display the merged dataset.

5.3) Plot the incoming solar and net radiation at each city

Make two separate plots. Try to have your legend created automatically from the data.