Python - plotting a profile#
Aim: To work with vertical profile data and make some standard calculations.
Learning outcomes: At the end of this lab, you will be able to:
Make a vertical profile plot of temperature and salinity data
Adjust figure settings
Data: You will work with one *.nc
file from the MSM121 cruise off of 47°N.
Directions: Create an *.ipynb
and 1 figure. This will form your answer to question 1 of the exercise sheet.
Start a new code cell.#
Create an *.ipynb
containing the commands for this assignment, or rename this file, e.g., computing-regoz-2-<Lastname>.ipynb
Open the notebook in jupyter#
Import your packages in a first code cell.
import matplotlib.pyplot as plt
import gsw
import numpy as np
import xarray as xr
Note that if you are having trouble importing one of the packages, you will need to install that package. If the only package you cannot import is gsw
then you can do most of this exercise without that package.
# Your code here
Load the data file with xarray
#
You may want to read the docs: https://docs.xarray.dev/en/stable/generated/xarray.open_dataset.html
# Your code here
filepath = ''
Take a look at your data#
Recall the print
command.
What do the data look like?#
What are the variable names for temperature? For salinity? for depth or pressure?
How long are the variables? You can see this from the print
or by using the len
command.
Create a basic profile plot#
Here we will use commands from the package matplotlib
to make some basic plots.
Some helpful links:
Getting started (quickstart): https://matplotlib.org/stable/users/explain/quick_start.html
Remembering command names (cheatsheet): https://matplotlib.org/cheatsheets/
Setting up the plot#
plt.figure(1)
plt.clf()
plt.subplot(1,2,1)
Find out your variable names#
You used xarray
above to load your *.nc
file containing vertical profile data. You now need to know what the variable names within the dataArray are. Try using the print
command to find out the exact variable name for pressure, temperature and salinity.
Note that there are a lot of different ways to name these variables.
plt.plot(DA.salinity,DA.pressure)
plt.xlabel('Practical salinity [ ]')
plt.ylabel('Pressure [dbar]')
plt.gca().invert_yaxis() # Question: What happens if you comment out this line?
Note that in the above commands, it assumes you named your dataset DA
(when you loaded the *.nc
file using xarray
above). In the plot command, you need to replace DA
with whatever you chose to name your dataset. Additionally, the variable names for variables contained with the DA
dataset are written in the plot command above as salinity
and pressure
, but you will need to replace these with whatever you found the variables names to be when you did a print(DA)
on your dataset.
Modify your plot:
Add grid lines
Limit the top of the plot to the surface (p=0)
Add a second plot for temperature, to the right of the salinity plot
Try the command:
fig, (ax1, ax2) = plt.subplots(2,1)
What does this do for you?
Try adding a title to your figure. This could be the station number, or location (lat/lon) or your name. For this exercise, the only purpose of adding a title is so you find the command for title.
Print the figure to a
*.png
file with a useful name for sending in as part of your assignment, likeregoz-fig1-<Lastname>.png
where you replace<Lastname>
with your last name.
Try the command:
fig.savefig("regoz-fig1-Lastname.png", dpi=300)
# Your code here
GSW: Calculate TEOS-10 parameters#
Calculate absolute salinity and conservative temperature
(Optional) Repeat for both primary and secondary channels
You will need to use the Gibbs Seawater toolbox (gsw). See the docs: https://teos-10.github.io/GSW-Python/conversions.html and look for a function that says
CT_from_t
which means, compute conservative temperature from temperature.SA_from_SP
which means, compute absolute salinity from practical salinity.
Note: if you were unable to load the gsw
package, then you can skip this step for now.
Once you’ve calculated absolute salinity and conservative temperature, repeat your profile plot from above, and save it as a png
file.
# Your code here