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)> Size: 4MB
[1036800 values with dtype=float32]
Coordinates:
  * time     (time) datetime64[ns] 8B 2022-03-04T12:00:00
  * zlev     (zlev) float32 4B 0.0
  * lat      (lat) float32 3kB -89.88 -89.62 -89.38 -89.12 ... 89.38 89.62 89.88
  * lon      (lon) float32 6kB 0.125 0.375 0.625 0.875 ... 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)> Size: 31kB
[7776 values with dtype=float32]
Coordinates:
  * time     (time) datetime64[ns] 8B 2022-03-04T12:00:00
  * zlev     (zlev) float32 4B 0.0
  * lat      (lat) float32 288B 30.12 30.38 30.62 30.88 ... 47.38 47.62 47.88
  * lon      (lon) float32 432B 3.125 3.375 3.625 3.875 ... 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 0x7fc475d24a50>
../../../_images/815ab45521544f7303fd60f5298f4a7695f48e39947446a5150e6faaede73df2.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)> Size: 240B
array([[0.03099462, 0.84940758, 0.91931751, 0.55953148, 0.5836294 ,
        0.93614941],
       [0.93086036, 0.91821045, 0.95579055, 0.46307724, 0.76313615,
        0.63400793],
       [0.73896308, 0.58550935, 0.35198374, 0.10810567, 0.97665882,
        0.96501744],
       [0.51766915, 0.23582265, 0.9609851 , 0.08292351, 0.74240511,
        0.43793496],
       [0.72111756, 0.90747595, 0.3797463 , 0.69833825, 0.3049408 ,
        0.94209267]])
Coordinates:
    time       <U10 40B '2022-02-01'
  * latitude   (latitude) float64 40B 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 48B -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)> Size: 240B
array([[0.17795687, 0.91465889, 0.73109899, 0.9320224 , 0.46743986,
        0.82442449],
       [0.53281445, 0.02343219, 0.9927104 , 0.55558013, 0.1921034 ,
        0.00141969],
       [0.90979485, 0.0659417 , 0.22718067, 0.89493512, 0.93766303,
        0.91519279],
       [0.71596726, 0.38829146, 0.2235889 , 0.83759917, 0.94837247,
        0.66213712],
       [0.31192498, 0.97613157, 0.172687  , 0.35845653, 0.87855183,
        0.72324491]])
Coordinates:
    time       <U10 40B '2022-03-01'
  * latitude   (latitude) float64 40B 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 48B -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)> Size: 960B
array([[[0.3671012 , 0.08925138, 0.51259145, 0.49512107, 0.21145983,
         0.84718412],
        [0.27009531, 0.64330196, 0.21852954, 0.10360361, 0.26840231,
         0.01821597],
        [0.41519366, 0.27539122, 0.39370917, 0.62393005, 0.5146907 ,
         0.82491494],
        [0.94810412, 0.66811975, 0.13155409, 0.23252963, 0.08776911,
         0.41326596],
        [0.3595948 , 0.23177384, 0.60311563, 0.55661162, 0.82161666,
         0.78511805]],

       [[0.66952306, 0.37676668, 0.12886538, 0.60615428, 0.68484646,
         0.18399785],
        [0.0101599 , 0.00393751, 0.69538273, 0.31502399, 0.23276772,
         0.43236117],
        [0.29506103, 0.39325447, 0.00975807, 0.04149834, 0.50096883,
         0.16720875],
        [0.53216329, 0.67685941, 0.19918645, 0.8963203 , 0.45541279,
         0.6836219 ],
        [0.06002596, 0.3370525 , 0.92511528, 0.71616304, 0.74077517,
...
         0.02100851],
        [0.35633081, 0.41445919, 0.19137011, 0.02554326, 0.47171186,
         0.01716106],
        [0.19010791, 0.05864335, 0.44620943, 0.63965452, 0.42754801,
         0.03335143],
        [0.62164701, 0.64169468, 0.97860332, 0.26987852, 0.44287017,
         0.82877511],
        [0.43645908, 0.0898842 , 0.86391429, 0.10317421, 0.3221624 ,
         0.06911131]],

       [[0.86836446, 0.56632507, 0.90172836, 0.88900674, 0.1269645 ,
         0.15565325],
        [0.47830937, 0.73578544, 0.72598081, 0.44775871, 0.23363971,
         0.66264761],
        [0.1411666 , 0.65489041, 0.01739616, 0.93293877, 0.43052098,
         0.40217861],
        [0.50005329, 0.28186412, 0.17327474, 0.40490377, 0.04826459,
         0.32960445],
        [0.88412984, 0.82717966, 0.34308688, 0.80799026, 0.95394364,
         0.75252419]]])
Coordinates:
  * time       (time) <U10 160B '2022-02-01' '2022-03-01' ... '2022-05-01'
  * latitude   (latitude) float64 40B 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 48B -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)> Size: 960B
array([[[0.3671012 , 0.08925138, 0.51259145, 0.49512107, 0.21145983,
         0.84718412],
        [0.27009531, 0.64330196, 0.21852954, 0.10360361, 0.26840231,
         0.01821597],
        [0.41519366, 0.27539122, 0.39370917, 0.62393005, 0.5146907 ,
         0.82491494],
        [0.94810412, 0.66811975, 0.13155409, 0.23252963, 0.08776911,
         0.41326596],
        [0.3595948 , 0.23177384, 0.60311563, 0.55661162, 0.82161666,
         0.78511805]],

       [[0.66952306, 0.37676668, 0.12886538, 0.60615428, 0.68484646,
         0.18399785],
        [0.0101599 , 0.00393751, 0.69538273, 0.31502399, 0.23276772,
         0.43236117],
        [0.29506103, 0.39325447, 0.00975807, 0.04149834, 0.50096883,
         0.16720875],
        [0.53216329, 0.67685941, 0.19918645, 0.8963203 , 0.45541279,
         0.6836219 ],
        [0.06002596, 0.3370525 , 0.92511528, 0.71616304, 0.74077517,
...
         0.02100851],
        [0.35633081, 0.41445919, 0.19137011, 0.02554326, 0.47171186,
         0.01716106],
        [0.19010791, 0.05864335, 0.44620943, 0.63965452, 0.42754801,
         0.03335143],
        [0.62164701, 0.64169468, 0.97860332, 0.26987852, 0.44287017,
         0.82877511],
        [0.43645908, 0.0898842 , 0.86391429, 0.10317421, 0.3221624 ,
         0.06911131]],

       [[0.86836446, 0.56632507, 0.90172836, 0.88900674, 0.1269645 ,
         0.15565325],
        [0.47830937, 0.73578544, 0.72598081, 0.44775871, 0.23363971,
         0.66264761],
        [0.1411666 , 0.65489041, 0.01739616, 0.93293877, 0.43052098,
         0.40217861],
        [0.50005329, 0.28186412, 0.17327474, 0.40490377, 0.04826459,
         0.32960445],
        [0.88412984, 0.82717966, 0.34308688, 0.80799026, 0.95394364,
         0.75252419]]])
Coordinates:
  * time       (time) <U10 160B '2022-02-01' '2022-03-01' ... '2022-05-01'
  * latitude   (latitude) float64 40B 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 48B -118.3 -118.2 -118.1 -118.0 -117.9 -117.8
ndvi
<xarray.DataArray (time: 4, latitude: 5, longitude: 6)> Size: 960B
array([[[0.3671012 , 0.08925138, 0.51259145, 0.49512107, 0.21145983,
         0.84718412],
        [0.27009531, 0.64330196, 0.21852954, 0.10360361, 0.26840231,
         0.01821597],
        [0.41519366, 0.27539122, 0.39370917, 0.62393005, 0.5146907 ,
         0.82491494],
        [0.94810412, 0.66811975, 0.13155409, 0.23252963, 0.08776911,
         0.41326596],
        [0.3595948 , 0.23177384, 0.60311563, 0.55661162, 0.82161666,
         0.78511805]],

       [[0.66952306, 0.37676668, 0.12886538, 0.60615428, 0.68484646,
         0.18399785],
        [0.0101599 , 0.00393751, 0.69538273, 0.31502399, 0.23276772,
         0.43236117],
        [0.29506103, 0.39325447, 0.00975807, 0.04149834, 0.50096883,
         0.16720875],
        [0.53216329, 0.67685941, 0.19918645, 0.8963203 , 0.45541279,
         0.6836219 ],
        [0.06002596, 0.3370525 , 0.92511528, 0.71616304, 0.74077517,
...
         0.02100851],
        [0.35633081, 0.41445919, 0.19137011, 0.02554326, 0.47171186,
         0.01716106],
        [0.19010791, 0.05864335, 0.44620943, 0.63965452, 0.42754801,
         0.03335143],
        [0.62164701, 0.64169468, 0.97860332, 0.26987852, 0.44287017,
         0.82877511],
        [0.43645908, 0.0898842 , 0.86391429, 0.10317421, 0.3221624 ,
         0.06911131]],

       [[0.86836446, 0.56632507, 0.90172836, 0.88900674, 0.1269645 ,
         0.15565325],
        [0.47830937, 0.73578544, 0.72598081, 0.44775871, 0.23363971,
         0.66264761],
        [0.1411666 , 0.65489041, 0.01739616, 0.93293877, 0.43052098,
         0.40217861],
        [0.50005329, 0.28186412, 0.17327474, 0.40490377, 0.04826459,
         0.32960445],
        [0.88412984, 0.82717966, 0.34308688, 0.80799026, 0.95394364,
         0.75252419]]])
Coordinates:
  * time       (time) <U10 160B '2022-02-01' '2022-03-01' ... '2022-05-01'
  * latitude   (latitude) float64 40B 34.3 34.4 34.5 34.6 34.7
  * longitude  (longitude) float64 48B -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)> Size: 960B
array([[[0.3671012 , 0.08925138, 0.51259145, 0.49512107, 0.21145983,
         0.84718412],
        [0.27009531, 0.64330196, 0.21852954, 0.10360361, 0.26840231,
         0.01821597],
        [0.41519366, 0.27539122, 0.39370917, 0.62393005, 0.5146907 ,
         0.82491494],
        [0.94810412, 0.66811975, 0.13155409, 0.23252963, 0.08776911,
         0.41326596],
        [0.3595948 , 0.23177384, 0.60311563, 0.55661162, 0.82161666,
         0.78511805]],

       [[0.66952306, 0.37676668, 0.12886538, 0.60615428, 0.68484646,
         0.18399785],
        [0.0101599 , 0.00393751, 0.69538273, 0.31502399, 0.23276772,
         0.43236117],
        [0.29506103, 0.39325447, 0.00975807, 0.04149834, 0.50096883,
         0.16720875],
        [0.53216329, 0.67685941, 0.19918645, 0.8963203 , 0.45541279,
         0.6836219 ],
        [0.06002596, 0.3370525 , 0.92511528, 0.71616304, 0.74077517,
...
         0.02100851],
        [0.35633081, 0.41445919, 0.19137011, 0.02554326, 0.47171186,
         0.01716106],
        [0.19010791, 0.05864335, 0.44620943, 0.63965452, 0.42754801,
         0.03335143],
        [0.62164701, 0.64169468, 0.97860332, 0.26987852, 0.44287017,
         0.82877511],
        [0.43645908, 0.0898842 , 0.86391429, 0.10317421, 0.3221624 ,
         0.06911131]],

       [[0.86836446, 0.56632507, 0.90172836, 0.88900674, 0.1269645 ,
         0.15565325],
        [0.47830937, 0.73578544, 0.72598081, 0.44775871, 0.23363971,
         0.66264761],
        [0.1411666 , 0.65489041, 0.01739616, 0.93293877, 0.43052098,
         0.40217861],
        [0.50005329, 0.28186412, 0.17327474, 0.40490377, 0.04826459,
         0.32960445],
        [0.88412984, 0.82717966, 0.34308688, 0.80799026, 0.95394364,
         0.75252419]]])
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)> Size: 960B
array([[[0.3671012 , 0.08925138, 0.51259145, 0.49512107, 0.21145983,
         0.84718412],
        [0.27009531, 0.64330196, 0.21852954, 0.10360361, 0.26840231,
         0.01821597],
        [0.41519366, 0.27539122, 0.39370917, 0.62393005, 0.5146907 ,
         0.82491494],
        [0.94810412, 0.66811975, 0.13155409, 0.23252963, 0.08776911,
         0.41326596],
        [0.3595948 , 0.23177384, 0.60311563, 0.55661162, 0.82161666,
         0.78511805]],

       [[0.66952306, 0.37676668, 0.12886538, 0.60615428, 0.68484646,
         0.18399785],
        [0.0101599 , 0.00393751, 0.69538273, 0.31502399, 0.23276772,
         0.43236117],
        [0.29506103, 0.39325447, 0.00975807, 0.04149834, 0.50096883,
         0.16720875],
        [0.53216329, 0.67685941, 0.19918645, 0.8963203 , 0.45541279,
         0.6836219 ],
        [0.06002596, 0.3370525 , 0.92511528, 0.71616304, 0.74077517,
...
         0.02100851],
        [0.35633081, 0.41445919, 0.19137011, 0.02554326, 0.47171186,
         0.01716106],
        [0.19010791, 0.05864335, 0.44620943, 0.63965452, 0.42754801,
         0.03335143],
        [0.62164701, 0.64169468, 0.97860332, 0.26987852, 0.44287017,
         0.82877511],
        [0.43645908, 0.0898842 , 0.86391429, 0.10317421, 0.3221624 ,
         0.06911131]],

       [[0.86836446, 0.56632507, 0.90172836, 0.88900674, 0.1269645 ,
         0.15565325],
        [0.47830937, 0.73578544, 0.72598081, 0.44775871, 0.23363971,
         0.66264761],
        [0.1411666 , 0.65489041, 0.01739616, 0.93293877, 0.43052098,
         0.40217861],
        [0.50005329, 0.28186412, 0.17327474, 0.40490377, 0.04826459,
         0.32960445],
        [0.88412984, 0.82717966, 0.34308688, 0.80799026, 0.95394364,
         0.75252419]]])
Coordinates:
  * dim_0    (dim_0) <U10 160B '2022-02-01' '2022-03-01' ... '2022-05-01'
  * dim_1    (dim_1) float64 40B 34.3 34.4 34.5 34.6 34.7
  * dim_2    (dim_2) float64 48B -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> Size: 147kB
Dimensions:  (time: 1, zlev: 1, lat: 60, lon: 152)
Coordinates:
  * time     (time) datetime64[ns] 8B 2022-03-04T12:00:00
  * zlev     (zlev) float32 4B 0.0
  * lat      (lat) float32 240B 10.12 10.38 10.62 10.88 ... 24.38 24.62 24.88
  * lon      (lon) float32 608B 272.1 272.4 272.6 272.9 ... 309.4 309.6 309.9
Data variables:
    sst      (time, zlev, lat, lon) float32 36kB ...
    anom     (time, zlev, lat, lon) float32 36kB ...
    err      (time, zlev, lat, lon) float32 36kB ...
    ice      (time, zlev, lat, lon) float32 36kB ...
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> Size: 147kB
Dimensions:  (time: 1, zlev: 1, lat: 60, lon: 152)
Coordinates:
  * time     (time) datetime64[ns] 8B 2022-03-04T12:00:00
  * zlev     (zlev) float32 4B 0.0
  * lat      (lat) float32 240B 10.12 10.38 10.62 10.88 ... 24.38 24.62 24.88
  * lon      (lon) float32 608B 272.1 272.4 272.6 272.9 ... 309.4 309.6 309.9
Data variables:
    sst      (time, zlev, lat, lon) float32 36kB ...
    anom     (time, zlev, lat, lon) float32 36kB ...
    err      (time, zlev, lat, lon) float32 36kB ...
    ice      (time, zlev, lat, lon) float32 36kB ...
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 0x7fc475b77f90>
../../../_images/aa2f5959fb5ed1286d513a1d9ee4072de0334b665da1e34a301ef9783aab7397.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)