|
| 1 | +""" |
| 2 | +Collocating external data |
| 3 | +========================== |
| 4 | +
|
| 5 | +Script will use py-eddy-tracker methods to upload external data (sea surface temperature, SST) |
| 6 | +in a common structure with altimetry. |
| 7 | +
|
| 8 | +Figures higlights the different steps. |
| 9 | +""" |
| 10 | + |
| 11 | +from matplotlib import pyplot as plt |
| 12 | +from py_eddy_tracker.dataset.grid import RegularGridDataset |
| 13 | +from datetime import datetime |
| 14 | +from py_eddy_tracker import data |
| 15 | + |
| 16 | +date = datetime(2016, 7, 7) |
| 17 | + |
| 18 | +filename_alt = data.get_path(f"dt_blacksea_allsat_phy_l4_{date:%Y%m%d}_20200801.nc") |
| 19 | +filename_sst = data.get_path( |
| 20 | + f"{date:%Y%m%d}000000-GOS-L4_GHRSST-SSTfnd-OISST_HR_REP-BLK-v02.0-fv01.0.nc" |
| 21 | +) |
| 22 | +var_name_sst = "analysed_sst" |
| 23 | + |
| 24 | +extent = [27, 42, 40.5, 47] |
| 25 | + |
| 26 | +# %% |
| 27 | +# Loading data |
| 28 | +# ----------------------------- |
| 29 | +sst = RegularGridDataset(filename=filename_sst, x_name="lon", y_name="lat") |
| 30 | +alti = RegularGridDataset( |
| 31 | + data.get_path(filename_alt), x_name="longitude", y_name="latitude" |
| 32 | +) |
| 33 | +# We can use `Grid` tools to interpolate ADT on the sst grid |
| 34 | +sst.regrid(alti, "sla") |
| 35 | +sst.add_uv("sla") |
| 36 | + |
| 37 | + |
| 38 | +# %% |
| 39 | +# Functions to initiate figure axes |
| 40 | +def start_axes(title, extent=extent): |
| 41 | + fig = plt.figure(figsize=(13, 6), dpi=120) |
| 42 | + ax = fig.add_axes([0.03, 0.05, 0.89, 0.91]) |
| 43 | + ax.set_xlim(extent[0], extent[1]) |
| 44 | + ax.set_ylim(extent[2], extent[3]) |
| 45 | + ax.set_title(title) |
| 46 | + ax.set_aspect("equal") |
| 47 | + return ax |
| 48 | + |
| 49 | + |
| 50 | +def update_axes(ax, mappable=None, unit=""): |
| 51 | + ax.grid() |
| 52 | + if mappable: |
| 53 | + cax = ax.figure.add_axes([0.93, 0.05, 0.01, 0.9], title=unit) |
| 54 | + plt.colorbar(mappable, cax=cax) |
| 55 | + |
| 56 | + |
| 57 | +# %% |
| 58 | +# ADT first display |
| 59 | +# ----------------------------- |
| 60 | +ax = start_axes("SLA", extent=extent) |
| 61 | +m = sst.display(ax, "sla", vmin=0.05, vmax=0.35) |
| 62 | +update_axes(ax, m, unit="[m]") |
| 63 | + |
| 64 | +# %% |
| 65 | +# SST first display |
| 66 | +# ----------------------------- |
| 67 | + |
| 68 | +# %% |
| 69 | +# We can now plot SST from `sst` |
| 70 | +ax = start_axes("SST") |
| 71 | +m = sst.display(ax, "analysed_sst", vmin=295, vmax=300) |
| 72 | +update_axes(ax, m, unit="[°K]") |
| 73 | + |
| 74 | +# %% |
| 75 | +ax = start_axes("SST") |
| 76 | +m = sst.display(ax, "analysed_sst", vmin=295, vmax=300) |
| 77 | +u, v = sst.grid("u").T, sst.grid("v").T |
| 78 | +ax.quiver(sst.x_c[::3], sst.y_c[::3], u[::3, ::3], v[::3, ::3], scale=10) |
| 79 | +update_axes(ax, m, unit="[°K]") |
| 80 | + |
| 81 | +# %% |
| 82 | +# Now, with eddy contours, and displaying SST anomaly |
| 83 | +sst.bessel_high_filter("analysed_sst", 400) |
| 84 | + |
| 85 | +# %% |
| 86 | +# Eddy detection |
| 87 | +sst.bessel_high_filter("sla", 400) |
| 88 | +# ADT filtered |
| 89 | +ax = start_axes("SLA", extent=extent) |
| 90 | +m = sst.display(ax, "sla", vmin=-0.1, vmax=0.1) |
| 91 | +update_axes(ax, m, unit="[m]") |
| 92 | +a, c = sst.eddy_identification("sla", "u", "v", date, 0.002) |
| 93 | + |
| 94 | +# %% |
| 95 | +kwargs_a = dict(lw=2, label="Anticyclonic", ref=-10, color="b") |
| 96 | +kwargs_c = dict(lw=2, label="Cyclonic", ref=-10, color="r") |
| 97 | +ax = start_axes("SST anomaly") |
| 98 | +m = sst.display(ax, "analysed_sst", vmin=-1, vmax=1) |
| 99 | +a.display(ax, **kwargs_a), c.display(ax, **kwargs_c) |
| 100 | +ax.legend() |
| 101 | +update_axes(ax, m, unit="[°K]") |
| 102 | + |
| 103 | +# %% |
| 104 | +# Example of post-processing |
| 105 | +# -------------------------- |
| 106 | +# Get mean of sst anomaly_high in each internal contour |
| 107 | +anom_a = a.interp_grid(sst, "analysed_sst", method="mean", intern=True) |
| 108 | +anom_c = c.interp_grid(sst, "analysed_sst", method="mean", intern=True) |
| 109 | + |
| 110 | +# %% |
| 111 | +# Are cyclonic (resp. anticyclonic) eddies generally associated with positive (resp. negative) SST anomaly ? |
| 112 | +fig = plt.figure(figsize=(7, 5)) |
| 113 | +ax = fig.add_axes([0.05, 0.05, 0.90, 0.90]) |
| 114 | +ax.set_xlabel("SST anomaly") |
| 115 | +ax.set_xlim([-1, 1]) |
| 116 | +ax.set_title("Histograms of SST anomalies") |
| 117 | +ax.hist( |
| 118 | + anom_a, 5, alpha=0.5, color="b", label="Anticyclonic (mean:%s)" % (anom_a.mean()) |
| 119 | +) |
| 120 | +ax.hist(anom_c, 5, alpha=0.5, color="r", label="Cyclonic (mean:%s)" % (anom_c.mean())) |
| 121 | +ax.legend() |
| 122 | + |
| 123 | +# %% |
| 124 | +# Not clearly so in that case .. |
0 commit comments