xarray
: World Ocean Atlas global ocean temperatures¶# Import NumPy, Pandas, xarray, Matplotlib, and datetime at the top of your code
import numpy as np
import pandas as pd
import xarray as xr
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
# Download netCDF4 library
!pip install netcdf4 # You can comment this out once it has run
# Give Colab access to Google Drive
from google.colab import drive
drive.mount('/content/drive')
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 2.7MB/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/81/f4/31cb9b65f462ea960bd334c5466313cb7b8af792f272546b68b7868fccd4/cftime-1.2.1-cp36-cp36m-manylinux1_x86_64.whl (287kB) |████████████████████████████████| 296kB 36.2MB/s Installing collected packages: cftime, netcdf4 Successfully installed cftime-1.2.1 netcdf4-1.5.4 Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
# Filepath for World Ocean Atlas 2018 (WOA18) temperature netCDF file
# Note: you may need to change this to match your own filepath,
# which you can get by opening the left sidebar (folder icon),
# navigating to the file, clicking the "..." on the file, and
# selecting "Copy path"
filepath = '/content/drive/My Drive/OCEAN 215 - Autumn \'20/OCEAN 215 - Autumn \'20 - Course documents/Zoom class slides and notebooks/2020-11-10 - class #11 - data/woa18_temp.nc'
- Choose one person to write code and share their screen.
- Choose a different person to take notes on the answers to report back to the class.
xr.open_dataset()
.# Load the data file from Google Drive as an xarray Dataset
temp = xr.open_dataset(filepath)
# View data and stats
display(temp)
# Answers
# a. 2 variables
# b. Average temperature and number of observations per grid cell
# c. 1960-2011
# d. 1°
# e. 5500 m
array([-89.5, -88.5, -87.5, -86.5, -85.5, -84.5, -83.5, -82.5, -81.5, -80.5, -79.5, -78.5, -77.5, -76.5, -75.5, -74.5, -73.5, -72.5, -71.5, -70.5, -69.5, -68.5, -67.5, -66.5, -65.5, -64.5, -63.5, -62.5, -61.5, -60.5, -59.5, -58.5, -57.5, -56.5, -55.5, -54.5, -53.5, -52.5, -51.5, -50.5, -49.5, -48.5, -47.5, -46.5, -45.5, -44.5, -43.5, -42.5, -41.5, -40.5, -39.5, -38.5, -37.5, -36.5, -35.5, -34.5, -33.5, -32.5, -31.5, -30.5, -29.5, -28.5, -27.5, -26.5, -25.5, -24.5, -23.5, -22.5, -21.5, -20.5, -19.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5, -9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5, 24.5, 25.5, 26.5, 27.5, 28.5, 29.5, 30.5, 31.5, 32.5, 33.5, 34.5, 35.5, 36.5, 37.5, 38.5, 39.5, 40.5, 41.5, 42.5, 43.5, 44.5, 45.5, 46.5, 47.5, 48.5, 49.5, 50.5, 51.5, 52.5, 53.5, 54.5, 55.5, 56.5, 57.5, 58.5, 59.5, 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5, 79.5, 80.5, 81.5, 82.5, 83.5, 84.5, 85.5, 86.5, 87.5, 88.5, 89.5], dtype=float32)
array([-179.5, -178.5, -177.5, ..., 177.5, 178.5, 179.5], dtype=float32)
array([0.00e+00, 5.00e+00, 1.00e+01, 1.50e+01, 2.00e+01, 2.50e+01, 3.00e+01, 3.50e+01, 4.00e+01, 4.50e+01, 5.00e+01, 5.50e+01, 6.00e+01, 6.50e+01, 7.00e+01, 7.50e+01, 8.00e+01, 8.50e+01, 9.00e+01, 9.50e+01, 1.00e+02, 1.25e+02, 1.50e+02, 1.75e+02, 2.00e+02, 2.25e+02, 2.50e+02, 2.75e+02, 3.00e+02, 3.25e+02, 3.50e+02, 3.75e+02, 4.00e+02, 4.25e+02, 4.50e+02, 4.75e+02, 5.00e+02, 5.50e+02, 6.00e+02, 6.50e+02, 7.00e+02, 7.50e+02, 8.00e+02, 8.50e+02, 9.00e+02, 9.50e+02, 1.00e+03, 1.05e+03, 1.10e+03, 1.15e+03, 1.20e+03, 1.25e+03, 1.30e+03, 1.35e+03, 1.40e+03, 1.45e+03, 1.50e+03, 1.55e+03, 1.60e+03, 1.65e+03, 1.70e+03, 1.75e+03, 1.80e+03, 1.85e+03, 1.90e+03, 1.95e+03, 2.00e+03, 2.10e+03, 2.20e+03, 2.30e+03, 2.40e+03, 2.50e+03, 2.60e+03, 2.70e+03, 2.80e+03, 2.90e+03, 3.00e+03, 3.10e+03, 3.20e+03, 3.30e+03, 3.40e+03, 3.50e+03, 3.60e+03, 3.70e+03, 3.80e+03, 3.90e+03, 4.00e+03, 4.10e+03, 4.20e+03, 4.30e+03, 4.40e+03, 4.50e+03, 4.60e+03, 4.70e+03, 4.80e+03, 4.90e+03, 5.00e+03, 5.10e+03, 5.20e+03, 5.30e+03, 5.40e+03, 5.50e+03], dtype=float32)
array(['1960-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000', '1980-01-01T00:00:00.000000000', '1990-01-01T00:00:00.000000000', '2000-01-01T00:00:00.000000000', '2011-01-01T00:00:00.000000000'], dtype='datetime64[ns]')
[39657600 values with dtype=float32]
[39657600 values with dtype=float64]
Steps to work on as a group:
.isel()
or .sel()
to do this.xarray
format to a single float number.Steps that we'll discuss as a class:
time_series
. (In other words, select in latitude, longitude, and depth, leaving a single dimension: time.)depth_profile
. (In other words, select in latitude, longitude, and time, leaving a single dimension: depth.)display()
. How would we convert these from xarray
format to 1-D NumPy arrays?# Write code here:
# -35°N, 110°E
temp.sel(depth=0,lat=-35,lon=110,time=datetime(2011,1,1),method='nearest')['t_an'].item()
time_series = temp.sel(depth=0,lat=46,lon=-128,method='nearest')['t_an']
display(time_series)
depth_profile = temp.sel(lat=46,lon=-128,time=datetime(2011,1,1),method='nearest')['t_an']
display(depth_profile)
array([12.13939 , 11.557309, 12.053809, 12.469 , 12.54699 , 12.28359 ], dtype=float32)
array(46.5, dtype=float32)
array(-127.5, dtype=float32)
array(0., dtype=float32)
array(['1960-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000', '1980-01-01T00:00:00.000000000', '1990-01-01T00:00:00.000000000', '2000-01-01T00:00:00.000000000', '2011-01-01T00:00:00.000000000'], dtype='datetime64[ns]')
array([12.28359 , 12.236891, 12.12149 , 11.995391, 11.823 , 11.56039 , 11.2394 , 10.86769 , 10.48499 , 10.13469 , 9.80599 , 9.45741 , 9.22761 , 9.02841 , 8.845091, 8.67829 , 8.51851 , 8.385091, 8.262091, 8.151409, 8.057509, 7.78181 , 7.60519 , 7.3891 , 7.13661 , 6.87709 , 6.62841 , 6.39269 , 6.17411 , 5.97299 , 5.79171 , 5.62489 , 5.4736 , 5.3348 , 5.20839 , 5.09211 , 4.98801 , 4.78741 , 4.60779 , 4.44011 , 4.28301 , 4.1431 , 3.99991 , 3.86751 , 3.74121 , 3.61401 , 3.49789 , 3.3786 , 3.26571 , 3.15461 , 3.05081 , 2.9508 , 2.8504 , 2.75689 , 2.6666 , 2.58041 , 2.49761 , 2.41569 , 2.34019 , 2.26849 , 2.20359 , 2.1448 , 2.09019 , 2.03921 , 1.99301 , 1.95381 , 1.91519 , 1.86401 , 1.8251 , 1.79659 , 1.7768 , 1.76599 , 1.76079 , 1.76041 , 1.765 , nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan], dtype=float32)
array(46.5, dtype=float32)
array(-127.5, dtype=float32)
array([0.00e+00, 5.00e+00, 1.00e+01, 1.50e+01, 2.00e+01, 2.50e+01, 3.00e+01, 3.50e+01, 4.00e+01, 4.50e+01, 5.00e+01, 5.50e+01, 6.00e+01, 6.50e+01, 7.00e+01, 7.50e+01, 8.00e+01, 8.50e+01, 9.00e+01, 9.50e+01, 1.00e+02, 1.25e+02, 1.50e+02, 1.75e+02, 2.00e+02, 2.25e+02, 2.50e+02, 2.75e+02, 3.00e+02, 3.25e+02, 3.50e+02, 3.75e+02, 4.00e+02, 4.25e+02, 4.50e+02, 4.75e+02, 5.00e+02, 5.50e+02, 6.00e+02, 6.50e+02, 7.00e+02, 7.50e+02, 8.00e+02, 8.50e+02, 9.00e+02, 9.50e+02, 1.00e+03, 1.05e+03, 1.10e+03, 1.15e+03, 1.20e+03, 1.25e+03, 1.30e+03, 1.35e+03, 1.40e+03, 1.45e+03, 1.50e+03, 1.55e+03, 1.60e+03, 1.65e+03, 1.70e+03, 1.75e+03, 1.80e+03, 1.85e+03, 1.90e+03, 1.95e+03, 2.00e+03, 2.10e+03, 2.20e+03, 2.30e+03, 2.40e+03, 2.50e+03, 2.60e+03, 2.70e+03, 2.80e+03, 2.90e+03, 3.00e+03, 3.10e+03, 3.20e+03, 3.30e+03, 3.40e+03, 3.50e+03, 3.60e+03, 3.70e+03, 3.80e+03, 3.90e+03, 4.00e+03, 4.10e+03, 4.20e+03, 4.30e+03, 4.40e+03, 4.50e+03, 4.60e+03, 4.70e+03, 4.80e+03, 4.90e+03, 5.00e+03, 5.10e+03, 5.20e+03, 5.30e+03, 5.40e+03, 5.50e+03], dtype=float32)
array('2011-01-01T00:00:00.000000000', dtype='datetime64[ns]')
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=(14,5))
ax1.plot(time_series['time'].values,time_series,c='k')
ax1.set_xlabel('Time')
ax1.set_ylabel('Temperature (°C)')
ax1.set_title('Time series')
ax1.grid()
ax2.plot(depth_profile.values,depth_profile['depth'],c='k')
ax2.invert_yaxis()
ax2.set_xlabel('Temperature (°C)')
ax2.set_ylabel('Depth (m)')
ax2.set_title('Profile')
ax2.grid()
plt.savefig()
temperature = temp['t_an'].values.flatten()
print(temperature.shape)
fig = plt.figure(figsize=(10,10))
p = plt.hist(temperature,bins=50, rwidth=0.85)
temp_ave = np.nanmean(temperature)
temp_std = np.nanstd(temperature)
plt.plot([temp_ave,temp_ave],[0,2.7e6],c='k',lw=2)
plt.text(temp_ave,1.5e6,'Average='+str(round(temp_ave,2))+'˚C')
plt.plot([temp_ave+temp_std,temp_ave+temp_std],[0,2.7e6],c ='k',ls='--',lw=2)
plt.plot([temp_ave-temp_std,temp_ave-temp_std],[0,2.7e6],c ='k',ls='--',lw=2)
plt.grid()
plt.ylim((0,2.7e6))
plt.xlabel('Temperatures ˚C',fontsize=14)
plt.ylabel('Counts',fontsize=14)
plt.title('WOA temperatures',fontsize=14)
plt.savefig('drive/My Drive/WOA_hist.png')
(39657600,)
/usr/local/lib/python3.6/dist-packages/numpy/lib/histograms.py:839: RuntimeWarning: invalid value encountered in greater_equal keep = (tmp_a >= first_edge) /usr/local/lib/python3.6/dist-packages/numpy/lib/histograms.py:840: RuntimeWarning: invalid value encountered in less_equal keep &= (tmp_a <= last_edge)
Below, you are given code from the lesson #11 code demo video. However, this code is filled with 18 different errors.
# Import statements and drive mount
import numpy as np
import matplotlib.pyplot as pl
from google.drive import drive
drive.mount('/content/drive')
# Where the file is and its name
folderpath= '/content/drive/My Drive/Data_folder/'
filename = 'Dissolved Inorganic Nutrients.csv
# Read the data
data = pd.read_csv(folderpath + filname, parse_dates=[1], na_values=-999)
data.describe
# Clean the data and take averages for each day of data
nitrate = data['Nitrite and Nitrate (µmol/L)']
data['Nitrite and Nitrate (µmol/L)'] = nitrate.where(nitrate>0)
cleandata = data.groupby('Datetime GMT').mean
# Assign variable names and change to np arrays
P = cleandata['Phosphate (µmol/L)'.values
S = clandata['Silicate (µmol/L)'].values
N = cleandata["Nitrite and Nitrate (µmol/L)'].values
dates = cleandata.index.values
# Make a figure and plot the data
fig = plt.figure(figsize=(10,10))
ax.plot(data,S,'s-',c='mediumblue',label='S')
ax.plot(dates,N,'*-',c='darkgreen',label='N')
ax.plot(datas,P,'o-',c='firebrick',label='P')
# Format the figure
plt.set_xlabel('Dates')
plt.set_ylabel('Concentration (µmol/L)")
plt.title('Inorganic nutrients')
plt.grid()
plt.yscale('log')
plt.gca.legend()
# Save the figure
plt.savefig(folderpath 'lineplots.png')