|
| 1 | +r""" |
| 2 | +Get Okubo Weis |
| 3 | +===================== |
| 4 | +
|
| 5 | +.. math:: OW = S_n^2 + S_s^2 + \omega^2 |
| 6 | +
|
| 7 | +with normal strain (:math:`S_n`), shear strain (:math:`S_s`) and vorticity (:math:`\omega`) |
| 8 | +
|
| 9 | +.. math:: |
| 10 | + S_n = \frac{\partial u}{\partial x} - \frac{\partial v}{\partial y}, |
| 11 | + S_s = \frac{\partial v}{\partial x} + \frac{\partial u}{\partial y}, |
| 12 | + \omega = \frac{\partial v}{\partial x} - \frac{\partial u}{\partial y} |
| 13 | +
|
| 14 | +""" |
| 15 | +from matplotlib import pyplot as plt |
| 16 | +from py_eddy_tracker.dataset.grid import RegularGridDataset |
| 17 | +from py_eddy_tracker.observations.observation import EddiesObservations |
| 18 | +from py_eddy_tracker import data |
| 19 | +from numpy import arange, ma, where |
| 20 | + |
| 21 | + |
| 22 | +# %% |
| 23 | +def start_axes(title, zoom=False): |
| 24 | + fig = plt.figure(figsize=(12, 6)) |
| 25 | + axes = fig.add_axes([0.03, 0.03, 0.90, 0.94]) |
| 26 | + axes.set_xlim(0, 360), axes.set_ylim(-80, 80) |
| 27 | + if zoom: |
| 28 | + axes.set_xlim(270, 340), axes.set_ylim(20, 50) |
| 29 | + axes.set_aspect("equal") |
| 30 | + axes.set_title(title) |
| 31 | + return axes |
| 32 | + |
| 33 | + |
| 34 | +def update_axes(axes, mappable=None): |
| 35 | + axes.grid() |
| 36 | + if mappable: |
| 37 | + plt.colorbar(mappable, cax=axes.figure.add_axes([0.94, 0.05, 0.01, 0.9])) |
| 38 | + |
| 39 | + |
| 40 | +# %% |
| 41 | +# Load detection files |
| 42 | +a = EddiesObservations.load_file(data.get_path("Anticyclonic_20190223.nc")) |
| 43 | +c = EddiesObservations.load_file(data.get_path("Cyclonic_20190223.nc")) |
| 44 | + |
| 45 | +# %% |
| 46 | +# Load Input grid, ADT will be used to detect eddies |
| 47 | +g = RegularGridDataset( |
| 48 | + data.get_path("nrt_global_allsat_phy_l4_20190223_20190226.nc"), |
| 49 | + "longitude", |
| 50 | + "latitude", |
| 51 | +) |
| 52 | + |
| 53 | +ax = start_axes("ADT (cm)") |
| 54 | +m = g.display(ax, "adt", vmin=-120, vmax=120, factor=100) |
| 55 | +update_axes(ax, m) |
| 56 | + |
| 57 | +# %% |
| 58 | +# Get parameter for ow |
| 59 | +u_x = g.compute_stencil(g.grid("ugos")) |
| 60 | +u_y = g.compute_stencil(g.grid("ugos"), vertical=True) |
| 61 | +v_x = g.compute_stencil(g.grid("vgos")) |
| 62 | +v_y = g.compute_stencil(g.grid("vgos"), vertical=True) |
| 63 | +ow = g.vars["ow"] = (u_x - v_y) ** 2 + (v_x + u_y) ** 2 - (v_x - u_y) ** 2 |
| 64 | + |
| 65 | + |
| 66 | +ax = start_axes("Okubo weis") |
| 67 | +m = g.display(ax, "ow", vmin=-1e-10, vmax=1e-10, cmap="bwr") |
| 68 | +update_axes(ax, m) |
| 69 | + |
| 70 | +# %% |
| 71 | +# Gulf stream zoom |
| 72 | +ax = start_axes("Okubo weis, Gulf stream", zoom=True) |
| 73 | +m = g.display(ax, "ow", vmin=-1e-10, vmax=1e-10, cmap="bwr") |
| 74 | +kw_ed = dict(intern_only=True, color="k", lw=1) |
| 75 | +a.display(ax, **kw_ed), c.display(ax, **kw_ed) |
| 76 | +update_axes(ax, m) |
| 77 | + |
| 78 | +# %% |
| 79 | +# only negative OW |
| 80 | +ax = start_axes("Okubo weis, Gulf stream", zoom=True) |
| 81 | +threshold = ow.std() * -0.2 |
| 82 | +ow = ma.array(ow, mask=ow > threshold) |
| 83 | +m = g.display(ax, ow, vmin=-1e-10, vmax=1e-10, cmap="bwr") |
| 84 | +a.display(ax, **kw_ed), c.display(ax, **kw_ed) |
| 85 | +update_axes(ax, m) |
| 86 | + |
| 87 | + |
| 88 | +# %% |
| 89 | +# Get okubo-weiss mean/min/center in eddies |
| 90 | +plt.figure(figsize=(8, 6)) |
| 91 | +ax = plt.subplot(111) |
| 92 | +ax.set_xlabel("Okubo-Weiss parameter") |
| 93 | +kw_hist = dict(bins=arange(-20e-10, 20e-10, 50e-12), histtype="step") |
| 94 | +for method in ("mean", "center", "min"): |
| 95 | + kw_interp = dict(grid_object=g, varname="ow", method=method, intern=True) |
| 96 | + _, _, m = ax.hist( |
| 97 | + a.interp_grid(**kw_interp), label=f"Anticyclonic - OW {method}", **kw_hist |
| 98 | + ) |
| 99 | + ax.hist( |
| 100 | + c.interp_grid(**kw_interp), |
| 101 | + label=f"Cyclonic - OW {method}", |
| 102 | + color=m[0].get_edgecolor(), |
| 103 | + ls="--", |
| 104 | + **kw_hist, |
| 105 | + ) |
| 106 | +ax.axvline(threshold, color="r") |
| 107 | +ax.set_yscale("log") |
| 108 | +ax.grid() |
| 109 | +ax.set_ylim(1, 1e4) |
| 110 | +ax.set_xlim(-15e-10, 15e-10) |
| 111 | +ax.legend() |
| 112 | + |
| 113 | +# %% |
| 114 | +# Catch eddies with bad OW |
| 115 | +ax = start_axes("Eddies with a min OW in speed contour over threshold") |
| 116 | +ow_min = a.interp_grid(**kw_interp) |
| 117 | +a_bad_ow = a.index(where(ow_min > threshold)[0]) |
| 118 | +a_bad_ow.display(ax, color="r", label="Anticyclonic") |
| 119 | +ow_min = c.interp_grid(**kw_interp) |
| 120 | +c_bad_ow = c.index(where(ow_min > threshold)[0]) |
| 121 | +c_bad_ow.display(ax, color="b", label="Cyclonic") |
| 122 | +ax.legend() |
| 123 | + |
| 124 | +# %% |
| 125 | +# Display Radius and amplitude of eddies |
| 126 | +fig = plt.figure(figsize=(12, 5)) |
| 127 | +fig.suptitle( |
| 128 | + "Parameter distribution (solid line) and cumulative distribution (dashed line)" |
| 129 | +) |
| 130 | +ax_amp, ax_rad = fig.add_subplot(121), fig.add_subplot(122) |
| 131 | +ax_amp_c, ax_rad_c = ax_amp.twinx(), ax_rad.twinx() |
| 132 | +ax_amp_c.set_ylim(0, 1), ax_rad_c.set_ylim(0, 1) |
| 133 | +kw_a = dict(xname="amplitude", bins=arange(0, 2, 0.002).astype("f4")) |
| 134 | +kw_r = dict(xname="radius_s", bins=arange(0, 500e6, 2e3).astype("f4")) |
| 135 | +for d, label, color in ( |
| 136 | + (a, "Anticyclonic all", "r"), |
| 137 | + (a_bad_ow, "Anticyclonic bad OW", "orange"), |
| 138 | + (c, "Cyclonic all", "blue"), |
| 139 | + (c_bad_ow, "Cyclonic bad OW", "lightblue"), |
| 140 | +): |
| 141 | + x, y = d.bins_stat(**kw_a) |
| 142 | + ax_amp.plot(x * 100, y, label=label, color=color) |
| 143 | + ax_amp_c.plot( |
| 144 | + x * 100, y.cumsum() / y.sum(), label=label, color=color, ls="-.", lw=0.5 |
| 145 | + ) |
| 146 | + x, y = d.bins_stat(**kw_r) |
| 147 | + ax_rad.plot(x * 1e-3, y, label=label, color=color) |
| 148 | + ax_rad_c.plot( |
| 149 | + x * 1e-3, y.cumsum() / y.sum(), label=label, color=color, ls="-.", lw=0.5 |
| 150 | + ) |
| 151 | + |
| 152 | +ax_amp.set_xlim(0, 12.5), ax_amp.grid(), ax_amp.set_ylim(0), ax_amp.legend() |
| 153 | +ax_rad.set_xlim(0, 120), ax_rad.grid(), ax_rad.set_ylim(0) |
| 154 | +ax_amp.set_xlabel("Amplitude (cm)"), ax_amp.set_ylabel("Nb eddies") |
| 155 | +ax_rad.set_xlabel("Speed radius (km)") |
0 commit comments