xarray Practice#

Part 1#

xarray Data Structures#

import xarray as xr
  1. The relative filepath for the SST dataset we saw in the lesson is ../data/oisst-avhrr-v02r01.20220304.nc. Use the open_dataset() function to access the file.

sst = xr.open_dataset('../data/oisst-avhrr-v02r01.20220304.nc')
  1. Display the HTML representation of the dataset (Put the Python variable name of the dataset alone in a cell, or as the last line of a cell). Read the following information from the visual display of the dataset.

  • What is the first and last values for latitude?

  • What is the “institution” listed in the metadata?

  • What date was this data taken?

  • How many latitude values are there? How many longitude values?

# -89.875, 89.875
# NOAA/National Centers for Environmental Information
# March 4th 2022
# 720 latitudes, 1440 longitudes
  1. Display just the ice data variable

sst['ice']
# or
sst.ice
<xarray.DataArray 'ice' (time: 1, zlev: 1, lat: 720, lon: 1440)>
[1036800 values with dtype=float32]
Coordinates:
  * time     (time) datetime64[ns] 2022-03-04T12:00:00
  * zlev     (zlev) float32 0.0
  * lat      (lat) float32 -89.88 -89.62 -89.38 -89.12 ... 89.38 89.62 89.88
  * lon      (lon) float32 0.125 0.375 0.625 0.875 ... 359.1 359.4 359.6 359.9
Attributes:
    long_name:  Sea ice concentration
    units:      %
    valid_min:  0
    valid_max:  100

Indexing and Selecting Values#

  1. Select only the SST data between 30-48 North and 3-30 East

sst['sst'].sel(lat=slice(30, 48), lon=slice(3, 30))
<xarray.DataArray 'sst' (time: 1, zlev: 1, lat: 72, lon: 108)>
[7776 values with dtype=float32]
Coordinates:
  * time     (time) datetime64[ns] 2022-03-04T12:00:00
  * zlev     (zlev) float32 0.0
  * lat      (lat) float32 30.12 30.38 30.62 30.88 ... 47.12 47.38 47.62 47.88
  * lon      (lon) float32 3.125 3.375 3.625 3.875 ... 29.12 29.38 29.62 29.88
Attributes:
    long_name:  Daily sea surface temperature
    units:      Celsius
    valid_min:  -300
    valid_max:  4500
  1. Use the .plot() method to view the subset of data you found in the previous question.

sst['sst'].sel(lat=slice(30, 48), lon=slice(3, 30)).plot()
<matplotlib.collections.QuadMesh at 0x16420a920>
../../../_images/7c4f1b3f67c2a3a79d9e83904e3455e9f5b5a9808c270c3bc939ab4a5be9c0fb.png

Use the fake data below for the follow 2 questions.

import numpy as np
ndvi_values = np.random.rand(4, 5, 6)

time = ['2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01']
latitude  = [34.3, 34.4, 34.5, 34.6, 34.7]
longitude = [-118.3, -118.2, -118.1, -118.0, -117.9, -117.8]

ndvi = xr.DataArray(
    ndvi_values, 
    dims = ['time', 'latitude', 'longitude'],
    coords=[time, latitude, longitude]
    )
  1. Write a line of code to select the 1st date from the ndvi data array.

ndvi.isel(time=0)
<xarray.DataArray (latitude: 5, longitude: 6)>
array([[0.17448699, 0.46382121, 0.70785558, 0.98192796, 0.01550836,
        0.09140627],
       [0.47970888, 0.98912372, 0.38093633, 0.48777636, 0.13838622,
        0.58566395],
       [0.4301748 , 0.08972506, 0.48844073, 0.06736549, 0.01058327,
        0.16540929],
       [0.01004352, 0.43254372, 0.39334168, 0.45682626, 0.43029295,
        0.9231626 ],
       [0.24807983, 0.28432955, 0.28637232, 0.47680053, 0.88341497,
        0.97857863]])
Coordinates:
    time       <U10 '2022-02-01'
  * latitude   (latitude) float64 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 -118.3 -118.2 -118.1 -118.0 -117.9 -117.8
  1. Write a line of code to select the date 2022-03-01 from the ndvi data array.

ndvi.sel(time='2022-03-01')
<xarray.DataArray (latitude: 5, longitude: 6)>
array([[0.60284707, 0.86396679, 0.7127189 , 0.17954338, 0.29369452,
        0.19422112],
       [0.57865021, 0.54091742, 0.56788866, 0.03936548, 0.07410923,
        0.18688124],
       [0.45490339, 0.96279133, 0.40160224, 0.55438141, 0.83831635,
        0.64495874],
       [0.22005558, 0.06077724, 0.70915742, 0.18582709, 0.87079562,
        0.10551424],
       [0.28394561, 0.54851699, 0.93040975, 0.60165735, 0.14559281,
        0.92725441]])
Coordinates:
    time       <U10 '2022-03-01'
  * latitude   (latitude) float64 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 -118.3 -118.2 -118.1 -118.0 -117.9 -117.8

Filepaths#

You have been given a laminated piece of paper with the following file structure on it. This file structure is an abbreviated version of the file structure of the SARP laptops. ... represents additional folder contents which are not shown in this diagram.

Folder structure

To help you determine the filepaths you can use the provided laminated sheet with the filepath on it. For each path, do the following:

  1. Circle the file that you are finding the path to

  2. Circle the starting location of your file path. What will the starting location always be for an absolute filepath? For a relative filepath, underline the file you are starting from and circle the starting directory.

  3. Write down the starting directory For a relative filepath. In this step there is 1 possible option for an absolute filepath and 2 options for a relative filepath

  4. Trace the lines from the starting directory to the end file. Whenever you cross a directory add the name of the directory to your filepath, followed by a \.

  5. Add the name of the file to the end of the filepath

Absolute Filepaths#

  1. geopandas.ipynb

  2. shrubland_change_Jan2022-Dec2006.mp4

  3. dramatic_chipmunk.gif

Relative Filepaths#

  1. example_code.py -> dramatic_chipmunk.gif

  2. geopandas.ipynb -> CAcountymap.geojson

  3. extract_features.ipynb -> aviris_f1806t01p00r02_img

  4. geopandas.ipynb -> aviris_f1806t01p00r02_img

  5. classify_shrublands.ipynb -> dramatic_chipmunk.gif

Absolute Filepaths#

  1. C:\\Users\SARP\Documents\projects\lessons\geopandas.ipynb

  2. C:\\Users\SARP\Documents\projects\crop_detection\visualizations\shrubland_change_Jan2022-Dec2006.mp4

  3. C:\\Users\SARP\Desktop\dramatic_chipmunk.gif

Relative Filepaths#

  1. from example_code.py: .\dramatic_chipmunk.gif

  2. from geopandas.ipynb: .\data\CAcountymap.geojson

  3. from extract_features.ipynb: ..\data\aviris_f1806t01p00r02_img

  4. from geopandas.ipynb: ..\crop_detection\data\aviris_f1806t01p00r02_img

  5. from classify_shrublands.ipynb: ..\..\..\..\Desktop\dramatic_chipmunk.gif

More Filepaths#

In the same folder as this one, there is a small file called filepath_practice.txt. Fill in the relative_filepath variable with a string of the relative path to that file. Run the pre-written code cell below it to read and print the contents of the text file.

relative_filepath = './filepath_practice.txt'

# Don't this code here. Just change the `relative_filepath` variable above
with open(relative_filepath) as f:
    line = f.readline()

print(line)
Congrats! You came up with the correct path to this file!

Now find the absolute filepath of that file. Copy and paste the code cell above, change the relative_filepath variable, and run the cell again.

# The exact answer will vary a bit based on where you put your file, but if it starts with
# C:\\ you correctly created an absolute filepath!
absolute_filepath = 'C:\\Users\SARP\Documents\projects\lessons\filepath_practice.txt'

# Don't this code here. Just change the `absolute_filepath` variable above
with open(relative_filepath) as f:
    line = f.readline()

print(line)
Congrats! You came up with the correct path to this file!

Part 2#

Question 1#

import numpy as np
ndvi_values = np.random.rand(4, 5, 6)

time = ['2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01']
latitude  = [34.3, 34.4, 34.5, 34.6, 34.7]
longitude = [-118.3, -118.2, -118.1, -118.0, -117.9, -117.8]

A) Make a DataArray out of the input values and dimensions above. The array should hold NDVI values (NDVI is a remote sensing calculation for the density of green stuff over an area). Be sure to specify both dimensions and coordinates. Describe in words what we know about the dimensions and location of the data.

import xarray as xr
ndvi = xr.DataArray(
    ndvi_values, 
    dims = ['time', 'latitude', 'longitude'],
    coords=[time, latitude, longitude]
    )

ndvi
<xarray.DataArray (time: 4, latitude: 5, longitude: 6)>
array([[[2.95614331e-01, 4.44776097e-01, 6.08122294e-01, 2.18095139e-02,
         9.96367718e-02, 1.57642574e-02],
        [1.34319278e-01, 2.38724370e-01, 6.25647059e-01, 2.41965567e-01,
         6.30893893e-01, 8.62253229e-01],
        [5.09902987e-01, 6.88284846e-01, 8.07033135e-01, 3.10434806e-01,
         5.78570429e-02, 1.21107481e-01],
        [5.13378527e-01, 1.70613193e-01, 7.48092565e-01, 7.09001964e-01,
         7.02919829e-02, 2.84907859e-01],
        [4.79328380e-01, 4.15120032e-01, 6.03568460e-01, 1.60375024e-01,
         3.63562348e-01, 7.15346296e-01]],

       [[3.03855211e-01, 2.29864343e-01, 6.23378441e-03, 4.51571964e-01,
         4.28675005e-01, 6.68232645e-01],
        [1.24086159e-01, 3.26849441e-01, 7.53441423e-01, 5.38457279e-01,
         1.19141177e-01, 8.37144090e-01],
        [3.55232692e-02, 1.71455376e-01, 4.28774724e-02, 9.06645090e-01,
         6.91436906e-01, 8.77888611e-01],
        [5.34174384e-01, 7.05727687e-01, 6.55241488e-01, 7.90302274e-01,
         6.36791015e-01, 6.01519693e-02],
        [2.75275608e-01, 2.44021320e-01, 3.63756285e-01, 7.76976519e-01,
...
         6.79099666e-02, 7.55819627e-01],
        [4.22637713e-02, 6.34603607e-01, 5.71845682e-01, 3.38653804e-01,
         6.06429722e-01, 6.98131152e-01],
        [5.68372011e-01, 7.66699600e-01, 5.00529275e-01, 6.00943237e-01,
         5.25258587e-01, 7.20904864e-03],
        [1.29448961e-01, 6.01596462e-05, 1.98458475e-01, 2.43754570e-01,
         8.79350871e-01, 8.18658109e-02],
        [7.15454773e-01, 6.87448497e-01, 8.53034412e-01, 4.43632678e-02,
         6.33322094e-02, 4.72881775e-01]],

       [[8.41745511e-01, 3.73002584e-01, 7.42023308e-01, 6.10194887e-01,
         8.09770707e-01, 5.71754221e-01],
        [2.68400131e-01, 1.92624617e-01, 8.71453493e-01, 1.61567682e-01,
         8.23286076e-01, 7.05203946e-01],
        [7.58135530e-01, 4.72284409e-01, 3.42290982e-01, 3.89725497e-01,
         4.95450240e-01, 7.51573424e-02],
        [7.80967939e-02, 5.81294238e-01, 4.67492684e-01, 9.59928369e-01,
         7.25062564e-01, 5.22423994e-01],
        [8.27547861e-01, 1.03037630e-01, 9.54565346e-01, 1.92937027e-02,
         2.10997627e-01, 7.70969249e-01]]])
Coordinates:
  * time       (time) <U10 '2022-02-01' '2022-03-01' '2022-04-01' '2022-05-01'
  * latitude   (latitude) float64 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 -118.3 -118.2 -118.1 -118.0 -117.9 -117.8
ndvi = xr.DataArray(
    ndvi_values, 
    dims = ['time', 'latitude', 'longitude'],
    coords=[time, latitude, longitude]
    )

ndvi
<xarray.DataArray (time: 4, latitude: 5, longitude: 6)>
array([[[2.95614331e-01, 4.44776097e-01, 6.08122294e-01, 2.18095139e-02,
         9.96367718e-02, 1.57642574e-02],
        [1.34319278e-01, 2.38724370e-01, 6.25647059e-01, 2.41965567e-01,
         6.30893893e-01, 8.62253229e-01],
        [5.09902987e-01, 6.88284846e-01, 8.07033135e-01, 3.10434806e-01,
         5.78570429e-02, 1.21107481e-01],
        [5.13378527e-01, 1.70613193e-01, 7.48092565e-01, 7.09001964e-01,
         7.02919829e-02, 2.84907859e-01],
        [4.79328380e-01, 4.15120032e-01, 6.03568460e-01, 1.60375024e-01,
         3.63562348e-01, 7.15346296e-01]],

       [[3.03855211e-01, 2.29864343e-01, 6.23378441e-03, 4.51571964e-01,
         4.28675005e-01, 6.68232645e-01],
        [1.24086159e-01, 3.26849441e-01, 7.53441423e-01, 5.38457279e-01,
         1.19141177e-01, 8.37144090e-01],
        [3.55232692e-02, 1.71455376e-01, 4.28774724e-02, 9.06645090e-01,
         6.91436906e-01, 8.77888611e-01],
        [5.34174384e-01, 7.05727687e-01, 6.55241488e-01, 7.90302274e-01,
         6.36791015e-01, 6.01519693e-02],
        [2.75275608e-01, 2.44021320e-01, 3.63756285e-01, 7.76976519e-01,
...
         6.79099666e-02, 7.55819627e-01],
        [4.22637713e-02, 6.34603607e-01, 5.71845682e-01, 3.38653804e-01,
         6.06429722e-01, 6.98131152e-01],
        [5.68372011e-01, 7.66699600e-01, 5.00529275e-01, 6.00943237e-01,
         5.25258587e-01, 7.20904864e-03],
        [1.29448961e-01, 6.01596462e-05, 1.98458475e-01, 2.43754570e-01,
         8.79350871e-01, 8.18658109e-02],
        [7.15454773e-01, 6.87448497e-01, 8.53034412e-01, 4.43632678e-02,
         6.33322094e-02, 4.72881775e-01]],

       [[8.41745511e-01, 3.73002584e-01, 7.42023308e-01, 6.10194887e-01,
         8.09770707e-01, 5.71754221e-01],
        [2.68400131e-01, 1.92624617e-01, 8.71453493e-01, 1.61567682e-01,
         8.23286076e-01, 7.05203946e-01],
        [7.58135530e-01, 4.72284409e-01, 3.42290982e-01, 3.89725497e-01,
         4.95450240e-01, 7.51573424e-02],
        [7.80967939e-02, 5.81294238e-01, 4.67492684e-01, 9.59928369e-01,
         7.25062564e-01, 5.22423994e-01],
        [8.27547861e-01, 1.03037630e-01, 9.54565346e-01, 1.92937027e-02,
         2.10997627e-01, 7.70969249e-01]]])
Coordinates:
  * time       (time) <U10 '2022-02-01' '2022-03-01' '2022-04-01' '2022-05-01'
  * latitude   (latitude) float64 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 -118.3 -118.2 -118.1 -118.0 -117.9 -117.8
ndvi
<xarray.DataArray (time: 4, latitude: 5, longitude: 6)>
array([[[2.95614331e-01, 4.44776097e-01, 6.08122294e-01, 2.18095139e-02,
         9.96367718e-02, 1.57642574e-02],
        [1.34319278e-01, 2.38724370e-01, 6.25647059e-01, 2.41965567e-01,
         6.30893893e-01, 8.62253229e-01],
        [5.09902987e-01, 6.88284846e-01, 8.07033135e-01, 3.10434806e-01,
         5.78570429e-02, 1.21107481e-01],
        [5.13378527e-01, 1.70613193e-01, 7.48092565e-01, 7.09001964e-01,
         7.02919829e-02, 2.84907859e-01],
        [4.79328380e-01, 4.15120032e-01, 6.03568460e-01, 1.60375024e-01,
         3.63562348e-01, 7.15346296e-01]],

       [[3.03855211e-01, 2.29864343e-01, 6.23378441e-03, 4.51571964e-01,
         4.28675005e-01, 6.68232645e-01],
        [1.24086159e-01, 3.26849441e-01, 7.53441423e-01, 5.38457279e-01,
         1.19141177e-01, 8.37144090e-01],
        [3.55232692e-02, 1.71455376e-01, 4.28774724e-02, 9.06645090e-01,
         6.91436906e-01, 8.77888611e-01],
        [5.34174384e-01, 7.05727687e-01, 6.55241488e-01, 7.90302274e-01,
         6.36791015e-01, 6.01519693e-02],
        [2.75275608e-01, 2.44021320e-01, 3.63756285e-01, 7.76976519e-01,
...
         6.79099666e-02, 7.55819627e-01],
        [4.22637713e-02, 6.34603607e-01, 5.71845682e-01, 3.38653804e-01,
         6.06429722e-01, 6.98131152e-01],
        [5.68372011e-01, 7.66699600e-01, 5.00529275e-01, 6.00943237e-01,
         5.25258587e-01, 7.20904864e-03],
        [1.29448961e-01, 6.01596462e-05, 1.98458475e-01, 2.43754570e-01,
         8.79350871e-01, 8.18658109e-02],
        [7.15454773e-01, 6.87448497e-01, 8.53034412e-01, 4.43632678e-02,
         6.33322094e-02, 4.72881775e-01]],

       [[8.41745511e-01, 3.73002584e-01, 7.42023308e-01, 6.10194887e-01,
         8.09770707e-01, 5.71754221e-01],
        [2.68400131e-01, 1.92624617e-01, 8.71453493e-01, 1.61567682e-01,
         8.23286076e-01, 7.05203946e-01],
        [7.58135530e-01, 4.72284409e-01, 3.42290982e-01, 3.89725497e-01,
         4.95450240e-01, 7.51573424e-02],
        [7.80967939e-02, 5.81294238e-01, 4.67492684e-01, 9.59928369e-01,
         7.25062564e-01, 5.22423994e-01],
        [8.27547861e-01, 1.03037630e-01, 9.54565346e-01, 1.92937027e-02,
         2.10997627e-01, 7.70969249e-01]]])
Coordinates:
  * time       (time) <U10 '2022-02-01' '2022-03-01' '2022-04-01' '2022-05-01'
  * latitude   (latitude) float64 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 -118.3 -118.2 -118.1 -118.0 -117.9 -117.8
# This is a 3 dimensional array. The array represents data spread across space (latitude and
# longitude) and in time.
# The values for time are monthly, 3 months in 2022. 
# Latitude values range from 34.3 to 34.7 and longitude values range from -118.2 to -117.8

B) Create the array again, but this time don’t specify coordinates. What is different about the DataArray? Describe in words what we know about this data.

ndvi = xr.DataArray(
    ndvi_values, 
    dims = ['time', 'latitude', 'longitude'],
    # coords=[time, latitude, longitude]
    )

ndvi
<xarray.DataArray (time: 4, latitude: 5, longitude: 6)>
array([[[2.95614331e-01, 4.44776097e-01, 6.08122294e-01, 2.18095139e-02,
         9.96367718e-02, 1.57642574e-02],
        [1.34319278e-01, 2.38724370e-01, 6.25647059e-01, 2.41965567e-01,
         6.30893893e-01, 8.62253229e-01],
        [5.09902987e-01, 6.88284846e-01, 8.07033135e-01, 3.10434806e-01,
         5.78570429e-02, 1.21107481e-01],
        [5.13378527e-01, 1.70613193e-01, 7.48092565e-01, 7.09001964e-01,
         7.02919829e-02, 2.84907859e-01],
        [4.79328380e-01, 4.15120032e-01, 6.03568460e-01, 1.60375024e-01,
         3.63562348e-01, 7.15346296e-01]],

       [[3.03855211e-01, 2.29864343e-01, 6.23378441e-03, 4.51571964e-01,
         4.28675005e-01, 6.68232645e-01],
        [1.24086159e-01, 3.26849441e-01, 7.53441423e-01, 5.38457279e-01,
         1.19141177e-01, 8.37144090e-01],
        [3.55232692e-02, 1.71455376e-01, 4.28774724e-02, 9.06645090e-01,
         6.91436906e-01, 8.77888611e-01],
        [5.34174384e-01, 7.05727687e-01, 6.55241488e-01, 7.90302274e-01,
         6.36791015e-01, 6.01519693e-02],
        [2.75275608e-01, 2.44021320e-01, 3.63756285e-01, 7.76976519e-01,
...
         6.79099666e-02, 7.55819627e-01],
        [4.22637713e-02, 6.34603607e-01, 5.71845682e-01, 3.38653804e-01,
         6.06429722e-01, 6.98131152e-01],
        [5.68372011e-01, 7.66699600e-01, 5.00529275e-01, 6.00943237e-01,
         5.25258587e-01, 7.20904864e-03],
        [1.29448961e-01, 6.01596462e-05, 1.98458475e-01, 2.43754570e-01,
         8.79350871e-01, 8.18658109e-02],
        [7.15454773e-01, 6.87448497e-01, 8.53034412e-01, 4.43632678e-02,
         6.33322094e-02, 4.72881775e-01]],

       [[8.41745511e-01, 3.73002584e-01, 7.42023308e-01, 6.10194887e-01,
         8.09770707e-01, 5.71754221e-01],
        [2.68400131e-01, 1.92624617e-01, 8.71453493e-01, 1.61567682e-01,
         8.23286076e-01, 7.05203946e-01],
        [7.58135530e-01, 4.72284409e-01, 3.42290982e-01, 3.89725497e-01,
         4.95450240e-01, 7.51573424e-02],
        [7.80967939e-02, 5.81294238e-01, 4.67492684e-01, 9.59928369e-01,
         7.25062564e-01, 5.22423994e-01],
        [8.27547861e-01, 1.03037630e-01, 9.54565346e-01, 1.92937027e-02,
         2.10997627e-01, 7.70969249e-01]]])
Dimensions without coordinates: time, latitude, longitude
# This is a 3 dimensional array. The array represents data spread across space (latitude and
# longitude) and in time.
# We don't know when in space or time the data was taken.

C) Create the array again, but this time don’t specify dimensions. How does that compare to Part B? Describe in words what we know about this data.

ndvi = xr.DataArray(
    ndvi_values, 
    # dims = ['time', 'latitude', 'longitude'],
    coords=[time, latitude, longitude]
    )

ndvi
<xarray.DataArray (dim_0: 4, dim_1: 5, dim_2: 6)>
array([[[2.95614331e-01, 4.44776097e-01, 6.08122294e-01, 2.18095139e-02,
         9.96367718e-02, 1.57642574e-02],
        [1.34319278e-01, 2.38724370e-01, 6.25647059e-01, 2.41965567e-01,
         6.30893893e-01, 8.62253229e-01],
        [5.09902987e-01, 6.88284846e-01, 8.07033135e-01, 3.10434806e-01,
         5.78570429e-02, 1.21107481e-01],
        [5.13378527e-01, 1.70613193e-01, 7.48092565e-01, 7.09001964e-01,
         7.02919829e-02, 2.84907859e-01],
        [4.79328380e-01, 4.15120032e-01, 6.03568460e-01, 1.60375024e-01,
         3.63562348e-01, 7.15346296e-01]],

       [[3.03855211e-01, 2.29864343e-01, 6.23378441e-03, 4.51571964e-01,
         4.28675005e-01, 6.68232645e-01],
        [1.24086159e-01, 3.26849441e-01, 7.53441423e-01, 5.38457279e-01,
         1.19141177e-01, 8.37144090e-01],
        [3.55232692e-02, 1.71455376e-01, 4.28774724e-02, 9.06645090e-01,
         6.91436906e-01, 8.77888611e-01],
        [5.34174384e-01, 7.05727687e-01, 6.55241488e-01, 7.90302274e-01,
         6.36791015e-01, 6.01519693e-02],
        [2.75275608e-01, 2.44021320e-01, 3.63756285e-01, 7.76976519e-01,
...
         6.79099666e-02, 7.55819627e-01],
        [4.22637713e-02, 6.34603607e-01, 5.71845682e-01, 3.38653804e-01,
         6.06429722e-01, 6.98131152e-01],
        [5.68372011e-01, 7.66699600e-01, 5.00529275e-01, 6.00943237e-01,
         5.25258587e-01, 7.20904864e-03],
        [1.29448961e-01, 6.01596462e-05, 1.98458475e-01, 2.43754570e-01,
         8.79350871e-01, 8.18658109e-02],
        [7.15454773e-01, 6.87448497e-01, 8.53034412e-01, 4.43632678e-02,
         6.33322094e-02, 4.72881775e-01]],

       [[8.41745511e-01, 3.73002584e-01, 7.42023308e-01, 6.10194887e-01,
         8.09770707e-01, 5.71754221e-01],
        [2.68400131e-01, 1.92624617e-01, 8.71453493e-01, 1.61567682e-01,
         8.23286076e-01, 7.05203946e-01],
        [7.58135530e-01, 4.72284409e-01, 3.42290982e-01, 3.89725497e-01,
         4.95450240e-01, 7.51573424e-02],
        [7.80967939e-02, 5.81294238e-01, 4.67492684e-01, 9.59928369e-01,
         7.25062564e-01, 5.22423994e-01],
        [8.27547861e-01, 1.03037630e-01, 9.54565346e-01, 1.92937027e-02,
         2.10997627e-01, 7.70969249e-01]]])
Coordinates:
  * dim_0    (dim_0) <U10 '2022-02-01' '2022-03-01' '2022-04-01' '2022-05-01'
  * dim_1    (dim_1) float64 34.3 34.4 34.5 34.6 34.7
  * dim_2    (dim_2) float64 -118.3 -118.2 -118.1 -118.0 -117.9 -117.8
# This is a 3 dimensional array. We don't know what those three dimensions are - they could be
# anything (altitude, pressure level, flight number, reserach institution, etc.)
# While we don't actually know what the dimensions represent, we know the values are
#  3 months in 2022, something that 34.3 to 34.7 and something else that ranges from 
# -118.2 to -117.8

Question 2#

sst = xr.open_dataset('../data/oisst-avhrr-v02r01.20220304.nc')

A) Select just the sst values from 10 to 25 degrees North and 50 to 88 degrees West.

sst.sel(lat=slice(10, 25), lon=slice(272, 310))
<xarray.Dataset>
Dimensions:  (time: 1, zlev: 1, lat: 60, lon: 152)
Coordinates:
  * time     (time) datetime64[ns] 2022-03-04T12:00:00
  * zlev     (zlev) float32 0.0
  * lat      (lat) float32 10.12 10.38 10.62 10.88 ... 24.12 24.38 24.62 24.88
  * lon      (lon) float32 272.1 272.4 272.6 272.9 ... 309.1 309.4 309.6 309.9
Data variables:
    sst      (time, zlev, lat, lon) float32 ...
    anom     (time, zlev, lat, lon) float32 ...
    err      (time, zlev, lat, lon) float32 ...
    ice      (time, zlev, lat, lon) float32 ...
Attributes: (12/37)
    Conventions:                CF-1.6, ACDD-1.3
    title:                      NOAA/NCEI 1/4 Degree Daily Optimum Interpolat...
    references:                 Reynolds, et al.(2007) Daily High-Resolution-...
    source:                     ICOADS, NCEP_GTS, GSFC_ICE, NCEP_ICE, Pathfin...
    id:                         oisst-avhrr-v02r01.20220304.nc
    naming_authority:           gov.noaa.ncei
    ...                         ...
    time_coverage_start:        2022-03-04T00:00:00Z
    time_coverage_end:          2022-03-04T23:59:59Z
    metadata_link:              https://doi.org/10.25921/RE9P-PT57
    ncei_template_version:      NCEI_NetCDF_Grid_Template_v2.0
    comment:                    Data was converted from NetCDF-3 to NetCDF-4 ...
    sensor:                     Thermometer, AVHRR
# a slightly overcomplicated but still fun way to do it - write a function to convert longitude
# from -180 -> 180 to 0 -> 360
# subtract 180 from 360 degree longitude to convert to -180/180 longitude
def convert_lon(lon_180):
    '''Convert a longitude from 0 to 360 degree range to a -180 to 180 degree range'''
    if lon_180 < 0:
        lon_360 = lon_180 + 360
    else:
        lon_360 = lon_180
    return lon_360

# from 272 to 310
sst.sel(lat=slice(10, 25), lon=slice(convert_lon(-88), convert_lon(-50)))
<xarray.Dataset>
Dimensions:  (time: 1, zlev: 1, lat: 60, lon: 152)
Coordinates:
  * time     (time) datetime64[ns] 2022-03-04T12:00:00
  * zlev     (zlev) float32 0.0
  * lat      (lat) float32 10.12 10.38 10.62 10.88 ... 24.12 24.38 24.62 24.88
  * lon      (lon) float32 272.1 272.4 272.6 272.9 ... 309.1 309.4 309.6 309.9
Data variables:
    sst      (time, zlev, lat, lon) float32 ...
    anom     (time, zlev, lat, lon) float32 ...
    err      (time, zlev, lat, lon) float32 ...
    ice      (time, zlev, lat, lon) float32 ...
Attributes: (12/37)
    Conventions:                CF-1.6, ACDD-1.3
    title:                      NOAA/NCEI 1/4 Degree Daily Optimum Interpolat...
    references:                 Reynolds, et al.(2007) Daily High-Resolution-...
    source:                     ICOADS, NCEP_GTS, GSFC_ICE, NCEP_ICE, Pathfin...
    id:                         oisst-avhrr-v02r01.20220304.nc
    naming_authority:           gov.noaa.ncei
    ...                         ...
    time_coverage_start:        2022-03-04T00:00:00Z
    time_coverage_end:          2022-03-04T23:59:59Z
    metadata_link:              https://doi.org/10.25921/RE9P-PT57
    ncei_template_version:      NCEI_NetCDF_Grid_Template_v2.0
    comment:                    Data was converted from NetCDF-3 to NetCDF-4 ...
    sensor:                     Thermometer, AVHRR

B) Use the .plot() function demonstrated at the end of the lesson to look at the data in your subset. Note that to use .plot() the way it is shown, you need to make sure two things are true:

  1. You are using a DataArray, not a Dataset

  2. You are giving it a 2d slice of data, where the 2 dimensions are latitude and longitude

sst.sst.sel(lat=slice(10, 25), lon=slice(272, 310)).plot()
<matplotlib.collections.QuadMesh at 0x1643b1900>
../../../_images/a92fb859197bd23efefe8b1ce80d748944576b5350fdeae358d2541ff6fc9bc8.png

Question 3#

How many dimensions should your output array have if you did each of the following:

  1. selected the 1st time value

  2. selected the 1st time value and the 100th to the 200th latitude values

  3. selected the 100th to the 200th latitude values and the 1000th to 1200th longitude values

  4. selected the 1st time value, the 1st elevation value, the 100th to the 200th latitude values and the 1000th to 1200th longitude values

  5. selected the 1st time value, 1st elevation value, the 1st latitude value and the 1st longitude value

# 1. 3 dimensions
# 2. 3 dimensions
# 3. 4 dimensions
# 4. 2 dimensions
# 5. 0 dimensions (a single number)