<jupyter_start><jupyter_text>Use `Ocetrac` to identify and track marine heatwaves<jupyter_code>import xarray as xr
import numpy as np
import pandas as pd
import dask.array as da
import ocetrac

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature

import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Import preprocessed data<jupyter_code>ds = xr.open_dataset('01_preprocessed.nc', engine='netcdf4')
ds
%%time 
features = ds.features_notrend
features.load()
# Binary conversion
binary_out = np.isfinite(features)
binary_out.isel(time=0).plot()
mask = ds.mask.where((ds.lat<65) & (ds.lat>-70), drop=False, other=0) 
mask.plot()
%%time
Tracker = ocetrac.Tracker(binary_out, mask, radius=8, min_size_quartile=0.75, timedim='time', xdim='lon', ydim='lat', positive=True)
blobs = Tracker.track()
blobs.attrs
blobs.sel(time='01-01-2014').plot()
from matplotlib.colors import ListedColormap
maxl = int(np.nanmax(blobs.values))
cm = ListedColormap(np.random.random(size=(maxl, 3)).tolist())

blobs.sel(time=slice('01-01-2014', '01-01-2015')).plot(col='time', col_wrap=4, cmap=cm)
blobs.to_netcdf('02_tracked.nc', mode='w')


plt.figure(figsize=(16,3))
ax1 = plt.subplot(121);blobs.isel(time=0).plot(cmap= cm)
mask.where(mask==0).plot.contourf(colors='k', add_colorbar=False); ax1.set_aspect('equal')
ax2 = plt.subplot(122); features_ssta.isel(time=0).plot(cmap='Reds', vmin=0, vmax=3);
mask.where(mask==0).plot.contourf(colors='k', add_colorbar=False); ax2.set_aspect('equal');
id = 35
event = blobs.where(blobs==id, drop=True)
event.shape
# Create intensity image for the Indian Ocean marine heatwave
event_intensity = ds.anom.isel(zlev=0).where((ds.time==event.time) & 
                                             (ds.lat==event.lat) & 
                                             (ds.lon==event.lon), 
                                             drop=True).load();
event_intensity = event_intensity.expand_dims(dim='intensity', axis=3)
events_contour = event.fillna(0)

plt.rc('font', size=12); plt.figure(figsize=(15,6));
for i in enumerate(range(1,len(event.time))):
    ax = plt.subplot(2,3,i[1],projection=ccrs.PlateCarree())
    event_intensity.isel(time=i[0], intensity=0).plot(transform=ccrs.PlateCarree(), vmin=-2, vmax=2,  
                                                      cmap='RdBu_r', extend='both', add_colorbar=True, add_labels=False)
    plt.title(event.isel(time=i[0]).time.values.astype('datetime64[D]'))
    ax.coastlines(resolution='110m', color='black', linewidth=1) 
    ax.add_feature(cfeature.LAND, facecolor='w');
    events_contour.isel(time=i[0]).plot.contour(levels=[34,35], transform=ccrs.PlateCarree(), colors='b', linewidths=4, add_colorbar=False, add_labels=False)
mo = Tracker._morphological_operations()
mo.isel(time=0).plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16324399582318133, 'fraction_numerical': 0.03202227636616777, 'mean_word_length': 4.713717693836978, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '626763', 'license': 'MIT', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1032, 'n_words': 161, 'path': 'notebooks/02_track.ipynb', 'repo_name': 'ocetrac/ocetrac', 'size': '2873'}