import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import xarray as xr
%matplotlib inline
plt.rcParams['image.cmap'] = 'jet'
nowcast_tracer_path = "/results/SalishSea/nowcast-green/18apr16/SalishSea_1h_20160418_20160418_ptrc_T.nc"
rerun_tracer_path = "/data/jpetrie/MEOPAR/SalishSea/results/apr18_full_run_old_forcing_2/SalishSea_1h_20160418_20160418_ptrc_T.nc"
n_grid_t = xr.open_dataset(nowcast_tracer_path)
r_grid_t = xr.open_dataset(rerun_tracer_path)
n_grid_t
<xarray.Dataset> Dimensions: (axis_nbounds: 2, deptht: 40, nvertex: 4, time_counter: 24, x: 398, y: 898) Coordinates: * deptht (deptht) float32 0.5 1.5 2.50001 3.50003 4.50007 ... nav_lat (y, x) float32 46.8597 46.8615 46.8634 46.8653 ... nav_lon (y, x) float32 -123.429 -123.424 -123.419 -123.413 ... time_centered (time_counter) datetime64[ns] 2016-04-18T00:30:00 ... * time_counter (time_counter) datetime64[ns] 2016-04-18T00:30:00 ... * axis_nbounds (axis_nbounds) int64 0 1 * nvertex (nvertex) int64 0 1 2 3 * x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... * y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... Data variables: DOC (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... MICZ (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... MYRI (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... NH4 (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... NO3 (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... O2 (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... PHY (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... PHY2 (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... POC (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... Si (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... area (y, x) float32 180762.0 189238.0 194104.0 197344.0 ... bSi (time_counter, deptht, y, x) float64 0.0 0.0 0.0 ... bounds_lat (y, x, nvertex) float32 46.8597 46.8597 46.8597 ... bounds_lon (y, x, nvertex) float32 -123.429 -123.429 -123.429 ... deptht_bounds (deptht, axis_nbounds) float32 0.0 1.0 1.0 2.00001 ... time_centered_bounds (time_counter, axis_nbounds) float64 3.67e+09 ... time_counter_bounds (time_counter, axis_nbounds) float64 3.67e+09 ... Attributes: name: SalishSea_1h_20160418_20160418 description: sog variables title: sog variables Conventions: CF-1.5 production: An IPSL model timeStamp: 2016-Jul-08 14:26:33 PDT history: Fri Jul 8 15:44:25 2016: ncks -4 -L4 -O SalishSea_1h_20160418_20160418_ptrc_T.nc SalishSea_1h_20160418_20160418_ptrc_T.nc NCO: "4.5.2"
def plot_time_dif(var_name, array_1, array_2):
dif_array = (array_1[var_name] - array_2[var_name])
max_index = np.unravel_index(dif_array.values.argmax(), dif_array.values.shape)
x_slice = max_index[3]
y_slice = max_index[2]
deptht = max_index[1]
time_counter = slice(0,24)
fig, ax = plt.subplots(1,2, figsize = (15,8))
(dif_array.isel(deptht = deptht, time_counter = time_counter, x = x_slice, y = y_slice)).plot(ax = ax[0])
(array_1[var_name].isel(deptht = deptht, time_counter = time_counter, x = x_slice, y = y_slice)).plot(ax = ax[1])
def plot_space_dif(var_name, array_1, array_2, grid_width = 10):
dif_array = (array_1[var_name] - array_2[var_name])
max_index = np.unravel_index(dif_array.values.argmax(), dif_array.values.shape)
print(array_1[var_name].isel(x = max_index[3], y = max_index[2], deptht = max_index[1], time_counter = max_index[0]))
x_slice = slice(max(0,max_index[3] - int(grid_width/2)),min(array_1.coords.dims['x'], max_index[3] + int(grid_width/2))) #max_index[3] + 0
y_slice = slice(max(0,max_index[2] - int(grid_width/2)), min(array_1.coords.dims['y'],max_index[2] + int(grid_width/2))) # max_index[2] + 0
deptht = max_index[1]
time_counter = max_index[0]
fig, ax = plt.subplots(1,2, figsize = (15,8))
(dif_array.isel(deptht = deptht, time_counter = time_counter, x = x_slice, y = y_slice)).plot(ax = ax[0])
(array_1[var_name].isel(deptht = deptht, time_counter = time_counter, x = x_slice, y = y_slice)).plot(ax = ax[1])
n_grid_t.PHY.isel(x=1,y=1,time_counter =1, deptht =1)
<xarray.DataArray 'PHY' ()> array(0.0) Coordinates: deptht float32 1.5 nav_lat float32 46.8648 nav_lon float32 -123.427 time_centered datetime64[ns] 2016-04-18T01:30:00 time_counter datetime64[ns] 2016-04-18T01:30:00 x int64 1 y int64 1 Attributes: long_name: (Nano)Phytoplankton Concentration units: mmol/m3 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area
plot_time_dif("NO3", n_grid_t, r_grid_t)
plot_space_dif("NO3", n_grid_t, r_grid_t, 16)
plot_space_dif("NO3", n_grid_t, r_grid_t, 1000)
<xarray.DataArray 'NO3' ()> array(4.286578178405762) Coordinates: deptht float32 2.50001 nav_lat float32 49.122 nav_lon float32 -123.192 time_centered datetime64[ns] 2016-04-18T22:30:00 time_counter datetime64[ns] 2016-04-18T22:30:00 x int64 311 y int64 423 Attributes: long_name: Nitrate Concentration units: mmol/m3 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area <xarray.DataArray 'NO3' ()> array(4.286578178405762) Coordinates: deptht float32 2.50001 nav_lat float32 49.122 nav_lon float32 -123.192 time_centered datetime64[ns] 2016-04-18T22:30:00 time_counter datetime64[ns] 2016-04-18T22:30:00 x int64 311 y int64 423 Attributes: long_name: Nitrate Concentration units: mmol/m3 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area
plot_time_dif("DOC", n_grid_t, r_grid_t)
plot_space_dif("DOC", n_grid_t, r_grid_t,16)
plot_space_dif("DOC", n_grid_t, r_grid_t, 1000)
<xarray.DataArray 'DOC' ()> array(7.327282428741455) Coordinates: deptht float32 2.50001 nav_lat float32 48.1433 nav_lon float32 -123.571 time_centered datetime64[ns] 2016-04-18T20:30:00 time_counter datetime64[ns] 2016-04-18T20:30:00 x int64 134 y int64 261 Attributes: long_name: Dissolved organic Concentration units: mmol/m3 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area <xarray.DataArray 'DOC' ()> array(7.327282428741455) Coordinates: deptht float32 2.50001 nav_lat float32 48.1433 nav_lon float32 -123.571 time_centered datetime64[ns] 2016-04-18T20:30:00 time_counter datetime64[ns] 2016-04-18T20:30:00 x int64 134 y int64 261 Attributes: long_name: Dissolved organic Concentration units: mmol/m3 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area
plot_time_dif("PHY2", n_grid_t, r_grid_t)
plot_space_dif("PHY2", n_grid_t, r_grid_t, 16)
plot_space_dif("PHY2", n_grid_t, r_grid_t, 1000)
<xarray.DataArray 'PHY2' ()> array(5.496196269989014) Coordinates: deptht float32 2.50001 nav_lat float32 49.1042 nav_lon float32 -123.17 time_centered datetime64[ns] 2016-04-18T23:30:00 time_counter datetime64[ns] 2016-04-18T23:30:00 x int64 312 y int64 418 Attributes: long_name: Diatoms Concentration units: mmol/m3 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area <xarray.DataArray 'PHY2' ()> array(5.496196269989014) Coordinates: deptht float32 2.50001 nav_lat float32 49.1042 nav_lon float32 -123.17 time_centered datetime64[ns] 2016-04-18T23:30:00 time_counter datetime64[ns] 2016-04-18T23:30:00 x int64 312 y int64 418 Attributes: long_name: Diatoms Concentration units: mmol/m3 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area
plot_time_dif("O2", n_grid_t, r_grid_t)
plot_space_dif("O2", n_grid_t, r_grid_t)
<xarray.DataArray 'O2' ()> array(6362.96533203125) Coordinates: deptht float32 2.50001 nav_lat float32 49.1042 nav_lon float32 -123.17 time_centered datetime64[ns] 2016-04-18T23:30:00 time_counter datetime64[ns] 2016-04-18T23:30:00 x int64 312 y int64 418 Attributes: long_name: Oxygen Concentration units: mmol/m3 online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area
nowcast_v_path = "/results/SalishSea/nowcast-green/18apr16/SalishSea_1h_20160418_20160418_grid_V.nc"
rerun_v_path = "/data/jpetrie/MEOPAR/SalishSea/results/apr18_full_run_round_2/SalishSea_1h_20160418_20160418_grid_V.nc"
n_grid_v = xr.open_dataset(nowcast_v_path)
r_grid_v = xr.open_dataset(rerun_v_path)
v_dif = (n_grid_v.vomecrty - r_grid_v.vomecrty)
v_dif.dot(v_dif)
v_dif.sum()
n_grid_v.sum()
n_grid_v
n_grid_v
plot_time_dif("vomecrty", n_grid_v, r_grid_v)
plot_space_dif("vomecrty", n_grid_v, r_grid_v)