There are 15 netCDF files in the folder OCEAN 215 - Autumn '20 - Course documents/Zoom class slides and notebooks/2020-12-01 - class #16 data/
Each netCDF file corresponds to a single profile made by Argo float number 5901105.
File names:
We will be reading temperature and salinity data from Argo netCDF files. We will be using the Gibbs Seawater Toolbox to convert the temperature and salinity into density ($\sigma_0$).
Then we will be plotting all of this data; at least one plot will rely on a colomap from cmocean.
# This code allows xarray and netCDF4 to work with Google Colab
!pip install netcdf4
# This code installs TEOS-10 gsw
!pip install gsw
# This code installs cmocean
!pip install cmocean
Collecting netcdf4 Downloading https://files.pythonhosted.org/packages/09/39/3687b2ba762a709cd97e48dfaf3ae36a78ae603ec3d1487f767ad58a7b2e/netCDF4-1.5.4-cp36-cp36m-manylinux1_x86_64.whl (4.3MB) |████████████████████████████████| 4.3MB 5.4MB/s Requirement already satisfied: numpy>=1.9 in /usr/local/lib/python3.6/dist-packages (from netcdf4) (1.18.5) Collecting cftime Downloading https://files.pythonhosted.org/packages/66/60/bad8525d2c046eb2964911bc412a85ba240b31c7b43f0c19377233992c6c/cftime-1.3.0-cp36-cp36m-manylinux1_x86_64.whl (295kB) |████████████████████████████████| 296kB 41.3MB/s Installing collected packages: cftime, netcdf4 Successfully installed cftime-1.3.0 netcdf4-1.5.4 Collecting gsw Downloading https://files.pythonhosted.org/packages/07/2d/b8a6d45211dac9cdb5019af825af532b9dde001eb9feec99f14147ce00b7/gsw-3.4.0-cp36-cp36m-manylinux2010_x86_64.whl (2.4MB) |████████████████████████████████| 2.4MB 5.7MB/s Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from gsw) (1.18.5) Installing collected packages: gsw Successfully installed gsw-3.4.0 Collecting cmocean Downloading https://files.pythonhosted.org/packages/48/02/d0f19b00b252fd972e3daec05be73aa811091528f21b90442a15d6a96d89/cmocean-2.0-py3-none-any.whl (223kB) |████████████████████████████████| 225kB 5.9MB/s Installing collected packages: cmocean Successfully installed cmocean-2.0
import numpy as np
import xarray as xr
import gsw
import cmocean
import matplotlib.pyplot as plt
from google.colab import drive
drive.mount('/content/drive/')
Mounted at /content/drive/
LATITUDE
), longitude (LONGITUDE
), temperature (TEMP
), salinity (PSAL
), and pressure (PRES
) from the data file.filepath = '/content/drive/MyDrive/OCEAN 215 - Autumn \'20/OCEAN 215 - Autumn \'20 - Course documents/Zoom class slides and notebooks/2020-12-01 - class #16 - data/D5901105_190.nc'
data = xr.open_dataset(filepath)
# display(data)
lat = data['LATITUDE'].values
lon = data['LONGITUDE'].values
T = data['TEMP'].values
S = data['PSAL'].values
P = data['PRES'].values
?
, find out the required inputs to each of these functions.
gsw.SA_from_SP
gsw.CT_from_t
gsw.sigma0
gsw.SA_from_SP?
gsw.CT_from_t?
gsw.sigma0?
#Required: SP, p, lon, lat
#Required: SA, t, p
#Required: SA, CT
SA = gsw.SA_from_SP(S,P,lon,lat)
CT = gsw.CT_from_t(SA,T,P)
sigma0 = gsw.sigma0(SA,CT)
dense_r
(you can find how to do this here: https://matplotlib.org/cmocean/ ). Make sure to properly label everything (and include a $\sigma_0$ on the colorbar label).fig = plt.figure(figsize=(8,8))
ax = plt.axes()
sctr = ax.scatter(S,T,c=sigma0,cmap=cmocean.cm.dense_r)
ax.set_xlabel('Salinity (PSU)')
ax.set_ylabel('Temperature (˚C)')
ax.set_title('T-S Diagram')
c = plt.colorbar(sctr,ax=ax)
c.set_label('$\sigma_0$ (kg/m$^3)$')
filenames
using string formatting in a for loop.filenames = []
floatnumber = 5901105
for index in range(176,191):
filenames.append('D{0}_{1:03d}.nc'.format(floatnumber,index))
LATITUDE
), longitude (LONGITUDE
), temperature (TEMP
), salinity (PSAL
), and pressure (PRES
). Make sure that your function is returning the data!def getdata(filepath,filename):
data = xr.open_dataset(filepath+filename)
lat = data['LATITUDE'].values
lon = data['LONGITUDE'].values
T = data['TEMP'].values
S = data['PSAL'].values
P = data['PRES'].values
return lat, lon, T, S, P
def calculate_density(lat,lon,T,S,P):
SA = gsw.SA_from_SP(S,P,lon,lat)
CT = gsw.CT_from_t(SA,T,P)
sigma0 = gsw.sigma0(SA,CT)
return sigma0
dense_r
(you can find how to do this here: https://matplotlib.org/cmocean/ ). Make sure to properly label everything (and include a $\sigma_0$ on the colorbar label). Double check that your plot title and filename will change between plots!def T_S_plot(T,S,sigma0,filepath,filename):
fig = plt.figure(figsize=(8,8))
ax = plt.axes()
sctr = ax.scatter(S,T,c=sigma0,cmap=cmocean.cm.dense_r)
ax.set_xlabel('Salinity (PSU)')
ax.set_ylabel('Temperature (˚C)')
ax.set_title('T-S Diagram:{}'.format(filename.split('.')[0]))
c = plt.colorbar(sctr,ax=ax)
c.set_label('$\sigma_0$ (kg/m$^3)$')
plt.savefig(filepath+filename.split('.')[0]+'_TSDiagram.png')
filepath = 'drive/MyDrive/OCEAN 215 - Autumn \'20/OCEAN 215 - Autumn \'20 - Course documents/Zoom class slides and notebooks/2020-12-01 - class #16 - data/'
for index in range(len(filenames)):
filename = filenames[index]
print(filename)
lat, lon, T, S, P = getdata(filepath,filename)
sigma0 = calculate_density(lat, lon, T, S, P)
T_S_plot(T,S,sigma0,'',filename)
D5901105_176.nc D5901105_177.nc D5901105_178.nc D5901105_179.nc D5901105_180.nc D5901105_181.nc D5901105_182.nc D5901105_183.nc D5901105_184.nc D5901105_185.nc D5901105_186.nc D5901105_187.nc D5901105_188.nc D5901105_189.nc D5901105_190.nc