From 8e76d8dd0b3cee7a609f1c012a5cfede8cf6920d Mon Sep 17 00:00:00 2001
From: "FR@29iduser" <rodolakis@anl.gov>
Date: Mon, 19 Sep 2022 13:38:31 -0500
Subject: [PATCH] mono scanning works

---
 .gitignore                          |  4 +++
 iexcode/instruments/Octupole.py     |  4 +--
 iexcode/instruments/VLS_PGM.py      |  2 +-
 iexcode/instruments/diagnostics.py  | 51 +++++++++++++++++------------
 iexcode/macros/commissioning.py     | 38 +++++++++++----------
 iexcode/macros/start_of_the_week.py | 11 +++++--
 6 files changed, 65 insertions(+), 45 deletions(-)

diff --git a/.gitignore b/.gitignore
index 6deb3d0..dd2ffdc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -137,3 +137,7 @@ dmypy.json
 # Cython debug symbols
 cython_debug/
 .loglogin
+build/lib/iexcode/instruments/diagnostics.py
+build/lib/iexcode/instruments/VLS_PGM.py
+build/lib/iexcode/macros/commissioning.py
+build/lib/iexcode/macros/start_of_the_week.py
diff --git a/iexcode/instruments/Octupole.py b/iexcode/instruments/Octupole.py
index be09d33..6a9c2bd 100644
--- a/iexcode/instruments/Octupole.py
+++ b/iexcode/instruments/Octupole.py
@@ -154,7 +154,7 @@ def _Octupole_detector_dictionary(**kwargs):
         40:"29ide:scaler1.S7",
         41:"29ide:scaler1.S8",
     }
-    motors={
+    xmcd_raw={
         51:"29ide:m11.RBV",
         52:"29ide:m12.RBV",
         53:"29ide:m13.RBV",
@@ -164,7 +164,7 @@ def _Octupole_detector_dictionary(**kwargs):
     #hkl are listed just a place holders, they are filled in by thier scanning functions
     det_dict.update(sample_temp)
     det_dict.update(scalers)
-    det_dict.update(motors)
+    det_dict.update(xmcd_raw)
     if kwargs['add_vortex']:
         det_dict.update(vortex)
 
diff --git a/iexcode/instruments/VLS_PGM.py b/iexcode/instruments/VLS_PGM.py
index ff754e5..4e3cbd5 100644
--- a/iexcode/instruments/VLS_PGM.py
+++ b/iexcode/instruments/VLS_PGM.py
@@ -546,7 +546,7 @@ def mono_scan_table(hv_array,**kwargs):
     scan_fillin_table(val_pv,rbv_pv,hv_array,**kwargs)
 
     #mono needs to stay and have a longer settling time
-    iex.BL.mda.positioner_settling_time(kwargs['positioner_settling_time'])
+    iex.BL.mda.positioner_settling_time(**kwargs)
     iex.BL.mda.positioner_after_scan("STAY")
 
 def mono_scan_after(**kwargs):
diff --git a/iexcode/instruments/diagnostics.py b/iexcode/instruments/diagnostics.py
index 1c970b7..d021dc4 100644
--- a/iexcode/instruments/diagnostics.py
+++ b/iexcode/instruments/diagnostics.py
@@ -9,19 +9,19 @@ from iexcode.instruments.utilities import print_warning_message
 ################################            default positions             ##############################
 ##############################################################################################################
 #For new endstation modify here:
-def _diagnostics_presets():
+def diagnostics_presets():
     presets={
-        'H-wire':['OUT','Start'],
-        'V-wire':['OUT','Start'],
-        'H-Diagon':['OUT','Si-2800','ML-400','ML-460'],
-        'V-Diagon':['OUT','Si-2800','ML-400','ML-460'],
-        'D1A':['OUT','Mesh'],
-        'D2B':['OUT','Mesh','YAG'],
-        'D3B':['OUT','YAG'],
-        'D4C':['OUT','Mesh','YAG'],
-        'gas-cell':['OUT','Diode1','Diode2'],
-        'D4D':['OUT','Mesh','YAG'],
-        'D5D':['OUT','Mesh','YAG','Diode'],
+        'H-wire':(['OUT','Start'],'29id:m1'),
+        'V-wire':(['OUT','Start'],'29id:m2'),
+        'H-Diagon':(['OUT','Si-2800','ML-400','ML-460'],'29id:m3'),
+        'V-Diagon':(['OUT','Si-2800','ML-400','ML-460'],'29id:m4'),
+        'D1A':(['OUT','Mesh'],'29id:m5'),
+        'D2B':(['OUT','Mesh','YAG'],'29id:m6'),
+        'D3B':(['OUT','YAG'],'29id:m7'),
+        'D4C':(['OUT','Mesh','YAG'],'29id:m17'),
+        'gas-cell':(['OUT','Diode1','Diode2'],'29id:m20'),
+        'D4D':(['OUT','Mesh','YAG'],'29id:m25'),
+        'D5D':(['OUT','Mesh','YAG','Diode'],'29id:m6')
     }
     return presets
 
@@ -46,6 +46,7 @@ def _diagnostic_read_dict():
     read_dict={
         'diode_c':Keithley('b',15),
         'mesh_d':SRS("29idMZ0:scaler1.S14", '29idd:A4'),
+        'diode_d':Keithley('b',14),
     }
     return read_dict
 
@@ -57,19 +58,19 @@ def _diagnostics_preset_pv(diag_name,preset_name):
     dictionary of the motor pvs for the beamline diagnostics
     """
     ioc = '29idb:'
-    d = _diagnostics_presets()
+    d = diagnostics_presets()
     pv=None
     
     if diag_name.casefold() in (key.casefold() for key in list(d.keys())):
         idx = [item.lower() for item in list(d.keys())].index(diag_name.lower())
         diag_name=list(d.keys())[idx]
-        if preset_name.casefold() in (preset.casefold() for preset in d[diag_name]):
-            idx = [item.lower() for item in d[diag_name]].index(preset_name.lower())
-            preset_name = d[diag_name][idx]
+        if preset_name.casefold() in (preset.casefold() for preset in d[diag_name][0]):
+            idx = [item.lower() for item in d[diag_name][0]].index(preset_name.lower())
+            preset_name = d[diag_name][0][idx]
             pv = ioc+diag_name+'_'+preset_name
         else:
             message ='preset_name = '+preset_name+' not a valid name'
-            message +='\npreset_names:'+str(d[diag_name])
+            message +='\npreset_names:'+str(d[diag_name][0])
             print_warning_message(message)
     
     else:
@@ -111,8 +112,8 @@ def diagnostic(name,in_out):
     use 
     """
     '''
-    Inserts/retracts a diagnostic(motor number or name) either = "In" or "Out"
-    
+    Inserts/retracts a diagnostic either = "In" or "Out"
+        => diagnostic_name_list: to list diagnostics
     '''
     d_quick = _diagnostics_inout_dict()
     if name in d_quick.keys():
@@ -125,7 +126,7 @@ def diagnostic(name,in_out):
         print("\n"+name+": "+ in_out)
     else:
         message = 'name = '+name+' is not a valid diagnostic name'
-        message +='\n valid names are '+str(diagnostics_list_all(verbose=False))
+        message +='\n valid names are '+str(d_quick.keys())
         print_warning_message(message)
 
 def diagnostics_all_out(diode_stay_in=False,mesh_stay_in=False):
@@ -139,7 +140,7 @@ def diagnostics_all_out(diode_stay_in=False,mesh_stay_in=False):
     message = "All diagnostics out"
     d_names = _diagnostics_inout_dict()
 
-    diag_list = list(_diagnostics_presets().keys())
+    diag_list = list(diagnostics_presets().keys())
 
     #if staying remove diag_name from list
     if diode_stay_in:
@@ -266,6 +267,14 @@ def mesh_d_read(verbose=True):
     val = diagnostic_read('mesh_d',verbose)
     return val
 
+def diode_d_read(verbose=True):
+    """
+    reads the current amplifier and returns the value
+    quiet = False to pring
+    """ 
+    val = diagnostic_read('diode_d',verbose)
+    return val
+
 
 ###############################################################################################
 ####################################### FLUX CONVERSION #######################################
diff --git a/iexcode/macros/commissioning.py b/iexcode/macros/commissioning.py
index 0425d5a..ab8f95e 100644
--- a/iexcode/macros/commissioning.py
+++ b/iexcode/macros/commissioning.py
@@ -5,18 +5,14 @@ from epics import caget, caput
 
 import  iexcode.instruments.cfg as iex
 
-from iexcode.instruments.ARPES import ARPES_init
-from iexcode.instruments.kappa import kappa_init
 from iexcode.instruments.xrays import *
-from iexcode.instruments.xrays import _energy_range_min_max
-from iexcode.instruments.current_amplifiers import current2flux
-from iexcode.instruments.diagnostics import _diagnostics_dict, diagnostics_all_out, diagnostics_all_in,diode_c,diode_d
+from iexcode.instruments.current_amplifiers import *
+from iexcode.instruments.diagnostics import diagnostics_all_out, diagnostics_all_in,diode_c,diode_d
 from iexcode.instruments.m3r import *
 from iexcode.instruments.slits import *
 from iexcode.instruments.utilities import *
 from iexcode.instruments.electron_analyzer import scanEA
-from iexcode.instruments.Logfile import log_name_set,log_print
-from iexcode.instruments.cameras import *
+
 from instruments.slits import exit_slit_get, slit1A_get, slit1A_set, slits_set_size
 from instruments.xrays import apertures_set
 
@@ -26,14 +22,14 @@ from .quick_plot import fit_mda, mda_1D
 ################################            checking alignment             ##############################
 ##############################################################################################################
 
-def check_flux_scan(hv=500,stay=False,**kwargs)
+def check_flux_scan(hv=500,stay=False,**kwargs):
     """
     puts the diode in and scans the mono
     stay: 
         True => leaves the diode in
         False => takes the diode out
     """
-    kwargs.setdefault('sound',Ture)
+    kwargs.setdefault('sound',True)
     diagnostics_all_out(diode_stay_in=True)
     slit(50)
     energy(hv)
@@ -59,7 +55,7 @@ def check_flux_read(hv=500,ID_mode='RCP',stay=False):
     ring_current=round(caget("S:SRcurrentAI.VAL"),2)
 
     diagnostics_all_out(diode_stay_in=True)
-    exit_slit(50)
+    exit_slit_get(50)
     sleep(10)
 
     if m3r_branch() == "c":
@@ -75,7 +71,7 @@ def check_flux_read(hv=500,ID_mode='RCP',stay=False):
     
     if not stay:
         diagnostics_all_out()
-    exit_slit(current_slit)
+    exit_slit_set(current_slit)
 
 
 def scan_wire(direction,all_diag=True,**kwargs):
@@ -101,8 +97,8 @@ def scan_wire(direction,all_diag=True,**kwargs):
     elif direction == 'V':
         name = "V-wire"
 
-    diag = _diagnostics_dict()
-    pv = "29idb:m"+str(diag["motor"][name])
+    diagnostics_presets_go(name,'Start')
+    pv = diagnostics_presets()[name][1]
     print("\n================== "+name+" scan (29idb:ca2):")
     iex.BL.mda.fillin(pv+".VAL",pv+".RBV",1,-17,-30,-0.25,**kwargs)
 
@@ -200,7 +196,7 @@ def mono_MIR_GRT_find_offset(grt,slit_list,**kwargs):
     kwargs.setdefault("detNum",15)
 
     grating(grt)
-    exit_slit(50)
+    exit_slit_set(50)
     diode_c('In')
     slit2B_set(2,0.5,0,0)
     
@@ -210,7 +206,7 @@ def mono_MIR_GRT_find_offset(grt,slit_list,**kwargs):
         mono_zero_order(ang)
         print("Mirror pitch: "+str(ang)+"\r")
         for s in slit_list:
-            exit_slit(s)
+            exit_slit_set(s)
             print("\r")
 
             mono_motor_scan_fillin("GRT:P",ang-0.0005*s, ang+0.0005*s ,0.00002*s)
@@ -457,7 +453,13 @@ def M0M1_steering(H2,V2):
     new_roll=current_roll+m1_roll
     print('\nREMEMBER: 2V slit should always be centered at zero by steering M1 roll!')
     print("   - vertical: "+str(current_roll)+str(v_steering[1])+" => Move_M1('RZ',"+str(new_roll)+")")
-    print('   - horizontal: reset slit 2BH center to ',str(H2))     
+    print('   - horizontal: reset slit 2BH center to ',str(H2))  
+
+    print("\n\nM1 roll vs energy calibration/flux:")
+    print("   - 1.0 mm @ slit2V ~ 1.0 mrad on M1 roll")
+    print("   - 1.0 mm @ slit2V = 0.4 eV shift in energy")
+    print("   - 0.8 mm @ slit2V = 25% loss in intensity on Au 4f\n")
+
          
 def beamsteering_2B(v_position):
     vroll=round(v_position*0.05/0.04,3)
@@ -733,7 +735,7 @@ def CheckSlitCalibration(slit_list,BL=500,hvList=[485,510],scanIOC=None,**kwargs
     """
     if scanIOC is None:
         scanIOC=BL_ioc()
-    exit_slit(50)
+    exit_slit_set(50)
     apertures_set(BL)
     for hv in hvList:
         mono_energy_set(hv)
@@ -788,7 +790,7 @@ def FermiEdges(Energy,Slit):
 def QP_curves(ID_energy,ID_mode_list,hv_start,hv_stop,hv_step):
     """
     """
-    exit_slit(200)
+    exit_slit_set(200)
     #Switch_Grating("MEG")
     #print "\r"
     #print "****************  QP OFF  ****************"
diff --git a/iexcode/macros/start_of_the_week.py b/iexcode/macros/start_of_the_week.py
index 7182a72..355cdc7 100644
--- a/iexcode/macros/start_of_the_week.py
+++ b/iexcode/macros/start_of_the_week.py
@@ -45,7 +45,7 @@ def interactive_fct(mirror=False):
         \t (4) move M1 roll
         ''')
         options=options+',4'
-    flag=input('\nType ('+options+') >')
+    flag=input('\nType ('+options+'), or press enter to abort script >')
     if flag not in options.split(","):
         print('\ninvalid input; please try again...\n')
         flag=input('\nType ('+options+') >')
@@ -54,7 +54,7 @@ def interactive_fct(mirror=False):
         print('\n================================')
         log_print(cmt)
         print('================================\n')
-        flag=input('\nType ('+options+') >')
+        flag=input('\nType ('+options+'), or press enter to abort script >')
     if flag == '1': print('\nresuming script...\n'); flag=1
     if flag == '2': print('\nrepeating scan...\n'); flag=2
     if flag == '3': flag=3
@@ -207,10 +207,11 @@ def StartOfTheWeek(GRT,branch,wait=False,**kwargs):
                     if foo.lower() == 'y' or foo.lower() ==  'yes': 
                         flag=interactive_fct()
                     
-
                 except:
                     print('\nUnable to fit position; try to tweak xrange again\n') 
                     flag=interactive_fct()
+        if flag == '':
+            return
 
     ###### Wire scans: 
     if 'wire' in kwargs['scanType']:
@@ -326,6 +327,8 @@ def StartOfTheWeek(GRT,branch,wait=False,**kwargs):
 
                 #interactive
                 flag=interactive_fct()
+            if flag == '':
+                return
                     
     ###### Check flux: 
     if 'flux' in kwargs['scanType']:
@@ -342,6 +345,8 @@ def StartOfTheWeek(GRT,branch,wait=False,**kwargs):
                 print('Unable to plot. Try:') 
                 print("plot_mda("+str(m)+","+str(detDiode)+",flux=3);plt.show()")  
             flag=interactive_fct()
+        if flag == '':
+            return
 
     diagnostics_all_out()
 
-- 
GitLab