import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
import glob
import xarray as xr
import grid
%matplotlib inline
base = '/ocean/nsoontie/MEOPAR/LiveOcean/*/*UBC.nc'
files = glob.glob(base)
files.sort()
d = xr.open_dataset(files[0])
for f in files[1:]:
with xr.open_dataset(f) as d1:
d =xr.concat([d,d1],dim='ocean_time',data_vars='minimal')
j=10
i=10
salt_ts = d.salt.sel(eta_rho=j,xi_rho=i)
fig, ax = plt.subplots(figsize=(10, 5))
salt_ts.plot(x='ocean_time', y='s_rho',ax=ax)
<matplotlib.collections.QuadMesh at 0x7f702e1dffd0>
## Using sel in vertical instead of isel to select based on value
t = 0
s = 0
fig, ax = plt.subplots(figsize=(5, 5))
salt_surface = (
d.salt.isel(ocean_time=t)
.sel(s_rho=s,method='nearest')
)
salt_surface.plot(x='lon_rho', y='lat_rho',ax=ax)
ax.plot(d.lon_rho.values[j,i], d.lat_rho.values[j,i], 'ro')
[<matplotlib.lines.Line2D at 0x7f702dd69588>]
## Using isel in vertical instead of sel to select based on index
t = 0
s = 0
fig, ax = plt.subplots(figsize=(5, 5))
salt_bottom = (
d.salt.isel(ocean_time=t,s_rho=s)
)
salt_bottom.plot(x='lon_rho', y='lat_rho',ax=ax)
ax.plot(i,j, 'ro')
[<matplotlib.lines.Line2D at 0x7f702dcdbda0>]
Be careful! There is a dimension and a variable named s_rho:
d.s_rho
<xarray.DataArray 's_rho' (s_rho: 40)> array([-0.9875, -0.9625, -0.9375, -0.9125, -0.8875, -0.8625, -0.8375, -0.8125, -0.7875, -0.7625, -0.7375, -0.7125, -0.6875, -0.6625, -0.6375, -0.6125, -0.5875, -0.5625, -0.5375, -0.5125, -0.4875, -0.4625, -0.4375, -0.4125, -0.3875, -0.3625, -0.3375, -0.3125, -0.2875, -0.2625, -0.2375, -0.2125, -0.1875, -0.1625, -0.1375, -0.1125, -0.0875, -0.0625, -0.0375, -0.0125]) Coordinates: * s_rho (s_rho) float64 -0.9875 -0.9625 -0.9375 -0.9125 -0.8875 -0.8625 ... Attributes: standard_name: ocean_s_coordinate_g1 valid_min: -1.0 valid_max: 0.0 field: s_rho, scalar long_name: S-coordinate at RHO-points positive: up formula_terms: s: s_rho C: Cs_r eta: zeta depth: h depth_c: hc
salt_surf_daily_avg = (
d.salt.sel(s_rho=s,method='nearest')).resample('1D',dim='ocean_time')
g_simple = salt_surf_daily_avg.plot(x='lon_rho', y='lat_rho', col='ocean_time', col_wrap=6)
Because of the way I orgnanized the files, the first panel only has 23 hours of results and the last panel has one hour. It maybe be better to time_slice and then daily average.
Can I add a depth coordinate (z_rho) to the xarray dataset?
import grid
#build depth
fname = '/ocean/nsoontie/MEOPAR/LiveOcean/20160601/ocean_his_0002_UBC.nc'
G, S, T = grid.get_basic_info(fname)
z_rho = np.zeros(d.salt.shape)
for t in np.arange(z_rho.shape[0]):
zeta = d.zeta.values[t,:,:]
z_rho[t,:,:,:] = grid.get_z(G['h'],zeta,S)
#quick peak
times, _ = np.meshgrid(np.arange(d.ocean_time.shape[0]),np.arange(z_rho.shape[1]))
plt.pcolormesh(times.T/24,z_rho[:,:,j,i],d.salt.values[:,:,j,i],cmap='viridis')
cbar=plt.colorbar()
plt.ylabel('Depth [m]')
plt.xlabel('Time [day]')
cbar.set_label('salt')
plt.axis([0,33,-310,5])
plt.title('lon: {:.3f} lat: {:.3f}'.format(d.lon_rho.values[j,i],d.lat_rho.values[j,i]))
<matplotlib.text.Text at 0x7f7029252978>
#build a z_rho data array
zrho_DA = xr.DataArray(z_rho, dims = ['ocean_time','s_rho', 'eta_rho', 'xi_rho'],
coords = {'ocean_time': d.ocean_time.values[:],
's_rho': d.s_rho.values[:],
'eta_rho': d.eta_rho.values[:],
'xi_rho': d.xi_rho.values[:]},
attrs = {'units': 'metres',
'positive': 'up',
'long_name': 'Depth at s-levels',
'field': 'z_rho ,scalar'})
#add z_rho to dataset
d = d.assign(z_rho = zrho_DA)
d = d.set_coords('z_rho') #make a coordinate
d.z_rho
<xarray.DataArray 'z_rho' (ocean_time: 792, s_rho: 40, eta_rho: 31, xi_rho: 26)> array([[[[ -2.02345870e+02, -2.35220118e+02, -2.44251343e+02, ..., nan, nan, nan], [ -2.07595682e+02, -2.23752651e+02, -1.99842358e+02, ..., nan, nan, nan], [ -2.38688055e+02, -2.41360546e+02, -1.97477155e+02, ..., nan, nan, -6.73887899e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97913595e+02, -2.30067128e+02, -2.38900355e+02, ..., nan, nan, nan], [ -2.03048342e+02, -2.18851077e+02, -1.95464947e+02, ..., nan, nan, nan], [ -2.33459144e+02, -2.36072998e+02, -1.93151641e+02, ..., nan, nan, -6.59158709e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93681109e+02, -2.25146416e+02, -2.33790569e+02, ..., nan, nan, nan], [ -1.98705979e+02, -2.14170448e+02, -1.91284853e+02, ..., nan, nan, nan], [ -2.28465932e+02, -2.31023792e+02, -1.89021104e+02, ..., nan, nan, -6.45093454e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.47022209e+00, -2.84355052e+00, -2.94590543e+00, ..., nan, nan, nan], [ -2.53116048e+00, -2.71378342e+00, -2.44087313e+00, ..., nan, nan, nan], [ -2.88776077e+00, -2.91595957e+00, -2.41595261e+00, ..., nan, nan, -9.66787331e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.43739125e+00, -1.64277551e+00, -1.69899167e+00, ..., nan, nan, nan], [ -1.47151683e+00, -1.57159446e+00, -1.42082721e+00, ..., nan, nan, nan], [ -1.66929411e+00, -1.68382888e+00, -1.40799979e+00, ..., nan, nan, -6.23560465e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -5.59287393e-01, -6.21886961e-01, -6.38876353e-01, ..., nan, nan, nan], [ -5.70616947e-01, -6.00515271e-01, -5.53592970e-01, ..., nan, nan, nan], [ -6.33364269e-01, -6.36281994e-01, -5.51046992e-01, ..., nan, nan, -3.31751946e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], [[[ -2.02340701e+02, -2.35214973e+02, -2.44246220e+02, ..., nan, nan, nan], [ -2.07590527e+02, -2.23747512e+02, -1.99837241e+02, ..., nan, nan, nan], [ -2.38682892e+02, -2.41355391e+02, -1.97472003e+02, ..., nan, nan, -6.73845743e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97898508e+02, -2.30052116e+02, -2.38885404e+02, ..., nan, nan, nan], [ -2.03033298e+02, -2.18836081e+02, -1.95450015e+02, ..., nan, nan, nan], [ -2.33444079e+02, -2.36057953e+02, -1.93136605e+02, ..., nan, nan, -6.59035688e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93656552e+02, -2.25121981e+02, -2.33766233e+02, ..., nan, nan, nan], [ -1.98681491e+02, -2.14146038e+02, -1.91260549e+02, ..., nan, nan, nan], [ -2.28441411e+02, -2.30999303e+02, -1.88996631e+02, ..., nan, nan, -6.44893215e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.01783983e+00, -2.39341695e+00, -2.49759299e+00, ..., nan, nan, nan], [ -2.08005438e+00, -2.26410107e+00, -1.99314513e+00, ..., nan, nan, nan], [ -2.43604568e+00, -2.46482868e+00, -1.96511994e+00, ..., nan, nan, -5.97908902e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -9.82698088e-01, -1.19034252e+00, -1.24838911e+00, ..., nan, nan, nan], [ -1.01810634e+00, -1.11961499e+00, -9.70812078e-01, ..., nan, nan, nan], [ -1.21527151e+00, -1.23039346e+00, -9.54864118e-01, ..., nan, nan, -2.52797690e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.02629510e-01, -1.67499019e-01, -1.86326745e-01, ..., nan, nan, nan], [ -1.15247284e-01, -1.46582808e-01, -1.01633336e-01, ..., nan, nan, nan], [ -1.77379854e-01, -1.80887300e-01, -9.59533380e-02, ..., nan, nan, 4.06128844e-02], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], [[[ -2.02335874e+02, -2.35210135e+02, -2.44241406e+02, ..., nan, nan, nan], [ -2.07585691e+02, -2.23742675e+02, -1.99832432e+02, ..., nan, nan, nan], [ -2.38678050e+02, -2.41350570e+02, -1.97467221e+02, ..., nan, nan, -6.73804468e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97884422e+02, -2.30037998e+02, -2.38871355e+02, ..., nan, nan, nan], [ -2.03019186e+02, -2.18821963e+02, -1.95435981e+02, ..., nan, nan, nan], [ -2.33429948e+02, -2.36043885e+02, -1.93122649e+02, ..., nan, nan, -6.58915237e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93633624e+02, -2.25099000e+02, -2.33743366e+02, ..., nan, nan, nan], [ -1.98658522e+02, -2.14123059e+02, -1.91237706e+02, ..., nan, nan, nan], [ -2.28418410e+02, -2.30976405e+02, -1.88973915e+02, ..., nan, nan, -6.44697156e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -1.59547173e+00, -1.97006182e+00, -2.07634590e+00, ..., nan, nan, nan], [ -1.65691737e+00, -1.84079529e+00, -1.57233964e+00, ..., nan, nan, nan], [ -2.01231929e+00, -2.04300012e+00, -1.54664370e+00, ..., nan, nan, -2.36733276e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -5.58172396e-01, -7.64824757e-01, -8.24990155e-01, ..., nan, nan, nan], [ -5.92807809e-01, -6.94146839e-01, -5.47856976e-01, ..., nan, nan, nan], [ -7.89380600e-01, -8.06410070e-01, -5.34250167e-01, ..., nan, nan, 1.10222935e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ 3.23730545e-01, 2.59857393e-01, 2.38901700e-01, ..., nan, nan, nan], [ 3.11888951e-01, 2.80723780e-01, 3.23149343e-01, ..., nan, nan, nan], [ 2.50351322e-01, 2.44928114e-01, 3.26478075e-01, ..., nan, nan, 4.05202111e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], ..., [[[ -2.02338788e+02, -2.35213065e+02, -2.44244268e+02, ..., nan, nan, nan], [ -2.07588693e+02, -2.23745692e+02, -1.99835371e+02, ..., nan, nan, nan], [ -2.38681145e+02, -2.41353633e+02, -1.97470220e+02, ..., nan, nan, -6.73827084e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97892927e+02, -2.30046548e+02, -2.38879708e+02, ..., nan, nan, nan], [ -2.03027947e+02, -2.18830769e+02, -1.95444558e+02, ..., nan, nan, nan], [ -2.33438980e+02, -2.36052823e+02, -1.93131400e+02, ..., nan, nan, -6.58981236e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93647468e+02, -2.25112917e+02, -2.33756962e+02, ..., nan, nan, nan], [ -1.98672781e+02, -2.14137392e+02, -1.91251666e+02, ..., nan, nan, nan], [ -2.28433112e+02, -2.30990953e+02, -1.88988158e+02, ..., nan, nan, -6.44804582e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -1.85049030e+00, -2.22643290e+00, -2.32681199e+00, ..., nan, nan, nan], [ -1.91960129e+00, -2.10483744e+00, -1.82951328e+00, ..., nan, nan, nan], [ -2.28315910e+00, -2.31101698e+00, -1.80903831e+00, ..., nan, nan, -4.34631258e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -8.14493681e-01, -1.02250546e+00, -1.07673570e+00, ..., nan, nan, nan], [ -8.56833601e-01, -9.59537793e-01, -8.06344342e-01, ..., nan, nan, nan], [ -1.06160394e+00, -1.07579605e+00, -7.97985173e-01, ..., nan, nan, -8.86859721e-02], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ 6.63017026e-02, 1.06325980e-03, -1.39316343e-02, ..., nan, nan, nan], [ 4.67223118e-02, 1.41860790e-02, 6.35450603e-02, ..., nan, nan, nan], [ -2.30482893e-02, -2.56218739e-02, 6.16034767e-02, ..., nan, nan, 2.05433724e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], [[[ -2.02342845e+02, -2.35217127e+02, -2.44248323e+02, ..., nan, nan, nan], [ -2.07592743e+02, -2.23749741e+02, -1.99839418e+02, ..., nan, nan, nan], [ -2.38685177e+02, -2.41357673e+02, -1.97474252e+02, ..., nan, nan, -6.73849728e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97904766e+02, -2.30058401e+02, -2.38891540e+02, ..., nan, nan, nan], [ -2.03039764e+02, -2.18842585e+02, -1.95456368e+02, ..., nan, nan, nan], [ -2.33450747e+02, -2.36064613e+02, -1.93143168e+02, ..., nan, nan, -6.59047318e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93666738e+02, -2.25132211e+02, -2.33776221e+02, ..., nan, nan, nan], [ -1.98692016e+02, -2.14156625e+02, -1.91270890e+02, ..., nan, nan, nan], [ -2.28452265e+02, -2.31010144e+02, -1.89007313e+02, ..., nan, nan, -6.44912144e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.20548210e+00, -2.58186235e+00, -2.68159061e+00, ..., nan, nan, nan], [ -2.27393505e+00, -2.45914438e+00, -2.18365057e+00, ..., nan, nan, nan], [ -2.63598068e+00, -2.66454362e+00, -2.16191058e+00, ..., nan, nan, -6.32780859e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.17129889e+00, -1.37975056e+00, -1.43332664e+00, ..., nan, nan, nan], [ -1.21297741e+00, -1.31565465e+00, -1.16229067e+00, ..., nan, nan, nan], [ -1.41622785e+00, -1.43112861e+00, -1.15266003e+00, ..., nan, nan, -2.87847784e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -2.92045251e-01, -3.57725482e-01, -3.72063394e-01, ..., nan, nan, nan], [ -3.10960387e-01, -3.43469544e-01, -2.93939303e-01, ..., nan, nan, nan], [ -3.79204514e-01, -3.82489820e-01, -2.94603917e-01, ..., nan, nan, 5.41133999e-03], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], [[[ -2.02346851e+02, -2.35221157e+02, -2.44252405e+02, ..., nan, nan, nan], [ -2.07596765e+02, -2.23753772e+02, -1.99843472e+02, ..., nan, nan, nan], [ -2.38689174e+02, -2.41361681e+02, -1.97478268e+02, ..., nan, nan, -6.73872976e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97916457e+02, -2.30070162e+02, -2.38903454e+02, ..., nan, nan, nan], [ -2.03051503e+02, -2.18854349e+02, -1.95468197e+02, ..., nan, nan, nan], [ -2.33462412e+02, -2.36076308e+02, -1.93154888e+02, ..., nan, nan, -6.59115159e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93685768e+02, -2.25151354e+02, -2.33795613e+02, ..., nan, nan, nan], [ -1.98711123e+02, -2.14175773e+02, -1.91290144e+02, ..., nan, nan, nan], [ -2.28471251e+02, -2.31029181e+02, -1.89026389e+02, ..., nan, nan, -6.45022569e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.55604723e+00, -2.93451922e+00, -3.03883973e+00, ..., nan, nan, nan], [ -2.62593181e+00, -2.81188227e+00, -2.53833669e+00, ..., nan, nan, nan], [ -2.98574908e+00, -3.01523115e+00, -2.51331224e+00, ..., nan, nan, -8.36203116e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.52365481e+00, -1.73420890e+00, -1.79240071e+00, ..., nan, nan, nan], [ -1.56677228e+00, -1.67019444e+00, -1.51878864e+00, ..., nan, nan, nan], [ -1.76778297e+00, -1.78360757e+00, -1.50585676e+00, ..., nan, nan, -4.92309185e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -6.45923696e-01, -7.13715436e-01, -7.32689005e-01, ..., nan, nan, nan], [ -6.66283996e-01, -6.99541291e-01, -6.51977691e-01, ..., nan, nan, nan], [ -7.32278698e-01, -7.36491827e-01, -6.49326801e-01, ..., nan, nan, -1.99933533e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]]]) Coordinates: lon_rho (eta_rho, xi_rho) float64 -125.0 -125.0 -125.0 -124.9 -124.9 ... lat_rho (eta_rho, xi_rho) float64 48.32 48.32 48.32 48.32 48.32 ... * s_rho (s_rho) float64 -0.9875 -0.9625 -0.9375 -0.9125 -0.8875 ... * eta_rho (eta_rho) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ... * xi_rho (xi_rho) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ... * ocean_time (ocean_time) datetime64[ns] 2016-06-01T01:00:00 ... z_rho (ocean_time, s_rho, eta_rho, xi_rho) float64 -202.3 -235.2 ... Attributes: positive: up long_name: Depth at s-levels field: z_rho ,scalar units: metres
# Select a depth
salt_100m = d.sel(z_rho=100, method='nearest')
salt_100m.isel(ocean_time=0).plot(x='lon_rho',y='lat_rho')
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-14-e2f140a35102> in <module>() 1 # Select a depth ----> 2 salt_100m = d.sel(z_rho=100, method='nearest') 3 salt_100m.isel(ocean_time=0).plot(x='lon_rho',y='lat_rho') /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/core/dataset.py in sel(self, method, tolerance, **indexers) 974 """ 975 return self.isel(**indexing.remap_label_indexers( --> 976 self, indexers, method=method, tolerance=tolerance)) 977 978 def isel_points(self, dim='points', **indexers): /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance) 189 return dict((dim, convert_label_indexer(data_obj[dim].to_index(), label, 190 dim, method, tolerance)) --> 191 for dim, label in iteritems(indexers)) 192 193 /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/core/indexing.py in <genexpr>(.0) 189 return dict((dim, convert_label_indexer(data_obj[dim].to_index(), label, 190 dim, method, tolerance)) --> 191 for dim, label in iteritems(indexers)) 192 193 /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/core/dataarray.py in to_index(self) 376 arrays. 377 """ --> 378 return self.variable.to_index() 379 380 @property /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/core/variable.py in to_index(self) 304 def to_index(self): 305 """Convert this variable to a pandas.Index""" --> 306 return self.to_coord().to_index() 307 308 @property /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/core/variable.py in to_coord(self) 300 """Return this variable as an xarray.Coordinate""" 301 return Coordinate(self.dims, self._data, self._attrs, --> 302 encoding=self._encoding, fastpath=True) 303 304 def to_index(self): /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/core/variable.py in __init__(self, name, data, attrs, encoding, fastpath) 1053 if self.ndim != 1: 1054 raise ValueError('%s objects must be 1-dimensional' % -> 1055 type(self).__name__) 1056 1057 def _data_cached(self): ValueError: Coordinate objects must be 1-dimensional
Ok, so xarray doens't easily extract to a coordinate value if the coordinate arrays are not 1D. What about plotting agains z_rho?
# check against s_rho first
salt_ts = d.salt.sel(eta_rho=10, xi_rho=10)
salt_ts.plot(x='ocean_time', y='s_rho')
<matplotlib.collections.QuadMesh at 0x7f7028f6b780>
# try plotting with z_rho on y-axis
salt_ts.plot(x='ocean_time', y='z_rho')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-17-59cdc6ff0a44> in <module>() 1 # try plotting with z_rho on y-axis ----> 2 salt_ts.plot(x='ocean_time', y='z_rho') /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/plot/plot.py in __call__(self, **kwargs) 244 245 def __call__(self, **kwargs): --> 246 return plot(self._da, **kwargs) 247 248 @functools.wraps(hist) /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/plot/plot.py in plot(darray, row, col, col_wrap, ax, rtol, subplot_kws, **kwargs) 122 kwargs['ax'] = ax 123 --> 124 return plotfunc(darray, **kwargs) 125 126 /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/plot/plot.py in newplotfunc(darray, x, y, ax, row, col, col_wrap, xincrease, yincrease, add_colorbar, add_labels, vmin, vmax, cmap, center, robust, extend, levels, colors, subplot_kws, **kwargs) 415 vmin=cmap_params['vmin'], 416 vmax=cmap_params['vmax'], --> 417 **kwargs) 418 419 # Label the plot with metadata /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/plot/plot.py in pcolormesh(x, y, z, ax, **kwargs) 547 y = _infer_interval_breaks(y) 548 --> 549 primitive = ax.pcolormesh(x, y, z, **kwargs) 550 551 # by default, pcolormesh picks "round" values for bounds /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs) 1810 warnings.warn(msg % (label_namer, func.__name__), 1811 RuntimeWarning, stacklevel=2) -> 1812 return func(ax, *args, **kwargs) 1813 pre_doc = inner.__doc__ 1814 if pre_doc is None: /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/axes/_axes.py in pcolormesh(self, *args, **kwargs) 5393 allmatch = (shading == 'gouraud') 5394 -> 5395 X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch) 5396 Ny, Nx = X.shape 5397 /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/axes/_axes.py in _pcolorargs(funcname, *args, **kw) 5009 raise TypeError( 5010 'Incompatible X, Y inputs to %s; see help(%s)' % ( -> 5011 funcname, funcname)) 5012 if allmatch: 5013 if not (Nx == numCols and Ny == numRows): TypeError: Incompatible X, Y inputs to pcolormesh; see help(pcolormesh)
I'm having troubles with this plot probably because ocean_time and z_rho are different shapes (z_rho has a time dimension too). Does the same thing happen if I try latitude on the y axis?
s=39
i=17
salt_surface_ts = d.salt.isel(s_rho=39, xi_rho=i)
salt_surface_ts.plot(x='ocean_time', y='lat_rho',vmin=28.5,vmax=32.5)
<matplotlib.collections.QuadMesh at 0x7f7028d5dc50>
fig,ax=plt.subplots(1,1)
surf=d.salt.isel(ocean_time=81,s_rho=s)
surf.plot(x='lon_rho', y='lat_rho',ax=ax,vmin=28.5,vmax=32.5)
ax.plot(d.lon_rho.values[:,i], d.lat_rho.values[:,i], 'k-')
[<matplotlib.lines.Line2D at 0x7f7028c666d8>]
This does work! Probably because the x-slice on lat_rho is now 1D and pcolormesh can deal with this.
For plotting with z_rho, I think that ocean_time is 1D and z_rho is 2D, so pcolormesh finds different shapes for X/Y and throws an error.
What if I try a 1D line plot with z_rho?
salt_ts.isel(ocean_time=0).plot(y='z_rho')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-20-1486d6471f62> in <module>() ----> 1 salt_ts.isel(ocean_time=0).plot(y='z_rho') /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/plot/plot.py in __call__(self, **kwargs) 244 245 def __call__(self, **kwargs): --> 246 return plot(self._da, **kwargs) 247 248 @functools.wraps(hist) /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/plot/plot.py in plot(darray, row, col, col_wrap, ax, rtol, subplot_kws, **kwargs) 122 kwargs['ax'] = ax 123 --> 124 return plotfunc(darray, **kwargs) 125 126 /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/plot/plot.py in line(darray, *args, **kwargs) 161 _ensure_plottable([x]) 162 --> 163 primitive = ax.plot(x, darray, *args, **kwargs) 164 165 ax.set_xlabel(xlabel) /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs) 1810 warnings.warn(msg % (label_namer, func.__name__), 1811 RuntimeWarning, stacklevel=2) -> 1812 return func(ax, *args, **kwargs) 1813 pre_doc = inner.__doc__ 1814 if pre_doc is None: /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/axes/_axes.py in plot(self, *args, **kwargs) 1422 kwargs['color'] = c 1423 -> 1424 for line in self._get_lines(*args, **kwargs): 1425 self.add_line(line) 1426 lines.append(line) /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/axes/_base.py in _grab_next_args(self, *args, **kwargs) 384 return 385 if len(remaining) <= 3: --> 386 for seg in self._plot_args(remaining, kwargs): 387 yield seg 388 return /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs) 372 ncx, ncy = x.shape[1], y.shape[1] 373 for j in xrange(max(ncx, ncy)): --> 374 seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs) 375 ret.append(seg) 376 return ret /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/axes/_base.py in _makeline(self, x, y, kw, kwargs) 279 self._setdefaults(default_dict, kw, kwargs) 280 seg = mlines.Line2D(x, y, **kw) --> 281 self.set_lineprops(seg, **kwargs) 282 return seg 283 /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/axes/_base.py in set_lineprops(self, line, **kwargs) 187 def set_lineprops(self, line, **kwargs): 188 assert self.command == 'plot', 'set_lineprops only works with "plot"' --> 189 line.set(**kwargs) 190 191 def set_patchprops(self, fill_poly, **kwargs): /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/matplotlib/artist.py in set(self, **kwargs) 934 if func is None: 935 raise TypeError('There is no %s property "%s"' % --> 936 (self.__class__.__name__, k)) 937 ret.extend([func(v)]) 938 return ret TypeError: There is no Line2D property "y"
That won't work either.
Nonetheless, having z_rho in the xarray DataSet could still be useful, particularly for resampling. Let's see if that works.
daily = d.resample('1D', 'ocean_time')
daily.z_rho
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-22-ee667854b461> in <module>() ----> 1 daily.z_rho /home/nsoontie/anaconda3/envs/analysis/lib/python3.5/site-packages/xarray/core/common.py in __getattr__(self, name) 192 return source[name] 193 raise AttributeError("%r object has no attribute %r" % --> 194 (type(self).__name__, name)) 195 196 def __setattr__(self, name, value): AttributeError: 'Dataset' object has no attribute 'z_rho'
Ok, so z_rho did not get resampled and is now not part of the Dataset. What if it was a variables instead of a coordinate?
d = d.reset_coords('z_rho')
daily = d.resample('1D', 'ocean_time')
daily.z_rho
<xarray.DataArray 'z_rho' (ocean_time: 34, s_rho: 40, eta_rho: 31, xi_rho: 26)> array([[[[ -2.02343463e+02, -2.35217722e+02, -2.44248967e+02, ..., nan, nan, nan], [ -2.07593279e+02, -2.23750267e+02, -1.99839994e+02, ..., nan, nan, nan], [ -2.38685642e+02, -2.41358158e+02, -1.97474777e+02, ..., nan, nan, -6.73865996e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97906569e+02, -2.30060137e+02, -2.38893420e+02, ..., nan, nan, nan], [ -2.03041330e+02, -2.18844120e+02, -1.95458048e+02, ..., nan, nan, nan], [ -2.33452103e+02, -2.36066029e+02, -1.93144699e+02, ..., nan, nan, -6.59094792e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93669674e+02, -2.25135036e+02, -2.33779280e+02, ..., nan, nan, nan], [ -1.98694565e+02, -2.14159123e+02, -1.91273623e+02, ..., nan, nan, nan], [ -2.28454471e+02, -2.31012448e+02, -1.89009805e+02, ..., nan, nan, -6.44989418e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.25957036e+00, -2.63389906e+00, -2.73795291e+00, ..., nan, nan, nan], [ -2.32089807e+00, -2.50516712e+00, -2.23400080e+00, ..., nan, nan, nan], [ -2.67663194e+00, -2.70699396e+00, -2.20780579e+00, ..., nan, nan, -7.75133210e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.22566345e+00, -1.43205309e+00, -1.48997687e+00, ..., nan, nan, nan], [ -1.26018034e+00, -1.36191249e+00, -1.21289811e+00, ..., nan, nan, nan], [ -1.45708676e+00, -1.47379580e+00, -1.19878968e+00, ..., nan, nan, -4.30927315e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -3.46644724e-01, -4.10254010e-01, -4.28958400e-01, ..., nan, nan, nan], [ -3.58367273e-01, -3.89927264e-01, -3.44765414e-01, ..., nan, nan, nan], [ -4.20239980e-01, -4.25341371e-01, -3.40932895e-01, ..., nan, nan, -1.38286434e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], [[[ -2.02343466e+02, -2.35217716e+02, -2.44248942e+02, ..., nan, nan, nan], [ -2.07593282e+02, -2.23750260e+02, -1.99839968e+02, ..., nan, nan, nan], [ -2.38685638e+02, -2.41358150e+02, -1.97474753e+02, ..., nan, nan, -6.73865795e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97906579e+02, -2.30060119e+02, -2.38893347e+02, ..., nan, nan, nan], [ -2.03041338e+02, -2.18844099e+02, -1.95457973e+02, ..., nan, nan, nan], [ -2.33452091e+02, -2.36066005e+02, -1.93144629e+02, ..., nan, nan, -6.59094205e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93669689e+02, -2.25135007e+02, -2.33779162e+02, ..., nan, nan, nan], [ -1.98694578e+02, -2.14159089e+02, -1.91273501e+02, ..., nan, nan, nan], [ -2.28454453e+02, -2.31012409e+02, -1.89009691e+02, ..., nan, nan, -6.44988462e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.25985475e+00, -2.63336780e+00, -2.73576664e+00, ..., nan, nan, nan], [ -2.32113658e+00, -2.50454170e+00, -2.23175700e+00, ..., nan, nan, nan], [ -2.67628967e+00, -2.70627115e+00, -2.20570729e+00, ..., nan, nan, -7.73372723e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.22594929e+00, -1.43151911e+00, -1.48777943e+00, ..., nan, nan, nan], [ -1.26042007e+00, -1.36128387e+00, -1.21064285e+00, ..., nan, nan, nan], [ -1.45674275e+00, -1.47306931e+00, -1.19668047e+00, ..., nan, nan, -4.29157835e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -3.46931798e-01, -4.09717725e-01, -4.26751464e-01, ..., nan, nan, nan], [ -3.58608042e-01, -3.89295930e-01, -3.42500408e-01, ..., nan, nan, nan], [ -4.19894479e-01, -4.24611737e-01, -3.38814570e-01, ..., nan, nan, -1.36509308e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], [[[ -2.02343366e+02, -2.35217612e+02, -2.44248837e+02, ..., nan, nan, nan], [ -2.07593179e+02, -2.23750152e+02, -1.99839862e+02, ..., nan, nan, nan], [ -2.38685535e+02, -2.41358041e+02, -1.97474654e+02, ..., nan, nan, -6.73863664e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97906285e+02, -2.30059816e+02, -2.38893041e+02, ..., nan, nan, nan], [ -2.03041036e+02, -2.18843784e+02, -1.95457662e+02, ..., nan, nan, nan], [ -2.33451790e+02, -2.36065687e+02, -1.93144341e+02, ..., nan, nan, -6.59087986e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93669211e+02, -2.25134514e+02, -2.33778664e+02, ..., nan, nan, nan], [ -1.98694086e+02, -2.14158576e+02, -1.91272996e+02, ..., nan, nan, nan], [ -2.28453962e+02, -2.31011892e+02, -1.89009222e+02, ..., nan, nan, -6.44978339e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.25103632e+00, -2.62428982e+00, -2.72660533e+00, ..., nan, nan, nan], [ -2.31206848e+00, -2.49507930e+00, -2.22244221e+00, ..., nan, nan, nan], [ -2.66724634e+00, -2.69674161e+00, -2.19707137e+00, ..., nan, nan, -7.54723794e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.21708581e+00, -1.42239476e+00, -1.47857131e+00, ..., nan, nan, nan], [ -1.25130564e+00, -1.35177314e+00, -1.20128048e+00, ..., nan, nan, nan], [ -1.44765322e+00, -1.46349108e+00, -1.18800043e+00, ..., nan, nan, -4.10413642e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -3.38030019e-01, -4.00553948e-01, -4.17503564e-01, ..., nan, nan, nan], [ -3.49454227e-01, -3.79744102e-01, -3.33097583e-01, ..., nan, nan, nan], [ -4.10765680e-01, -4.14992123e-01, -3.30097029e-01, ..., nan, nan, -1.17684122e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], ..., [[[ -2.02343926e+02, -2.35218181e+02, -2.44249415e+02, ..., nan, nan, nan], [ -2.07593741e+02, -2.23750726e+02, -1.99840443e+02, ..., nan, nan, nan], [ -2.38686103e+02, -2.41358616e+02, -1.97475230e+02, ..., nan, nan, -6.73868899e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97907921e+02, -2.30061478e+02, -2.38894728e+02, ..., nan, nan, nan], [ -2.03042678e+02, -2.18845460e+02, -1.95459358e+02, ..., nan, nan, nan], [ -2.33453448e+02, -2.36067365e+02, -1.93146022e+02, ..., nan, nan, -6.59103264e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93671874e+02, -2.25137220e+02, -2.33781410e+02, ..., nan, nan, nan], [ -1.98696760e+02, -2.14161304e+02, -1.91275755e+02, ..., nan, nan, nan], [ -2.28456661e+02, -2.31014624e+02, -1.89011958e+02, ..., nan, nan, -6.45003207e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.30010576e+00, -2.67413373e+00, -2.77719199e+00, ..., nan, nan, nan], [ -2.36132353e+00, -2.54534437e+00, -2.27328145e+00, ..., nan, nan, nan], [ -2.71696283e+00, -2.74706532e+00, -2.24748043e+00, ..., nan, nan, -8.00535256e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.26640592e+00, -1.47249328e+00, -1.52941639e+00, ..., nan, nan, nan], [ -1.30081230e+00, -1.40229498e+00, -1.25237942e+00, ..., nan, nan, nan], [ -1.49762368e+00, -1.51407186e+00, -1.23866699e+00, ..., nan, nan, -4.56459123e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -3.87563234e-01, -4.50868948e-01, -4.68568338e-01, ..., nan, nan, nan], [ -3.99174804e-01, -4.30484245e-01, -3.84417322e-01, ..., nan, nan, nan], [ -4.60952058e-01, -4.65791467e-01, -3.80982518e-01, ..., nan, nan, -1.63928564e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], [[[ -2.02344075e+02, -2.35218338e+02, -2.44249585e+02, ..., nan, nan, nan], [ -2.07593896e+02, -2.23750893e+02, -1.99840623e+02, ..., nan, nan, nan], [ -2.38686264e+02, -2.41358790e+02, -1.97475416e+02, ..., nan, nan, -6.73869861e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97908354e+02, -2.30061934e+02, -2.38895223e+02, ..., nan, nan, nan], [ -2.03043129e+02, -2.18845946e+02, -1.95459885e+02, ..., nan, nan, nan], [ -2.33453919e+02, -2.36067873e+02, -1.93146564e+02, ..., nan, nan, -6.59106071e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93672579e+02, -2.25137962e+02, -2.33782216e+02, ..., nan, nan, nan], [ -1.98697493e+02, -2.14162095e+02, -1.91276613e+02, ..., nan, nan, nan], [ -2.28457427e+02, -2.31015450e+02, -1.89012841e+02, ..., nan, nan, -6.45007777e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.31308597e+00, -2.68780866e+00, -2.79203300e+00, ..., nan, nan, nan], [ -2.37484141e+00, -2.55991183e+00, -2.28908565e+00, ..., nan, nan, nan], [ -2.73108991e+00, -2.76228318e+00, -2.26373929e+00, ..., nan, nan, -8.08953568e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.27945243e+00, -1.48623807e+00, -1.54433321e+00, ..., nan, nan, nan], [ -1.31439923e+00, -1.41693685e+00, -1.26826435e+00, ..., nan, nan, nan], [ -1.51182292e+00, -1.52936746e+00, -1.25500891e+00, ..., nan, nan, -4.64920438e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -4.00666121e-01, -4.64673130e-01, -4.83549614e-01, ..., nan, nan, nan], [ -4.12820448e-01, -4.45189383e-01, -4.00370891e-01, ..., nan, nan, nan], [ -4.75212653e-01, -4.81153158e-01, -3.97395049e-01, ..., nan, nan, -1.72426440e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]], [[[ -2.02346851e+02, -2.35221157e+02, -2.44252405e+02, ..., nan, nan, nan], [ -2.07596765e+02, -2.23753772e+02, -1.99843472e+02, ..., nan, nan, nan], [ -2.38689174e+02, -2.41361681e+02, -1.97478268e+02, ..., nan, nan, -6.73872976e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.97916457e+02, -2.30070162e+02, -2.38903454e+02, ..., nan, nan, nan], [ -2.03051503e+02, -2.18854349e+02, -1.95468197e+02, ..., nan, nan, nan], [ -2.33462412e+02, -2.36076308e+02, -1.93154888e+02, ..., nan, nan, -6.59115159e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.93685768e+02, -2.25151354e+02, -2.33795613e+02, ..., nan, nan, nan], [ -1.98711123e+02, -2.14175773e+02, -1.91290144e+02, ..., nan, nan, nan], [ -2.28471251e+02, -2.31029181e+02, -1.89026389e+02, ..., nan, nan, -6.45022569e+01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], ..., [[ -2.55604723e+00, -2.93451922e+00, -3.03883973e+00, ..., nan, nan, nan], [ -2.62593181e+00, -2.81188227e+00, -2.53833669e+00, ..., nan, nan, nan], [ -2.98574908e+00, -3.01523115e+00, -2.51331224e+00, ..., nan, nan, -8.36203116e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -1.52365481e+00, -1.73420890e+00, -1.79240071e+00, ..., nan, nan, nan], [ -1.56677228e+00, -1.67019444e+00, -1.51878864e+00, ..., nan, nan, nan], [ -1.76778297e+00, -1.78360757e+00, -1.50585676e+00, ..., nan, nan, -4.92309185e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]], [[ -6.45923696e-01, -7.13715436e-01, -7.32689005e-01, ..., nan, nan, nan], [ -6.66283996e-01, -6.99541291e-01, -6.51977691e-01, ..., nan, nan, nan], [ -7.32278698e-01, -7.36491827e-01, -6.49326801e-01, ..., nan, nan, -1.99933533e-01], ..., [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan], [ nan, nan, nan, ..., nan, nan, nan]]]]) Coordinates: lon_rho (eta_rho, xi_rho) float64 -125.0 -125.0 -125.0 -124.9 -124.9 ... lat_rho (eta_rho, xi_rho) float64 48.32 48.32 48.32 48.32 48.32 ... * s_rho (s_rho) float64 -0.9875 -0.9625 -0.9375 -0.9125 -0.8875 ... * eta_rho (eta_rho) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ... * xi_rho (xi_rho) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ... * ocean_time (ocean_time) datetime64[ns] 2016-06-01 2016-06-02 2016-06-03 ...
Excellent! So basically, I shouldn't set z_rho as a coordinate because it is multi-dimensional and time dependent. I don't get the same features as other 1D coordinates.
I think it is still worthwhile to include z_rho in the Dataset. Plotting vs z_rho will have to be done outside of xarray. And interpolating, etc too. But at least it is there.