From f89e543beb06f389a1970e0a894bc12f2f3b10dc Mon Sep 17 00:00:00 2001 From: ryantan Date: Wed, 23 Apr 2025 14:05:04 +0200 Subject: [PATCH] implemented basic logging helper functions for the rotation and angle sweep functions --- Mag_Field_Sweep_2025_04_15.py | 18 ++++----- Test2.py | 39 +++++++++++++++++++ .../2025-04-23_14-03.txt | 6 +++ 3 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 Test2.py create mode 100644 Test_Map_2025_04_23_14.03/2025-04-23_14-03.txt diff --git a/Mag_Field_Sweep_2025_04_15.py b/Mag_Field_Sweep_2025_04_15.py index fdbcc63..2ce020c 100644 --- a/Mag_Field_Sweep_2025_04_15.py +++ b/Mag_Field_Sweep_2025_04_15.py @@ -1012,14 +1012,12 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R # call the helper function to carry out the rotation/measurement of spectrum monitor_devices(device_target_values, angles, intensity_data) -# TODO: implement b_sweep at an arbitrary angle in the Bx-By plane -# copy the functionality of loading the powerboxsupplies from b_rotation and the other sweep functionalities of -# b_sweep_val and implement it in this function -# CHECK: B_abs <= BX_MAX or BY_MAX + def sweep_b_angle(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Resource, min_bval:float, max_bval:float, res:float, angle:float, Settings:str, clockwise=True, base_file_name='', - reversescan_bool=False, zerowhenfin_bool=False, loopscan_bool=False): + reversescan_bool=False, zerowhenfin_bool=False, loopscan_bool=False, + log_folder_path:str=""): # check if the given limits exceed the max limits of the device if (abs(min_bval) > min(BX_MAX, BY_MAX)) or (abs(max_bval) > min(BX_MAX, BY_MAX)): @@ -1060,9 +1058,10 @@ def sweep_b_angle(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Reso instr2_bsettings[1][0] = instr2_bsettings[1][0]*0.1 # rescale kG to T, device accepts values only in kG or A, eventho we set it to T instr2_bsettings[2][0] = instr2_bsettings[2][0]*0.1 - # initialise the sweep angle list as well as the sweep limits and directions for each instrument - instr1_lim, instr2_lim = 'LLIM', 'ULIM' - instr1_sweep, instr2_sweep = 'DOWN', 'UP' + # NOTE: this code block unused, remove later + # # initialise the sweep angle list as well as the sweep limits and directions for each instrument + # instr1_lim, instr2_lim = 'LLIM', 'ULIM' + # instr1_sweep, instr2_sweep = 'DOWN', 'UP' # TODO: see later parts of b_field_rotation from line 820 onwards, and see if same logic can be applied here # acquire coordinates along the fixed axis, threading, sweep both supplies till desired value (with lock) @@ -1084,9 +1083,6 @@ def sweep_b_angle(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Reso write_no_echo(instr2, f'RATE 0 {min_range_lst[0]};RATE 1 {min_range_lst[1]}') ''' - # TODO: mod copied code (from b_field_rotation) forsweep_b_angle - # NEED TO MOD THE LOGIC OF listen_to_device FUNCTION LOGIC - # TO SWEEP FROM LOWER TO HIGHER ANGLES, SEE SWEEP_B_VAL FUNCTION # NOTE: implement PID control, possibly best option to manage the b field DO THIS LATER ON, WE DO DISCRETE B VALUES RN # Helper function that listens to a device diff --git a/Test2.py b/Test2.py new file mode 100644 index 0000000..a761b1e --- /dev/null +++ b/Test2.py @@ -0,0 +1,39 @@ +import os +import datetime + +# List to accumulate measurement data +measurement_data = [] + +def append_measurement(target_b_abs, b_x, b_y, measurement_data=measurement_data): + """Append a single measurement to the global list.""" + measurement = { + "Target B_abs": target_b_abs, + "Datetime": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), + "B_x": b_x, + "B_y": b_y + } + measurement_data.append(measurement) + +def save_measurements_to_file(relative_directory, measurement_data=measurement_data): + """Save accumulated measurements to a file in the specified directory.""" + script_dir = os.path.dirname(os.path.abspath(__file__)) + directory = os.path.join(script_dir, relative_directory) + os.makedirs(directory, exist_ok=True) + + filename = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M") + ".txt" + file_path = os.path.join(directory, filename) + + # Write header and data + with open(file_path, 'w') as f: + f.write("Target B_abs, Datetime, B_x, B_y\n") + for entry in measurement_data: + line = f"{entry['Target B_abs']}, {entry['Datetime']}, {entry['B_x']}, {entry['B_y']}\n" + f.write(line) + +# Example usage +for i in range(5): + append_measurement(target_b_abs=0.5 + i, b_x=1.0 * i, b_y=2.0 * i) + +save_measurements_to_file("Test_Map_" + f"{datetime.datetime.now().strftime('%Y_%m_%d_%H.%M')}") + +# print(datetime.datetime.now()) diff --git a/Test_Map_2025_04_23_14.03/2025-04-23_14-03.txt b/Test_Map_2025_04_23_14.03/2025-04-23_14-03.txt new file mode 100644 index 0000000..24f3318 --- /dev/null +++ b/Test_Map_2025_04_23_14.03/2025-04-23_14-03.txt @@ -0,0 +1,6 @@ +Target B_abs, Datetime, B_x, B_y +0.5, 2025-04-23 14:03:43, 0.0, 0.0 +1.5, 2025-04-23 14:03:43, 1.0, 2.0 +2.5, 2025-04-23 14:03:43, 2.0, 4.0 +3.5, 2025-04-23 14:03:43, 3.0, 6.0 +4.5, 2025-04-23 14:03:43, 4.0, 8.0