%reset
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'
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
nowcast_tracer_path = "/results/SalishSea/nowcast-green/12apr16/SalishSea_1h_20160412_20160412_ptrc_T.nc"
diat_iopt_84_tracer_path = "/data/jpetrie/MEOPAR/SalishSea/results/apr12_double_iopt_diat/SalishSea_1h_20160412_20160412_ptrc_T.nc"
diat_iopt_55_tracer_path = "/data/jpetrie/MEOPAR/SalishSea/results/april12_55_iopt_diat/SalishSea_1h_20160412_20160412_ptrc_T.nc"
n_grid_t = xr.open_dataset(nowcast_tracer_path)
#m_grid_t = xr.open_dataset(diat_iopt_84_tracer_path)
m_grid_t = xr.open_dataset(diat_iopt_55_tracer_path)
n_grid_t.votemper
<xarray.DataArray 'votemper' (time_counter: 24, deptht: 40, y: 898, x: 398)> [343107840 values with dtype=float64] Coordinates: * deptht (deptht) float32 0.5 1.5 2.50001 3.50003 4.50007 5.50015 ... nav_lat (y, x) float32 46.8597 46.8615 46.8634 46.8653 46.8672 ... nav_lon (y, x) float32 -123.429 -123.424 -123.419 -123.413 ... time_centered (time_counter) datetime64[ns] 2016-04-12T00:30:00 ... * time_counter (time_counter) datetime64[ns] 2016-04-12T00:30:00 ... * x (x) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ... * y (y) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ... Attributes: standard_name: sea_water_potential_temperature long_name: temperature units: degC online_operation: average interval_operation: 40 s interval_write: 1 h cell_methods: time: mean (interval: 40 s) cell_measures: area: area
m_grid_t.PHY
NO3_subset_20m = ((m_grid_t["NO3"] - n_grid_t["NO3"]).where(m_grid_t["O2"] != 0).isel(deptht = 18, time_counter = 23))
fig, ax = plt.subplots(1,1, figsize = (15,15))
NO3_subset_20m.plot(ax = ax, cmap = "jet", robust = True)
plt.title("NO3(Iopt_diat=55) - NO3(Iopt_diat=42) After 1 Day at Depth=20m ", size = 35)
NO3_subset_0m = ((m_grid_t["NO3"] - n_grid_t["NO3"]).where(m_grid_t["O2"] != 0).isel(deptht = 0, time_counter = 23))
fig, ax = plt.subplots(1,1, figsize = (15,15))
NO3_subset_0m.plot(ax = ax, cmap = "jet", robust = True)
plt.title("NO3(Iopt_diat=55) - NO3(Iopt_diat=42) After 1 Day at Depth=0m ", size = 35)
PHY2_subset_20m = ((m_grid_t["PHY2"] - n_grid_t["PHY2"]).where(m_grid_t["O2"] != 0).isel(deptht = 19, time_counter = 23))
fig, ax = plt.subplots(1,1, figsize = (15,15))
PHY2_subset_20m.plot(ax = ax, cmap = "jet", robust = True)
plt.title("PHY2(Iopt_diat=55) - PHY2(Iopt_diat=42) After 1 Day at Depth=20m ", size = 35)
PHY2_subset_0m = ((m_grid_t["PHY2"] - n_grid_t["PHY2"]).where(m_grid_t["O2"] != 0).isel(deptht = 0, time_counter = 23))
fig, ax = plt.subplots(1,1, figsize = (15,15))
PHY2_subset_0m.plot(ax = ax, cmap = "jet", robust = True)
plt.title("PHY2(Iopt_diat=55) - PHY2(Iopt_diat=42) After 1 Day at Depth=0m ", size = 35)
# Have to keep this and the next cell apart or it fails with memory error. Something to do with
# Jupyter notebook caching
tracer = "PHY2"
mod_tracer_depth_time = m_grid_t[tracer].where(m_grid_t[tracer] != 0).mean(dim = ["x","y"]).to_dataframe(name = "MEAN_" + tracer)
now_tracer_depth_time = n_grid_t[tracer].where(n_grid_t[tracer] != 0).mean(dim = ["x","y"]).to_dataframe(name = "MEAN_" + tracer)
var_name = "MEAN_" + tracer
mod_tracer_depth_time["DATA_SOURCE"] = "IOPT DIAT = 55"
now_tracer_depth_time["DATA_SOURCE"] = "IOPT DIAT = 42"
combined = pd.concat([mod_tracer_depth_time, now_tracer_depth_time])
combined.reset_index(inplace=True)
%load_ext rpy2.ipython
%%R
require(ggplot2)
require(data.table)
library(repr)
--------------------------------------------------------------------------- RRuntimeError Traceback (most recent call last) <ipython-input-9-2765bc9a58c7> in <module>() ----> 1 get_ipython().run_cell_magic('R', '', 'require(ggplot2)\nrequire(data.table)\nlibrary(repr)') /home/jpetrie/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2118 magic_arg_s = self.var_expand(line, stack_depth) 2119 with self.builtin_trap: -> 2120 result = fn(magic_arg_s, cell) 2121 return result 2122 <decorator-gen-128> in R(self, line, cell, local_ns) /home/jpetrie/anaconda3/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k) 191 # but it's overkill for just that one bit of state. 192 def magic_deco(arg): --> 193 call = lambda f, *a, **k: f(*a, **k) 194 195 if callable(arg): /home/jpetrie/anaconda3/lib/python3.5/site-packages/rpy2/ipython/rmagic.py in R(self, line, cell, local_ns) 663 ro.r.assign(input, localconverter.py2ri(val)) 664 --> 665 tmpd = self.setup_graphics(args) 666 667 text_output = '' /home/jpetrie/anaconda3/lib/python3.5/site-packages/rpy2/ipython/rmagic.py in setup_graphics(self, args) 413 # Note: that %% is to pass into R for interpolation there 414 ro.r.png("%s/Rplots%%03d.png" % tmpd_fix_slashes, --> 415 **argdict) 416 elif self.device == 'svg': 417 self.cairo.CairoSVG("%s/Rplot.svg" % tmpd_fix_slashes, /home/jpetrie/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 176 v = kwargs.pop(k) 177 kwargs[r_k] = v --> 178 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs) 179 180 pattern_link = re.compile(r'\\link\{(.+?)\}') /home/jpetrie/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 104 for k, v in kwargs.items(): 105 new_kwargs[k] = conversion.py2ri(v) --> 106 res = super(Function, self).__call__(*new_args, **new_kwargs) 107 res = conversion.ri2ro(res) 108 return res RRuntimeError: Error in .External2(C_X11, paste("png::", filename, sep = ""), g$width, : unable to start device PNG
%%R -i combined,var_name -h 1200 -w 1500 -r 400
combined = data.table(combined)
ggplot(combined[deptht < 25 & time_counter == max(time_counter) ], aes_string(y = "deptht", x = var_name, colour = "DATA_SOURCE")) +
geom_path() + ylim(25,0) +
facet_wrap(~time_counter) +
ylab("Depth (m)") +
xlab("Phytoplankton (mmolN/m^3)")
# Have to keep this and the next cell apart or it fails with memory error. Something to do with
# Jupyter notebook caching
tracer = "NO3"
mod_tracer_depth_time = m_grid_t[tracer].where(m_grid_t["O2"] != 0).mean(dim = ["x","y"]).to_dataframe(name = "MEAN_" + tracer)
now_tracer_depth_time = n_grid_t[tracer].where(m_grid_t["O2"] != 0).mean(dim = ["x","y"]).to_dataframe(name = "MEAN_" + tracer)
var_name = "MEAN_" + tracer
mod_tracer_depth_time["DATA_SOURCE"] = "IOPT_DIAT_55"
now_tracer_depth_time["DATA_SOURCE"] = "IOPT_DIAT_42"
combined = pd.concat([mod_tracer_depth_time, now_tracer_depth_time])
combined.reset_index(inplace=True)
fg = sns.FacetGrid(data=combined, hue="DATA_SOURCE")
fg.map(plt.plot,"deptht", var_name).add_legend()
<seaborn.axisgrid.FacetGrid at 0x7f24d81a0048>
%%R
ggplot(combined, aes(x = ))
--------------------------------------------------------------------------- RRuntimeError Traceback (most recent call last) <ipython-input-29-f8881ecb569a> in <module>() ----> 1 get_ipython().run_cell_magic('R', '', 'wide_combined[, DIF := IOPT_DIAT_55 - IOPT_DIAT_42]') /home/jpetrie/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2118 magic_arg_s = self.var_expand(line, stack_depth) 2119 with self.builtin_trap: -> 2120 result = fn(magic_arg_s, cell) 2121 return result 2122 <decorator-gen-128> in R(self, line, cell, local_ns) /home/jpetrie/anaconda3/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k) 191 # but it's overkill for just that one bit of state. 192 def magic_deco(arg): --> 193 call = lambda f, *a, **k: f(*a, **k) 194 195 if callable(arg): /home/jpetrie/anaconda3/lib/python3.5/site-packages/rpy2/ipython/rmagic.py in R(self, line, cell, local_ns) 680 old_writeconsole_regular = ri.get_writeconsole_regular() 681 ri.set_writeconsole_regular(self.write_console_regular) --> 682 ro.r.show(result) 683 text_output += self.flush() 684 ri.set_writeconsole_regular(old_writeconsole_regular) /home/jpetrie/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 176 v = kwargs.pop(k) 177 kwargs[r_k] = v --> 178 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs) 179 180 pattern_link = re.compile(r'\\link\{(.+?)\}') /home/jpetrie/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs) 104 for k, v in kwargs.items(): 105 new_kwargs[k] = conversion.py2ri(v) --> 106 res = super(Function, self).__call__(*new_args, **new_kwargs) 107 res = conversion.ri2ro(res) 108 return res RRuntimeError: Error in SYS[[length(SYS) - 3L]][[1L]] == "knit_print.default" : comparison (1) is possible only for atomic and list types
%%R -i combined,var_name -h 4200 -w 2400 -r 400
combined = data.table(combined)
ggplot(combined[deptht < 25,], aes_string(y = "deptht", x = var_name)) +
geom_path() + ylim(25,0) +
facet_wrap(~time_counter, ncol = 3)
ERROR: Cell magic `%%R` not found.
combined
time_counter | deptht | time_centered | MEAN_NO3 | DATA_SOURCE | |
---|---|---|---|---|---|
0 | 2016-04-12 00:30:00 | 0.500000 | 2016-04-12 00:30:00 | 5.214778 | IOPT_DIAT_55 |
1 | 2016-04-12 00:30:00 | 1.500003 | 2016-04-12 00:30:00 | 5.452885 | IOPT_DIAT_55 |
2 | 2016-04-12 00:30:00 | 2.500011 | 2016-04-12 00:30:00 | 5.486822 | IOPT_DIAT_55 |
3 | 2016-04-12 00:30:00 | 3.500031 | 2016-04-12 00:30:00 | 5.594474 | IOPT_DIAT_55 |
4 | 2016-04-12 00:30:00 | 4.500071 | 2016-04-12 00:30:00 | 5.920553 | IOPT_DIAT_55 |
5 | 2016-04-12 00:30:00 | 5.500151 | 2016-04-12 00:30:00 | 6.151240 | IOPT_DIAT_55 |
6 | 2016-04-12 00:30:00 | 6.500310 | 2016-04-12 00:30:00 | 6.447138 | IOPT_DIAT_55 |
7 | 2016-04-12 00:30:00 | 7.500623 | 2016-04-12 00:30:00 | 6.792811 | IOPT_DIAT_55 |
8 | 2016-04-12 00:30:00 | 8.501236 | 2016-04-12 00:30:00 | 7.200050 | IOPT_DIAT_55 |
9 | 2016-04-12 00:30:00 | 9.502433 | 2016-04-12 00:30:00 | 7.727039 | IOPT_DIAT_55 |
10 | 2016-04-12 00:30:00 | 10.504766 | 2016-04-12 00:30:00 | 8.421840 | IOPT_DIAT_55 |
11 | 2016-04-12 00:30:00 | 11.509312 | 2016-04-12 00:30:00 | 9.241469 | IOPT_DIAT_55 |
12 | 2016-04-12 00:30:00 | 12.518167 | 2016-04-12 00:30:00 | 10.177139 | IOPT_DIAT_55 |
13 | 2016-04-12 00:30:00 | 13.535412 | 2016-04-12 00:30:00 | 11.197317 | IOPT_DIAT_55 |
14 | 2016-04-12 00:30:00 | 14.568982 | 2016-04-12 00:30:00 | 12.282061 | IOPT_DIAT_55 |
15 | 2016-04-12 00:30:00 | 15.634288 | 2016-04-12 00:30:00 | 13.473866 | IOPT_DIAT_55 |
16 | 2016-04-12 00:30:00 | 16.761173 | 2016-04-12 00:30:00 | 14.781768 | IOPT_DIAT_55 |
17 | 2016-04-12 00:30:00 | 18.007135 | 2016-04-12 00:30:00 | 16.144154 | IOPT_DIAT_55 |
18 | 2016-04-12 00:30:00 | 19.481785 | 2016-04-12 00:30:00 | 17.475997 | IOPT_DIAT_55 |
19 | 2016-04-12 00:30:00 | 21.389978 | 2016-04-12 00:30:00 | 18.891814 | IOPT_DIAT_55 |
20 | 2016-04-12 00:30:00 | 24.100256 | 2016-04-12 00:30:00 | 20.516083 | IOPT_DIAT_55 |
21 | 2016-04-12 00:30:00 | 28.229916 | 2016-04-12 00:30:00 | 22.333914 | IOPT_DIAT_55 |
22 | 2016-04-12 00:30:00 | 34.685757 | 2016-04-12 00:30:00 | 24.324998 | IOPT_DIAT_55 |
23 | 2016-04-12 00:30:00 | 44.517723 | 2016-04-12 00:30:00 | 26.184282 | IOPT_DIAT_55 |
24 | 2016-04-12 00:30:00 | 58.484333 | 2016-04-12 00:30:00 | 27.685490 | IOPT_DIAT_55 |
25 | 2016-04-12 00:30:00 | 76.585587 | 2016-04-12 00:30:00 | 28.915714 | IOPT_DIAT_55 |
26 | 2016-04-12 00:30:00 | 98.062958 | 2016-04-12 00:30:00 | 29.956416 | IOPT_DIAT_55 |
27 | 2016-04-12 00:30:00 | 121.866516 | 2016-04-12 00:30:00 | 30.934335 | IOPT_DIAT_55 |
28 | 2016-04-12 00:30:00 | 147.089462 | 2016-04-12 00:30:00 | 31.828866 | IOPT_DIAT_55 |
29 | 2016-04-12 00:30:00 | 173.114487 | 2016-04-12 00:30:00 | 32.697370 | IOPT_DIAT_55 |
... | ... | ... | ... | ... | ... |
1890 | 2016-04-12 23:30:00 | 10.504766 | 2016-04-12 23:30:00 | 8.792402 | IOPT_DIAT_42 |
1891 | 2016-04-12 23:30:00 | 11.509312 | 2016-04-12 23:30:00 | 9.443269 | IOPT_DIAT_42 |
1892 | 2016-04-12 23:30:00 | 12.518167 | 2016-04-12 23:30:00 | 10.178115 | IOPT_DIAT_42 |
1893 | 2016-04-12 23:30:00 | 13.535412 | 2016-04-12 23:30:00 | 10.986300 | IOPT_DIAT_42 |
1894 | 2016-04-12 23:30:00 | 14.568982 | 2016-04-12 23:30:00 | 11.851626 | IOPT_DIAT_42 |
1895 | 2016-04-12 23:30:00 | 15.634288 | 2016-04-12 23:30:00 | 12.771132 | IOPT_DIAT_42 |
1896 | 2016-04-12 23:30:00 | 16.761173 | 2016-04-12 23:30:00 | 13.818082 | IOPT_DIAT_42 |
1897 | 2016-04-12 23:30:00 | 18.007135 | 2016-04-12 23:30:00 | 15.014769 | IOPT_DIAT_42 |
1898 | 2016-04-12 23:30:00 | 19.481785 | 2016-04-12 23:30:00 | 16.283267 | IOPT_DIAT_42 |
1899 | 2016-04-12 23:30:00 | 21.389978 | 2016-04-12 23:30:00 | 17.844842 | IOPT_DIAT_42 |
1900 | 2016-04-12 23:30:00 | 24.100256 | 2016-04-12 23:30:00 | 19.783106 | IOPT_DIAT_42 |
1901 | 2016-04-12 23:30:00 | 28.229916 | 2016-04-12 23:30:00 | 21.978578 | IOPT_DIAT_42 |
1902 | 2016-04-12 23:30:00 | 34.685757 | 2016-04-12 23:30:00 | 24.191177 | IOPT_DIAT_42 |
1903 | 2016-04-12 23:30:00 | 44.517723 | 2016-04-12 23:30:00 | 26.089551 | IOPT_DIAT_42 |
1904 | 2016-04-12 23:30:00 | 58.484333 | 2016-04-12 23:30:00 | 27.568494 | IOPT_DIAT_42 |
1905 | 2016-04-12 23:30:00 | 76.585587 | 2016-04-12 23:30:00 | 28.797396 | IOPT_DIAT_42 |
1906 | 2016-04-12 23:30:00 | 98.062958 | 2016-04-12 23:30:00 | 29.944437 | IOPT_DIAT_42 |
1907 | 2016-04-12 23:30:00 | 121.866516 | 2016-04-12 23:30:00 | 31.001501 | IOPT_DIAT_42 |
1908 | 2016-04-12 23:30:00 | 147.089462 | 2016-04-12 23:30:00 | 31.909937 | IOPT_DIAT_42 |
1909 | 2016-04-12 23:30:00 | 173.114487 | 2016-04-12 23:30:00 | 32.749400 | IOPT_DIAT_42 |
1910 | 2016-04-12 23:30:00 | 199.573044 | 2016-04-12 23:30:00 | 33.508556 | IOPT_DIAT_42 |
1911 | 2016-04-12 23:30:00 | 226.260300 | 2016-04-12 23:30:00 | 34.428200 | IOPT_DIAT_42 |
1912 | 2016-04-12 23:30:00 | 253.066635 | 2016-04-12 23:30:00 | 35.240501 | IOPT_DIAT_42 |
1913 | 2016-04-12 23:30:00 | 279.934540 | 2016-04-12 23:30:00 | 36.002680 | IOPT_DIAT_42 |
1914 | 2016-04-12 23:30:00 | 306.834198 | 2016-04-12 23:30:00 | 36.545321 | IOPT_DIAT_42 |
1915 | 2016-04-12 23:30:00 | 333.750183 | 2016-04-12 23:30:00 | 36.801954 | IOPT_DIAT_42 |
1916 | 2016-04-12 23:30:00 | 360.674530 | 2016-04-12 23:30:00 | 37.012192 | IOPT_DIAT_42 |
1917 | 2016-04-12 23:30:00 | 387.603210 | 2016-04-12 23:30:00 | 37.074555 | IOPT_DIAT_42 |
1918 | 2016-04-12 23:30:00 | 414.534088 | 2016-04-12 23:30:00 | 37.063028 | IOPT_DIAT_42 |
1919 | 2016-04-12 23:30:00 | 441.466095 | 2016-04-12 23:30:00 | NaN | IOPT_DIAT_42 |
1920 rows × 5 columns