<jupyter_start><jupyter_text>Run Ocetrac<jupyter_code>import xarray as xr
import numpy as np
import ocetrac<jupyter_output><empty_output><jupyter_text>Import MHW candidate data<jupyter_code>ds_new = xr.open_dataset('~/marine-heatwaves/data/mhw_candidates.nc')

ds_new<jupyter_output><empty_output><jupyter_text>Adjust land mask to ignore points poleward of 65ºN and 70ºS<jupyter_code>path = 'https://psl.noaa.gov/thredds/dodsC/Datasets/noaa.oisst.v2.highres/lsmask.oisst.nc'
mask = xr.open_dataset(path, use_cftime=True) 
lsmask = mask.lsmask.isel(time=0).where((mask.lat<65) & (mask.lat>-70), drop=False, other=0) 
lsmask.plot()<jupyter_output><empty_output><jupyter_text>Run Ocetrac<jupyter_code># Set model parameters
da = ds_new.mhw_ssta.load() # load the DataArray into memory
radius = 8 # radius for structuring element
min_size_quartile = 0.75 # threshold for object areas
timedim = 'time'
xdim = 'lon'
ydim = 'lat'
Tracker =  ocetrac.Tracker(da, lsmask, radius, min_size_quartile, timedim, xdim, ydim, positive=True)
%%time 
blobs = Tracker.track()<jupyter_output>minimum area: 2487.0
inital objects identified 	 12704
final objects tracked 	 779
CPU times: user 10min 16s, sys: 2min 11s, total: 12min 27s
Wall time: 12min 30s<jupyter_text>**The basic output of `Tracker.track` provides:**- the minimum object area used to filter out MHWs that are smaller than this threshold. Area is computed for all objects at each time step. `Ocetrac` looks at the global distribution of object area and finds the size that corresponds to a certain percentile defined by `area_quantile`, which can range from 0.0 to 1.0. Larger values for `area_quantile` exclude more MHW objects and will increase the minimum object area threshold. - inital features identified have undergone size thresholding, however are not connected in time. - final features tracked are the number of unique MHW objects tracked in space and time.<jupyter_code>blobs.mean(('lat','lon')).plot()
from matplotlib.colors import ListedColormap
maxl = int(np.nanmax(blobs.values))
cm = ListedColormap(np.random.random(size=(maxl, 3)).tolist())

# Make a quick plot of the labeled MHWs identified with Ocetrac
blobs.sel(time=('2013-12-01')).plot(cmap= cm)<jupyter_output><empty_output><jupyter_text>Save tracked labels<jupyter_code>blobs.attrs
# xarray Dataset to save
ds_out = xr.Dataset(
    data_vars=dict(
        blobs=(['time', 'lat', 'lon'], blobs.values, blobs.attrs),
        lsmask=(['lat', 'lon'], lsmask.values),

    ),
    coords=dict(
        lon=ds_new.lon,
        lat=ds_new.lat,
        time=ds_new.time,
    ),
    attrs=dict(description="Ocetrac labels",
               data="OISST v2.1 preprocessed for Ocetrac",
               threshold='90th percentile',
               climatology='entire period'),
)
ds_out
ds_out.to_netcdf('~/marine-heatwaves/data/ocetrac_labels.nc')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11061026352288489, 'fraction_numerical': 0.020804438280166437, 'mean_word_length': 5.060924369747899, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '633885', 'license': 'MIT', 'n_tokens_mistral': 1065, 'n_tokens_neox': 996, 'n_words': 261, 'path': 'code/run_ocetrac.ipynb', 'repo_name': 'ocetrac/marine-heatwaves', 'size': '2884'}