<jupyter_start><jupyter_text>Ocean surface Eddy Kinetic Energy trends This Jupyter notebook shows how to compute the EKE trend maps reported in the manuscript "Global changes in oceanic mesoscale currents over the satellite altimetry record".<jupyter_code># Import libraries
import xarray as xr
import cmocean as cm
import cartopy.crs as ccrs

import pylab as plt
import numpy as np
# Inline plotting
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Import Mann Kendall test:<jupyter_code>from xarrayMannKendall import Mann_Kendall_test
from utils import area,ccrs_land,add_patches
import datetime as datetime
from dask.distributed import Client
c = Client()
c
dataset_EKE = xr.open_dataset('../datasets/EKE_timeseries.nc',chunks={'lat':10,'lon':10})
rho=1025 #kg/m^3
EKE_density = rho*dataset_EKE.EKE
EKE = EKE_density.sortby('time').sel(time=slice('1993','2020'))
# Print function used.
?Mann_Kendall_test<jupyter_output><empty_output><jupyter_text>For more information in the Mann-Kendall method, please refer to: https://doi.org/10.1023/B:WARM.0000043140.61082.60 The reported trends use a modified Mann-Kendall method. A modified Mann-Kendall test is used to assess statistical significance of trends, while properly taking into account the autocorrelation in the time-series. The effective sample size for all the reported trends is always smaller than the actual sample size, due to autocorrelation of the time series.<jupyter_code>tke_trends = Mann_Kendall_test(EKE,'time',MK_modified=True,
                               method="linregress",alpha=0.05, 
                               coords_name = {'time':'time','x':'lon','y':'lat'})<jupyter_output><empty_output><jupyter_text>Note that the following cell will take ~1 hour depending on the number of cores, as the Mann-Kendall method requirest to invert a matrix of shape (len(time),len(time)) for each grid point.<jupyter_code>EKE_trends = tke_trends.compute()
EKE_trends.attrs['title'] = "Eddy Kinetic Energy"
EKE_trends.attrs['Description'] = """Eddy Kinetic Energy computed from AVISO+ altimetry. Then trends were computed using a modified Mann-Kendall test. \n See: https://github.com/josuemtzmo/xarrayMannKendall."""
EKE_trends.attrs['Publication'] = "Dataset created for Martínez-Moreno, J. et. al. 2020: \n 'Mesoscale kinetic energy response to changing oceans'"
EKE_trends.attrs['Author'] = "<NAME>"
EKE_trends.attrs['Contact'] = "<EMAIL>"

EKE_trends.attrs['Created date'] = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")

######################################################

EKE_trends['trend'].attrs['units'] = r"$J m^{-3} day^{-1}$"
EKE_trends['trend'].attrs['name'] = 'trend'
EKE_trends['trend'].attrs['long_name'] = "Eddy Kinetic Energy trends"

EKE_trends['trend'].attrs['missing_value'] = np.nan
EKE_trends['trend'].attrs['valid_min'] = np.nanmin(EKE_trends['trend'])
EKE_trends['trend'].attrs['valid_max'] = np.nanmax(EKE_trends['trend'])
EKE_trends['trend'].attrs['valid_range'] = [np.nanmin(EKE_trends['trend']),np.nanmax(EKE_trends['trend'])]

######################################################

EKE_trends['signif'].attrs['units'] = ""
EKE_trends['signif'].attrs['name'] = 'signif'
EKE_trends['signif'].attrs['long_name'] = "Eddy Kinetic Energy trends significance"

EKE_trends['signif'].attrs['missing_value'] = np.nan
EKE_trends['signif'].attrs['valid_min'] = np.nanmin(EKE_trends['signif'])
EKE_trends['signif'].attrs['valid_max'] = np.nanmax(EKE_trends['signif'])
EKE_trends['signif'].attrs['valid_range'] = [np.nanmin(EKE_trends['signif']),np.nanmax(EKE_trends['signif'])]

######################################################

EKE_trends['p'].attrs['units'] = ""
EKE_trends['p'].attrs['name'] = 'p'
EKE_trends['p'].attrs['long_name'] = "Eddy Kinetic Energy trends p"

EKE_trends['p'].attrs['missing_value'] = np.nan
EKE_trends['p'].attrs['valid_min'] = np.nanmin(EKE_trends['p'])
EKE_trends['p'].attrs['valid_max'] = np.nanmax(EKE_trends['p'])
EKE_trends['p'].attrs['valid_range'] = [np.nanmin(EKE_trends['p']),np.nanmax(EKE_trends['p'])]
comp = dict(zlib=True, complevel=5)
encoding = {var: comp for var in EKE_trends.data_vars}

EKE_trends.to_netcdf('../datasets/EKE_trends.nc', encoding=encoding)
EKE_trends
trends = EKE_trends.trend*10*365 # Convert to trends per decade
fig = plt.figure(figsize=(6,2), dpi=200)
ax = fig.add_subplot(1, 1, 1, projection = ccrs.Robinson(central_longitude=180))

im = trends.plot(transform=ccrs.PlateCarree(),cbar_kwargs=dict(label="$J\ m^{-3}\ decade^{-1}$"),
              cmap=cm.cm.balance, vmin=-10, vmax=10,ax=ax)
ax.set_extent([0.1,359.99,-60, 60], crs=ccrs.PlateCarree())
ax.add_feature(ccrs_land)
add_patches(ax)<jupyter_output><empty_output><jupyter_text>EKE decomposition trends<jupyter_code>dataset_EKE = xr.open_dataset('../datasets/EKE_decomposition_timeseries.nc',chunks={'lat':10,'lon':10})
dataset_EKE<jupyter_output><empty_output><jupyter_text>Mesoscale EKE trends<jupyter_code>EKE_m_density = dataset_EKE.EKE_m
EKE_m = EKE_m_density.sortby('time').sel(time=slice('1993','2020'))

eke_m_trends = Mann_Kendall_test(EKE_m,'time',MK_modified=True,
                               method="linregress",alpha=0.05, 
                               coords_name = {'time':'time','x':'lon','y':'lat'})
EKE_m_trends = eke_m_trends.compute()
EKE_m_trends.attrs['title'] = "Mesoscale Eddy Kinetic Energy trends"
EKE_m_trends.attrs['Description'] = """Mesoscale Eddy Kinetic Energy computed from AVISO+ altimetry. Then trends were computed using a modified Mann-Kendall test. \n See: https://github.com/josuemtzmo/xarrayMannKendall."""
EKE_m_trends.attrs['Publication'] = "Dataset created for Martínez-Moreno, J. et. al. 2020: \n 'Mesoscale kinetic energy response to changing oceans'"
EKE_m_trends.attrs['Author'] = "<NAME>"
EKE_m_trends.attrs['Contact'] = "<EMAIL>"

EKE_m_trends.attrs['Created date'] = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")

######################################################

EKE_m_trends['trend'].attrs['units'] = r"$J m^{-3} day^{-1}$"
EKE_m_trends['trend'].attrs['name'] = 'trend'
EKE_m_trends['trend'].attrs['long_name'] = "Mesoscale Eddy Kinetic Energy trends"

EKE_m_trends['trend'].attrs['missing_value'] = np.nan
EKE_m_trends['trend'].attrs['valid_min'] = np.nanmin(EKE_m_trends['trend'])
EKE_m_trends['trend'].attrs['valid_max'] = np.nanmax(EKE_m_trends['trend'])
EKE_m_trends['trend'].attrs['valid_range'] = [np.nanmin(EKE_m_trends['trend']),np.nanmax(EKE_m_trends['trend'])]

######################################################

EKE_m_trends['signif'].attrs['units'] = ""
EKE_m_trends['signif'].attrs['name'] = 'signif'
EKE_m_trends['signif'].attrs['long_name'] = "Mesoscale Eddy Kinetic Energy trends significance"

EKE_m_trends['signif'].attrs['missing_value'] = np.nan
EKE_m_trends['signif'].attrs['valid_min'] = np.nanmin(EKE_m_trends['signif'])
EKE_m_trends['signif'].attrs['valid_max'] = np.nanmax(EKE_m_trends['signif'])
EKE_m_trends['signif'].attrs['valid_range'] = [np.nanmin(EKE_m_trends['signif']),np.nanmax(EKE_m_trends['signif'])]

######################################################

EKE_m_trends['p'].attrs['units'] = ""
EKE_m_trends['p'].attrs['name'] = 'p'
EKE_m_trends['p'].attrs['long_name'] = "Mesoscale Eddy Kinetic Energy trends p"

EKE_m_trends['p'].attrs['missing_value'] = np.nan
EKE_m_trends['p'].attrs['valid_min'] = np.nanmin(EKE_m_trends['p'])
EKE_m_trends['p'].attrs['valid_max'] = np.nanmax(EKE_m_trends['p'])
EKE_m_trends['p'].attrs['valid_range'] = [np.nanmin(EKE_m_trends['p']),np.nanmax(EKE_m_trends['p'])]
comp = dict(zlib=True, complevel=5)
encoding = {var: comp for var in EKE_m_trends.data_vars}

EKE_m_trends.to_netcdf('../datasets/mesoscale_EKE_trends.nc', encoding=encoding)<jupyter_output><empty_output><jupyter_text>Large-scale EKE trends<jupyter_code>EKE_ls_density = dataset_EKE.EKE_ls
EKE_ls = EKE_ls_density.sortby('time').sel(time=slice('1993','2020'))
eke_ls_trends = Mann_Kendall_test(EKE_ls,'time',MK_modified=True,
                               method="linregress",alpha=0.05, 
                               coords_name = {'time':'time','x':'lon','y':'lat'})
EKE_ls_trends = eke_ls_trends.compute()
EKE_ls_trends.attrs['title'] = "Large-scale Eddy Kinetic Energy trends"
EKE_ls_trends.attrs['Description'] = """Mesoscale Eddy Kinetic Energy computed from AVISO+ altimetry. Then trends were computed using a modified Mann-Kendall test. \n See: https://github.com/josuemtzmo/xarrayMannKendall."""
EKE_ls_trends.attrs['Publication'] = "Dataset created for Martínez-Moreno, J. et. al. 2020: \n 'Mesoscale kinetic energy response to changing oceans'"
EKE_ls_trends.attrs['Author'] = "<NAME>"
EKE_ls_trends.attrs['Contact'] = "<EMAIL>"

EKE_ls_trends.attrs['Created date'] = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")

######################################################

EKE_ls_trends['trend'].attrs['units'] = r"$J m^{-3} day^{-1}$"
EKE_ls_trends['trend'].attrs['name'] = 'trend'
EKE_ls_trends['trend'].attrs['long_name'] = "Large-scale Eddy Kinetic Energy trends"

EKE_ls_trends['trend'].attrs['missing_value'] = np.nan
EKE_ls_trends['trend'].attrs['valid_min'] = np.nanmin(EKE_ls_trends['trend'])
EKE_ls_trends['trend'].attrs['valid_max'] = np.nanmax(EKE_ls_trends['trend'])
EKE_ls_trends['trend'].attrs['valid_range'] = [np.nanmin(EKE_ls_trends['trend']),np.nanmax(EKE_ls_trends['trend'])]

######################################################

EKE_ls_trends['signif'].attrs['units'] = ""
EKE_ls_trends['signif'].attrs['name'] = 'signif'
EKE_ls_trends['signif'].attrs['long_name'] = "Large-scale Eddy Kinetic Energy trends significance"

EKE_ls_trends['signif'].attrs['missing_value'] = np.nan
EKE_ls_trends['signif'].attrs['valid_min'] = np.nanmin(EKE_ls_trends['signif'])
EKE_ls_trends['signif'].attrs['valid_max'] = np.nanmax(EKE_ls_trends['signif'])
EKE_ls_trends['signif'].attrs['valid_range'] = [np.nanmin(EKE_ls_trends['signif']),np.nanmax(EKE_ls_trends['signif'])]

######################################################

EKE_ls_trends['p'].attrs['units'] = ""
EKE_ls_trends['p'].attrs['name'] = 'p'
EKE_ls_trends['p'].attrs['long_name'] = "Large-scale Eddy Kinetic Energy trends p"

EKE_ls_trends['p'].attrs['missing_value'] = np.nan
EKE_ls_trends['p'].attrs['valid_min'] = np.nanmin(EKE_ls_trends['p'])
EKE_ls_trends['p'].attrs['valid_max'] = np.nanmax(EKE_ls_trends['p'])
EKE_ls_trends['p'].attrs['valid_range'] = [np.nanmin(EKE_ls_trends['p']),np.nanmax(EKE_ls_trends['p'])]
comp = dict(zlib=True, complevel=5)
encoding = {var: comp for var in EKE_ls_trends.data_vars}

EKE_ls_trends.to_netcdf('../datasets/largescale_EKE_trends.nc', encoding=encoding)<jupyter_output><empty_output><jupyter_text>Plot trends<jupyter_code>fig = plt.figure(figsize=(6,4), dpi=200)
ax = fig.add_subplot(2, 1, 1, projection = ccrs.Robinson(central_longitude=180))

im = (EKE_ls_trends.trend*10*365).plot(transform=ccrs.PlateCarree(),cbar_kwargs=dict(label="$J\ m^{-3}\ decade^{-1}$"),
              cmap=cm.cm.balance,vmin=-10,vmax=10,ax=ax)
ax.set_extent([0.1,359.99,-60, 60], crs=ccrs.PlateCarree())
ax.add_feature(ccrs_land)
ax.set_title('Large-scale EKE trends')
add_patches(ax)

ax = fig.add_subplot(2, 1, 2, projection = ccrs.Robinson(central_longitude=180))

im = (EKE_m_trends.trend*10*365).plot(transform=ccrs.PlateCarree(),cbar_kwargs=dict(label="$J\ m^{-3}\ decade^{-1}$"),
              cmap=cm.cm.balance,vmin=-10,vmax=10,ax=ax)
ax.set_extent([0.1,359.99,-60, 60], crs=ccrs.PlateCarree())
ax.add_feature(ccrs_land)
ax.set_title('Mesoscale EKE trends')
add_patches(ax)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21128948731227343, 'fraction_numerical': 0.015967547039530468, 'mean_word_length': 7.433042212518195, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '493174', 'license': 'MIT', 'n_tokens_mistral': 4698, 'n_tokens_neox': 4368, 'n_words': 642, 'path': 'trends/Compute_EKE_trends.ipynb', 'repo_name': 'josuemtzmo/EKE_SST_trends', 'size': '11703'}