|
1 | 1 | import csv |
2 | 2 | import logging |
3 | 3 |
|
| 4 | +import imaspy |
4 | 5 | import numpy as np |
5 | 6 | import plotly.graph_objects as go |
6 | 7 |
|
@@ -28,9 +29,75 @@ def __init__(self, waveform, times=None): |
28 | 29 | self.time_label = "Time [s]" |
29 | 30 | self.value_label = "Value [unit]" |
30 | 31 |
|
31 | | - # # TODO: implement export to IDS |
32 | | - # def to_ids(self, uri): |
33 | | - # pass |
| 32 | + def parse_uri(self, uri): |
| 33 | + uri_entry, fragment = uri.split("#") |
| 34 | + fragment_parts = fragment.split("/") |
| 35 | + idsname_part = fragment_parts[0] |
| 36 | + |
| 37 | + # Get occurrence number and IDS name |
| 38 | + if ":" in idsname_part: |
| 39 | + ids_name, occurrence_str = idsname_part.split(":") |
| 40 | + occurrence = int(occurrence_str) |
| 41 | + else: |
| 42 | + ids_name = idsname_part |
| 43 | + occurrence = 0 |
| 44 | + |
| 45 | + ids_path = "/" + "/".join(fragment_parts[1:]) if len(fragment_parts) > 1 else "" |
| 46 | + return uri_entry, ids_name, occurrence, ids_path |
| 47 | + |
| 48 | + def to_ids(self, uri, dd_version=None): |
| 49 | + uri_entry, uri_ids, occurrence, path = self.parse_uri(uri) |
| 50 | + entry = imaspy.DBEntry(uri_entry, "r", dd_version=dd_version) |
| 51 | + ids = entry.get(uri_ids, occurrence, autoconvert=False) |
| 52 | + |
| 53 | + if ( |
| 54 | + ids.ids_properties.homogeneous_time |
| 55 | + == imaspy.ids_defs.IDS_TIME_MODE_HETEROGENEOUS |
| 56 | + ): |
| 57 | + is_homogeneous = False |
| 58 | + elif ( |
| 59 | + ids.ids_properties.homogeneous_time |
| 60 | + == imaspy.ids_defs.IDS_TIME_MODE_HOMOGENEOUS |
| 61 | + ): |
| 62 | + is_homogeneous = True |
| 63 | + ids.time = self.times |
| 64 | + else: |
| 65 | + raise NotImplementedError( |
| 66 | + "The time mode must be homogeneous or heterogeneous." |
| 67 | + ) |
| 68 | + |
| 69 | + if "()" in path: |
| 70 | + self._fill_flt_0d(entry, ids, path, is_homogeneous) |
| 71 | + else: |
| 72 | + self._fill_flt_1d(entry, ids, path, is_homogeneous) |
| 73 | + |
| 74 | + entry.put(ids) |
| 75 | + entry.close() |
| 76 | + |
| 77 | + def _fill_flt_0d(self, ids, path, is_homogeneous): |
| 78 | + aos_path, remaining_path = path.split("()") |
| 79 | + aos_path = aos_path.strip("/") |
| 80 | + remaining_path = remaining_path.strip("/") |
| 81 | + aos = ids[aos_path] |
| 82 | + aos.resize(len(self.times)) |
| 83 | + |
| 84 | + for i, time in enumerate(self.times): |
| 85 | + if aos[i][remaining_path].data_type == "FLT_0D": |
| 86 | + aos[i][remaining_path] = self.values[i] |
| 87 | + if not is_homogeneous: |
| 88 | + aos[i].time = time |
| 89 | + else: |
| 90 | + raise NotImplementedError("Should be float 0d") |
| 91 | + |
| 92 | + def _fill_flt_1d(self, ids, path, is_homogeneous): |
| 93 | + quantity = ids[path] |
| 94 | + struct_ref = quantity.metadata.structure_reference |
| 95 | + if struct_ref == "signal_flt_1d": |
| 96 | + quantity.data = self.values |
| 97 | + if not is_homogeneous: |
| 98 | + quantity.time = self.times |
| 99 | + else: |
| 100 | + raise NotImplementedError("Invalid data") |
34 | 101 |
|
35 | 102 | def to_csv(self, file_path): |
36 | 103 | with open(file_path, mode="w", newline="") as file: |
|
0 commit comments