Created by Ivan Lima on Wed Jul 22 2020 16:06:51 -0400
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import os, datetime, warnings, cmocean
from cartopy import crs as ccrs
from cartopy import feature as cfeature
from cesm_utils import fixtime, da2ma
from tqdm.notebook import trange, tqdm
warnings.filterwarnings('ignore')
print('Last updated on {}'.format(datetime.datetime.now().ctime()))
%matplotlib inline
plt.rcParams['figure.dpi'] = 110
infile = '/media/ivan/WD/Data/Postproc/1DPOP/cesm.1dpop.07.abs.max_2D.nc'
ds_max = xr.open_dataset(infile, decode_times=False,
drop_variables=['EVAP_F','ROFF_F','IFRAC','HMXL','TEMP','UVEL','VVEL','ULAT','ULONG','z_w','dz','dzw'])
ds_max = fixtime(ds_max, 2051)
stacked = ds_max.SALT.stack(z=('nlat','nlon'))
lonmax, latmax = [], []
imax, jmax = [], []
ntime = ds_max.dims['time']
for n in trange(1,11):
maxind = np.argsort(stacked.fillna(-999), axis=1)[:,-n]
maxpos = stacked['z'][maxind]
lonmax = lonmax + [maxpos.TLONG[t].item() for t in range(ntime)]
latmax = latmax + [maxpos.TLAT[t].item() for t in range(ntime)]
imax = imax + [maxpos.values[t][0] for t in range(ntime)]
jmax = jmax + [maxpos.values[t][1] for t in range(ntime)]
fig, ax = plt.subplots(subplot_kw={'projection':ccrs.Robinson(central_longitude=210)})
ax.set_global()
ax.add_feature(cfeature.LAND, facecolor='#b0b0b0')
ax.coastlines(linewidth=0.5)
for lon, lat in sorted(set([p for p in zip(lonmax,latmax)])):
print('lat = {:.2f}N, lon = {:.2f}E'.format(lat, lon))
_ = ax.plot(lon, lat, 'C3x', transform=ccrs.PlateCarree(), alpha=0.75)
_ = ax.set_title('position of salinity maxima')
# fig.savefig('salt_max_pos.png', dpi=200)
for i, j in sorted(set([p for p in zip(imax,jmax)])):
print('({},{:3d}) lat = {:.2f}N, lon = {:.2f}E'.format(i, j, ds_max.TLAT[i,j].item(), ds_max.TLONG[i,j].item()))
minlon, maxlon = 300, 330
minlat, maxlat = 52, 65
lonc = (minlon + maxlon)/2.
latc = (minlat + maxlat)/2.
proj = ccrs.EquidistantConic(central_longitude=lonc,central_latitude=latc)
# proj = ccrs.Mercator(central_longitude=lonc)
tlat, tlon = ds_max.TLAT, ds_max.TLONG
tlat, tlon = np.c_[tlat,tlat[:,0]], np.c_[tlon,tlon[:,0]]
fig, ax = plt.subplots(subplot_kw={'projection':proj}, figsize=(9,6.4))
ax.set_extent([minlon,maxlon,minlat,maxlat])
ax.coastlines(linewidth=0.5)
# ax.add_feature(cfeature.LAND, facecolor='#b0b0b0')
gl = ax.gridlines(xlocs=np.arange(-180,180,10),ylocs=np.arange(0,90,5),draw_labels=True)
gl.xlabels_top, gl.ylabels_right = False, False
pm = ax.pcolormesh(tlon, tlat, ds_max.SALT[-1,:,:], transform=ccrs.PlateCarree(), cmap=cmocean.cm.haline,
vmin=25, vmax=54.25)
cb = fig.colorbar(pm, ax=ax, orientation='horizontal', pad=0.075)
cb.set_label(ds_max.SALT.units)
_ = ax.set_title('salinity on %d-%02d-%02d'%(ds_max.time[-1]['time.year'],
ds_max.time[-1]['time.month'],
ds_max.time[-1]['time.day']))
for lon, lat in sorted(set([p for p in zip(lonmax,latmax)])):
_ = ax.plot(lon, lat, 'C3o', transform=ccrs.PlateCarree(), alpha=0.75)
minlon, maxlon = -7, 37
minlat, maxlat = 28, 45
lonc = (minlon + maxlon)/2.
latc = (minlat + maxlat)/2.
proj = ccrs.EquidistantConic(central_longitude=lonc,central_latitude=latc)
fig, ax = plt.subplots(subplot_kw={'projection':proj}, figsize=(9,6.4))
ax.set_extent([minlon,maxlon,minlat,maxlat])
ax.coastlines(linewidth=0.5)
# ax.add_feature(cfeature.LAND, facecolor='#b0b0b0')
gl = ax.gridlines(xlocs=np.arange(-180,180,10),ylocs=np.arange(0,90,5),draw_labels=True)
gl.xlabels_top, gl.ylabels_right = False, False
pm = ax.pcolormesh(tlon, tlat, ds_max.SALT[-1,:,:], transform=ccrs.PlateCarree(), cmap=cmocean.cm.haline,
vmin=36, vmax=43)
cb = fig.colorbar(pm, ax=ax, orientation='horizontal', pad=0.075)
cb.set_label(ds_max.SALT.units)
_ = ax.set_title('salinity on %d-%02d-%02d'%(ds_max.time[-1]['time.year'],
ds_max.time[-1]['time.month'],
ds_max.time[-1]['time.day']))
for lon, lat in sorted(set([p for p in zip(lonmax,latmax)])):
_ = ax.plot(lon, lat, 'C3o', transform=ccrs.PlateCarree(), alpha=0.75)
minlon, maxlon = 40, 63
minlat, maxlat = 9, 32
lonc = (minlon + maxlon)/2.
latc = (minlat + maxlat)/2.
proj = ccrs.EquidistantConic(central_longitude=lonc,central_latitude=latc)
fig, ax = plt.subplots(subplot_kw={'projection':proj}, figsize=(9,6.4))
ax.set_extent([minlon,maxlon,minlat,maxlat])
ax.coastlines(linewidth=0.5)
# ax.add_feature(cfeature.LAND, facecolor='#b0b0b0')
gl = ax.gridlines(xlocs=np.arange(-180,180,10),ylocs=np.arange(0,90,5),draw_labels=True)
gl.xlabels_top, gl.ylabels_right = False, False
pm = ax.pcolormesh(tlon, tlat, ds_max.SALT[-1,:,:], transform=ccrs.PlateCarree(), cmap=cmocean.cm.haline,
vmin=35, vmax=43)
cb = fig.colorbar(pm, ax=ax, orientation='horizontal', pad=0.075)
cb.set_label(ds_max.SALT.units)
_ = ax.set_title('salinity on %d-%02d-%02d'%(ds_max.time[-1]['time.year'],
ds_max.time[-1]['time.month'],
ds_max.time[-1]['time.day']))
for lon, lat in sorted(set([p for p in zip(lonmax,latmax)])):
_ = ax.plot(lon, lat, 'C3o', transform=ccrs.PlateCarree(), alpha=0.75)