|
1 | 1 | import csv |
2 | 2 | from pathlib import Path |
3 | 3 |
|
| 4 | +# TODO: use netcdf and imas instead of imaspy |
| 5 | +import imaspy as imas |
4 | 6 | import numpy as np |
5 | 7 | import pytest |
6 | 8 |
|
@@ -109,6 +111,95 @@ def test_to_png(exporter, tmp_path): |
109 | 111 | assert Path(file_path).exists() |
110 | 112 |
|
111 | 113 |
|
112 | | -# TODO: Write tests for exporting to IDS, this requires IMASPy as a dependency |
113 | | -# def test_to_ids(exporter): |
114 | | -# pass |
| 114 | +def test_to_ids_flt1d(exporter, tmp_path): |
| 115 | + ids = imas.IDSFactory().ec_launchers() |
| 116 | + ids.time = [0] |
| 117 | + ids.ids_properties.homogeneous_time = imas.ids_defs.IDS_TIME_MODE_HOMOGENEOUS |
| 118 | + file_path = f"imas:hdf5?path={tmp_path}/test_ec_launchers" |
| 119 | + with imas.DBEntry(file_path, "w") as dbentry: |
| 120 | + dbentry.put(ids) |
| 121 | + |
| 122 | + uri = f"{file_path}#ec_launchers/beam(1)/power_launched" |
| 123 | + exporter.to_ids(uri) |
| 124 | + |
| 125 | + with imas.DBEntry(file_path, "r") as dbentry: |
| 126 | + ids = dbentry.get("ec_launchers", autoconvert=False) |
| 127 | + assert np.all(ids.time == exporter.times) |
| 128 | + assert np.all(ids.beam[0].power_launched.data == exporter.values) |
| 129 | + |
| 130 | + |
| 131 | +def test_to_ids_flt1d_heterogeneous(exporter, tmp_path): |
| 132 | + ids = imas.IDSFactory().ec_launchers() |
| 133 | + ids.time = [0] |
| 134 | + ids.ids_properties.homogeneous_time = imas.ids_defs.IDS_TIME_MODE_HETEROGENEOUS |
| 135 | + file_path = f"imas:hdf5?path={tmp_path}/test_ec_launchers" |
| 136 | + with imas.DBEntry(file_path, "w") as dbentry: |
| 137 | + dbentry.put(ids) |
| 138 | + |
| 139 | + uri = f"{file_path}#ec_launchers/beam(1)/power_launched" |
| 140 | + exporter.to_ids(uri) |
| 141 | + |
| 142 | + with imas.DBEntry(file_path, "r") as dbentry: |
| 143 | + ids = dbentry.get("ec_launchers", autoconvert=False) |
| 144 | + assert np.all(ids.beam[0].power_launched.time == exporter.times) |
| 145 | + assert np.all(ids.beam[0].power_launched.data == exporter.values) |
| 146 | + |
| 147 | + |
| 148 | +def test_to_ids_flt1d_occurrence(exporter, tmp_path): |
| 149 | + ids = imas.IDSFactory().ec_launchers() |
| 150 | + ids.time = [0] |
| 151 | + ids.ids_properties.homogeneous_time = imas.ids_defs.IDS_TIME_MODE_HOMOGENEOUS |
| 152 | + file_path = f"imas:hdf5?path={tmp_path}/test_ec_launchers" |
| 153 | + with imas.DBEntry(file_path, "w") as dbentry: |
| 154 | + dbentry.put(ids) |
| 155 | + dbentry.put(ids, occurrence=1) |
| 156 | + |
| 157 | + uri = f"{file_path}#ec_launchers:1/beam(1)/power_launched" |
| 158 | + exporter.to_ids(uri) |
| 159 | + |
| 160 | + with imas.DBEntry(file_path, "r") as dbentry: |
| 161 | + # Check if only occurrence 1 is filled |
| 162 | + ids = dbentry.get("ec_launchers", occurrence=0, autoconvert=False) |
| 163 | + assert np.all(ids.time == [0]) |
| 164 | + assert not ids.beam |
| 165 | + |
| 166 | + ids = dbentry.get("ec_launchers", occurrence=1, autoconvert=False) |
| 167 | + assert np.all(ids.time == exporter.times) |
| 168 | + assert np.all(ids.beam[0].power_launched.data == exporter.values) |
| 169 | + |
| 170 | + |
| 171 | +def test_to_ids_flt0d(exporter, tmp_path): |
| 172 | + ids = imas.IDSFactory().equilibrium() |
| 173 | + ids.time = [0] |
| 174 | + ids.ids_properties.homogeneous_time = imas.ids_defs.IDS_TIME_MODE_HOMOGENEOUS |
| 175 | + file_path = f"imas:hdf5?path={tmp_path}/test_equilibrium" |
| 176 | + with imas.DBEntry(file_path, "w") as dbentry: |
| 177 | + dbentry.put(ids) |
| 178 | + |
| 179 | + uri = f"{file_path}#equilibrium/time_slice()/boundary/elongation" |
| 180 | + exporter.to_ids(uri) |
| 181 | + |
| 182 | + with imas.DBEntry(file_path, "r") as dbentry: |
| 183 | + ids = dbentry.get("equilibrium", autoconvert=False) |
| 184 | + assert np.all(ids.time == exporter.times) |
| 185 | + for i, time_slice in enumerate(ids.time_slice): |
| 186 | + assert time_slice.boundary.elongation == exporter.values[i] |
| 187 | + |
| 188 | + |
| 189 | +def test_to_ids_flt0d_heterogeneous(exporter, tmp_path): |
| 190 | + ids = imas.IDSFactory().equilibrium() |
| 191 | + ids.time = [0] |
| 192 | + ids.ids_properties.homogeneous_time = imas.ids_defs.IDS_TIME_MODE_HETEROGENEOUS |
| 193 | + file_path = f"imas:hdf5?path={tmp_path}/test_equilibrium" |
| 194 | + with imas.DBEntry(file_path, "w") as dbentry: |
| 195 | + dbentry.put(ids) |
| 196 | + |
| 197 | + uri = f"{file_path}#equilibrium/time_slice()/boundary/elongation" |
| 198 | + exporter.to_ids(uri) |
| 199 | + |
| 200 | + with imas.DBEntry(file_path, "r") as dbentry: |
| 201 | + ids = dbentry.get("equilibrium", autoconvert=False) |
| 202 | + imas.util.print_tree(ids) |
| 203 | + for i, time_slice in enumerate(ids.time_slice): |
| 204 | + assert time_slice.boundary.elongation == exporter.values[i] |
| 205 | + assert time_slice.time == exporter.times[i] |
0 commit comments