Created by Ivan Lima on Tue Jun 1 2021 12:50:51 -0400
%matplotlib inline
import xarray as xr
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import os, datetime, warnings, cmocean
from cesm_utils import fixtime, spd
print('Last updated on {}'.format(datetime.datetime.now().ctime()))
Last updated on Thu Jun 3 10:34:21 2021
sns.set_theme(context='paper', palette='tab10', rc={'figure.dpi':100})
warnings.filterwarnings('ignore')
datadir = '/home/ivan/Data/Postproc/1DPOP_CESM2/'
infile = datadir + 'b.e21.B1850.f09_g17.CMIP6-piControl.001.hudson_bay.nc'
ds_tseries = xr.open_dataset(infile, decode_times=False)
# ds_tseries = fixtime(ds_tseries, 1452)
ds_tseries = ds_tseries.squeeze(drop=True)
ds_tseries['FRAZIL'] = - ds_tseries.QFLUX/ds_tseries.latent_heat_fusion_mks
ds_tseries['FRAZIL'].attrs = {'long_name':'frazil ice formation', 'units':'kg/m^2/s'}
# - [Kg of salt / m^2 / s] x (1/0.0347) [ Kg of sea water / Kg of salt] x (1/1027) [m^3/kg of sea water] x 1000 [kg of freshwater / m^3]
ds_tseries['SALT_F'] = - ds_tseries.SALT_F * 1/0.0347 * 1/1027 * 1000
ds_tseries['SALT_F'].attrs = {'long_name':'fresh water flux', 'units':'kg/m^2/s'}
zz = 4
fig, ax = plt.subplots(1,2,figsize=(12,6))
pm = ax[0].pcolormesh(ds_tseries.time.values[-120:]/365, -ds_tseries.z_t[:zz].values/100, ds_tseries.SALT[-120:,:zz,0,0].transpose().values,
cmap=cmocean.cm.haline)
_ = ax[0].set(title='salinity profile at Hudson Bay', ylabel='depth (m)')
cb = fig.colorbar(pm, ax=ax[0], orientation='horizontal', pad=0.075)
cb.set_label('g/kg')
_ = sns.lineplot(ds_tseries.time[-120:]/365, ds_tseries.SALT[-120:,0,0,0].values, ax=ax[1])
_ = ax[1].set(xlabel='time', ylabel=ds_tseries['SALT'].units, title='surface salinity')
_ = ax[1].autoscale(axis='x', tight=True)
varlist = ['SFWF','EVAP_F','PREC_F','ROFF_F','MELT_F','IOFF_F','SALT_F','FRAZIL']
data_dict = {vname: ds_tseries[vname][:,0,0].values for vname in varlist}
df_fluxes = pd.DataFrame(data_dict, index=ds_tseries.time/365)
df_fluxes.index.name = 'time'
df = df_fluxes.loc[1991:].stack().reset_index().rename(columns={'level_1':'source', 0:'flux'})
g = sns.relplot(x='time', y='flux', data=df, kind='line', col='source', col_wrap=4,
height=4, facet_kws={'sharey': False, 'sharex': True})
_ = g.set_xticklabels(rotation=-45, ha='left')
_ = g.set_titles(col_template='{col_name}')
for ax, vname in zip(g.axes.flat, varlist):
ax.set_ylabel('{}'.format(ds_tseries[vname].units))
ax.autoscale(axis='x', tight=True)
df_fluxes['sum'] = df_fluxes.EVAP_F + df_fluxes.PREC_F + df_fluxes.ROFF_F + df_fluxes.MELT_F + df_fluxes.IOFF_F + df_fluxes.SALT_F + df_fluxes.FRAZIL
df = df_fluxes.loc[1991:,['SFWF','sum']].stack().reset_index().rename(columns={'level_1':'source', 0:'flux'})
g = sns.relplot(x='time', y='flux', data=df, kind='line', hue='source', alpha=0.7, aspect=1.1)
_ = g.set_xticklabels(rotation=-45, ha='left')
g.ax.set_ylabel('{}'.format(ds_tseries['SFWF'].units))
g.ax.autoscale(axis='x', tight=True)
df_fluxes['sum'] = df_fluxes.EVAP_F + df_fluxes.PREC_F + df_fluxes.MELT_F + df_fluxes.IOFF_F + df_fluxes.SALT_F + df_fluxes.FRAZIL
df = df_fluxes.loc[1991:,['SFWF','sum']].stack().reset_index().rename(columns={'level_1':'source', 0:'flux'})
g = sns.relplot(x='time', y='flux', data=df, kind='line', hue='source', alpha=0.7, aspect=1.1)
_ = g.set_xticklabels(rotation=-45, ha='left')
g.ax.set_ylabel('{}'.format(ds_tseries['SFWF'].units))
g.ax.autoscale(axis='x', tight=True)