Skip to content

Commit 1e7d2df

Browse files
committed
add exporting to IDSs
1 parent ee24add commit 1e7d2df

File tree

2 files changed

+79
-13
lines changed

2 files changed

+79
-13
lines changed

waveform_editor/cli.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,15 @@ def export_png(yaml, output):
7474
exporter.to_png(output)
7575

7676

77-
# # TODO:
78-
# @cli.command("export-ids")
79-
# @click.argument("yaml", type=str)
80-
# @click.argument("uri", type=str)
81-
# @click.option("--dd-version", type=str)
82-
# def export_ids(yaml, uri, path):
83-
# """Export waveform data to an IDS."""
84-
# # waveform = load_waveform_from_yaml(yaml)
85-
# # exporter = WaveformExporter(waveform)
86-
# # exporter.to_ids(uri, path)
77+
@cli.command("export-ids")
78+
@click.argument("yaml", type=str)
79+
@click.argument("uri", type=str)
80+
@click.option("--dd-version", type=str)
81+
def export_ids(yaml, uri, dd_version):
82+
"""Export waveform data to an IDS."""
83+
waveform = load_waveform_from_yaml(yaml)
84+
exporter = WaveformExporter(waveform)
85+
exporter.to_ids(uri)
8786

8887

8988
def load_waveform_from_yaml(yaml_file):

waveform_editor/waveform_exporter.py

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import csv
22
import logging
33

4+
import imaspy
45
import numpy as np
56
import plotly.graph_objects as go
67

@@ -28,9 +29,75 @@ def __init__(self, waveform, times=None):
2829
self.time_label = "Time [s]"
2930
self.value_label = "Value [unit]"
3031

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")
34101

35102
def to_csv(self, file_path):
36103
with open(file_path, mode="w", newline="") as file:

0 commit comments

Comments
 (0)