Skip to content

Commit b93850a

Browse files
committed
Add example with correspondance
1 parent 1b9ab25 commit b93850a

File tree

4 files changed

+258
-4
lines changed

4 files changed

+258
-4
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Correspondances
3+
===============
4+
5+
Correspondances is a mechanism to intend to continue tracking with new detection
6+
7+
"""
8+
9+
import logging
10+
11+
# %%
12+
from matplotlib import pyplot as plt
13+
from netCDF4 import Dataset
14+
15+
from py_eddy_tracker import start_logger
16+
from py_eddy_tracker.data import get_remote_demo_sample
17+
from py_eddy_tracker.featured_tracking.area_tracker import AreaTracker
18+
19+
# In order to hide some warning
20+
import py_eddy_tracker.observations.observation
21+
from py_eddy_tracker.tracking import Correspondances
22+
23+
py_eddy_tracker.observations.observation._display_check_warning = False
24+
25+
26+
# %%
27+
def plot_eddy(ed):
28+
fig = plt.figure(figsize=(10, 5))
29+
ax = fig.add_axes([0.05, 0.03, 0.90, 0.94])
30+
ed.plot(ax, ref=-10, marker="x")
31+
lc = ed.display_color(ax, field=ed.time, ref=-10, intern=True)
32+
plt.colorbar(lc).set_label("Time in Julian days (from 1950/01/01)")
33+
ax.set_xlim(4.5, 8), ax.set_ylim(36.8, 38.3)
34+
ax.set_aspect("equal")
35+
ax.grid()
36+
37+
38+
# %%
39+
# Get remote data, we will keep only 20 first days,
40+
# `get_remote_demo_sample` function is only to get demo dataset, in your own case give a list of identification filename
41+
# and don't mix cyclonic and anticyclonic files.
42+
file_objects = get_remote_demo_sample(
43+
"eddies_med_adt_allsat_dt2018/Anticyclonic_2010_2011_2012"
44+
)[:20]
45+
46+
# %%
47+
# We run a traking with a tracker which use contour overlap, on 10 first time step
48+
c_first_run = Correspondances(
49+
datasets=file_objects[:10], class_method=AreaTracker, virtual=4
50+
)
51+
start_logger().setLevel("INFO")
52+
c_first_run.track()
53+
start_logger().setLevel("WARNING")
54+
with Dataset("correspondances.nc", "w") as h:
55+
c_first_run.to_netcdf(h)
56+
# Next step are done only to build atlas and display it
57+
c_first_run.prepare_merging()
58+
59+
# We have now an eddy object
60+
eddies_area_tracker = c_first_run.merge(raw_data=False)
61+
eddies_area_tracker.virtual[:] = eddies_area_tracker.time == 0
62+
eddies_area_tracker.filled_by_interpolation(eddies_area_tracker.virtual == 1)
63+
64+
# %%
65+
# Plot from first ten days
66+
plot_eddy(eddies_area_tracker)
67+
68+
# %%
69+
# Restart from previous run
70+
# -------------------------
71+
# We give all filenames, the new one and filename from previous run
72+
c_second_run = Correspondances(
73+
datasets=file_objects[:20],
74+
# This parameter must be identical in each run
75+
class_method=AreaTracker,
76+
virtual=4,
77+
# Previous saved correspondancs
78+
previous_correspondance="correspondances.nc",
79+
)
80+
start_logger().setLevel("INFO")
81+
c_second_run.track()
82+
start_logger().setLevel("WARNING")
83+
c_second_run.prepare_merging()
84+
# We have now another eddy object
85+
eddies_area_tracker_extend = c_second_run.merge(raw_data=False)
86+
eddies_area_tracker_extend.virtual[:] = eddies_area_tracker_extend.time == 0
87+
eddies_area_tracker_extend.filled_by_interpolation(
88+
eddies_area_tracker_extend.virtual == 1
89+
)
90+
91+
92+
# %%
93+
# Plot with time extension
94+
plot_eddy(eddies_area_tracker_extend)
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"\n# Correspondances\n\nCorrespondances is a mechanism to intend to continue tracking with new detection\n"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {
14+
"collapsed": false
15+
},
16+
"outputs": [],
17+
"source": [
18+
"import logging"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"from matplotlib import pyplot as plt\nfrom netCDF4 import Dataset\n\nfrom py_eddy_tracker import start_logger\nfrom py_eddy_tracker.data import get_remote_demo_sample\nfrom py_eddy_tracker.featured_tracking.area_tracker import AreaTracker\n\n# In order to hide some warning\nimport py_eddy_tracker.observations.observation\nfrom py_eddy_tracker.tracking import Correspondances\n\npy_eddy_tracker.observations.observation._display_check_warning = False"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {
36+
"collapsed": false
37+
},
38+
"outputs": [],
39+
"source": [
40+
"def plot_eddy(ed):\n fig = plt.figure(figsize=(10, 5))\n ax = fig.add_axes([0.05, 0.03, 0.90, 0.94])\n ed.plot(ax, ref=-10, marker=\"x\")\n lc = ed.display_color(ax, field=ed.time, ref=-10, intern=True)\n plt.colorbar(lc).set_label(\"Time in Julian days (from 1950/01/01)\")\n ax.set_xlim(4.5, 8), ax.set_ylim(36.8, 38.3)\n ax.set_aspect(\"equal\")\n ax.grid()"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"Get remote data, we will keep only 20 first days,\n`get_remote_demo_sample` function is only to get demo dataset, in your own case give a list of identification filename\nand don't mix cyclonic and anticyclonic files.\n\n"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {
54+
"collapsed": false
55+
},
56+
"outputs": [],
57+
"source": [
58+
"file_objects = get_remote_demo_sample(\n \"eddies_med_adt_allsat_dt2018/Anticyclonic_2010_2011_2012\"\n)[:20]"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"We run a traking with a tracker which use contour overlap, on 10 first time step\n\n"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {
72+
"collapsed": false
73+
},
74+
"outputs": [],
75+
"source": [
76+
"c_first_run = Correspondances(\n datasets=file_objects[:10], class_method=AreaTracker, virtual=4\n)\nstart_logger().setLevel(\"INFO\")\nc_first_run.track()\nstart_logger().setLevel(\"WARNING\")\nwith Dataset(\"correspondances.nc\", \"w\") as h:\n c_first_run.to_netcdf(h)\n# Next step are done only to build atlas and display it\nc_first_run.prepare_merging()\n\n# We have now an eddy object\neddies_area_tracker = c_first_run.merge(raw_data=False)\neddies_area_tracker.virtual[:] = eddies_area_tracker.time == 0\neddies_area_tracker.filled_by_interpolation(eddies_area_tracker.virtual == 1)"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"Plot from first ten days\n\n"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {
90+
"collapsed": false
91+
},
92+
"outputs": [],
93+
"source": [
94+
"plot_eddy(eddies_area_tracker)"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"## Restart from previous run\nWe give all filenames, the new one and filename from previous run\n\n"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {
108+
"collapsed": false
109+
},
110+
"outputs": [],
111+
"source": [
112+
"c_second_run = Correspondances(\n datasets=file_objects[:20],\n # This parameter must be identical in each run\n class_method=AreaTracker,\n virtual=4,\n # Previous saved correspondancs\n previous_correspondance=\"correspondances.nc\",\n)\nstart_logger().setLevel(\"INFO\")\nc_second_run.track()\nstart_logger().setLevel(\"WARNING\")\nc_second_run.prepare_merging()\n# We have now another eddy object\neddies_area_tracker_extend = c_second_run.merge(raw_data=False)\neddies_area_tracker_extend.virtual[:] = eddies_area_tracker_extend.time == 0\neddies_area_tracker_extend.filled_by_interpolation(\n eddies_area_tracker_extend.virtual == 1\n)"
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"Plot with time extension\n\n"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {
126+
"collapsed": false
127+
},
128+
"outputs": [],
129+
"source": [
130+
"plot_eddy(eddies_area_tracker_extend)"
131+
]
132+
}
133+
],
134+
"metadata": {
135+
"kernelspec": {
136+
"display_name": "Python 3",
137+
"language": "python",
138+
"name": "python3"
139+
},
140+
"language_info": {
141+
"codemirror_mode": {
142+
"name": "ipython",
143+
"version": 3
144+
},
145+
"file_extension": ".py",
146+
"mimetype": "text/x-python",
147+
"name": "python",
148+
"nbconvert_exporter": "python",
149+
"pygments_lexer": "ipython3",
150+
"version": "3.10.10"
151+
}
152+
},
153+
"nbformat": 4,
154+
"nbformat_minor": 0
155+
}

src/py_eddy_tracker/observations/observation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
_software_version_reduced = packaging.version.Version(
8181
"{v.major}.{v.minor}".format(v=packaging.version.parse(__version__))
8282
)
83+
_display_check_warning = True
8384

8485

8586
def _check_versions(version):
@@ -90,7 +91,8 @@ def _check_versions(version):
9091
:param version: string version of software used to create the file. If None, version was not provided
9192
:type version: str, None
9293
"""
93-
94+
if not _display_check_warning:
95+
return
9496
file_version = packaging.version.parse(version) if version is not None else None
9597
if file_version is None or file_version < _software_version_reduced:
9698
logger.warning(
@@ -774,7 +776,7 @@ def load_file(cls, filename, **kwargs):
774776
zarr_file = filename_.endswith(end)
775777
else:
776778
zarr_file = False
777-
logger.info(f"loading file '{filename}'")
779+
logger.info(f"loading file '{filename_}'")
778780
if zarr_file:
779781
return cls.load_from_zarr(filename, **kwargs)
780782
else:

src/py_eddy_tracker/tracking.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"""
33
Class to store link between observations
44
"""
5-
65
from datetime import datetime, timedelta
76
import json
87
import logging
98
import platform
9+
from tarfile import ExFileObject
1010

1111
from netCDF4 import Dataset, default_fillvals
1212
from numba import njit, types as numba_types
@@ -375,7 +375,10 @@ def track(self):
375375
# We begin with second file, first one is in previous
376376
for file_name in self.datasets[first_dataset:]:
377377
self.swap_dataset(file_name, **kwargs)
378-
logger.info("%s match with previous state", file_name)
378+
filename_ = (
379+
file_name.filename if isinstance(file_name, ExFileObject) else file_name
380+
)
381+
logger.info("%s match with previous state", filename_)
379382
logger.debug("%d obs to match", len(self.current_obs))
380383

381384
nb_real_obs = len(self.previous_obs)

0 commit comments

Comments
 (0)