|
| 1 | +""" |
| 2 | +[draft] Loopers vs eddies from altimetry |
| 3 | +======================================== |
| 4 | +
|
| 5 | +All loopers datas used in this example are a subset from the dataset describe in this article |
| 6 | +[Lumpkin, R. : Global characteristics of coherent vortices from surface drifter trajectories](https://doi.org/10.1002/2015JC011435) |
| 7 | +
|
| 8 | +
|
| 9 | +""" |
| 10 | + |
| 11 | +import re |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import py_eddy_tracker_sample |
| 15 | +from IPython.display import HTML |
| 16 | +from matplotlib import pyplot as plt |
| 17 | +from matplotlib.animation import FuncAnimation |
| 18 | + |
| 19 | +from py_eddy_tracker import data |
| 20 | +from py_eddy_tracker.appli.gui import Anim |
| 21 | +from py_eddy_tracker.observations.tracking import TrackEddiesObservations |
| 22 | + |
| 23 | + |
| 24 | +# %% |
| 25 | +class VideoAnimation(FuncAnimation): |
| 26 | + def _repr_html_(self, *args, **kwargs): |
| 27 | + """To get video in html and have a player""" |
| 28 | + content = self.to_html5_video() |
| 29 | + return re.sub( |
| 30 | + r'width="[0-9]*"\sheight="[0-9]*"', 'width="100%" height="100%"', content |
| 31 | + ) |
| 32 | + |
| 33 | + def save(self, *args, **kwargs): |
| 34 | + if args[0].endswith("gif"): |
| 35 | + # In this case gif is use to create thumbnail which are not use but consume same time than video |
| 36 | + # So we create an empty file, to save time |
| 37 | + with open(args[0], "w") as _: |
| 38 | + pass |
| 39 | + return |
| 40 | + return super().save(*args, **kwargs) |
| 41 | + |
| 42 | + |
| 43 | +def start_axes(title): |
| 44 | + fig = plt.figure(figsize=(13, 5)) |
| 45 | + ax = fig.add_axes([0.03, 0.03, 0.90, 0.94], aspect="equal") |
| 46 | + ax.set_xlim(-6, 36.5), ax.set_ylim(30, 46) |
| 47 | + ax.set_title(title, weight="bold") |
| 48 | + return ax |
| 49 | + |
| 50 | + |
| 51 | +def update_axes(ax, mappable=None): |
| 52 | + ax.grid() |
| 53 | + if mappable: |
| 54 | + plt.colorbar(mappable, cax=ax.figure.add_axes([0.94, 0.05, 0.01, 0.9])) |
| 55 | + |
| 56 | + |
| 57 | +# %% |
| 58 | +# Load eddies dataset |
| 59 | +cyclonic_eddies = TrackEddiesObservations.load_file( |
| 60 | + py_eddy_tracker_sample.get_demo_path("eddies_med_adt_allsat_dt2018/Cyclonic.zarr") |
| 61 | +) |
| 62 | +anticyclonic_eddies = TrackEddiesObservations.load_file( |
| 63 | + py_eddy_tracker_sample.get_demo_path( |
| 64 | + "eddies_med_adt_allsat_dt2018/Anticyclonic.zarr" |
| 65 | + ) |
| 66 | +) |
| 67 | + |
| 68 | +# %% |
| 69 | +# Load loopers dataset |
| 70 | +loopers_med = TrackEddiesObservations.load_file( |
| 71 | + data.get_demo_path("loopers_lumpkin_med.nc") |
| 72 | +) |
| 73 | + |
| 74 | +# %% |
| 75 | +# |
| 76 | +ax = start_axes("All drifter available in med from Lumpkin dataset") |
| 77 | +loopers_med.plot(ax, lw=0.5, color="r", ref=-10) |
| 78 | +update_axes(ax) |
| 79 | + |
| 80 | +# %% |
| 81 | +# Only long period drifter |
| 82 | +long_drifter = loopers_med.extract_with_length((400, -1)) |
| 83 | +ax = start_axes("Long period drifter") |
| 84 | +long_drifter.plot(ax, lw=0.5, color="r", ref=-10) |
| 85 | +update_axes(ax) |
| 86 | +print(np.unique(long_drifter.tracks)) |
| 87 | + |
| 88 | +# %% |
| 89 | +drifter_1 = long_drifter.extract_ids((3588,)) |
| 90 | +fig = plt.figure(figsize=(8, 4)) |
| 91 | +ax = fig.add_subplot(111, aspect="equal") |
| 92 | +ax.grid() |
| 93 | +drifter_1.plot(ax, lw=0.5) |
| 94 | +# %% |
| 95 | +drifter_1.close_tracks( |
| 96 | + anticyclonic_eddies, method="close_center", delta=0.5, nb_obs_min=25 |
| 97 | +).tracks |
| 98 | + |
| 99 | +# %% |
| 100 | +# Animation which show drifter with colocated eddies |
| 101 | +ed = anticyclonic_eddies.extract_ids((4738, 4836, 4910,)) |
| 102 | +x, y, t = drifter_1.lon, drifter_1.lat, drifter_1.time |
| 103 | + |
| 104 | + |
| 105 | +def update(frame): |
| 106 | + # We display last 5 days of loopers trajectory |
| 107 | + m = (t < frame) * (t > (frame - 5)) |
| 108 | + a.func_animation(frame) |
| 109 | + line.set_data(x[m], y[m]) |
| 110 | + |
| 111 | + |
| 112 | +a = Anim(ed, intern=True, figsize=(8, 8), cmap="magma_r", nb_step=10, dpi=60) |
| 113 | +line = a.ax.plot([], [], "r", lw=4, zorder=100)[0] |
| 114 | +kwargs = dict(frames=np.arange(*a.period, 1), interval=100) |
| 115 | +a.fig.suptitle("") |
| 116 | +_ = VideoAnimation(a.fig, update, **kwargs) |
| 117 | + |
| 118 | +# %% |
| 119 | +fig = plt.figure(figsize=(12, 6)) |
| 120 | +ax = fig.add_subplot(111) |
| 121 | +ax.plot(drifter_1.time, drifter_1.radius_s / 1e3, label="loopers") |
| 122 | +drifter_1.median_filter(7, "time", "radius_s", inplace=True) |
| 123 | +drifter_1.loess_filter(15, "time", "radius_s", inplace=True) |
| 124 | +ax.plot( |
| 125 | + drifter_1.time, |
| 126 | + drifter_1.radius_s / 1e3, |
| 127 | + label="loopers (filtered half window 15 days)", |
| 128 | +) |
| 129 | +ax.plot(ed.time, ed.radius_s / 1e3, label="altimetry") |
| 130 | +ed.median_filter(7, "time", "radius_s", inplace=True) |
| 131 | +ed.loess_filter(15, "time", "radius_s", inplace=True) |
| 132 | +ax.plot(ed.time, ed.radius_s / 1e3, label="altimetry (filtered half window 15 days)") |
| 133 | +ax.set_ylabel("radius(km)"), ax.set_ylim(0, 100) |
| 134 | +ax.legend() |
| 135 | +ax.grid() |
| 136 | +_ = ax.set_title("Radius from loopers and altimeter") |
0 commit comments