From fd11813da25cbb8967e2103687c1e8bd722bb6af Mon Sep 17 00:00:00 2001
From: jmcchesn <jmcchesn@aps.anl.gov>
Date: Sun, 29 May 2022 16:41:47 -0500
Subject: [PATCH] refactoring

---
 instruments/ARPES.py                          |   3 +-
 instruments/IEX_VPU.py                        |  12 +-
 instruments/Kappa.py                          |  12 +-
 instruments/cameras.py                        | 286 +++-------------
 instruments/logfile.py                        |   7 +-
 instruments/xrays.py                          |  36 +-
 .../ARPES_macros.py                           |   0
 {macros_and_scripts => macros}/BL_shutdown.py |   0
 .../Kappa_optimization.py                     |   0
 .../ScanFunctions_plot.py                     |   0
 .../commissioning.py                          | 308 +++++++-----------
 .../start_of_the_week.py                      |   0
 12 files changed, 218 insertions(+), 446 deletions(-)
 rename {macros_and_scripts => macros}/ARPES_macros.py (100%)
 rename {macros_and_scripts => macros}/BL_shutdown.py (100%)
 rename {macros_and_scripts => macros}/Kappa_optimization.py (100%)
 rename {macros_and_scripts => macros}/ScanFunctions_plot.py (100%)
 rename {macros_and_scripts => macros}/commissioning.py (83%)
 rename {macros_and_scripts => macros}/start_of_the_week.py (100%)

diff --git a/instruments/ARPES.py b/instruments/ARPES.py
index e753494..22e7253 100644
--- a/instruments/ARPES.py
+++ b/instruments/ARPES.py
@@ -763,4 +763,5 @@ def mvsample(position_list):
 
     Previously: sample
     """
-    ARPES_Motors.mvsample(position_list)
\ No newline at end of file
+    ARPES_Motors.mvsample(position_list)
+
diff --git a/instruments/IEX_VPU.py b/instruments/IEX_VPU.py
index 71de8b5..10274c0 100644
--- a/instruments/IEX_VPU.py
+++ b/instruments/IEX_VPU.py
@@ -134,7 +134,7 @@ def ID_mode_get(verbose=True):
         print('ID mode: '+ID_mode)
     return ID_mode
 
-def ID_mode_put(ID_mode):
+def ID_mode_set(ID_mode):
     """
     writes the desired mode to the correct ID pv
     """
@@ -195,7 +195,7 @@ def ID_start(ID_mode='RCP'):
     caput("ID29:EnergySet.VAL",3.8)
     caput("ID29:Main_on_off.VAL",0,wait=True,timeout=18000)
     ID_ready()
-    ID_mode_put(ID_mode)
+    ID_mode_set(ID_mode)
    
     ID_wait_for_permission()
     main_shutter_check_open()
@@ -204,7 +204,7 @@ def ID_start(ID_mode='RCP'):
 
 def ID_switch_mode(ID_mode):
     """
-    Change ID polarization; which = \"H\", \"V\", \"RCP\" or \"LCP\"
+    Change ID polarization; which = 'H', 'V', 'RCP' or 'LCP'
         if ID_mode = current mode then does nothing
 
     WARNING: Does not set the energy
@@ -222,7 +222,7 @@ def ID_switch_mode(ID_mode):
             sleep(10)
             
             print("Switching ID mode, please wait...")
-            ID_mode_put(ID_mode) 
+            ID_mode_set(ID_mode) 
             ID_ready()
         print("ID Mode:",ID_mode)
     except:
@@ -280,9 +280,9 @@ def ID_restart():
     ID_start(verbose=False)
     
     print("ID is back ON", dateandtime())
-    ID_SP_put(hv)
+    ID_SP_set(hv)
 
-def ID_SP_put(hv_eV,verbose=True):
+def ID_SP_set(hv_eV,verbose=True):
     """
     "Sets the ID set point to a specific value (hv(eV)) which will not likely be optimum"
 
diff --git a/instruments/Kappa.py b/instruments/Kappa.py
index d780949..6fcb40d 100644
--- a/instruments/Kappa.py
+++ b/instruments/Kappa.py
@@ -683,21 +683,23 @@ def Kappa_scan_th2th(tth_start,tth_stop,tth_step,th_offset,**kwargs):
     kwargs.setdefault('cts',0.1)
     kwargs.setdefault('execute',True)
     
-    th_start=(tth_start)/2+th_offset
-    th_stop=(tth_stop)/2+th_offset
+    th_start = (tth_start)/2+th_offset
+    th_stop = (tth_stop)/2+th_offset
+    th_step = int(tth_step/2)
     
     print('tth: '+str(tth_start)+"/"+str(tth_stop)+"/"+str(tth_step))
     print('th: '+str(th_start)+"/"+str(th_stop)+"/"+str(tth_step/2.0))
     
     #write to the scanRecord
-    kth_val,kth_rbv,kth_spmg,kth_pv = Kappa_motor_dictionary['kth']
+    th_val,th_rbv,th_spmg,th_pv = Kappa_motor_dictionary['th']
     tth_val,tth_rbv,tth_spmg,tth_pv = Kappa_motor_dictionary['tth']
 
-    kwargs.update("positioner_num",1)
-    BL.mda.fillin(tth_val,tth_rbv,tth_start,tth_stop,tth_step,**kwargs)
     kwargs.update("positioner_num",2)
     BL.mda.fillin(th_val,th_rbv,th_start,th_stop,th_step,**kwargs)
     
+    kwargs.update("positioner_num",1)
+    BL.mda.fillin(tth_val,tth_rbv,tth_start,tth_stop,tth_step,**kwargs)
+    
     scaler_cts(kwargs['cts'],verbose=False)
     if kwargs['execute']:
         BL.mda.go(**kwargs)
diff --git a/instruments/cameras.py b/instruments/cameras.py
index a020292..56ac0ce 100644
--- a/instruments/cameras.py
+++ b/instruments/cameras.py
@@ -1,242 +1,52 @@
 
 from epics import caget, caput
-from .userCalcs import userStringSeq_clear
-
-def Cam_SaveStrSeq(camNUM):
-    ioc="b"
-    pvCam="29id_ps"+str(camNUM)+":"
-    pvIOC="29id"+ioc+":"
-    userStringSeq_clear("29idb:",2)
-    caput(pvIOC+"userStringSeq2.DESC","Camera datamode")
-    caput(pvIOC+"userStringSeq2.LNK1",pvCam+"cam1:Acquire CA NMS")
-    caput(pvIOC+"userStringSeq2.STR1","Done")
-    caput(pvIOC+"userStringSeq2.LNK2",pvCam+"cam1:ImageMode PP NMS")
-    caput(pvIOC+"userStringSeq2.STR2","Single")
-    caput(pvIOC+"userStringSeq2.LNK3",pvCam+"TIFF1:EnableCallbacks PP NMS")
-    caput(pvIOC+"userStringSeq2.STR3","Enable")
-    caput(pvIOC+"userStringSeq2.LNK4",pvCam+"TIFF1:AutoSave PP NMS")
-    caput(pvIOC+"userStringSeq2.STR4","Yes")
-    caput(pvIOC+"userStringSeq2.LNK5",pvCam+"TIFF1:FileNumber PP NMS")
-    caput(pvIOC+"userStringSeq2.DO5",1)
-    caput(pvIOC+"userStringSeq2.WAIT1","Wait")
-    caput(pvIOC+"userStringSeqEnable.VAL",1)
-
-def Cam_FreeStrSeq(camNUM):
-    ioc="b"
-    pvCam="29id_ps"+str(camNUM)+":"
-    pvIOC="29id"+ioc+":"
-    userStringSeq_clear("29idb:",2)
-    caput(pvIOC+"userStringSeq1.DESC","Camera freerun")
-    caput(pvIOC+"userStringSeq1.LNK1",pvCam+"TIFF1:EnableCallbacks PP NMS")
-    caput(pvIOC+"userStringSeq1.STR1","Disable")
-    caput(pvIOC+"userStringSeq1.LNK2",pvCam+"cam1:ImageMode PP NMS")
-    caput(pvIOC+"userStringSeq1.STR2","Continuous")
-    caput(pvIOC+"userStringSeq1.LNK3",pvCam+"cam1:Acquire PP NMS")
-    caput(pvIOC+"userStringSeq1.STR3","Acquire")
-    caput(pvIOC+"userStringSeq1.LNK4",pvCam+"TIFF1:AutoSave PP NMS")
-    caput(pvIOC+"userStringSeq1.STR4","No")
-    caput(pvIOC+"userStringSeq1.LNK5",pvCam+"TIFF1:FileNumber PP NMS")
-    caput(pvIOC+"userStringSeq1.DO5",1)
-    caput(pvIOC+"userStringSeq1.LNK6",pvCam+"cam1:AcquireTime PP NMS")
-    caput(pvIOC+"userStringSeq1.DO6",0.015)
-    caput(pvIOC+"userStringSeq1.WAIT1","Wait")
-    caput(pvIOC+"userStringSeqEnable.VAL",1)
-
-
-def Cam_ScanSetup(mda,scanDIM,camNUM):        #individual files saving
-    pvCam="29id_ps"+str(camNUM)+":"
-    pvIOC=mda.ioc
-    #beforscan
-    Cam_SaveStrSeq(camNUM)
-    caput(pvIOC+"scan"+str(scanDIM)+".BSPV",pvIOC+"userStringSeq2.PROC")
-    #scan record (filename and trigger)
-    nextfile=str(caget(pvIOC+"saveData_baseName"))+str(caget(pvIOC+"saveData_scanNumber"))
-    filepath=caget(pvIOC+"saveData_fileSystem")
-    filepath="/net"+filepath[1:len(filepath)]+"/tif"
-    caput(pvCam+"TIFF1:FilePath",filepath)
-    caput(pvCam+"TIFF1:FileName",nextfile)
-    caput(pvCam+"TIFF1:FileWriteMode","Single")
-    caput(pvIOC+"scan1.T2PV",pvCam+"cam1:Acquire")
-    #afterscan
-    Cam_FreeStrSeq(camNUM)
-    caput(pvIOC+"scan"+str(scanDIM)+".ASPV",pvIOC+"userStringSeq1.PROC")
-    caput(pvIOC+"scan1.ASCD",1)
-    print("DON'T FORGET TO CLEAR SCAN RECORD AT THE END OF THE SCRIPT!!!!! ")
-    print("Use Script: Cam_ScanClear(scanIOC,scanDIM)")
-
-
-
-def Cam_FreeRun(camNUM):
-    camNUM=str(camNUM)
-    pv="29id_ps"+camNUM+":"
-    caput(pv+"TIFF1:AutoSave",0)
-    caput(pv+"TIFF1:EnableCallbacks",0)
-    sleep(0.5)
-    caput(pv+"cam1:ImageMode",2)
-    caput(pv+"cam1:Acquire",1)
-    caput(pv+"cam1:AcquireTime",0.015)
-    caput(pv+"cam1:AcquirePeriod",0.030)
-    print("Cam"+str(camNUM)+": Free run mode")
-
-
-
-
-
-def Cam_SaveMode(camNUM):
-    camNUM=str(camNUM)
-    pv="29id_ps"+camNUM+":"
-    caput(pv+"cam1:Acquire",0)
-    sleep(0.5)
-    caput(pv+"cam1:ImageMode",0)
-    caput(pv+"TIFF1:AutoSave",1)
-    caput(pv+"TIFF1:EnableCallbacks",1)
-    caput(pv+"TIFF1:FileNumber",1)
-    caput(pv+"cam1:AcquireTime",0.015)
-    caput(pv+"cam1:AcquirePeriod",0.030)
-    print("Cam"+str(camNUM)+": Saving mode")
-
-def Cam_Start(camNUM):
-    Cam_FreeRun(camNUM)
-
-def Cam_Stop(camNUM):
-    camNUM=str(camNUM)
-    pv="29id_ps"+camNUM+":"
-    caput(pv+"cam1:Acquire",0)
-
-
-
-def Cam_ROI_SetUp(xstart,ystart,xsize,ysize,camNUM,roiNUM=1,binX=1,binY=1):
-    pv="29id_ps"+str(camNUM)+":ROI"+str(roiNUM)+':'
-    caput(pv+'MinX',xstart)
-    caput(pv+'MinY',ystart)
-    caput(pv+'SizeX',xsize)
-    caput(pv+'SizeY',ysize)
-    caput(pv+'BinX',binX)
-    caput(pv+'BinY',binY)
-    caput(pv+'EnableCallbacks','Enable')
-    print(('ROI'+str(roiNUM)+' - '+caget(pv+'EnableCallbacks_RBV',as_string=True)))
-
-def Cam_ROI_Stats(xstart,ystart,xsize,ysize,camNUM,roiNUM=1,binX=1,binY=1):
-    pv="29id_ps"+str(camNUM)+":Stats1:"
-    Cam_ROI_SetUp(xstart,ystart,xsize,ysize,camNUM,roiNUM,binX,binY)
-    caput(pv+'EnableCallbacks','Enable')
-    roi=caget(pv+'NDArrayPort_RBV',as_string=True)
-    print((roi+' Stats => '+pv+'Total_RBV'))
-    print('To set-up as detector use:')
-    print('Cam_ROI_Det(detNUM,scanIOC,camNUM='+str(camNUM)+',roiNUM='+str(roiNUM)+')')
-    
-def Cam_ROI_Det(detNUM,scanIOC,camNUM,roiNUM,scanDIM=1):
-    pvdet='29id'+scanIOC+':scan'+str(scanDIM)+'.D'+str(detNUM)+'PV'
-    pvroi="29id_ps"+str(camNUM)+":Stats1:Total_RBV"
-    caput(pvdet,pvroi)
-    print('ROI stats set up as detector D'+str(detNUM)+' in '+scanIOC+' scan'+str(scanDIM))
 
-### Scan Record Set-Up:
-
-def Scan_Cam_Go(mda,VAL,RBV,scanDIM,start,stop,step,camNUM):
-    mda.fillin(scanDIM,VAL,RBV,start,stop,step)
-    Cam_ScanSetup(scanDIM,camNUM)
-
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".PASM","STAY")
-
-    # Scan_Go() without Before_After_Scan()
-    BL_mode=BL_Mode_Read()[0]
-    if BL_mode != 2:
-        Check_MainShutter()
-    FileName = caget("29id"+scanIOC+":saveData_baseName",as_string=True)
-    FileNum  = caget("29id"+scanIOC+":saveData_scanNumber")
-    print(FileName+": #"+str(FileNum)+" started at ", dateandtime())
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".EXSC",1,wait=True,timeout=900000)  #pushes scan button
-    print(FileName+": #"+str(FileNum)+" finished at ", dateandtime())
-
-    Cam_ScanClear(scanIOC,scanDIM)
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".PASM","PRIOR POS")
-
-def Scan_Cam_Pos2_Go(VAL1,RBV1,VAL2,RBV2,scanDIM,start1,stop1,step1,start2,stop2,camNUM):
-    scanIOC=BL_ioc()
-    pvCam="29id_ps"+str(camNUM)+":"
-    Cam_ScanSetup(scanDIM,camNUM)
-    Scan_FillIn(VAL1,RBV1,scanIOC,scanDIM,start1,stop1,step1)
-    Scan_FillIn_Pos2(VAL2,RBV2,scanIOC,scanDIM,start2,stop2)
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".PASM","STAY")
-    mda.goScan_Go(scanIOC,scanDIM=1 )
-    Cam_ScanClear(scanIOC,scanDIM)
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".PASM","PRIOR POS")
-
-def Cam_ScanClear(scanIOC,scanDIM):
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".BSPV","")
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".ASPV","")
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".T2PV","")
-    caput("29id"+scanIOC+":scan"+str(scanDIM)+".DDLY",0.5)
-    #print "Scan Record cleared from Camera Settings"
-
-### Image Acquisition:
-
-def TakeImageSetFolder(camNUM,NewFolder=""):
-    scanIOC=BL.ioc
-    pvCam="29id_ps"+str(camNUM)+":"
-    pvIOC="29id"+scanIOC+":"
-    #beforscan
-    Cam_SaveMode(camNUM)
-    #FilePath
-    filepath=caget(pvIOC+"saveData_fileSystem")+"/tif"
-    filepath="/net"+filepath[1:len(filepath)]+"/"+NewFolder
-    caput(pvCam+"TIFF1:FilePath",filepath)
-    print("WARNING: Make sure NewFolder exists !!!")
-
-def TakeImageSetFileNum(camNUM,n):
-    scanIOC=BL.mda.ioc
-    pvCam="29id_ps"+str(camNUM)+":"
-    pvIOC="29id"+scanIOC+":"
-    #beforscan
-#    Cam_SaveMode(camNUM)
-    #Filename
-    if n<10:
-        nextfile="29id"+scanIOC+"_000"+str(n)
-    elif n<100:
-        nextfile="29id"+scanIOC+"_00"+str(n)
-    elif n<1000:
-        nextfile="29id"+scanIOC+"_0"+str(n)
-    elif n<10000:
-        nextfile="29id"+scanIOC+"_"+str(n)
-    caput(pvCam+"TIFF1:FileName",nextfile)
-    caput(pvCam+"TIFF1:AutoIncrement",1)
-    caput(pvCam+"TIFF1:FileNumber",2)
-
-def TakeImageSetup(camNUM):
-    scanIOC=BL.mda.ioc
-    pvCam="29id_ps"+str(camNUM)+":"
-    pvIOC="29id"+scanIOC+":"
-    #beforscan
-    Cam_SaveMode(camNUM)
-    #Filename
-    nextfile=str(caget(pvIOC+"saveData_baseName"))+str(caget(pvIOC+"saveData_scanNumber")-1)
-    filepath=caget(pvIOC+"saveData_fileSystem")+"/tif"
-    if scanIOC == "Test":
-        filepath="/"+filepath[1:len(filepath)]        # for 29idTest
-    else:
-        filepath="/net"+filepath[1:len(filepath)]    # for 29idb/c/d
-
-    caput(pvCam+"TIFF1:FilePath",filepath)
-    caput(pvCam+"TIFF1:FileName",nextfile)
-    #afterscan
-
-def TakeImage(camNUM,AcqTime):
-    AcqPeriode=AcqTime+0.040
-    pvCam="29id_ps"+str(camNUM)+":"
-    caput(pvCam+"cam1:AcquireTime",AcqTime)
-    caput(pvCam+"cam1:AcquirePeriod",AcqPeriode)
-    sleep(1)
-    caput(pvCam+"cam1:Acquire",1,wait=True,timeout=500)
-    sleep(1)
-    TiffNum =caget(pvCam+"TIFF1:FileNumber_RBV")-1
-    FileName= caget(pvCam+"TIFF1:FileName_RBV",as_string=True)
-    print("\n"+FileName+": #"+str(TiffNum)+" started at ", dateandtime())
-    print("\n================================================\n")
-
-
-
-
-    
+from .userCalcs import userStringSeq_clear
+from .AD_utilites import *
+from .IEX_endstations import *
+
+
+def cam_pv_dictionary(cam_num):
+    """
+    dictionary of pv names for the beamline cameras
+    """
+    d={
+        1:"29id_ps1:",
+        2:"29id_ps2:",
+        3:"29id_ps3:",
+        4:"29id_ps4:",
+        5:"29idarv5:",
+        6:"29id_ps6:",
+    }
+    return d[cam_num]
+
+def cam_scan_setup(cam_num,ADtype='TIFF',**kwargs):
+    """
+    sets up the BL scanRecord to trigger the camera (trigger - 2)
+    """
+    ADplugin = cam_pv_dictionary(cam_num)+ADtype+":"
+    ADplugin_ScanSetup(ADplugin,BL.mda, **kwargs)  
+
+def cam_snap(cam_num,ADtype='TIFF',**kwargs):
+    """
+    takes a camera image and saves it in the user folder
     
-
+    **kwargs:
+    ExposureTime: changes both the exposure time and the acquire time for the snapshot
+                    resets after acquisition
+    FreeRun: True => disable setting and go back to continuous acquision 
+                False => leave saving enabled and camera in single acquision
+
+    """
+    ADplugin = cam_pv_dictionary(cam_num)+ADtype+":"
+    AD_SaveFileSetup(ADplugin,BL.mda,**kwargs)
+    AD_snap(ADplugin,**kwargs)
+
+def cam_live(cam_num,ADtype='TIFF',**kwargs):
+    """
+    puts camera in no save and continuous
+    a.k.a free run
+
+    """
+    ADplugin = cam_pv_dictionary(cam_num)+ADtype+":"
+    AD_FreeRun(ADplugin,**kwargs)
diff --git a/instruments/logfile.py b/instruments/logfile.py
index 91a2023..2f158f6 100644
--- a/instruments/logfile.py
+++ b/instruments/logfile.py
@@ -10,11 +10,11 @@ from .Kappa import Kappa_log_entries
 ##############################################################################################################
 ##############################             logging           ##############################
 ##############################################################################################################
-def log_print(comment=''):
+def log_print(**kwargs):
     """
     prints a comment to the logfile
     """
-    logfile_print(BL.endstation_name,BL.ioc,comment=comment)
+    logfile_print(BL.endstation_name,BL.ioc,kwargs['comment'])
 
 def log_update():
     """
@@ -61,7 +61,8 @@ def logfile_name_set(endstation_name,**kwargs):
 
     Previously: logname_set
     """
-    kwargs.setdefault('filename',today()+'_log.txt')
+    kwargs.setdefault('filename_suffix','_log')
+    kwargs.setdefault('filename',today()+kwargs['filename_suffix']+'.txt')
 
     filename = kwargs['filename']
 
diff --git a/instruments/xrays.py b/instruments/xrays.py
index 105500f..eca3055 100644
--- a/instruments/xrays.py
+++ b/instruments/xrays.py
@@ -177,7 +177,6 @@ def xrays_log_entries(**kwargs):
     format_list.append("s","s")
 
     try:
-        FileNameWithSubFolder=logfile_name_get(BL.endstation)
         logfile_update(BL.endstation,BL.ioc,entry_list,pv_list,format_list)
     except:
         print("scanlog did not write to file, check for errors.")
@@ -211,6 +210,7 @@ def energy_get_all(verbose=True):
             print(key,d[key])
 
     return tuple(values)
+    
 def energy_get():
     hv = getE()
     return hv
@@ -227,9 +227,9 @@ def energy(hv_eV,slit_coeff=1,m3r=True,verbose=True):
     Previously: Set_BL, energy
     """
     if BL.xrays:
-        if hv_eV != check_energy_allowed(hv_eV):
+        if hv_eV != energy_range_check(hv_eV):
             message_string = 'request photon energy '+str(hv_eV)+' not with the allowed range'
-            message_string = '\n closest allowed energy is '+str(check_energy_allowed(hv_eV))
+            message_string = '\n closest allowed energy is '+str(energy_range_check(hv_eV))
             print_warning_message(message_string)
 
 
@@ -251,18 +251,26 @@ def energy(hv_eV,slit_coeff=1,m3r=True,verbose=True):
         message_string = 'BL.xrays = False, energy is not set'
         print_warning_message(message_string)
 
+def energy_range_min_max():
+    """
+    returns the min,max energies for the current grating/ID setting
+    """
+    ID_min, ID_max = ID_energy_range()
+    mono_min, mono_max = mono_energy_range()
+    hv_max = np.min(ID_max, mono_max)
+    hv_min = np.max(ID_min, mono_min)
+    return hv_min,hv_max
 
-def check_energy_allowed(hv):
+def energy_range_check(hv):
     """
     checks if the energy is within the allowed range for both the mono
     and the ID. If outside the ranges set it to the closest allowed value
 
     Previously: SetRange()
     """
-    ID_min, ID_max = ID_energy_range()
-    mono_min, mono_max = mono_energy_range()
-    hv = np.min(ID_max, mono_max, hv)
-    hv = np.max(ID_min, ID_max, hv)
+    hv_min,hv_max = energy_range_min_max()
+    hv = np.min(hv_max,hv)
+    hv = np.max(hv_min,hv)
     return hv
 
 def mvid(val):
@@ -271,7 +279,7 @@ def mvid(val):
     
     to go to most flux use energy
     """
-    ID_SP_put()
+    ID_SP_set()
 
 def scan_ID(ID_sp_start,ID_sp_stop,ID_sp_step,**kwargs):
     """
@@ -329,10 +337,16 @@ def mvgrating(grating):
 
 def polarization(ID_mode):
     """
-    Change beam polarization: ID_mode = \"H\", \"V\", \"RCP\" or \"LCP\"
+    Change beam polarization: ID_mode = 'H', 'V', 'RCP' or 'LCP'
     """
     ID_switch_mode(ID_mode)
 
+def polarization_get():
+    """
+    gets the current polarization
+    """
+    ID_mode_get()
+
 def scanhv(start,stop,step,average_pnts=1,**kwargs):
     """
     scans the mono at the current ID value
@@ -490,7 +504,7 @@ def scanXAS_BL(start_stop_step_lists,**kwargs):
     #Scanning
     if kwargs['execute']:
         mono_energy_set(mono_array[0])
-        ID_SP_put(ID_array[0])
+        ID_SP_set(ID_array[0])
         BL.mda.go(scan_dim)
 
         #Setting everything back
diff --git a/macros_and_scripts/ARPES_macros.py b/macros/ARPES_macros.py
similarity index 100%
rename from macros_and_scripts/ARPES_macros.py
rename to macros/ARPES_macros.py
diff --git a/macros_and_scripts/BL_shutdown.py b/macros/BL_shutdown.py
similarity index 100%
rename from macros_and_scripts/BL_shutdown.py
rename to macros/BL_shutdown.py
diff --git a/macros_and_scripts/Kappa_optimization.py b/macros/Kappa_optimization.py
similarity index 100%
rename from macros_and_scripts/Kappa_optimization.py
rename to macros/Kappa_optimization.py
diff --git a/macros_and_scripts/ScanFunctions_plot.py b/macros/ScanFunctions_plot.py
similarity index 100%
rename from macros_and_scripts/ScanFunctions_plot.py
rename to macros/ScanFunctions_plot.py
diff --git a/macros_and_scripts/commissioning.py b/macros/commissioning.py
similarity index 83%
rename from macros_and_scripts/commissioning.py
rename to macros/commissioning.py
index 6bf488c..995fa4d 100644
--- a/macros_and_scripts/commissioning.py
+++ b/macros/commissioning.py
@@ -18,6 +18,7 @@ from ..instruments.diagnostics import diagnostics_all_out, diagnostics_all_in,di
 from ..instruments.current_amplifiers import current2flux
 from ..instruments.slits import set_exit_slit
 from ..instruments.logfile import *
+from ..instruments.cameras import *
 
 from ..instruments.ARPES import *
 from ..instruments.electron_analyzer import *
@@ -573,7 +574,7 @@ def check_ID_steering(hv=2000):
         - beam center vs fixed aperture: given by the position of the bump in the middle
     """
     slit1A_set(0.25,0.25,0,0)
-    ID_SP_put(hv)
+    ID_SP_set(hv)
     scan_slit_center('1H',-3,3,0.1)
     scan_slit_center('1V',-3,3,0.1)
         
@@ -748,7 +749,7 @@ def QP_curves(ID_energy,ID_mode_list,hv_start,hv_stop,hv_step):
     #Switch_IDQP("on")
     for ID_mode in ID_mode_list:
         polarization(ID_mode)
-        ID_SP_put(ID_energy)
+        ID_SP_set(ID_energy)
         slits_set_BL()()
         mono_energy_set(250)
         mono_scan_fillin(hv_start,hv_stop,hv_step)
@@ -764,7 +765,7 @@ def QP_curves(ID_energy,ID_mode_list,hv_start,hv_stop,hv_step):
 
 ######  Energy range follows ID ########
 
-def ID_calibration_scan(start,stop,step,bandwidth=10,QP=None,Harm=1,scanIOC=None):
+def ID_calibration_scan(ID_start,ID_stop,ID_step,bandwidth=10,QP=None,Harm=1,scanIOC=None):
     """
     Sets the ID_SP and scans the mono:
         start,stop,step are for ID
@@ -777,222 +778,165 @@ def ID_calibration_scan(start,stop,step,bandwidth=10,QP=None,Harm=1,scanIOC=None
              !!! DOES NOT CHANGE QP VALUE !!!!
              !!! Doesn't put diodes in !!!""")
     
-    if scanIOC is None:
-        scanIOC=BL_ioc()
-    scanDIM=1
-    #Setting up scan record to go to peak position at the end of the scan
-    PV='29id'+scanIOC+':scan'+str(scanDIM)
-    VAL="29idmono:ENERGY_SP"
-    RBV="29idmono:ENERGY_MON"
-    caput(PV+'.PDLY',0.2)        #Detector settling time
-    caput(PV+'.PASM','STAY')
+    logfile_name_set(BL.endstation,filename_suffix='IDCalibration')
 
     #Getting mono max range based on grating
-    GRT=caget("29idmono:GRT_DENSITY")
-    c=GRT/1200            # c = 1 for MEG, 2 for HEG
-    if c == 1: maxhv = 3000
-    elif c == 2: maxhv = 2000
-    kwargs={'comment': "====== Starting Mono Scan vs ID"}
-    kwargs.update({'FileSuffix': "IDCalibration"})    
-    log_print(**kwargs)
-    for ID in RangeUp(start,stop,step):
-        print("\n------------------  ID-SP @ "+str(ID)+"   ------------------")
-        SetMono(ID)
-        SetID_Raw(ID)
-        SetSlit_BL()
-        branch=CheckBranch()
+    grt_density = mono_grating_density_get()
+    c=grt_density/1200            # c = 1 for MEG, 2 for HEG
+    if c == 1: 
+        maxhv = 3000
+    elif c == 2: 
+        maxhv = 2000
+   
+    log_print(comment="====== Starting Mono Scan vs ID ======")
+
+    for ID_energy in RangeUp(ID_start,ID_stop,ID_step):
+        print("\n------------------  ID-SP @ "+str(ID_energy)+"   ------------------")
+        mono_energy_set(ID_energy)
+        ID_SP_set(ID_energy)
+        slits_set_BL()
+        branch = m3r_branch()
         if branch == 'd':
-            align_m3r_epics()
+            m3r_align()
         if QP is None:
-            start_hv=min(ID-ID/(bandwidth*1.0)*Harm,maxhv)  # FR update range 11/20/2019
-            stop_hv=min(ID+ID/(bandwidth*2.0)*Harm,maxhv)
-            step_hv=round(ID/300.0,1)
-#            start_hv=min(ID-ID/(bandwidth*2.0)*Harm,maxhv)  # shift the window by doing -BW*2/+BW*1 instead of -BW*1'/'+BW*2
-#            stop_hv=min(ID+ID/(bandwidth*1.0)*Harm,maxhv)
-#            step_hv=round(ID/500.0,1)
+            start_hv=min(ID_energy-ID_energy/(bandwidth*1.0)*Harm,maxhv)  # FR update range 11/20/2019
+            stop_hv=min(ID_energy+ID_energy/(bandwidth*2.0)*Harm,maxhv)
+            step_hv=round(ID_energy/300.0,1)
+#            start_hv=min(ID_energy-ID_energy/(bandwidth*2.0)*Harm,maxhv)  # shift the window by doing -BW*2/+BW*1 instead of -BW*1'/'+BW*2
+#            stop_hv=min(ID_energy+ID_energy/(bandwidth*1.0)*Harm,maxhv)
+#            step_hv=round(ID_energy/500.0,1)
         else:
-            start_hv=min(ID-ID/(bandwidth*2.5)*Harm,maxhv)
-            stop_hv=min(ID+ID/(bandwidth*0.7)*Harm,maxhv)
-            step_hv=round(ID/300.0,1)
-        Scan_FillIn(VAL,RBV,scanIOC,1,start_hv,stop_hv,step_hv)
-        Scan_Go(scanIOC,scanDIM=scanDIM,FileSuffix='IDCalibration',comment="ID-SP @ "+str(ID))
+            start_hv=min(ID_energy-ID_energy/(bandwidth*2.5)*Harm,maxhv)
+            stop_hv=min(ID_energy+ID_energy/(bandwidth*0.7)*Harm,maxhv)
+            step_hv=round(ID_energy/300.0,1)
+
+        mono_scan_fillin(start_hv,stop_hv,step_hv)
+        BL.mda.go()
         sleep(1)
-        FileNum  = caget("29id"+scanIOC+":saveData_scanNumber")-1
-        if ID == start:
-            FirstFileNum=FileNum     # Record First Scan Number
-    LastFileNum=FileNum              # Record Last Scan Number
+
+        FileNum  = BL.mda.lastFileNum
+        if ID_energy == ID_start:
+            FirstFileNum = FileNum     # Record First Scan Number
+    
+    LastFileNum = FileNum              # Record Last Scan Number
 
     print("Use:  update_id_dict("+str(FirstFileNum)+","+str(LastFileNum)+"ignore=[] ,update_file,path=None,prefix='ARPES',q=True)")
 
 
 
 
-def IDCalibration_Mode(which,start=None,stop=None,step=25,Harm=1):
+def ID_calibration_mode(ID_mode,start=None,stop=None,step=25,Harm=1):
     '''
     Runs full calibration with a 25 eV step (by default) for a given mode:
             which = 'RCP','LCP', 'V','H'
     Better to run each mode in a separated cell (print statement at the end helps with calibration) 
     '''
-    branch=CheckBranch()
-    #AllDiagOut(DiodeStayIn=branch)
-    #DiodeC('In')
-    print('WARNING: commented out AllFDiagOut and DiodeC("In")')
-    polarization(which)
-    GRT=caget("29idmono:GRT_DENSITY")
-    if GRT==2400: GRT='HEG'
-    if GRT==1200: GRT='MEG'
-    QP_ratio=caget("ID29:QuasiRatioIn.C")
+    branch = m3r_branch()
+    diagnostics_all_out(diode_stay_in=True)
+
+    polarization(ID_mode)
+
+    grt = mono_grating_get() 
+
+    QP_ratio = ID_QP_ratio_get()
+    
+    hv_min,hv_max = energy_range_min_max()
     if start == None:
-        start=Energy_Range(GRT,which)[0]
+        start = hv_min
     if stop == None:
-        stop=Energy_Range(GRT,which)[1]-50
+        stop=hv_max-50
+
     if QP_ratio <100:
         stop=min(stop,1500)
         QP_ratio=1
-    else: QP_ratio=None
-    IDCalibration_Scan(start,stop,step,bandwidth=10,QP=QP_ratio,Harm=Harm,scanIOC=None)
-
-
-
+    else: 
+        QP_ratio=None
 
+    ID_calibration_scan(start,stop,step,QP=QP_ratio,Harm=Harm)
 
 
+##############################################################################################################
+##############################             Beam Motion            ##############################
+##############################################################################################################
+def scan_cam_motor(motor_name,start,stop,step,cam_num,ExposureTime):
+    """   
+    motor_name = 'x','y','z'...
+    cam_num = 1,2,3... uses cam_pv to get the pv
 
+    Previously: Pixel_Calibration
+    """    
+    cam_scan_setup(cam_num,ADtype='TIFF')
 
+    if BL.branch == 'ARPES':
+        ARPES_Motors.scan(motor_name,start,stop,step)
+    if BL.branch == 'Kappa':
+        Kappa_Motors.scan(motor_name,start,stop,step)
+    
+    cam_live()
 
 
 
+def scan_cam_hv(start,stop,step,cam_num,**kwargs):
+    """   
+    takes and image as a function of photon energy
+    **kwargs:
+        ExposureTime
+    Previously: BeamMotion_Vs_BL_Go
+    """
+    cam_scan_setup(cam_num,ADtype='TIFF')
+    scanhv(start,stop,step,**kwargs)
 
+    cam_live()
 
-##############################################################################################################
-##############################             Beam Motion            ##############################
-##############################################################################################################
 
 
-def Pixel_Calibration(motorName,start,stop,step,camNUM,AcqTime):
-    """   
-    """    
-    scanIOC=BL_ioc()
-    scanDIM=1
-
-    branch=CheckBranch()
-    if branch == "c":
-        m_RBV,m_VAL=ARPES_PVmotor(motorName)
-    elif branch == "d":
-        m_RBV,m_VAL=Kappa_PVmotor(motorName)
-    pvCam="29id_ps"+str(camNUM)+":"
-    Scan_FillIn("","",scanIOC,scanDIM,1,1,1)
-    Scan_Go(scanIOC,scanDIM=scanDIM)
-    TakeImageSetup(camNUM)
-    caput(pvCam+"TIFF1:FileNumber",1)
-    for value in RangeUp(start,stop,step):
-        caput(m_VAL,value)
-        sleep(2)
-        print(motorName+" = "+str(value))
-        TakeImage(camNUM,AcqTime)
-    Cam_FreeRun(camNUM)
-
-
-
-def BeamMotion_Vs_BL_Go(start,stop,step,camNUM,AcqTime,scanDIM=1):
-    """
-    default: scanDIM=1  
-    """
-    scanIOC=BL_ioc()
-    pvCam="29id_ps"+str(camNUM)+":"
-    Scan_FillIn("","",scanIOC,scanDIM,1,1,1)
-    Scan_Go(scanIOC,scanDIM=scanDIM)
-    TakeImageSetup(camNUM)
-    caput(pvCam+"TIFF1:FileNumber",1)
-    for eV in RangeUp(start,stop,step):
-        SetBL(eV)
-    #    if mode == 0 or mode == 1:
-    #        if eV>=600 or eV<=1600:
-    #            AcqTime=AcqTime/2.0         #JM commented out
-        if eV > 2200:
-            AcqTime=AcqTime*10
-        TakeImage(camNUM,AcqTime)
-    Cam_FreeRun(camNUM)
-
-def BeamMotion_Vs_hv_Go(ioc,scannum,ID,hv,AcqTime):
-    ioc="b"
-    scannum=1
-    camNUM=4
-    pvCam="29id_ps"+str(camNUM)+":"
-    SetID(ID)
-    SetMono(hv)
-    Scan_FillIn("","",ioc,scannum,1,1,1)
-    Scan_Go(ioc,scanDIM=1)
-    TakeImageSetup(camNUM)
-    caput(pvCam+"TIFF1:FileNumber",1)
-    sleep(3)
-    TakeImage(camNUM,AcqTime)
-    Cam_FreeRun(camNUM)
-
-def BeamMotion_Vs_MonoZero_Go(ioc,scannum,start,stop,step,camNUM,iteration,AcqTime):
-    pvCam="29id_ps"+str(camNUM)+":"
-    Scan_FillIn("","",ioc,scannum,1,1,1)
-    Scan_Go(ioc,scanDIM=1)
-    TakeImageSetup(camNUM)
-    caput(pvCam+"TIFF1:FileNumber",1)
-    i=1
-    while i<=iteration:
-        n=start*1.0
-        while n<=stop:
-            Mono_zero(n)
-            TakeImage(camNUM,AcqTime)
-            n+=step
+def scan_cam_mono_zero_order(start,stop,step,cam_num,iterations=1,**kwargs):
+    """
+    Previously: BeamMotion_Vs_MonoZero_Go
+    """
+    i = 0
+    while i < iterations:
+        for angle in range(start,stop+step,step):
+            mono_zero_order(angle)
+            sleep(5)
+            cam_snap(cam_num,ADtype='TIFF',**kwargs)
         i+=1
-    Cam_FreeRun(camNUM)
-
-def BeamMotion_Vs_MirPitch_Go(ioc,scannum,angle1,angle2,camNUM,iteration,AcqTime):
-    pvCam="29id_ps"+str(camNUM)+":"
-    Scan_FillIn("","",ioc,scannum,1,1,1)
-    Scan_Go(ioc,scanDIM=1)
-    TakeImageSetup(camNUM)
-    caput(pvCam+"TIFF1:FileNumber",1)
-    i=1
-    while i<=iteration:
-        Mono_angle(angle1,angle1)
+    
+    cam_live()
+
+def scan_cam_mono_pitch(motor,angle,delta,cam_num,iterations=1,**kwargs):
+    """
+    takes image at zero order (alpha = beta = angle)
+    takes image at zeror order with delta 
+
+    motor = 'grating' or 'mirror
+        if 'grating': 
+            alpha = angle
+            beta = angle + delta 
+        if 'mirror': 
+            alpha = angle + delta 
+            beta = angle
+
+    Previously:BeamMotion_Vs_MirPitch_Go
+    """
+    if motor == 'grating': 
+        alpha = angle
+        beta = angle + delta 
+    if motor == 'mirror': 
+        alpha = angle + delta 
+        beta = angle
+    i=0
+    while i<iterations:
+        mono_angles_set(angle,angle)
         sleep(5)
-        TakeImage(camNUM,AcqTime)
-        Mono_angle(angle2,angle1)
-        i+=1
-#    Cam_FreeRun(camNUM)
-
-def BeamMotion_Vs_GrtPitch_Go(ioc,scannum,angle1,angle2,camNUM,iteration,AcqTime):
-    pvCam="29id_ps"+str(camNUM)+":"
-    Scan_FillIn("","",ioc,scannum,1,1,1)
-    Scan_Go(ioc,scanDIM=1)
-    TakeImageSetup(camNUM)
-    caput(pvCam+"TIFF1:FileNumber",1)
-    i=1
-    while i<=iteration:
-        Mono_angle(angle1,angle1)
+        cam_snap(cam_num,ADtype='TIFF',**kwargs)
+        
+        mono_angles_set(alpha,beta)
         sleep(5)
-        TakeImage(camNUM,AcqTime)
-        Mono_angle(angle1,angle2)
+        cam_snap(cam_num,ADtype='TIFF',**kwargs)
         i+=1
-#    Cam_FreeRun(camNUM)
-
-def BeamMotion_Vs_GrtTx_Go(ioc,scannum,camNUM,iteration,AcqTime):
-    pvCam="29id_ps"+str(camNUM)+":"
-    Scan_FillIn("","",ioc,scannum,1,1,1)
-    Scan_Go(ioc,scanDIM=1)
-    TakeImageSetup(camNUM)
-    caput(pvCam+"TIFF1:FileNumber",1)
-
-    i=1
-    while i<=iteration:
-        grating("HEG")
-        Mono_zero(2)
-        sleep(10)
-        TakeImage(camNUM,AcqTime)
-        grating("MEG")
-        Mono_zero(2)
-        sleep(10)
-        TakeImage(camNUM,AcqTime)
-        i+=1
-#    Cam_FreeRun(camNUM)
+
+    cam_live()
+
 
 
diff --git a/macros_and_scripts/start_of_the_week.py b/macros/start_of_the_week.py
similarity index 100%
rename from macros_and_scripts/start_of_the_week.py
rename to macros/start_of_the_week.py
-- 
GitLab