Created by Ivan Lima on 2021-08-14 16:23:49
import xarray as xr
import pandas as pd
import numpy as np
import hvplot.pandas
import hvplot.xarray
import os, datetime, warnings
import cartopy.crs as ccrs
from cesm_utils import fixtime, spd
from my_bokeh_themes import *
warnings.filterwarnings('ignore')
print('Last updated on {}'.format(datetime.datetime.now().ctime()))
Last updated on Wed Sep 15 13:08:52 2021
from pencil_cases import *
case = 'cesm2.B1850.f09_g17.1dpop.005'
datadir = '/home/ivan/Data/Postproc/1DPOP_CESM2/'
# datadir = '/mnt/chromeos/removable/WD/Data/Postproc/1DPOP_CESM2/'
infile = datadir + '{}.abs.max_2D.nc'.format(case)
ds_max_2D = xr.open_dataset(infile, decode_times=False, chunks='auto',
drop_variables=['HMXL','TEMP','UVEL','VVEL','ULAT','ULONG','z_w','dz','dzw'])
ds_max_2D = fixtime(ds_max_2D, 1851)
print('{}: {}'.format(case, case_info[case]))
stacked = ds_max_2D.SALT.stack(z=('nlat','nlon'))
maxind = stacked.argmax(axis=1)
maxpos = stacked['z'][maxind]
ntime = ds_max_2D.dims['time']
imax = [maxpos.values[t][0] for t in range(ntime)]
jmax = [maxpos.values[t][1] for t in range(ntime)]
cesm2.B1850.f09_g17.1dpop.005: 1D + G-terms + SSS restoring (tau = 30 days) + EBM off
smax = ds_max_2D.SALT.max(dim='time')
data = []
tlat, tlon = ds_max_2D.TLAT.values, ds_max_2D.TLONG.values
for i, j in sorted(set([p for p in zip(imax,jmax)])):
data.append([smax.values[i,j], tlat[i,j], tlon[i,j], i, j])
df = pd.DataFrame(data, columns=['SALT', 'lat', 'lon', 'i', 'j'])
df
SALT | lat | lon | i | j | |
---|---|---|---|---|---|
0 | 85.994736 | -74.946788 | 325.062509 | 8 | 4 |
df.hvplot.points('lon', 'lat', geo=True, color='red', marker='x', coastline=True, global_extent=True, frame_width=400, tiles='EsriTerrain')
# df.hvplot.points('lon', 'lat', geo=True, color='red', marker='x', coastline=True, frame_width=400, tiles='EsriTerrain')
# df.hvplot.points?
# from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature as sst
# sst.hvplot()
ds_max = xr.open_dataset(os.path.join(datadir, '{}.abs.max.nc'.format(case)), decode_times=False)
ds_max = fixtime(ds_max, 1851)
ds_max['time'] = ds_max.time.to_index().to_datetimeindex()
data_dict = {vname: ds_max[vname].to_series() for vname in ['HMXL','TEMP','SALT','UVEL','VVEL']}
df_series = pd.DataFrame(data_dict)#.reset_index()
df_series.head()
HMXL | TEMP | SALT | UVEL | VVEL | |
---|---|---|---|---|---|
time | |||||
1851-01-01 | 52526.050781 | 32.273994 | 45.962933 | 136.165512 | 90.186417 |
1851-01-02 | 78700.289062 | 32.121567 | 45.936596 | 95.371529 | 92.088448 |
1851-01-03 | 107083.554688 | 32.048378 | 45.926575 | 82.971550 | 102.034966 |
1851-01-04 | 106188.929688 | 32.000782 | 45.914780 | 74.679840 | 91.994476 |
1851-01-05 | 105998.046875 | 32.021751 | 45.903111 | 71.240166 | 82.425491 |
plist = df_series.hvplot.line(aspect=1, frame_height=350, subplots=True, shared_axes=False, grid=True).cols(3)
for p in plist.items():
# print(p[0], ds_max[p[0]].units)
p[1].opts(ylabel=ds_max[p[0]].units)
plist
infile = datadir + '{}.weddell_sea.nc'.format(case)
ds_tseries = xr.open_dataset(infile, decode_times=False)
ds_tseries = fixtime(ds_tseries, 1851)
ds_tseries['time'] = ds_tseries.time.to_index().to_datetimeindex()
ds_tseries = ds_tseries.squeeze(drop=True)
subset = ds_tseries.SALT.sel(time=slice('1930-01-01','1950-12-31'), z_t=slice(500,6.3886262e+04))
df_salt = subset.to_dataframe().reset_index()
# df_salt = ds_tseries.SALT.to_dataframe().reset_index()
df_salt['z_t'] = df_salt.z_t / 100 # cm -> m
df_salt.hvplot.heatmap(x='time', y='z_t', C='SALT', cmap='bgy', height=400, title='Salinity - Weddell Sea',
clabel='SALT ({})'.format(ds_tseries.SALT.units)).opts(invert_yaxis=True)