|
| 1 | +import pytest |
| 2 | + |
| 3 | +import imas |
| 4 | +from imas_core import _al_lowlevel |
| 5 | +from imas.exception import DataEntryException |
| 6 | +from imas.ids_defs import IDS_TIME_MODE_HOMOGENEOUS, IDS_TIME_MODE_INDEPENDENT |
| 7 | + |
| 8 | + |
| 9 | +if not hasattr(_al_lowlevel, "al_list_filled_paths"): |
| 10 | + marker = pytest.mark.xfail(reason="list_filled_paths not available in imas_core") |
| 11 | +else: |
| 12 | + marker = [] |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(params=["netcdf", pytest.param("hdf5", marks=marker)]) |
| 16 | +def testuri(request, tmp_path): |
| 17 | + if request.param == "netcdf": |
| 18 | + return str(tmp_path / "list_filled_paths.nc") |
| 19 | + return f"imas:{request.param}?path={tmp_path}/list_filled_paths_{request.param}" |
| 20 | + |
| 21 | + |
| 22 | +def test_list_filled_paths(testuri): |
| 23 | + with imas.DBEntry(testuri, "w", dd_version="4.0.0") as dbentry: |
| 24 | + # No IDSs in the DBEntry yet, expect an exception |
| 25 | + with pytest.raises(DataEntryException): |
| 26 | + dbentry.list_filled_paths("core_profiles") |
| 27 | + |
| 28 | + cp = dbentry.factory.core_profiles() |
| 29 | + cp.ids_properties.homogeneous_time = IDS_TIME_MODE_HOMOGENEOUS |
| 30 | + cp.ids_properties.comment = "comment" |
| 31 | + cp.time = [0.1, 0.2] |
| 32 | + cp.profiles_1d.resize(2) |
| 33 | + cp.profiles_1d[0].grid.rho_tor_norm = [1.0, 2.0] |
| 34 | + cp.profiles_1d[0].ion.resize(2) |
| 35 | + cp.profiles_1d[0].ion[1].temperature = [1.0, 2.0] |
| 36 | + cp.profiles_1d[1].grid.psi = [1.0, 2.0] |
| 37 | + cp.profiles_1d[1].q = [1.0, 2.0] |
| 38 | + cp.profiles_1d[1].e_field.radial = [1.0, 2.0] |
| 39 | + cp.profiles_1d[1].neutral.resize(2) |
| 40 | + cp.global_quantities.ip = [1.0, 2.0] |
| 41 | + |
| 42 | + dbentry.put(cp) |
| 43 | + |
| 44 | + filled_paths = dbentry.list_filled_paths("core_profiles") |
| 45 | + assert isinstance(filled_paths, list) |
| 46 | + assert set(filled_paths) == { |
| 47 | + "ids_properties/version_put/access_layer", |
| 48 | + "ids_properties/version_put/access_layer_language", |
| 49 | + "ids_properties/version_put/data_dictionary", |
| 50 | + "ids_properties/homogeneous_time", |
| 51 | + "ids_properties/comment", |
| 52 | + "time", |
| 53 | + "profiles_1d/grid/rho_tor_norm", |
| 54 | + "profiles_1d/ion/temperature", |
| 55 | + "profiles_1d/grid/psi", |
| 56 | + "profiles_1d/q", |
| 57 | + "profiles_1d/e_field/radial", |
| 58 | + "profiles_1d/e_field/radial", |
| 59 | + "global_quantities/ip", |
| 60 | + } |
| 61 | + # Other occurrence should still raise an error: |
| 62 | + with pytest.raises(DataEntryException): |
| 63 | + dbentry.list_filled_paths("core_profiles", 1) |
| 64 | + # Until we write data to the occurrence: |
| 65 | + dbentry.put(cp, 3) |
| 66 | + assert set(filled_paths) == set(dbentry.list_filled_paths("core_profiles", 3)) |
| 67 | + |
| 68 | + |
| 69 | +def test_list_filled_paths_autoconvert(testuri): |
| 70 | + with imas.DBEntry(testuri, "w", dd_version="3.25.0") as entry: |
| 71 | + ps = entry.factory.pulse_schedule() |
| 72 | + ps.ids_properties.homogeneous_time = IDS_TIME_MODE_INDEPENDENT |
| 73 | + ps.ec.antenna.resize(1) |
| 74 | + ps.ec.antenna[0].launching_angle_pol.reference_name = "test" |
| 75 | + entry.put(ps) |
| 76 | + |
| 77 | + filled_paths = entry.list_filled_paths("pulse_schedule") |
| 78 | + assert set(filled_paths) == { |
| 79 | + "ids_properties/version_put/access_layer", |
| 80 | + "ids_properties/version_put/access_layer_language", |
| 81 | + "ids_properties/version_put/data_dictionary", |
| 82 | + "ids_properties/homogeneous_time", |
| 83 | + "ec/antenna/launching_angle_pol/reference_name", |
| 84 | + } |
| 85 | + |
| 86 | + # Check autoconvert with DD 3.28.0 |
| 87 | + with imas.DBEntry(testuri, "r", dd_version="3.28.0") as entry: |
| 88 | + assert set(entry.list_filled_paths("pulse_schedule", autoconvert=False)) == { |
| 89 | + "ids_properties/version_put/access_layer", |
| 90 | + "ids_properties/version_put/access_layer_language", |
| 91 | + "ids_properties/version_put/data_dictionary", |
| 92 | + "ids_properties/homogeneous_time", |
| 93 | + "ec/antenna/launching_angle_pol/reference_name", # original name |
| 94 | + } |
| 95 | + assert set(entry.list_filled_paths("pulse_schedule")) == { |
| 96 | + "ids_properties/version_put/access_layer", |
| 97 | + "ids_properties/version_put/access_layer_language", |
| 98 | + "ids_properties/version_put/data_dictionary", |
| 99 | + "ids_properties/homogeneous_time", |
| 100 | + "ec/launcher/steering_angle_pol/reference_name", # autoconverted name |
| 101 | + } |
0 commit comments