From 880368870c7b556fa2814434dbcca8163e256a75 Mon Sep 17 00:00:00 2001
From: jmcchesn <jmcchesn@aps.anl.gov>
Date: Thu, 8 Sep 2022 11:09:12 -0500
Subject: [PATCH] debugging

---
 iexcode/cheatsheet.txt               |  2 +
 iexcode/instruments/Kappa.py         | 10 +++--
 iexcode/instruments/Motors.py        |  1 -
 iexcode/instruments/cfg.py           |  2 +-
 iexcode/instruments/staff.py         |  1 +
 iexcode/instruments/vortex.py        |  8 ++--
 iexcode/macros/Kappa_optimization.py | 63 +++++++++++++++-------------
 iexcode/macros/quick_plot.py         | 46 +++++++-------------
 8 files changed, 63 insertions(+), 70 deletions(-)

diff --git a/iexcode/cheatsheet.txt b/iexcode/cheatsheet.txt
index 1a5bfd1..2cc6961 100644
--- a/iexcode/cheatsheet.txt
+++ b/iexcode/cheatsheet.txt
@@ -29,6 +29,8 @@ ca_detector_list => list of detectors which are used in a given branch ('b for b
 ca_average => turns on averaging for all Keithleys in ca_list
 ca_reset_all => resets all Keithleys in all branches
 SRS => class for SRS current amplifiers
+.get
+.setgain
 current2flux => calculates the flux based on the current reading in an AUX100 photodiode
 flux2current => calculates the current reading for a given flux for and AUX100 photodiode
 
diff --git a/iexcode/instruments/Kappa.py b/iexcode/instruments/Kappa.py
index d5b1668..86a4a84 100644
--- a/iexcode/instruments/Kappa.py
+++ b/iexcode/instruments/Kappa.py
@@ -1,3 +1,4 @@
+from json import detect_encoding
 import numpy as np
 from time import sleep
 from math import floor
@@ -25,12 +26,15 @@ from iexcode.instruments.shutters import branch_shutter_close
 #branch specific
 from iexcode.instruments.slits import slit3D_get
 #endstation specific
-from iexcode.instruments.kappa_det import kappa_Detector
+from iexcode.instruments.kappa_det import Kappa_Detector
 from iexcode.instruments.scalers import scaler_cts
 from iexcode.instruments.MPA import MPA, mpa_ROI_reset
 from iexcode.instruments.spec_stuff import folders_spec
 
 default_ioc = '29idKappa:'
+
+global tey,d3,d4,mesh,kappa_scaler_pv,tth_pv,tthdet
+
 #############################################################################
 def kappa_init(*userName,**kwargs):
     """
@@ -76,7 +80,7 @@ def kappa_init(*userName,**kwargs):
         iex.BL.mda.log('kappa',user_name,_kappa_log_header(),_kappa_log_entries)
 
     #global detectors
-    global tey,d3,d4,mesh,kappa_scaler_pv,tth_pv,tthdet
+    
     tey = SRS("29idMZ0:scaler1.S2", '29idd:A1')
     d3 = SRS("29idMZ0:scaler1.S3", '29idd:A2')
     d4 = SRS("29idMZ0:scaler1.S4", '29idd:A3')
@@ -84,7 +88,7 @@ def kappa_init(*userName,**kwargs):
     kappa_scaler_pv = '29idMZ0:scaler1.CNT'
     tth_pv = _kappa_motor_dictionary('tth')[3]
     iex.mpa = MPA()
-    tthdet = kappa_Detector(kappa_Motors)
+    tthdet = Kappa_Detector(kappa_Motors)
     
     #resetting everything
     if kwargs['reset']:
diff --git a/iexcode/instruments/Motors.py b/iexcode/instruments/Motors.py
index e4e9a2c..5e7756b 100644
--- a/iexcode/instruments/Motors.py
+++ b/iexcode/instruments/Motors.py
@@ -1,5 +1,4 @@
 import time
-from math import floor
 
 from epics import caget, caput
 
diff --git a/iexcode/instruments/cfg.py b/iexcode/instruments/cfg.py
index 9de5488..8e9c946 100644
--- a/iexcode/instruments/cfg.py
+++ b/iexcode/instruments/cfg.py
@@ -1,3 +1,3 @@
 BL=None
 
-mpa=None
\ No newline at end of file
+mpa=None
diff --git a/iexcode/instruments/staff.py b/iexcode/instruments/staff.py
index 2624d35..4206824 100644
--- a/iexcode/instruments/staff.py
+++ b/iexcode/instruments/staff.py
@@ -1,6 +1,7 @@
 from os import listdir,mkdir,chmod
 from os.path import join, isfile, exists
 
+import iexcode.instruments.cfg as iex
 from iexcode.instruments.ARPES import folders_ARPES, ARPES_init
 from iexcode.instruments.kappa import folders_kappa, kappa_init
 
diff --git a/iexcode/instruments/vortex.py b/iexcode/instruments/vortex.py
index b57d15d..aa71435 100644
--- a/iexcode/instruments/vortex.py
+++ b/iexcode/instruments/vortex.py
@@ -1,4 +1,4 @@
-from this import d
+from time import sleep
 from epics import caput, caget
 
 import iexcode.instruments.cfg as iex
@@ -39,7 +39,7 @@ def mca_scaler(time_sec):
 def mca_run(time_sec):
     caput(vortex_pv+'mca1.PRTM',time_sec)
     sleep(0.1)
-    caput(vortex_pv'mca1EraseStart',1,wait=True,timeout = 100000)
+    caput(vortex_pv+'mca1EraseStart',1,wait=True,timeout = 100000)
 
     
 def mca_off():
@@ -54,7 +54,7 @@ def mca_on():
 
 def _mca_file_setup():
     mda_saveData=saveData_get_all(iex.BL.mda.ioc)
-    caput(vortex_pv+'saveData_fileSystem',mda_saveData[fileSystem])
+    caput(vortex_pv+'saveData_fileSystem',mda_saveData['fileSystem'])
     caput(vortex_pv+'saveData_subDir', '/'+ mda_saveData['subDir']+'/S'+str(mda_saveData['scanNumber']))
     caput(vortex_pv+'saveData_scanNumber',1)
 
@@ -97,7 +97,7 @@ def scanmca(add_mca=True,save_image=False,**kwargs):
         sleep(1)
         if save_image:
             _mca_file_setup()
-            chkmcasave()
+            mca_check_save()
     else:
         #remove vortex trigger and detectors
         iex.BL.mda.triggers_clear()
diff --git a/iexcode/macros/Kappa_optimization.py b/iexcode/macros/Kappa_optimization.py
index 54a89ae..fac1150 100644
--- a/iexcode/macros/Kappa_optimization.py
+++ b/iexcode/macros/Kappa_optimization.py
@@ -1,4 +1,11 @@
+from time import sleep
 
+from iexcode.instruments.utilities import *
+from iexcode.instruments.kappa import uan
+from iexcode.instruments.kappa import tey,d3,d4,mesh,kappa_scaler_pv,tth_pv,tthdet
+from iexcode.instruments.Motors import *
+from iexcode.instruments.scanRecord import last_mda
+from iexcode.macros.quick_plot import *
 
 
 ##### Kappa Optimization scans - 2022 versions #####
@@ -17,16 +24,16 @@ def Find_kth_zero(th_0,th_180):
 def align_d4(z0,th):
     ''' th = 0 or 180
     '''
-    setdet('d4')
+    tthdet.set('d4')
     sleep(1)
     uan(0,th)
     mvz(z0-1500)
-    setgain('d4',10,'uA')
+    d4.setgain(10,'uA')
     dscantth(-0.25,0.25,0.01)
     sleep(1)
-    setgain('d4',1,'nA')
+    d4.setgain(1,'nA')
     mvz(z0)
-    n=LastMDA()
+    n=last_mda()
     fit_mda(n,25,0,'poly',graph=False) 
     tth0=fit_d4(n)
     print('tth0 = '+str(round(tth0,3)))
@@ -38,12 +45,12 @@ def align_z0_chi0(question=True):
     if question:
         foo=input_d('Are you at th=90/270, chi=0, phi=90 (Y or N)? >')
         if foo == 'Y'.lower() or foo == 'yes'.lower():   
-            setdet('d3')
+            tthdet.set('d3')
             sleep(1)
             mvtth(0)
             dscanz(-750,750,50)
             sleep(1)
-            n=LastMDA()
+            n=last_mda()
             fit_mda(n,25,0,'poly',graph=False) 
             z0=fit_z(n)
             print('z0 = '+str(round(z0,0)))
@@ -53,27 +60,25 @@ def align_z0_chi0(question=True):
             print('Canceling')
             return
     else:
-        setdet('d3')
+        tthdet.set('d3')
         sleep(1)
         mvtth(0)
         dscanz(-750,750,50)
         sleep(1)
-        n=LastMDA()
+        n=last_mda()
         fit_mda(n,25,0,'poly',graph=False) 
         z0=fit_z(n)
         print('z0 = '+str(round(z0,0)))
         playsound()
         return z0
 
-
-
 def align_z0(omega):
-    setdet('d3')
+    tthdet.set('d3')
     sleep(1)
     uan(0,omega)
     dscanz(-750,750,50)
     sleep(1)
-    n=LastMDA()
+    n=last_mda()
     #fit_mda(n,25,0,'poly',graph=False) 
     #z0=fit_z(n)
     #print('z0 = '+str(round(z0,0)))
@@ -82,12 +87,12 @@ def align_z0(omega):
 
 
 def align_z180(omega):
-    setdet('d3')
+    tthdet.set('d3')
     sleep(1)
     uan(0,180+omega)
     dscanz(-750,750,50)
     sleep(1)
-    n=LastMDA()
+    n=last_mda()
     #fit_mda(n,25,0,'poly',graph=False) 
     #z180=fit_z(n)
     #print('z180 = '+str(round(z180,0)))
@@ -95,15 +100,15 @@ def align_z180(omega):
     #return z180
     
 def align_th0(omega,z):
-    setdet('d4')
+    tthdet.set('d4')
     sleep(1)
     #uan(20,10+omega)
     uan(30,15+omega)
     mvz(z)
     sleep(30)
-    setgain('d4',1,'nA')
+    d4.setgain(1,'nA')
     dscanth(-0.25,0.25,0.01)
-    n=LastMDA()
+    n=last_mda()
     fit_mda(n,25,0,'poly',graph=False) 
     th_spec=fit_d4(n)
     print('th specular = '+str(round(th_spec,3)))
@@ -115,14 +120,14 @@ def align_th0(omega,z):
 
 
 def align_th180(omega,z):
-    setdet('d4')
+    tthdet.set('d4')
     sleep(1)
     uan(-30,180+omega-15)
     #uan(-20,180+omega-10)
     mvz(z)
-    setgain('d4',1,'nA')
+    d4.setgain(1,'nA')
     dscanth(-0.25,0.25,0.01)
-    n=LastMDA()
+    n=last_mda()
     fit_mda(n,25,0,'poly',graph=False) 
     th_spec=fit_d4(n)
     print('th specular = '+str(round(th_spec,3)))
@@ -147,13 +152,13 @@ def Opt_d4(iteration,moveZ,graph='y',srs=None,start=-0.5,stop=0.5,step=0.04):
     current_det=caget('29idKappa:userStringSeq6.STR1',as_string=True)
     if current_det != 'd4':
         print('Switching detector to d4')
-        setdet('d4')
+    tthdet.set('d4')
     mvtth(0)
     if moveZ is not None:
         mvz(z-1000)
     for i in range(iteration):
         scantth(start,stop,step,'relative')
-        scannum=LastMDA()
+        scannum=last_mda()
         tth0=fit_mda(scannum,det,tth_w,'gauss',graph=graph)
         mvtth(tth0)
     mvz(z)
@@ -164,7 +169,7 @@ def Opt_z0(iteration,movetth,movekth,det='d4',srs=None,graph='y'):
     current_det=caget('29idKappa:userStringSeq6.STR1',as_string=True)
     if current_det != det:
         print('Switching detector to '+det)
-        setdet(det)
+    tthdet.set(det)
     if det=='d3' and srs==None: det=20
     if det=='d3' and srs!=None: det=33
     if det=='d4' and srs==None: det=21
@@ -175,11 +180,11 @@ def Opt_z0(iteration,movetth,movekth,det='d4',srs=None,graph='y'):
         mvkth(movekth)
     if iteration>1:
         scanz(-2000,2000,250,'relative')
-        scannum=LastMDA()
+        scannum=last_mda()
         z0=fit_mda(scannum,det,z_w,'erf',graph=graph)
         mvz(z0)
     scanz(-700,700,50,'relative')
-    scannum=LastMDA()
+    scannum=last_mda()
     z0=fit_mda(scannum,det,z_w,'erf',graph=graph)
     mvz(z0)
     return z0,scannum
@@ -188,7 +193,7 @@ def Opt_kth(kth_0,theta,det='d4',srs=None,graph='y'):
     current_det=caget('29idKappa:userStringSeq6.STR1',as_string=True)
     if current_det != det:
         print('Switching detector to '+det)
-        setdet(det)
+    tthdet.set(det)
     if det == 'd4': 
         if srs==None: det=21
         else: det=34
@@ -200,12 +205,12 @@ def Opt_kth(kth_0,theta,det='d4',srs=None,graph='y'):
     mvtth(theta*2)
     mvkth(kth_0+theta)
     scankth(-0.5*i,0.5*i,0.05*i,'relative')
-    new_kth=fit_mda(LastMDA(),det,0.1,'gauss',graph=graph)
+    new_kth=fit_mda(last_mda(),det,0.1,'gauss',graph=graph)
     mvkth(new_kth)
     scankth(-0.2*i,0.2*i,0.02*i,'relative')
-    new_kth=fit_mda(LastMDA(),det,0.1,'gauss',graph=graph)
+    new_kth=fit_mda(last_mda(),det,0.1,'gauss',graph=graph)
     mvkth(new_kth)
-    scannum=LastMDA()
+    scannum=last_mda()
     kth0=round(new_kth-theta,3)
     print('\nkth0 = ',kth0)
     print('To plot use:') 
diff --git a/iexcode/macros/quick_plot.py b/iexcode/macros/quick_plot.py
index c49d213..f8210df 100755
--- a/iexcode/macros/quick_plot.py
+++ b/iexcode/macros/quick_plot.py
@@ -1,24 +1,11 @@
-"""
-#ScanFunctions_plot.py
-how to copy tabs in stabs and not spaces jupyter
-
-For Fanny:    Converting spaces to tabs from the terminal
-cp ScanFunctions_IEX.py ScanFunctions_IEXbackup.py
-sed -e 's/    /\t/g' ScanFunctions_IEXbackup.py > ScanFunctions_IEX.py
-"""
 
-#### Other utilities:
-import csv
 from os import listdir
 from os.path import join, isfile, dirname
+
+### Data analysis:
 from scipy.optimize import curve_fit
 from scipy.special import erf
 import numpy.polynomial.polynomial as poly
-import ast
-
-### Data analysis:
-from inspect import CO_ASYNC_GENERATOR
-from sre_constants import CATEGORY_DIGIT
 import matplotlib.pyplot as plt
 import matplotlib.image as mpimg
 import numpy as np
@@ -28,15 +15,10 @@ from netCDF4 import Dataset
 
 
 ##### APS / 29ID-IEX:
-from IEX_plotting_and_analysis.mda import readMDA,scanDim
-from IEX_plotting_and_analysis.IEX_nData import * 
-
-try:
-    from epics import caget
-    from ..instruments.IEX_BL_config import BL
-    from ..instruments.utilities import read_dict,today
-    from ..instruments.slits import apertures_set
-    from IEXcode.iexcode.current_amplifiers import current2flux
+from epics import caget
+from iexplot.mda import readMDA,scanDim
+import iexcode.instruments.cfg as iex
+from iexcode.instruments.current_amplifiers import current2flux
 except:
     print("EPICS package and dependent functions are not installed")
 
@@ -146,9 +128,9 @@ def mda_unpack(ScanNum,filepath=None,prefix=None):
     """
     try:
         if filepath is None:
-            filepath = BL.mda.filepath
+            filepath = iex.BL.mda.filepath()
         if prefix is None:
-            prefix = BL.mda.prefix
+            prefix = iex.BL.mda.prefix()
     except:
         print('Please specify filepath and prefix, BL is not defined')
 
@@ -1243,7 +1225,7 @@ def fit_mda(scannum,det,FWHM_or_PolyOrder,fct,hkl_positionner=False,xrange=None,
 
     
 
-    if BL.endstation == 'Kappa' and fct != 'poly':
+    if iex.BL.endstation_name == 'kappa' and fct != 'poly':
         try:
             title=title + ' centroid = '+str(centroid_avg(scannum))
         except:
@@ -1525,21 +1507,21 @@ def fit_hkl(mydata,n,pos,det,FWHM,fct='gauss',xrange=None,title='',marker='x',gr
     return center
 
 
-def fit_d4(n=BL.mda.lastFileNum()):    
+def fit_d4(n=iex.BL.mda.lastFileNum()):    
     d4=fit_mda(n,34,0.1,'gauss',title='mda_'+str(n).zfill(4))  
     return round(d4,3)
 
-def fit_d3(n=BL.mda.lastFileNum()):
+def fit_d3(n=iex.BL.mda.lastFileNum()):
     d3=fit_mda(n,33,3,'gauss',title='mda_'+str(n).zfill(4))  
     return round(d3,3)
 
-def fit_z(n=BL.mda.lastFileNum(),d=33):
+def fit_z(n=iex.BL.mda.lastFileNum(),d=33):
     z=fit_mda(n,d,500,'erf',title='mda_'+str(n).zfill(4))  
     return round(z,0)
     
 def plotd():
     dets=[34,33,35]
-    n=BL.mda.lastFileNum()
+    n=iex.BL.mda.lastFileNum()
     d=dets[caget('29idKappa:det:set')]
     plot_mda(n,d,title='mda_'+str(n).zfill(4))  
 
@@ -1805,7 +1787,7 @@ class mdaFile:
 
     def __init__(self,first=1,last=None,name='mydata',filepath=None,prefix=None,q=False):
         if filepath == None:
-            filepath = BL.mda.filepath()
+            filepath = iex.BL.mda.filepath()
         self.path  = filepath
         self._name  = name
         self._first = first
-- 
GitLab