import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib as mpl
import xarray as xr
import pandas as pd
## data and ref imports
ds = xr.open_dataset('/ocean/cdonaldson/research_runs/long_dye_tracers/extractions/SalishSeaCast_river_trace_day_avg_Fraser_all_20170101_20200101.nc')
mesh = xr.open_dataset('/home/sallen/MEOPAR/grid/mesh_mask202108.nc')
# the mask is 1 where there is water, we want the opposite. The meshmask has an extra dimension, hence the [0]
tmask = 1- mesh.tmask[0]
## global definitions
rho = 1026
rho_inv = 1/rho
# set up the volume with the tmask
volume = (1-tmask) * mesh.e3t_0[0] * mesh.e1t[0] * mesh.e2t[0]
# need a fudge factor to use log scales
fudge = 0.0001
def dye_to_volume(jdepth, volume, rho_inv, data):
data_np = data[0:jdepth].values
river_arr = rho_inv * np.multiply(volume[0:jdepth], data_np)
return river_arr
time_idx = 64
depth = 0
volume_conv = dye_to_volume(40, volume, rho_inv, ds.fraser_river[time_idx])[depth]
# volume_conv[300:500, 1:30] = np.nanmean(volume_conv[300:500, 30:59])
# volume_conv.where()
# time_idx = 1000
# depth = 0
# cmap = mpl.colormaps['Reds']
cmap = mpl.colormaps['viridis']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv, mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds['depth'][depth].values), 2)))
Text(0.5, 1.0, 'Fraser River 2017-03-06 \n depth = 0.5 m')
# time_idx = 500
# depth = 0
# cmap = mpl.colormaps['Reds']
cmap = mpl.colormaps['viridis']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 5))
freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv, mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds['depth'][depth].values), 2)))
ax.set_ylim([440, 470])
ax.set_xlim([0, 30])
(0.0, 30.0)
# time_idx = 10
# depth = 0
cmap = mpl.colormaps['Reds']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(np.nansum(dye_to_volume(40, volume, rho_inv, ds.fraser_river[time_idx]), axis=0), mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds['depth'][depth].values), 2)))
Text(0.5, 1.0, 'Fraser River 2017-03-06 \n depth = 0.5 m')
ds2 = xr.open_dataset('/ocean/cdonaldson/research_runs/dye_reflect_test/06mar17/SalishSea_1d_20170306_20170306_rivr_T.nc')
dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0])[0].plot()
<matplotlib.collections.QuadMesh at 0x7ff20175edd0>
volume_conv2 = dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0])[depth]
# cmap = mpl.colormaps['Reds']
cmap = mpl.colormaps['viridis']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv2, mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds['depth'][depth].values), 2)))
Text(0.5, 1.0, 'Fraser River 2017-03-06 \n depth = 0.5 m')
cmap = mpl.colormaps['viridis']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 5))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv2, mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds['depth'][depth].values), 2)))
ax.set_ylim([440, 470])
ax.set_xlim([0, 30])
(0.0, 30.0)
cmap = mpl.colormaps['Reds']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(np.nansum(dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0]), axis=0), mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds['depth'][depth].values), 2)))
Text(0.5, 1.0, 'Fraser River 2017-03-06 \n depth = 0.5 m')
cmap = mpl.colormaps['bwr']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv2 - volume_conv, mask=tmask[depth])
Z = freshwater + fudge
max_value = np.abs(volume_conv2 - volume_conv).max()
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=-max_value, vmax=max_value, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('ver_frs - ver_none \n Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds['depth'][depth].values), 2)))
Text(0.5, 1.0, 'ver_frs - ver_none \n Fraser River 2017-03-06 \n depth = 0.5 m')
cmap = mpl.colormaps['bwr']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 5))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv2 - volume_conv, mask=tmask[depth])
Z = freshwater + fudge
max_value = np.abs(volume_conv2 - volume_conv).max()
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=-max_value, vmax=max_value, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('ver_frs - ver_none \n Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds['depth'][depth].values), 2)))
ax.set_ylim([440, 470])
ax.set_xlim([0, 30])
(0.0, 30.0)
cmap = mpl.colormaps['bwr']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
diff = np.nansum(dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0]), axis=0) - np.nansum(dye_to_volume(40, volume, rho_inv, ds.fraser_river[time_idx]), axis=0)
freshwater = np.ma.masked_array(diff, mask=tmask[depth])
max_value = np.abs(diff).max()
Z = freshwater + fudge
colours = ax.pcolormesh(X, Y, Z, vmin=-max_value, vmax=max_value, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('ver_frs - ver_none \nFraser River {} \n depth summed'.format(str(dtime.date())))
Text(0.5, 1.0, 'ver_frs - ver_none \nFraser River 2017-03-06 \n depth summed')
cmap = mpl.colormaps['bwr']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 5))
diff = np.nansum(dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0]), axis=0) - np.nansum(dye_to_volume(40, volume, rho_inv, ds.fraser_river[time_idx]), axis=0)
freshwater = np.ma.masked_array(diff, mask=tmask[depth])
max_value = np.abs(diff).max()
Z = freshwater + fudge
colours = ax.pcolormesh(X, Y, Z, vmin=-max_value, vmax=max_value, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('ver_frs - ver_none \nFraser River {} \n depth summed'.format(str(dtime.date())))
ax.set_ylim([440, 470])
ax.set_xlim([0, 30])
(0.0, 30.0)
cmap = mpl.colormaps['bwr']
cmap.set_bad('gray')
X = np.arange(0,398)[0:30]
Y = np.arange(0,898)[440:470]
fig, ax = plt.subplots(1,1, figsize = (5, 5))
diff = np.nansum(dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0]), axis=0)[440:470, 0:30] - np.nansum(dye_to_volume(40, volume, rho_inv, ds.fraser_river[time_idx]), axis=0)[440:470, 0:30]
freshwater = np.ma.masked_array(diff, mask=tmask[depth][440:470, 0:30])
max_value = np.abs(diff).max()
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=-max_value, vmax=max_value, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds['time'].values[time_idx])
ax.set_title('ver_frs - ver_none \nFraser River {} \n depth summed'.format(str(dtime.date())))
# ax.set_ylim([440, 470])
# ax.set_xlim([0, 30])
Text(0.5, 1.0, 'ver_frs - ver_none \nFraser River 2017-03-06 \n depth summed')
test_day = xr.open_dataset('/ocean/sallen/timbucktwo/rivers/01jan12/SalishSea_1d_20120101_20120101_rivr_T.nc')
test_day2 = xr.open_dataset('/ocean/sallen/timbucktwo/rivers/02jan12/SalishSea_1d_20120102_20120102_rivr_T.nc')
ds2 = xr.open_dataset('/ocean/cdonaldson/research_runs/hindcast_auto/extractions/SalishSeaCast_river_trace_day_avg_Fraser_all_20120101_20120730.nc')
time_idx = 211
depth = 0
volume_conv2 = dye_to_volume(40, volume, rho_inv, ds2.fraser_river[time_idx])[depth]
# cmap = mpl.colormaps['Reds']
cmap = mpl.colormaps['viridis']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv2, mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds2['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds2['depth'][depth].values), 2)))
Text(0.5, 1.0, 'Fraser River 2012-07-30 \n depth = 0.5 m')
cmap = mpl.colormaps['viridis']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv2, mask=tmask[depth])
Z = freshwater + fudge
colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds2['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds2['depth'][depth].values), 2)))
Text(0.5, 1.0, 'Fraser River 2012-07-30 \n depth = 0.5 m')
cmap = mpl.colormaps['viridis']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 5))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(volume_conv2, mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds2['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth = {} m'.format(str(dtime.date()), round(float(ds2['depth'][depth].values), 2)))
ax.set_ylim([440, 470])
ax.set_xlim([0, 30])
(0.0, 30.0)
cmap = mpl.colormaps['Reds']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(np.nansum(dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0]), axis=0), mask=tmask[depth])
Z = freshwater + fudge
# colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
colours = ax.pcolormesh(X, Y, Z, vmin=0, cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds2['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth summed'.format(str(dtime.date())))
Text(0.5, 1.0, 'Fraser River 2012-07-30 \n depth summed')
cmap = mpl.colormaps['Reds']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(np.nansum(dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0]), axis=0), mask=tmask[depth])
Z = freshwater + fudge
colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds2['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth summed'.format(str(dtime.date())))
Text(0.5, 1.0, 'Fraser River 2012-07-30 \n depth summed')
cmap = mpl.colormaps['viridis']
cmap.set_bad('gray')
X = np.arange(0,398)
Y = np.arange(0,898)
fig, ax = plt.subplots(1,1, figsize = (5, 9))
# freshwater = np.ma.masked_array(ds.fraser_river[time_idx, depth], mask=tmask[depth])
freshwater = np.ma.masked_array(np.nansum(dye_to_volume(40, volume, rho_inv, ds2['fraser_river'][0]), axis=0), mask=tmask[depth])
Z = freshwater + fudge
colours = ax.pcolormesh(X, Y, Z, norm=colors.LogNorm(vmin=fudge), cmap=cmap)
cb = fig.colorbar(colours, ax=ax, label=r'Volume Freshwater $m^3$')
dtime = pd.to_datetime(ds2['time'].values[time_idx])
ax.set_title('Fraser River {} \n depth summed'.format(str(dtime.date())))
Text(0.5, 1.0, 'Fraser River 2012-07-30 \n depth summed')