@@ -19,6 +19,18 @@ def __init__(self, waveform, times):
1919 self .value_label = "Value [unit]"
2020
2121 def parse_uri (self , uri ):
22+ """Parse URI into its constituents.
23+
24+ Args:
25+ uri: String containing the URI.
26+
27+ Returns:
28+ Constituents of the URI:
29+ - The scheme, backend and query parts of the URI
30+ - The name of the IDS.
31+ - The occurrence number (defaults to 0 if not provided)
32+ - Path of the IDS quantity to export to
33+ """
2234 uri_entry , fragment = uri .split ("#" )
2335 fragment_parts = fragment .split ("/" )
2436 idsname_part = fragment_parts [0 ]
@@ -35,6 +47,13 @@ def parse_uri(self, uri):
3547 return uri_entry , ids_name , occurrence , ids_path
3648
3749 def to_ids (self , uri , dd_version = None ):
50+ """Export the waveform to an IDS.
51+
52+ Args:
53+ uri: URI containing scheme, backend, query and fragment parts.
54+ dd_version: The data dictionary version to export to. If None, IMASPy's
55+ default version will be used.
56+ """
3857 uri_entry , uri_ids , occurrence , path = self .parse_uri (uri )
3958 entry = imaspy .DBEntry (uri_entry , "r" , dd_version = dd_version )
4059 ids = entry .get (uri_ids , occurrence , autoconvert = False )
@@ -63,39 +82,23 @@ def to_ids(self, uri, dd_version=None):
6382 entry .put (ids )
6483 entry .close ()
6584
66- def _fill_flt_0d (self , ids , path , is_homogeneous ):
67- aos_path , remaining_path = path .split ("()" )
68- aos_path = aos_path .strip ("/" )
69- remaining_path = remaining_path .strip ("/" )
70- aos = ids [aos_path ]
71- aos .resize (len (self .times ))
72-
73- for i , time in enumerate (self .times ):
74- if aos [i ][remaining_path ].data_type == "FLT_0D" :
75- aos [i ][remaining_path ] = self .values [i ]
76- if not is_homogeneous :
77- aos [i ].time = time
78- else :
79- raise NotImplementedError ("Should be float 0d" )
80-
81- def _fill_flt_1d (self , ids , path , is_homogeneous ):
82- quantity = ids [path ]
83- struct_ref = quantity .metadata .structure_reference
84- if struct_ref == "signal_flt_1d" :
85- quantity .data = self .values
86- if not is_homogeneous :
87- quantity .time = self .times
88- else :
89- raise NotImplementedError ("Invalid data" )
90-
9185 def to_csv (self , file_path ):
86+ """Export the waveform to a CSV.
87+
88+ Args:
89+ file_path: The file path and name to store the CSV to.
90+ """
9291 with open (file_path , mode = "w" , newline = "" ) as file :
9392 writer = csv .writer (file )
9493 writer .writerow ([self .time_label , self .value_label ])
9594 writer .writerows (zip (self .times , self .values ))
9695
9796 def to_png (self , file_path ):
98- """Export waveform data as a PNG plot using Plotly Express."""
97+ """Export the waveform to a PNG.
98+
99+ Args:
100+ file_path: The file path and name to store the PNG to.
101+ """
99102
100103 fig = go .Figure (
101104 data = go .Scatter (
@@ -113,3 +116,47 @@ def to_png(self, file_path):
113116 # TODO: implement export to XML format
114117 # def to_pcssp(self, file_path)
115118 # pass
119+
120+ def _fill_flt_0d (self , ids , path , is_homogeneous ):
121+ """Fill a FLT_0D IDS quantity in an IDS.
122+
123+ It is assumed that the time dependent AoS is provided in the path using `()`,
124+ for example:
125+
126+ imas:hdf5?path=./test_equilibrium#equilibrium/time_slice()/boundary/elongation
127+
128+ Arguments:
129+ ids: The IDS to fill.
130+ path: The path to the FLT_0D quantity to fill.
131+ is_homogeneous: Whether to fill the local time array, or the ids.time array
132+ """
133+ aos_path , remaining_path = path .split ("()" )
134+ aos_path = aos_path .strip ("/" )
135+ remaining_path = remaining_path .strip ("/" )
136+ aos = ids [aos_path ]
137+ aos .resize (len (self .times ))
138+
139+ for i , time in enumerate (self .times ):
140+ if aos [i ][remaining_path ].data_type == "FLT_0D" :
141+ aos [i ][remaining_path ] = self .values [i ]
142+ if not is_homogeneous :
143+ aos [i ].time = time
144+ else :
145+ raise NotImplementedError ("Should be float 0d" )
146+
147+ def _fill_flt_1d (self , ids , path , is_homogeneous ):
148+ """Fill a FLT_1D IDS quantity in an IDS.
149+
150+ Arguments:
151+ ids: The IDS to fill.
152+ path: The path to the FLT_1D quantity to fill.
153+ is_homogeneous: Whether to fill the local time array, or the ids.time array
154+ """
155+ quantity = ids [path ]
156+ struct_ref = quantity .metadata .structure_reference
157+ if struct_ref == "signal_flt_1d" :
158+ quantity .data = self .values
159+ if not is_homogeneous :
160+ quantity .time = self .times
161+ else :
162+ raise NotImplementedError ("Invalid data" )
0 commit comments