Skip to content

Commit f72b9a6

Browse files
committed
Give direct access to variable for eddies dataset i.e. : obj.amplitude return amplitude
Could use extern and intern name
1 parent 3a3a84b commit f72b9a6

File tree

7 files changed

+32
-26
lines changed

7 files changed

+32
-26
lines changed

examples/02_eddy_identification/pet_eddy_detection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def update_axes(ax, mappable=None):
4242

4343
# %%
4444
# Get geostrophic speed u,v
45-
# -------
45+
# -------------------------
4646
# U/V are deduced from ADT, this algortihm is not ok near the equator (~+- 2°)
4747
g.add_uv("adt")
4848
ax = start_axes("U/V deduce from ADT (m)")
@@ -82,7 +82,7 @@ def update_axes(ax, mappable=None):
8282

8383
# %%
8484
# Post analysis
85-
# ------------
85+
# -------------
8686
# Contours can be rejected for several reasons (shape error to high, several extremum in contour, ...)
8787
ax = start_axes("ADT rejected contours")
8888
g.contours.display(ax, only_unused=True)
@@ -149,7 +149,7 @@ def update_axes(ax, mappable=None):
149149
# Filling the effective radius contours with the effective radius values
150150
ax = start_axes("Effective Radius (km)")
151151
kwargs = dict(vmin=10, vmax=80, cmap="magma_r", factor=0.001, lut=14, ref=-10)
152-
a.filled(ax, "radius_e", **kwargs)
152+
a.filled(ax, "effective_radius", **kwargs)
153153
m = c.filled(
154154
ax, "radius_e", vmin=10, vmax=80, cmap="magma_r", factor=0.001, lut=14, ref=-10
155155
)

examples/06_grid_manipulation/pet_okubo_weiss.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def update_axes(axes, mappable=None):
6262
v_y = g.compute_stencil(g.grid("vgos"), vertical=True)
6363
ow = g.vars["ow"] = (u_x - v_y) ** 2 + (v_x + u_y) ** 2 - (v_x - u_y) ** 2
6464

65-
6665
ax = start_axes("Okubo weis")
6766
m = g.display(ax, "ow", vmin=-1e-10, vmax=1e-10, cmap="bwr")
6867
update_axes(ax, m)
@@ -84,7 +83,6 @@ def update_axes(axes, mappable=None):
8483
a.display(ax, **kw_ed), c.display(ax, **kw_ed)
8584
update_axes(ax, m)
8685

87-
8886
# %%
8987
# Get okubo-weiss mean/min/center in eddies
9088
plt.figure(figsize=(8, 6))

examples/10_tracking_diagnostics/pet_histo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
for x0, name, title, xmax, factor, bins in zip(
2727
(0.4, 0.72, 0.08),
28-
("radius_s", "speed_average", "amplitude"),
28+
("speed_radius", "speed_average", "amplitude"),
2929
("Speed radius (km)", "Speed average (cm/s)", "Amplitude (cm)"),
3030
(100, 50, 20),
3131
(0.001, 100, 100),

notebooks/python_module/02_eddy_identification/pet_eddy_detection.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
},
272272
"outputs": [],
273273
"source": [
274-
"ax = start_axes(\"Effective Radius (km)\")\nkwargs = dict(vmin=10, vmax=80, cmap=\"magma_r\", factor=0.001, lut=14, ref=-10)\na.filled(ax, \"radius_e\", **kwargs)\nm = c.filled(\n ax, \"radius_e\", vmin=10, vmax=80, cmap=\"magma_r\", factor=0.001, lut=14, ref=-10\n)\nupdate_axes(ax, m)"
274+
"ax = start_axes(\"Effective Radius (km)\")\nkwargs = dict(vmin=10, vmax=80, cmap=\"magma_r\", factor=0.001, lut=14, ref=-10)\na.filled(ax, \"effective_radius\", **kwargs)\nm = c.filled(\n ax, \"radius_e\", vmin=10, vmax=80, cmap=\"magma_r\", factor=0.001, lut=14, ref=-10\n)\nupdate_axes(ax, m)"
275275
]
276276
}
277277
],

notebooks/python_module/06_grid_manipulation/pet_okubo_weiss.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
},
9292
"outputs": [],
9393
"source": [
94-
"u_x = g.compute_stencil(g.grid(\"ugos\"))\nu_y = g.compute_stencil(g.grid(\"ugos\"), vertical=True)\nv_x = g.compute_stencil(g.grid(\"vgos\"))\nv_y = g.compute_stencil(g.grid(\"vgos\"), vertical=True)\now = g.vars[\"ow\"] = (u_x - v_y) ** 2 + (v_x + u_y) ** 2 - (v_x - u_y) ** 2\n\n\nax = start_axes(\"Okubo weis\")\nm = g.display(ax, \"ow\", vmin=-1e-10, vmax=1e-10, cmap=\"bwr\")\nupdate_axes(ax, m)"
94+
"u_x = g.compute_stencil(g.grid(\"ugos\"))\nu_y = g.compute_stencil(g.grid(\"ugos\"), vertical=True)\nv_x = g.compute_stencil(g.grid(\"vgos\"))\nv_y = g.compute_stencil(g.grid(\"vgos\"), vertical=True)\now = g.vars[\"ow\"] = (u_x - v_y) ** 2 + (v_x + u_y) ** 2 - (v_x - u_y) ** 2\n\nax = start_axes(\"Okubo weis\")\nm = g.display(ax, \"ow\", vmin=-1e-10, vmax=1e-10, cmap=\"bwr\")\nupdate_axes(ax, m)"
9595
]
9696
},
9797
{

notebooks/python_module/10_tracking_diagnostics/pet_histo.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
},
6363
"outputs": [],
6464
"source": [
65-
"fig = plt.figure(figsize=(12, 7))\n\nfor x0, name, title, xmax, factor, bins in zip(\n (0.4, 0.72, 0.08),\n (\"radius_s\", \"speed_average\", \"amplitude\"),\n (\"Speed radius (km)\", \"Speed average (cm/s)\", \"Amplitude (cm)\"),\n (100, 50, 20),\n (0.001, 100, 100),\n (arange(0, 2000, 1), arange(0, 1000, 0.5), arange(0.0005, 1000, 0.2)),\n):\n ax_hist = fig.add_axes((x0, 0.24, 0.27, 0.35))\n nb_a, _, _ = ax_hist.hist(a[name] * factor, bins=bins, **kwargs_a)\n nb_c, _, _ = ax_hist.hist(c[name] * factor, bins=bins, **kwargs_c)\n ax_hist.set_xticklabels([])\n ax_hist.set_xlim(0, xmax)\n ax_hist.grid()\n\n ax_cum = fig.add_axes((x0, 0.62, 0.27, 0.35))\n ax_cum.hist(a[name] * factor, bins=bins, cumulative=-1, **kwargs_a)\n ax_cum.hist(c[name] * factor, bins=bins, cumulative=-1, **kwargs_c)\n ax_cum.set_xticklabels([])\n ax_cum.set_title(title)\n ax_cum.set_xlim(0, xmax)\n ax_cum.set_ylim(0, 1)\n ax_cum.grid()\n\n ax_ratio = fig.add_axes((x0, 0.06, 0.27, 0.15))\n ax_ratio.set_xlim(0, xmax)\n ax_ratio.set_ylim(0, 2)\n ax_ratio.plot((bins[1:] + bins[:-1]) / 2, nb_c / nb_a)\n ax_ratio.axhline(1, color=\"k\")\n ax_ratio.grid()\n ax_ratio.set_xlabel(title)\n\nax_cum.set_ylabel(\"Cumulative\\npercent distribution\")\nax_hist.set_ylabel(\"Percent of observations\")\nax_ratio.set_ylabel(\"Ratio percent\\nCyc/Acyc\")\nax_cum.legend()"
65+
"fig = plt.figure(figsize=(12, 7))\n\nfor x0, name, title, xmax, factor, bins in zip(\n (0.4, 0.72, 0.08),\n (\"speed_radius\", \"speed_average\", \"amplitude\"),\n (\"Speed radius (km)\", \"Speed average (cm/s)\", \"Amplitude (cm)\"),\n (100, 50, 20),\n (0.001, 100, 100),\n (arange(0, 2000, 1), arange(0, 1000, 0.5), arange(0.0005, 1000, 0.2)),\n):\n ax_hist = fig.add_axes((x0, 0.24, 0.27, 0.35))\n nb_a, _, _ = ax_hist.hist(a[name] * factor, bins=bins, **kwargs_a)\n nb_c, _, _ = ax_hist.hist(c[name] * factor, bins=bins, **kwargs_c)\n ax_hist.set_xticklabels([])\n ax_hist.set_xlim(0, xmax)\n ax_hist.grid()\n\n ax_cum = fig.add_axes((x0, 0.62, 0.27, 0.35))\n ax_cum.hist(a[name] * factor, bins=bins, cumulative=-1, **kwargs_a)\n ax_cum.hist(c[name] * factor, bins=bins, cumulative=-1, **kwargs_c)\n ax_cum.set_xticklabels([])\n ax_cum.set_title(title)\n ax_cum.set_xlim(0, xmax)\n ax_cum.set_ylim(0, 1)\n ax_cum.grid()\n\n ax_ratio = fig.add_axes((x0, 0.06, 0.27, 0.15))\n ax_ratio.set_xlim(0, xmax)\n ax_ratio.set_ylim(0, 2)\n ax_ratio.plot((bins[1:] + bins[:-1]) / 2, nb_c / nb_a)\n ax_ratio.axhline(1, color=\"k\")\n ax_ratio.grid()\n ax_ratio.set_xlabel(title)\n\nax_cum.set_ylabel(\"Cumulative\\npercent distribution\")\nax_hist.set_ylabel(\"Percent of observations\")\nax_ratio.set_ylabel(\"Ratio percent\\nCyc/Acyc\")\nax_cum.legend()"
6666
]
6767
}
6868
],

src/py_eddy_tracker/observations/observation.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,11 @@ def __init__(
159159
raise Exception("Unknown element : %s" % elt)
160160
self.observations = zeros(size, dtype=self.dtype)
161161
self.sign_type = None
162-
163-
@property
164-
def longitude(self):
165-
return self.observations["lon"]
166-
167-
@property
168-
def latitude(self):
169-
return self.observations["lat"]
170-
171-
@property
172-
def time(self):
173-
return self.observations["time"]
174-
162+
175163
@property
176164
def tracks(self):
177165
return self.observations["track"]
178166

179-
@property
180-
def observation_number(self):
181-
return self.observations["n"]
182-
183167
@property
184168
def sign_legend(self):
185169
return "Cyclonic" if self.sign_type != 1 else "Anticyclonic"
@@ -267,11 +251,35 @@ def __repr__(self):
267251
Mean effective radius (km): {self.box_display(self.hist('radius_e', 'lat', bins_lat, mean=True) / 1000.)}
268252
Mean amplitude (cm) : {self.box_display(self.hist('amplitude', 'lat', bins_lat, mean=True) * 100.)}"""
269253

254+
def __dir__(self):
255+
"""Provide method name lookup and completion.
256+
"""
257+
base = set(dir(type(self)))
258+
intern_name = set(self.elements)
259+
extern_name = set([VAR_DESCR[k]['nc_name'] for k in intern_name])
260+
# Must be check in init not here
261+
if base & intern_name:
262+
logger.warning("Some variable name have a common name with class attrs: %s", base & intern_name)
263+
if base & extern_name:
264+
logger.warning("Some variable name have a common name with class attrs: %s", base & extern_name)
265+
return sorted(base.union(intern_name).union(extern_name))
266+
270267
def __getitem__(self, attr):
271268
if attr in self.elements:
272269
return self.observations[attr]
270+
elif attr in VAR_DESCR_inv:
271+
return self.observations[VAR_DESCR_inv[attr]]
273272
raise KeyError("%s unknown" % attr)
274273

274+
def __getattr__(self, attr):
275+
if attr in self.elements:
276+
return self.observations[attr]
277+
elif attr in VAR_DESCR_inv:
278+
return self.observations[VAR_DESCR_inv[attr]]
279+
raise AttributeError(
280+
"{!r} object has no attribute {!r}".format(type(self).__name__, attr)
281+
)
282+
275283
@classmethod
276284
def needed_variable(cls):
277285
return None

0 commit comments

Comments
 (0)