|
| 1 | +# This file is part of IMAS-Python. |
| 2 | +# You should have received the IMAS-Python LICENSE file with this project. |
| 3 | +"""Logic to convert core/edge IDSs to their corresponding plasma ID.""" |
| 4 | + |
| 5 | +from packaging.version import Version |
| 6 | + |
| 7 | +from imas.ids_toplevel import IDSToplevel |
| 8 | +from imas.ids_factory import IDSFactory |
| 9 | +from imas.exception import IDSNameError |
| 10 | +from imas.ids_convert import DDVersionMap, NBCPathMap, _copy_structure |
| 11 | + |
| 12 | + |
| 13 | +def convert_to_plasma_profiles( |
| 14 | + core_or_edge_profiles: IDSToplevel, *, deepcopy: bool = False |
| 15 | +) -> IDSToplevel: |
| 16 | + return _convert_to_plasma(core_or_edge_profiles, "profiles", deepcopy) |
| 17 | + |
| 18 | + |
| 19 | +def convert_to_plasma_sources( |
| 20 | + core_or_edge_sources: IDSToplevel, *, deepcopy: bool = False |
| 21 | +) -> IDSToplevel: |
| 22 | + return _convert_to_plasma(core_or_edge_sources, "sources", deepcopy) |
| 23 | + |
| 24 | + |
| 25 | +def convert_to_plasma_transport( |
| 26 | + core_or_edge_transport: IDSToplevel, *, deepcopy: bool = False |
| 27 | +) -> IDSToplevel: |
| 28 | + return _convert_to_plasma(core_or_edge_transport, "transport", deepcopy) |
| 29 | + |
| 30 | + |
| 31 | +class CoreEdgePlasmaMap(DDVersionMap): |
| 32 | + """Subclass of DDVersionMap to generate an NBCPathMap that is suitable to copy |
| 33 | + between a core/edge IDS and the corresponding plasma IDS.""" |
| 34 | + |
| 35 | + def __init__(self, source, target, factory): |
| 36 | + self.ids_name = source |
| 37 | + self.old_version = factory._etree |
| 38 | + self.new_version = factory._etree |
| 39 | + self.version_old = Version(factory.version) |
| 40 | + |
| 41 | + self.old_to_new = NBCPathMap() |
| 42 | + self.new_to_old = NBCPathMap() |
| 43 | + |
| 44 | + old_ids_object = factory._etree.find(f"IDS[@name='{source}']") |
| 45 | + new_ids_object = factory._etree.find(f"IDS[@name='{target}']") |
| 46 | + self._build_map(old_ids_object, new_ids_object) |
| 47 | + |
| 48 | + |
| 49 | +def _convert_to_plasma(source: IDSToplevel, suffix: str, deepcopy: bool) -> IDSToplevel: |
| 50 | + # Sanity checks for input data |
| 51 | + if not isinstance(source, IDSToplevel): |
| 52 | + raise TypeError( |
| 53 | + f"First argument to convert_to_plasma_{suffix} must be a core_{suffix} or " |
| 54 | + f"edge_{suffix} of type IDSToplevel. Got a type {type(source)} instead." |
| 55 | + ) |
| 56 | + if source.metadata.name not in [f"core_{suffix}", f"edge_{suffix}"]: |
| 57 | + raise ValueError( |
| 58 | + f"First argument to convert_to_plasma_{suffix} must be a core_{suffix} or " |
| 59 | + f"edge_{suffix} IDS. Got a {source.metadata.name} IDS instead." |
| 60 | + ) |
| 61 | + |
| 62 | + # Construct target plasma_{suffix} IDS |
| 63 | + factory: IDSFactory = source._parent |
| 64 | + try: |
| 65 | + target = factory.new(f"plasma_{suffix}") |
| 66 | + except IDSNameError: |
| 67 | + raise ValueError( |
| 68 | + f"Cannot convert {source.metadata.name} IDS to plasma_{suffix}: the source " |
| 69 | + f"IDS uses Data Dictionary version {factory.dd_version} which doesn't have " |
| 70 | + f"a plasma_{suffix} IDS. Please convert the source IDS to a supported Data " |
| 71 | + "Dictionary version using `imas.convert_ids` and try again." |
| 72 | + ) from None |
| 73 | + |
| 74 | + # Leverage existing logic from ids_convert to do the copying |
| 75 | + # First construct a map (to handle missing items in the target IDS) |
| 76 | + data_map = CoreEdgePlasmaMap(source.metadata.name, target.metadata.name, factory) |
| 77 | + path_map = data_map.old_to_new # old = core/edge, new = plasma IDS |
| 78 | + _copy_structure(source, target, deepcopy, path_map) |
| 79 | + |
| 80 | + return target |
0 commit comments