added loopscan_bool option, helper function for the scanning loop and implemented the possible loopscan logic conditional in the scanning loop

This commit is contained in:
Ryan Tan 2024-07-15 16:17:10 +02:00
parent 1ddd3e96a4
commit c859404a15

View File

@ -32,6 +32,7 @@ from System import String
import numpy as np
import matplotlib.pyplot as plt
import datetime
from typing import Union
#First choose your controller
@ -353,10 +354,11 @@ def write_no_echo(instr:pyvisa.resources.Resource, command:str, sleeptime=0.01)-
print(f"Error communicating with instrument: {e}")
# receive values in units of T, rescale in kg to talk with the power supplyy. 1T = 10kG
# NOTE: removed singlepowersupply_bool, reading serial-nr. of the device instead.
# NOTE: removed singlepowersupply_bool, reading serial-nr. of the device instead.
# TODO: add a param to allow the
def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
res:float, magnet_coil:str, Settings:str, base_file_name='', path_save="C:/Users/localadmin/Desktop/Users/Lukas/2024_02_08_Map_test",
reversescan_bool=False, zerowhenfin_bool=False)->None:
reversescan_bool=False, zerowhenfin_bool=False, loopscan_bool=False)->None:
# TODO: update docs in the end
""" this function performs a sweep of the B field of the chosen magnet coil. It creates a list o B values from the given min and max values,
with the given resolution. For each value, a measurement of the spectrum of the probe in the cryostat is made, using the LightField spectrometer.
@ -380,12 +382,29 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
ValueError: when Bx limit is exceeded.
ConnectionError: when no device is connected.
""" ''''''
def pyramid_list(lst) -> Union[list, np.ndarray]:
"""reverses the list and removes the first element of reversed list. Then, this is appended to
the end of the original list and returned as the 'pyramid' list.
Args:
lst (list or np.ndarray):
Raises:
TypeError: if the input object isn't a list or np.ndarray
Returns:
Union[list, np.ndarray]: the pyramid list
""" ''''''
if isinstance(lst, list):
return lst + lst[-2::-1]
elif isinstance(lst, np.ndarray):
return np.append(lst, lst[-2::-1])
else:
raise TypeError('Please input a list!')
if base_file_name =='':
base_file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H.%M')
start_time = time.time() # start of the scan function
# TODO: queries the serial number of the device, and from there, check the if the input limits exceed the recommended limits
instr_info = query_no_echo(instr, '*IDN?')
instr_bsettings = list(sep_num_from_units(el) for el in query_no_echo(instr, 'UNITS?;LLIM?;ULIM?').split(';')) # deliver a 3 element tuple of tuples containing the set unit, llim and ulim
@ -441,18 +460,18 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
init_lim, subsequent_lim = subsequent_lim, init_lim
init_sweep, subsequent_sweep = subsequent_sweep, init_sweep
if loopscan_bool:
bval_lst = pyramid_list(bval_lst)
total_points = len(bval_lst)
middle_index_bval_lst = total_points // 2
intensity_data = [] # To store data from each scan
cwd = os.getcwd() # save original directory
#scanning loop
for i, bval in enumerate(bval_lst):
# if init_bval == bval:
# # if initial bval is equal to the element of the given iteration from the bval_lst, then commence measuring the spectrum
# pass
# else:
if i == 0: # for first iteration, sweep to one of the limits
# NOTE: helper function for the scanning loop
def helper_scan_func(idx, bval, instr=instr, init_lim=init_lim, init_sweep=init_sweep,
subsequent_lim=subsequent_lim, subsequent_sweep=subsequent_sweep, sleep=5):
if idx == 0: # for first iteration, sweep to one of the limits
write_no_echo(instr, f'{init_lim} {bval*10}') # convert back to kG
write_no_echo(instr, f'SWEEP {init_sweep}')
else:
@ -467,6 +486,40 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
actual_bval = sep_num_from_units(query_no_echo(instr, 'IMAG?'))[0]*0.1
# update the actual bval
print(f'Actual magnet strength: {actual_bval} T,', f'Target magnet strength: {bval} T')
#scanning loop
for i, bval in enumerate(bval_lst):
# if init_bval == bval:
# # if initial bval is equal to the element of the given iteration from the bval_lst, then commence measuring the spectrum
# pass
# else:
# NOTE: original code without the loop scan
################################################
# if i == 0: # for first iteration, sweep to one of the limits
# write_no_echo(instr, f'{init_lim} {bval*10}') # convert back to kG
# write_no_echo(instr, f'SWEEP {init_sweep}')
# else:
# write_no_echo(instr, f'{subsequent_lim} {bval*10}') # convert back to kG
# write_no_echo(instr, f'SWEEP {subsequent_sweep}')
# actual_bval = sep_num_from_units(query_no_echo(instr, 'IMAG?'))[0]*0.1 # convert kG to T
# print(f'Actual magnet strength: {actual_bval} T,', f'Target magnet strength: {bval} T')
# while abs(actual_bval - bval) > 0.0001:
# time.sleep(5) # little break
# actual_bval = sep_num_from_units(query_no_echo(instr, 'IMAG?'))[0]*0.1
# # update the actual bval
# print(f'Actual magnet strength: {actual_bval} T,', f'Target magnet strength: {bval} T')
###############################################
if not loopscan_bool:
helper_scan_func(i, bval)
else:
if i <= middle_index_bval_lst:
helper_scan_func(i, bval)
else:
helper_scan_func(i, bval, instr=instr, init_lim=subsequent_lim, init_sweep=subsequent_sweep,
subsequent_lim=init_lim, subsequent_sweep=init_sweep, sleep=5)
time.sleep(5)
# we acquire with the LF