Created by Ivan Lima on Tue Aug 16 2022 08:47:06 -0400
import xarray as xr
import pandas as pd
import numpy as np
import holoviews as hv
import hvplot.xarray, hvplot.pandas
import os, datetime, warnings, my_bokeh_themes
from cesm_utils import spd
warnings.filterwarnings('ignore')
print('Last updated on {}'.format(datetime.datetime.now().ctime()))
Last updated on Tue Aug 16 16:08:19 2022
def fixtime(ds_in):
ds_out = ds_in.assign(time = ds_in.time.values - 1)
ds_out['time'] = ds_out.time.assign_attrs(**ds_in.time.attrs)
return xr.decode_cf(ds_out)
datadir = '/home/ivan/Data/Postproc/1DPOP_CESM2'
ds = xr.open_dataset(os.path.join(datadir, 'b.e21.B1850.f09_g17.CMIP6-piControl.001.ts_global.nc'), decode_times=False)
ds = fixtime(ds)
ds = ds.resample(time='A').mean() # convert monthly to annual means
ds['time'] = ds.time.dt.year
df = ds[['SHF', 'SFWF', 'QFLUX', 'MELTH_F', 'MELT_F']].to_dataframe()
df.index.name = 'year'
latent_heat_fusion_mks = 333700.
df['SHF+QFLUX'] = df.SHF + df.QFLUX
df['-QFLUX/L_f'] = - df.QFLUX/latent_heat_fusion_mks
df['SFWF-QFLUX/L_f'] = df.SFWF - df.QFLUX/latent_heat_fusion_mks
df['MELTH_F+QFLUX'] = df.MELTH_F + df.QFLUX
df['MELT_F-QFLUX/L_f'] = df.MELT_F - df.QFLUX/latent_heat_fusion_mks
df['SHF-MELTH_F'] = df.SHF - df.MELTH_F
df['SFWF-MELT_F'] = df.SFWF - df.MELT_F
units = {'SHF':'watt/m^2', 'SFWF':'kg/m^2/s', 'QFLUX':'watt/m^2', 'MELTH_F':'watt/m^2', 'MELT_F':'kg/m^2/s'}
units['-QFLUX/L_f'] = units['SFWF']
units['SHF+QFLUX'] = units['SHF']
units['SFWF-QFLUX/L_f'] = units['SFWF']
units['MELTH_F+QFLUX'] = units['MELTH_F']
units['MELT_F-QFLUX/L_f'] = units['MELT_F']
units['SHF-MELTH_F'] = units['SHF']
units['SFWF-MELT_F'] = units['SFWF']
# df.head()
The red dot marks the year 1161
plist = hv.Layout()
aspect, height = 1.6, 350
for vname in df.columns:
ts = df[vname].hvplot(aspect=aspect, frame_height=height, grid=True, title='Global annual mean {}'.format(vname), ylabel=units[vname], legend=False)
sp = hvplot.plot(pd.DataFrame({vname: df.loc[1161,vname]}, index=[1161]), kind='scatter', aspect=aspect, frame_height=height, grid=True, color='red')
plist += ts * sp
plist.cols(2).opts(shared_axes=False)
ds_monthly = xr.open_dataset(os.path.join(datadir, 'b.e21.B1850.f09_g17.CMIP6-piControl.001.ts_global.nc'), decode_times=False)
ds_monthly = fixtime(ds_monthly)
ds_monthly = ds_monthly.sel(time=slice('1151-01-31','1171-01-31'))
df_monthly = ds_monthly[['SHF', 'SFWF', 'QFLUX', 'MELTH_F', 'MELT_F']].to_dataframe()
df_monthly['SHF+QFLUX'] = df_monthly.SHF + df_monthly.QFLUX
df_monthly['-QFLUX/L_f'] = - df_monthly.QFLUX/latent_heat_fusion_mks
df_monthly['SFWF-QFLUX/L_f'] = df_monthly.SFWF - df_monthly.QFLUX/latent_heat_fusion_mks
df_monthly['MELTH_F+QFLUX'] = df_monthly.MELTH_F + df_monthly.QFLUX
df_monthly['MELT_F-QFLUX/L_f'] = df_monthly.MELT_F - df_monthly.QFLUX/latent_heat_fusion_mks
df_monthly['SHF-MELTH_F'] = df_monthly.SHF - df_monthly.MELTH_F
df_monthly['SFWF-MELT_F'] = df_monthly.SFWF - df_monthly.MELT_F
The red dot marks the date 1161-01
plist2 = hv.Layout()
# dd = datetime.datetime.strptime('1161-01-31 00:00:00','%Y-%m-%d %H:%M:%S')
dd = df_monthly.iloc[120].name
for vname in df_monthly.columns:
ts = df_monthly[vname].hvplot(aspect=aspect, frame_height=height, grid=True, title='Global monthly mean {}'.format(vname),
ylabel=units[vname], legend=False)
sp = hvplot.plot(pd.DataFrame({vname: df_monthly.loc['1161-01-31 00:00:00',vname]}, index=[dd]), kind='scatter',
aspect=aspect, frame_height=height, grid=True, color='red')
plist2 += ts * sp
plist2.cols(2).opts(shared_axes=False)
WARNING:param.CurvePlot14229: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot14246: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot14343: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot14360: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot14456: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot14473: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot14569: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot14586: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot14682: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot14699: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot14795: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot14812: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot14908: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot14925: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot15021: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot15038: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot15134: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot15151: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot15247: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot15264: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot15360: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot15377: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.CurvePlot15473: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend. WARNING:param.PointPlot15490: Converting cftime.datetime from a non-standard calendar (noleap) to a standard calendar for plotting. This may lead to subtle errors in formatting dates, for accurate tick formatting switch to the matplotlib backend.