.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "python_module/12_external_data/pet_drifter_loopers.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_python_module_12_external_data_pet_drifter_loopers.py: Colocate looper with eddy from altimetry ======================================== All loopers data used in this example are a subset from the dataset described in this article [Lumpkin, R. : Global characteristics of coherent vortices from surface drifter trajectories](https://doi.org/10.1002/2015JC011435) .. GENERATED FROM PYTHON SOURCE LINES 8-21 .. code-block:: Python import re from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation import numpy as np import py_eddy_tracker_sample from py_eddy_tracker import data from py_eddy_tracker.appli.gui import Anim from py_eddy_tracker.observations.tracking import TrackEddiesObservations .. GENERATED FROM PYTHON SOURCE LINES 22-54 .. code-block:: Python class VideoAnimation(FuncAnimation): def _repr_html_(self, *args, **kwargs): """To get video in html and have a player""" content = self.to_html5_video() return re.sub( r'width="[0-9]*"\sheight="[0-9]*"', 'width="100%" height="100%"', content ) def save(self, *args, **kwargs): if args[0].endswith("gif"): # In this case gif is used to create thumbnail which is not used but consume same time than video # So we create an empty file, to save time with open(args[0], "w") as _: pass return return super().save(*args, **kwargs) def start_axes(title): fig = plt.figure(figsize=(13, 5)) ax = fig.add_axes([0.03, 0.03, 0.90, 0.94], aspect="equal") ax.set_xlim(-6, 36.5), ax.set_ylim(30, 46) ax.set_title(title, weight="bold") return ax def update_axes(ax, mappable=None): ax.grid() if mappable: plt.colorbar(mappable, cax=ax.figure.add_axes([0.94, 0.05, 0.01, 0.9])) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Load eddies dataset .. GENERATED FROM PYTHON SOURCE LINES 56-65 .. code-block:: Python cyclonic_eddies = TrackEddiesObservations.load_file( py_eddy_tracker_sample.get_demo_path("eddies_med_adt_allsat_dt2018/Cyclonic.zarr") ) anticyclonic_eddies = TrackEddiesObservations.load_file( py_eddy_tracker_sample.get_demo_path( "eddies_med_adt_allsat_dt2018/Anticyclonic.zarr" ) ) .. GENERATED FROM PYTHON SOURCE LINES 66-67 Load loopers dataset .. GENERATED FROM PYTHON SOURCE LINES 67-71 .. code-block:: Python loopers_med = TrackEddiesObservations.load_file( data.get_demo_path("loopers_lumpkin_med.nc") ) .. GENERATED FROM PYTHON SOURCE LINES 72-74 Global view =========== .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: Python ax = start_axes("All drifters available in Med from Lumpkin dataset") loopers_med.plot(ax, lw=0.5, color="r", ref=-10) update_axes(ax) .. image-sg:: /python_module/12_external_data/images/sphx_glr_pet_drifter_loopers_001.png :alt: All drifters available in Med from Lumpkin dataset :srcset: /python_module/12_external_data/images/sphx_glr_pet_drifter_loopers_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-83 One segment of drifter ====================== Get a drifter segment (the indexes used have no correspondance with the original dataset). .. GENERATED FROM PYTHON SOURCE LINES 83-99 .. code-block:: Python looper = loopers_med.extract_ids((3588,)) fig = plt.figure(figsize=(16, 6)) ax = fig.add_subplot(111, aspect="equal") looper.plot(ax, lw=0.5, label="Original position of drifter") looper_filtered = looper.copy() looper_filtered.position_filter(1, 13) s = looper_filtered.scatter( ax, "time", cmap=plt.get_cmap("Spectral_r", 20), label="Filtered position of drifter", ) plt.colorbar(s).set_label("time (days from 1/1/1950)") ax.legend() ax.grid() .. image-sg:: /python_module/12_external_data/images/sphx_glr_pet_drifter_loopers_002.png :alt: pet drifter loopers :srcset: /python_module/12_external_data/images/sphx_glr_pet_drifter_loopers_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 100-101 Try to find a detected eddies with adt at same place. We used filtered track to simulate an eddy center .. GENERATED FROM PYTHON SOURCE LINES 101-112 .. code-block:: Python match = looper_filtered.close_tracks( anticyclonic_eddies, method="close_center", delta=0.1, nb_obs_min=50 ) fig = plt.figure(figsize=(16, 6)) ax = fig.add_subplot(111, aspect="equal") looper.plot(ax, lw=0.5, label="Original position of drifter") looper_filtered.plot(ax, lw=1.5, label="Filtered position of drifter") match.plot(ax, lw=1.5, label="Matched eddy") ax.legend() ax.grid() .. image-sg:: /python_module/12_external_data/images/sphx_glr_pet_drifter_loopers_003.png :alt: pet drifter loopers :srcset: /python_module/12_external_data/images/sphx_glr_pet_drifter_loopers_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 113-114 Display radius of this 2 datasets. .. GENERATED FROM PYTHON SOURCE LINES 114-140 .. code-block:: Python fig = plt.figure(figsize=(20, 8)) ax = fig.add_subplot(111) ax.plot(looper.time, looper.radius_s / 1e3, label="loopers") looper_radius = looper.copy() looper_radius.median_filter(1, "time", "radius_s", inplace=True) looper_radius.loess_filter(13, "time", "radius_s", inplace=True) ax.plot( looper_radius.time, looper_radius.radius_s / 1e3, label="loopers (filtered half window 13 days)", ) ax.plot(match.time, match.radius_s / 1e3, label="altimetry") match_radius = match.copy() match_radius.median_filter(1, "time", "radius_s", inplace=True) match_radius.loess_filter(13, "time", "radius_s", inplace=True) ax.plot( match_radius.time, match_radius.radius_s / 1e3, label="altimetry (filtered half window 13 days)", ) ax.set_ylabel("radius(km)"), ax.set_ylim(0, 100) ax.legend() ax.set_title("Radius from loopers and altimeter") ax.grid() .. image-sg:: /python_module/12_external_data/images/sphx_glr_pet_drifter_loopers_004.png :alt: Radius from loopers and altimeter :srcset: /python_module/12_external_data/images/sphx_glr_pet_drifter_loopers_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 141-142 Animation of a drifter and its colocated eddy .. GENERATED FROM PYTHON SOURCE LINES 142-154 .. code-block:: Python def update(frame): # We display last 5 days of loopers trajectory m = (looper.time < frame) * (looper.time > (frame - 5)) anim.func_animation(frame) line.set_data(looper.lon[m], looper.lat[m]) anim = Anim(match, intern=True, figsize=(8, 8), cmap="magma_r", nb_step=10, dpi=75) # mappable to show drifter in red line = anim.ax.plot([], [], "r", lw=4, zorder=100)[0] anim.fig.suptitle("") _ = VideoAnimation(anim.fig, update, frames=np.arange(*anim.period, 1), interval=125) .. container:: sphx-glr-animation .. raw:: html .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.961 seconds) .. _sphx_glr_download_python_module_12_external_data_pet_drifter_loopers.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AntSimi/py-eddy-tracker/master?urlpath=lab/tree/notebooks/python_module/12_external_data/pet_drifter_loopers.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pet_drifter_loopers.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pet_drifter_loopers.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: pet_drifter_loopers.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_