Created by Ivan Lima on Thu Jul 23 2020 10:02:21 -0400
%matplotlib inline
import xarray as xr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os, datetime, warnings
from cesm_utils import fixtime, spd
print('Last updated on {}'.format(datetime.datetime.now().ctime()))
Last updated on Mon Oct 12 11:40:22 2020
warnings.filterwarnings('ignore')
plt.rcParams['figure.dpi'] = 100
plt.rcParams['xtick.minor.bottom'] = False # fix strange minor ticks in time series plots
infile = '/media/ivan/WD/Data/Postproc/1DPOP/cesm.1dpop.03.surf.nc'
ds_restore = xr.open_dataset(infile, decode_times=False)
ds_restore = fixtime(ds_restore, 2051)
ds_restore['time'] = ds_restore.time.to_index().to_datetimeindex()
ds_restore = ds_restore.squeeze('z_t', drop=True)
infile = '/media/ivan/WD/Data/Postproc/1DPOP/cesm.1dpop.09.surf.nc'
ds_gterm = xr.open_dataset(infile, decode_times=False)
ds_gterm = fixtime(ds_gterm, 2051)
ds_gterm['time'] = ds_gterm.time.to_index().to_datetimeindex()
ds_gterm = ds_gterm.squeeze('z_t', drop=True)
points = {'Greenland': (353,316), 'East China Sea': (274,144), 'Persian Gulf': (273, 79),
'Bering Sea': (330, 193)}
varlist = ['EVAP_F', 'PREC_F', 'ROFF_F','SALT']
for loc in sorted(points):
ii, jj = points[loc]
fig, axs = plt.subplots(2, 2, sharex=True, figsize=(9,7))
fig.subplots_adjust(wspace=0.3)
for vname, ax in zip(varlist, axs.ravel()):
df = pd.DataFrame({'restore' : ds_restore[vname][:,ii,jj].to_series(),
'G-term' : ds_gterm[vname][:,ii,jj].to_series()})
_ = df.plot(ax=ax, alpha=0.8, title=vname)
_ = ax.set(ylabel='{} ({})'.format(vname, ds_restore[vname].units))
fig.suptitle(loc)
fluxes = ['EVAP_F', 'PREC_F', 'ROFF_F']
varlist2 = ['SALT','HMXL','IFRAC']
for loc in sorted(points):
ii, jj = points[loc]
fig, axs = plt.subplots(2, 2, sharex=True, figsize=(9,7))
fig.subplots_adjust(wspace=0.3)
for vname, ax in zip(varlist2, axs.ravel()[:-1]):
df = pd.DataFrame({'restore' : ds_restore[vname][:,ii,jj].to_series(),
'G-term' : ds_gterm[vname][:,ii,jj].to_series()})
_ = df.plot(ax=ax, alpha=0.8, title=vname)
_ = ax.set(ylabel='{} ({})'.format(vname, ds_restore[vname].units))
df_tot = pd.DataFrame({
'restore': pd.DataFrame({vname: ds_restore[vname][:,ii,jj].to_series() for vname in fluxes}).sum(axis=1),
'G-term' : pd.DataFrame({vname: ds_gterm[vname][:,ii,jj].to_series() for vname in fluxes}).sum(axis=1)
})
_ = df_tot.plot(ax=axs[-1,-1], alpha=0.8, title='EVAP_F + PREC_F + ROFF_F')
_ = axs[-1,-1].set(ylabel='EVAP_F + PREC_F + ROFF_F ({})'.format(ds_restore['EVAP_F'].units))
fig.suptitle(loc)
for loc in sorted(points):
ii, jj = points[loc]
# fig, axs = plt.subplots(1, 2, sharex=True, figsize=(9.5,4.3))
fig, axs = plt.subplots(2, 2, sharex=True, figsize=(9,7))
axs = axs.ravel()
fig.subplots_adjust(wspace=0.3)
df1 = pd.DataFrame({'restore' : ds_restore['SALT'][:,ii,jj].to_series(),
'G-term' : ds_gterm['SALT'][:,ii,jj].to_series()})
_ = df1.plot(ax=axs[0], alpha=0.8, title='salinity')
_ = axs[0].set(ylabel='SALT ({})'.format(ds_restore['SALT'].units))
df2 = pd.DataFrame({'restore' : ds_restore['SFWF'][:,ii,jj].to_series().cumsum(axis=0),
'G-term' : ds_gterm['SFWF'][:,ii,jj].to_series().cumsum(axis=0)})
_ = df2.plot(ax=axs[1], alpha=0.8, title='cumulative virtual salinity flux')
_ = axs[1].set(ylabel='$\sum$SFWF ({})'.format(ds_restore['SFWF'].units))
df3 = pd.DataFrame({'restore' : ds_restore['SFWF'][:,ii,jj].to_series(),
'G-term' : ds_gterm['SFWF'][:,ii,jj].to_series()})
_ = df3.plot(ax=axs[2], alpha=0.8, title='virtual salinity flux')
_ = axs[2].set(ylabel='SFWF ({})'.format(ds_restore['SFWF'].units))
df4 = pd.DataFrame({'restore' : ds_restore['S_INTERIOR'][:,ii,jj].to_series(),
'G-term' : ds_gterm['SFWF'][:,ii,jj].to_series()})
_ = df4.restore.plot(ax=axs[3], alpha=0.8, title='salinity G/restoring term')
_ = axs[3].set(ylabel='surface S_INTERIOR ({})'.format(ds_restore['S_INTERIOR'].units))
fig.suptitle(loc)
infile = '/media/ivan/WD/Data/Postproc/1DPOP/forcing/gterms_restoring_monthly_clim.nc'
ds_forcing = xr.open_dataset(infile, decode_times=False)
ds_forcing = fixtime(ds_forcing, 2051)
# ds_forcing
for loc in sorted(points):
ii, jj = points[loc]
fig, ax = plt.subplots()
_ = ax.plot(ds_forcing.time, ds_forcing.GS[:,0,ii,jj])
_ = ax.set(title='input GS', ylabel='surface GS ({})'.format(ds_forcing['GS'].units))
fig.suptitle(loc)
# fig, ax = plt.subplots()
# df_tot.restore.resample('W').mean().plot(ax=ax)
# ds_restore.TAREA[ii,jj].item() * 1.e-4 * spd