Coverage for python/gsfit/database_readers/st40_mdsplus/setup_flux_loops.py: 0%

30 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-07 13:12 +0000

1import typing 

2from typing import TYPE_CHECKING 

3 

4import numpy as np 

5import numpy.typing as npt 

6from gsfit_rs import FluxLoops 

7from st40_database import GetData 

8 

9if TYPE_CHECKING: 

10 from . import DatabaseReader 

11 

12 

13def setup_flux_loops( 

14 self: "DatabaseReader", 

15 pulseNo: int, 

16 settings: dict[str, typing.Any], 

17) -> FluxLoops: 

18 """ 

19 This method initialises the Rust `FluxLoops` class. 

20 

21 :param pulseNo: Pulse number, used to read from the database 

22 :param settings: Dictionary containing the JSON settings read from the `settings` directory 

23 

24 **This method is specific to ST40's experimental MDSplus database.** 

25 

26 See `python/gsfit/database_readers/interface.py` for more details on how a new database_reader should be implemented. 

27 """ 

28 

29 # Initialise the FluxLoops Rust class 

30 flux_loops = FluxLoops() 

31 

32 # read in mag data from MDSplus 

33 mag_run_name = settings["GSFIT_code_settings.json"]["database_reader"]["st40_mdsplus"]["workflow"]["mag"]["run_name"] 

34 mag = GetData(pulseNo, f"MAG#{mag_run_name}", is_fail_quiet=False) 

35 

36 # FL-probes 

37 names_long = typing.cast(list[str], mag.get("FLOOP.ALL.NAMES")) 

38 sensors_names = np.char.replace(names_long, "FLOOP_", "L") 

39 sensors_r = typing.cast(npt.NDArray[np.float64], mag.get("FLOOP.ALL.R")) 

40 sensors_z = typing.cast(npt.NDArray[np.float64], mag.get("FLOOP.ALL.Z")) 

41 

42 n_sensors = len(sensors_names) 

43 for i_sensor in range(0, n_sensors): 

44 sensor_name = sensors_names[i_sensor] 

45 if sensor_name in settings["sensor_weights_flux_loops.json"]: 

46 fit_settings_comment = settings["sensor_weights_flux_loops.json"][sensor_name]["fit_settings"]["comment"] 

47 fit_settings_expected_value = settings["sensor_weights_flux_loops.json"][sensor_name]["fit_settings"]["expected_value"] 

48 fit_settings_include = settings["sensor_weights_flux_loops.json"][sensor_name]["fit_settings"]["include"] 

49 fit_settings_weight = settings["sensor_weights_flux_loops.json"][sensor_name]["fit_settings"]["weight"] / (2.0 * np.pi) 

50 else: 

51 fit_settings_comment = "" 

52 fit_settings_expected_value = np.nan 

53 fit_settings_include = False 

54 fit_settings_weight = np.nan 

55 

56 # Measured signals 

57 time = typing.cast(npt.NDArray[np.float32], mag.get("TIME")).astype(np.float64) 

58 measured = typing.cast(npt.NDArray[np.float32], mag.get(f"FLOOP.{sensor_name}.PSI")).astype(np.float64) 

59 

60 # Add the sensor to the Rust class 

61 flux_loops.add_sensor( 

62 name=sensors_names[i_sensor], 

63 geometry_r=sensors_r[i_sensor], 

64 geometry_z=sensors_z[i_sensor], 

65 fit_settings_comment=fit_settings_comment, 

66 fit_settings_expected_value=fit_settings_expected_value, 

67 fit_settings_include=fit_settings_include, 

68 fit_settings_weight=fit_settings_weight, 

69 time=time, 

70 measured=measured, 

71 ) 

72 

73 return flux_loops