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.

  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?

  1. Display just the ice data variable

Indexing and Selecting Values#

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

  1. Use the .plot() method to view the subset of data you found in the previous question.

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.

  1. Write a line of code to select the date 2022-03-01 from the ndvi data array.

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

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 = ''

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

print(line)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[4], line 4
      1 relative_filepath = ''
      3 # Don't this code here. Just change the `relative_filepath` variable above
----> 4 with open(relative_filepath) as f:
      5     line = f.readline()
      7 print(line)

File ~/miniconda3/envs/sarp/lib/python3.10/site-packages/IPython/core/interactiveshell.py:284, in _modified_open(file, *args, **kwargs)
    277 if file in {0, 1, 2}:
    278     raise ValueError(
    279         f"IPython won't let you open fd={file} by default "
    280         "as it is likely to crash IPython. If you know what you are doing, "
    281         "you can use builtins' open."
    282     )
--> 284 return io_open(file, *args, **kwargs)

FileNotFoundError: [Errno 2] No such file or directory: ''

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.

absolute_filepath = ''

# 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.

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.

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.

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.

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

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