Created by Ivan Lima on Wed May 26 2021 10:27:55 -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 Wed May 26 16:22:19 2021
sns.set_theme(context='paper', palette='tab10', rc={'figure.dpi':100})
warnings.filterwarnings('ignore')
datadir = '/home/ivan/Data/Postproc/1DPOP_CESM2/'
infile = datadir + 'cesm2.B1850.f09_g17.1dpop.003.natl.nc'
ds_tseries = xr.open_dataset(infile, decode_times=False)
ds_tseries = fixtime(ds_tseries, 1851)
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'}
zz = 25
fig, ax = plt.subplots(1,2,figsize=(12,6))
pm = ax[0].pcolormesh(ds_tseries.time.values, -ds_tseries.z_t[:zz].values/100, ds_tseries.SALT[:,:zz].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.to_index().to_datetimeindex(), ds_tseries.SALT[:,0].values, ax=ax[1])
_ = ax[1].set(xlabel='time', ylabel=ds_tseries['SALT'].units, title='surface salinity')
varlist = ['SFWF','EVAP_F','PREC_F','ROFF_F','MELT_F','IOFF_F','SALT_F','FRAZIL','IFRAC']
data_dict = {vname: ds_tseries[vname].values for vname in varlist}
for vname in ['S_INTERIOR']:
data_dict[vname] = ds_tseries[vname][:,0].values
df_fluxes = pd.DataFrame(data_dict, index=ds_tseries.time.to_index().to_datetimeindex())
df_fluxes.index.name = 'time'
df = df_fluxes.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 + ['S_INTERIOR','SALT']):
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[['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_monthly = df_fluxes.resample('M').mean()
df_fluxes_monthly['sum'] = (df_fluxes_monthly.EVAP_F + df_fluxes_monthly.PREC_F + df_fluxes_monthly.ROFF_F + df_fluxes_monthly.MELT_F +
df_fluxes_monthly.IOFF_F + df_fluxes_monthly.SALT_F + df_fluxes_monthly.FRAZIL)
df = df_fluxes_monthly[['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.75, 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_monthly
SFWF | EVAP_F | PREC_F | ROFF_F | MELT_F | IOFF_F | SALT_F | FRAZIL | IFRAC | S_INTERIOR | sum | |
---|---|---|---|---|---|---|---|---|---|---|---|
time | |||||||||||
1851-01-31 | -6.533538e-06 | -0.000063 | 0.000056 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -3.554562e-11 | -7.369134e-06 |
1851-02-28 | 1.075058e-05 | -0.000075 | 0.000085 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -2.783932e-11 | 9.808529e-06 |
1851-03-31 | -3.773256e-05 | -0.000075 | 0.000036 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -2.070065e-11 | -3.893238e-05 |
1851-04-30 | 1.612895e-05 | -0.000034 | 0.000048 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -1.505104e-11 | 1.475011e-05 |
1851-05-31 | -1.580216e-05 | -0.000038 | 0.000020 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -2.159142e-11 | -1.741568e-05 |
1851-06-30 | 4.520905e-06 | -0.000020 | 0.000022 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -2.058928e-11 | 2.520106e-06 |
1851-07-31 | 2.675369e-05 | -0.000025 | 0.000050 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 6.836893e-12 | 2.527385e-05 |
1851-08-31 | -2.081891e-05 | -0.000038 | 0.000016 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 4.419851e-12 | -2.202718e-05 |
1851-09-30 | -7.867239e-06 | -0.000046 | 0.000037 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -1.437811e-11 | -9.025152e-06 |
1851-10-31 | 3.248177e-07 | -0.000064 | 0.000063 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -1.696747e-11 | -7.687340e-07 |
1851-11-30 | 2.078587e-05 | -0.000050 | 0.000070 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -9.618006e-12 | 1.999790e-05 |
1851-12-31 | 1.026176e-05 | -0.000070 | 0.000080 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | -9.240487e-12 | 9.439122e-06 |