Skip to content

Commit ce8ab42

Browse files
warning when loading data with different py-eddy-tracker versions
1 parent b0eecd9 commit ce8ab42

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/py_eddy_tracker/observations/observation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from tarfile import ExFileObject
99
from tokenize import TokenError
1010

11+
import packaging
1112
import zarr
1213
from matplotlib.cm import get_cmap
1314
from matplotlib.collections import PolyCollection
@@ -74,6 +75,25 @@
7475

7576
logger = logging.getLogger("pet")
7677

78+
# keep only major and minor version number
79+
_software_version_reduced = packaging.version.Version("{v.major}.{v.minor}".format(v=packaging.version.parse(__version__)))
80+
81+
82+
def _check_versions(version):
83+
"""Check if version of py_eddy_tracker used to create the file is compatible with software version
84+
85+
if not, warn user with both versions
86+
87+
:param version: string version of software used to create the file. If None, version was not provided
88+
:type version: str, None
89+
"""
90+
91+
file_version = packaging.version.parse(version) if version is not None else None
92+
if file_version is None or file_version < _software_version_reduced:
93+
logger.warning("File was created with py-eddy-tracker version '%s' but software version is '%s'",
94+
file_version, _software_version_reduced
95+
)
96+
7797

7898
@njit(cache=True, fastmath=True)
7999
def shifted_ellipsoid_degrees_mask2(lon0, lat0, lon1, lat1, minor=1.5, major=1.5):
@@ -765,6 +785,8 @@ def load_from_zarr(
765785
if not isinstance(filename, str):
766786
filename = filename.astype(str)
767787
h_zarr = zarr.open(filename)
788+
789+
_check_versions(h_zarr.attrs.get("framework_version", None))
768790
var_list = cls.build_var_list(list(h_zarr.keys()), remove_vars, include_vars)
769791

770792
nb_obs = getattr(h_zarr, var_list[0]).shape[0]
@@ -908,6 +930,8 @@ def load_from_netcdf(
908930
else:
909931
args, kwargs = (filename,), dict()
910932
with Dataset(*args, **kwargs) as h_nc:
933+
_check_versions(getattr(h_nc, "framework_version", None))
934+
911935
var_list = cls.build_var_list(
912936
list(h_nc.variables.keys()), remove_vars, include_vars
913937
)

0 commit comments

Comments
 (0)