|
6 | 6 |
|
7 | 7 | import logging |
8 | 8 | import re |
9 | | -from typing import Any, Callable, Iterator, List, Tuple, Union |
| 9 | +from typing import Any, Callable, Iterator, List, Optional, Tuple, Union |
10 | 10 |
|
11 | 11 | import numpy |
12 | 12 |
|
| 13 | +from imaspy.db_entry import DBEntry |
13 | 14 | from imaspy.ids_base import IDSBase |
| 15 | +from imaspy.ids_factory import IDSFactory |
14 | 16 | from imaspy.ids_metadata import IDSMetadata |
15 | | -from imaspy.ids_primitive import IDSPrimitive |
| 17 | +from imaspy.ids_primitive import IDSInt0D, IDSPrimitive |
16 | 18 | from imaspy.ids_struct_array import IDSStructArray |
17 | 19 | from imaspy.ids_structure import IDSStructure |
| 20 | +from imaspy.ids_toplevel import IDSToplevel |
18 | 21 |
|
19 | 22 | logger = logging.getLogger(__name__) |
20 | 23 |
|
@@ -404,3 +407,120 @@ def calc_hash(node: IDSBase) -> bytes: |
404 | 407 | print(imaspy.util.calc_hash(cp).hex()) # 3b9b929756a242fd |
405 | 408 | """ |
406 | 409 | return node._xxhash() |
| 410 | + |
| 411 | + |
| 412 | +def get_parent(node: IDSBase) -> Optional[IDSBase]: |
| 413 | + """Get the parent of any IDS node. |
| 414 | +
|
| 415 | + Args: |
| 416 | + node: Any node (structure, array of structures, data node) of an IDS. |
| 417 | +
|
| 418 | + Returns: |
| 419 | + The parent node of the provided node, or None if the node is an IDS toplevel. |
| 420 | +
|
| 421 | + Example: |
| 422 | + .. code-block:: python |
| 423 | +
|
| 424 | + >>> cp = imaspy.IDSFactory().core_profiles() |
| 425 | + >>> cp.profiles_1d.resize(2) |
| 426 | + >>> imaspy.util.get_parent(cp.profiles_1d[0].electrons.temperature) |
| 427 | + <IDSStructure (IDS:core_profiles, profiles_1d[0]/electrons)> |
| 428 | + >>> imaspy.util.get_parent(cp.profiles_1d[0].electrons) |
| 429 | + <IDSStructure (IDS:core_profiles, profiles_1d[0])> |
| 430 | + >>> imaspy.util.get_parent(cp.profiles_1d[0]) |
| 431 | + <IDSStructArray (IDS:core_profiles, profiles_1d with 2 items)> |
| 432 | + >>> imaspy.util.get_parent(cp.profiles_1d) |
| 433 | + <IDSToplevel (IDS:core_profiles)> |
| 434 | + >>> imaspy.util.get_parent(cp) |
| 435 | + >>> |
| 436 | + """ |
| 437 | + if isinstance(node, IDSToplevel): |
| 438 | + return None |
| 439 | + return node._parent |
| 440 | + |
| 441 | + |
| 442 | +def get_time_mode(node: IDSBase) -> IDSInt0D: |
| 443 | + """Retrieve ``ids_properties/homogeneous_time`` for any node in the IDS. |
| 444 | +
|
| 445 | + Args: |
| 446 | + node: Any node (structure, array of structures, data node) of an IDS. |
| 447 | +
|
| 448 | + Returns: |
| 449 | + ``ids_properties/homogeneous_time``. |
| 450 | +
|
| 451 | + Example: |
| 452 | + .. code-block:: python |
| 453 | +
|
| 454 | + >>> cp = imaspy.IDSFactory().core_profiles() |
| 455 | + >>> cp.ids_properties.homogeneous_time = 0 |
| 456 | + >>> cp.profiles_1d.resize(2) |
| 457 | + >>> imaspy.util.get_time_mode(cp.profiles_1d[0].electrons.temperature) |
| 458 | + <IDSInt0D (IDS:core_profiles, ids_properties/homogeneous_time, INT_0D)> |
| 459 | + int(0) |
| 460 | + """ |
| 461 | + return node._time_mode |
| 462 | + |
| 463 | + |
| 464 | +def get_toplevel(node: IDSBase) -> IDSToplevel: |
| 465 | + """Retrieve the toplevel IDS object for any node in the IDS. |
| 466 | +
|
| 467 | + Args: |
| 468 | + node: Any node (structure, array of structures, data node) of an IDS. |
| 469 | +
|
| 470 | + Returns: |
| 471 | + The toplevel IDS object. |
| 472 | +
|
| 473 | + Example: |
| 474 | + .. code-block:: python |
| 475 | +
|
| 476 | + >>> cp = imaspy.IDSFactory().core_profiles() |
| 477 | + >>> cp.profiles_1d.resize(2) |
| 478 | + >>> imaspy.util.get_toplevel(cp.profiles_1d[0].electrons.temperature) |
| 479 | + <IDSToplevel (IDS:core_profiles)> |
| 480 | + """ |
| 481 | + return node._toplevel |
| 482 | + |
| 483 | + |
| 484 | +def is_lazy_loaded(node: IDSBase) -> bool: |
| 485 | + """Find out if the provided (node of an) IDS is lazy loaded. |
| 486 | +
|
| 487 | + Args: |
| 488 | + node: Any node (structure, array of structures, data node) of an IDS. |
| 489 | + """ |
| 490 | + return node._lazy |
| 491 | + |
| 492 | + |
| 493 | +def get_full_path(node: IDSBase) -> str: |
| 494 | + """Get the full path (relative to the IDS toplevel) of the provided node. |
| 495 | +
|
| 496 | + Caution: |
| 497 | + Determining the path is relatively expensive in large, nested Arrays of |
| 498 | + Structures: the calculation of the index suffix is O(N) in the size of the AoS. |
| 499 | +
|
| 500 | + Using this function may result in a performance bottleneck for your application. |
| 501 | +
|
| 502 | + Example: |
| 503 | + .. code-block:: python |
| 504 | +
|
| 505 | + >>> cp = imaspy.IDSFactory().core_profiles() |
| 506 | + >>> cp.profiles_1d.resize(2) |
| 507 | + >>> imaspy.util.get_full_path(cp.profiles_1d[1].electrons.temperature) |
| 508 | + 'profiles_1d[1]/electrons/temperature' |
| 509 | + """ |
| 510 | + return node._path |
| 511 | + |
| 512 | + |
| 513 | +def get_data_dictionary_version(obj: Union[IDSBase, DBEntry, IDSFactory]) -> str: |
| 514 | + """Find out the version of the data dictionary definitions that this object uses. |
| 515 | +
|
| 516 | + Args: |
| 517 | + obj: Any IMASPy object that is data-dictionary dependent. |
| 518 | +
|
| 519 | + Returns: |
| 520 | + The data dictionary version, e.g. ``"3.38.1"``. |
| 521 | + """ |
| 522 | + if isinstance(obj, (DBEntry, IDSFactory)): |
| 523 | + return obj.dd_version |
| 524 | + if isinstance(obj, IDSBase): |
| 525 | + return obj._version |
| 526 | + raise TypeError(f"Cannot get data dictionary version of '{type(obj)}'") |
0 commit comments