xarray
Practice#
Part 1#
xarray
Data Structures#
import xarray as xr
The relative filepath for the SST dataset we saw in the lesson is
../data/oisst-avhrr-v02r01.20220304.nc
. Use theopen_dataset()
function to access the file.
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?
Display just the
ice
data variable
Indexing and Selecting Values#
Select only the SST data between 30-48 North and 3-30 East
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]
)
Write a line of code to select the 1st date from the
ndvi
data array.
Write a line of code to select the date
2022-03-01
from thendvi
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.
To help you determine the filepaths you can use the provided laminated sheet with the filepath on it. For each path, do the following:
Circle the file that you are finding the path to
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.
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
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
\
.Add the name of the file to the end of the filepath
Absolute Filepaths#
geopandas.ipynb
shrubland_change_Jan2022-Dec2006.mp4
dramatic_chipmunk.gif
Relative Filepaths#
example_code.py
->dramatic_chipmunk.gif
geopandas.ipynb
->CAcountymap.geojson
extract_features.ipynb
->aviris_f1806t01p00r02_img
geopandas.ipynb
->aviris_f1806t01p00r02_img
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 /srv/conda/envs/notebook/lib/python3.11/site-packages/IPython/core/interactiveshell.py:310, in _modified_open(file, *args, **kwargs)
303 if file in {0, 1, 2}:
304 raise ValueError(
305 f"IPython won't let you open fd={file} by default "
306 "as it is likely to crash IPython. If you know what you are doing, "
307 "you can use builtins' open."
308 )
--> 310 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.
Hint
Watch the units on longitude. What are the min and max values of the longitude coordinate array?
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:
You are using a DataArray, not a Dataset
You are giving it a 2d slice of data, where the 2 dimensions are latitude and longitude
Hint
Watch your data structure. Recall that .plot()
works on a DataArray, not a Dataset.
Question 3#
How many dimensions should your output array have if you did each of the following:
selected the 1st time value
selected the 1st time value and the 100th to the 200th latitude values
selected the 100th to the 200th latitude values and the 1000th to 1200th longitude values
selected the 1st time value, the 1st elevation value, the 100th to the 200th latitude values and the 1000th to 1200th longitude values
selected the 1st time value, 1st elevation value, the 1st latitude value and the 1st longitude value