Skip to content

Commit 8f1db5f

Browse files
maarten-icolivhoenen
authored andcommitted
Disable implicit conversion between major versions of the DD
Implicit conversion between major versions of the DD is almost always giving incorrect results, so it's better to disallow this instead of emitting warnings (which people usually don't read). This commit changes the warnings that were previously emitted into runtime errors. The errors refer to the (updated) documentation, which provides an example that does work correctly.
1 parent f84445e commit 8f1db5f

File tree

3 files changed

+78
-17
lines changed

3 files changed

+78
-17
lines changed

docs/source/multi-dd.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,70 @@ explicit conversion mechanisms.
213213
``boundary_secondary_separatrix`` structures from DD3. See also:
214214
https://github.com/iterorganization/IMAS-Python/issues/60.
215215
216+
217+
.. _`Loading IDSs from a different major version`:
218+
219+
Loading IDSs from a different major version
220+
-------------------------------------------
221+
222+
If you try to load an IDS that was stored in a different major version of the DD than
223+
you are using, IMAS-Python will raise a runtime error, for example:
224+
225+
.. code-block:: text
226+
227+
On-disk data is stored in DD 3.39.1 which has a different major version than the
228+
requested DD version (4.0.0). IMAS-Python will not automatically convert this
229+
data for you.
230+
231+
You need to explicitly convert the data, which you can do as follows:
232+
233+
.. code-block:: python
234+
235+
# Opened data entry
236+
entry = imas.DBEntry(...)
237+
238+
# A plain get, or get_slice will raise a RuntimeError when the data is stored in
239+
# a different major version of the DD:
240+
# entry.get("equilibrium")
241+
242+
# So instead, we'll load the IDS in the DD version it is stored on disk
243+
tmp_eq = entry.get("equilibrium", autoconvert=False)
244+
# And explicitly convert it to the target version
245+
equilibrium = imas.convert_ids(tmp_eq, entry.dd_version)
246+
247+
248+
.. _`Storing IDSs with a different major version`:
249+
250+
Storing IDSs with a different major version
251+
-------------------------------------------
252+
253+
If you try to put an IDS that was created for a different major version of the DD than
254+
the Data Entry you want to store it in, IMAS-Python raise a runtime error, for example:
255+
256+
.. code-block:: text
257+
258+
Provided IDS uses DD 3.42.2 which has a different major version than the Data
259+
Entry (4.0.0). IMAS-Python will not automatically convert this data for you.
260+
261+
You need to explicitly convert the data, which you can do as follows:
262+
263+
.. code-block:: python
264+
265+
# IDS with data, in DD 3.42.2
266+
equilibrium = imas.IDSFactory("3.42.2").equilibrium()
267+
...
268+
269+
# Data Entry uses DD 4.0.0
270+
with imas.DBEntry(uri, "w", dd_version="4.0.0") as entry:
271+
# This put would raise a runtime error, because the major version of the IDS
272+
# and the DBEntry don't match:
273+
# entry.put(equilibrium)
274+
275+
# So instead, we'll explicitly convert the IDS and put that one
276+
entry.put(imas.convert_ids(equilibrium, entry.dd_version))
277+
278+
279+
216280
.. _`DD background`:
217281

218282
Background information

imas/backends/imas_core/db_entry_al.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,13 @@ def put(self, ids: IDSToplevel, occurrence: int, is_slice: bool) -> None:
282282
nbc_map = None
283283
if ids._version != self._ids_factory._version:
284284
if ids._version.split(".")[0] != self._ids_factory._version.split(".")[0]:
285-
logger.warning(
286-
"Provided IDS uses DD %s which has a different major version than "
287-
"the Data Entry (%s). IMAS-Python will convert the data "
288-
"automatically, but this does not cover all changes. "
289-
"See %s/multi-dd.html#conversion-of-idss-between-dd-versions",
290-
ids._version,
291-
self._ids_factory._version,
292-
imas.PUBLISHED_DOCUMENTATION_ROOT,
285+
raise RuntimeError(
286+
f"Provided IDS uses DD {ids._version} which has a different major "
287+
f"version than the Data Entry ({self._ids_factory._version}). "
288+
"IMAS-Python will not automatically convert this data for you."
289+
"See the documentation for more details and fixes: "
290+
f"{imas.PUBLISHED_DOCUMENTATION_ROOT}"
291+
"/multi-dd.html#storing-idss-with-a-different-major-version"
293292
)
294293
ddmap, source_is_older = dd_version_map_from_factories(
295294
ids_name, ids._parent, self._ids_factory

imas/db_entry.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -605,15 +605,13 @@ def _get(
605605
nbc_map = None
606606
if dd_version and dd_version != destination._dd_version:
607607
if dd_version.split(".")[0] != destination._dd_version.split(".")[0]:
608-
logger.warning(
609-
"On-disk data is stored in DD %s which has a different major "
610-
"version than the requested DD version (%s). IMAS-Python will "
611-
"convert the data automatically, but this does not cover all "
612-
"changes. "
613-
"See %s/multi-dd.html#conversion-of-idss-between-dd-versions",
614-
dd_version,
615-
destination._dd_version,
616-
imas.PUBLISHED_DOCUMENTATION_ROOT,
608+
raise RuntimeError(
609+
f"On-disk data is stored in DD {dd_version} which has a different "
610+
"major version than the requested DD version "
611+
f"({destination._dd_version}). IMAS-Python will not automatically "
612+
"convert this data for you. See the documentation for more "
613+
f"details and fixes: {imas.PUBLISHED_DOCUMENTATION_ROOT}"
614+
"/multi-dd.html#loading-idss-from-a-different-major-version"
617615
)
618616
ddmap, source_is_older = dd_version_map_from_factories(
619617
ids_name, IDSFactory(version=dd_version), self._ids_factory

0 commit comments

Comments
 (0)