Skip to content

Commit

Permalink
Merge pull request #16 from NSLS-II-CHX/enh_chxtools_pkg
Browse files Browse the repository at this point in the history
ENH: chxtools package update for 2.0.x
  • Loading branch information
yugangzhang authored Dec 20, 2017
2 parents eaef81d + 91dc6ba commit 0261082
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 120 deletions.
2 changes: 1 addition & 1 deletion chxtools/Time_Correlation_Functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
import sys
import time
import skxray.core.roi as roi
import skbeam.core.roi as roi
from matplotlib import gridspec

import itertools
Expand Down
238 changes: 126 additions & 112 deletions chxtools/chx_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
by LW 03/20/2015
some wrapper functions for ophyd
"""
import Tkinter, tkFileDialog
from __future__ import print_function


def det_select(det):
"""
Expand All @@ -16,19 +17,19 @@ def det_select(det):
calling sequence: det_select(det)
"""
try:
rm_det=ascan.user_detectors[0].name
ascan.user_detectors.remove(session_mgr[rm_det])
rm_det=ascan.user_detectors[0].name
ascan.user_detectors.remove(session_mgr[rm_det])
ascan.default_triggers = []
ascan.default_detectors = []
print 'removed previous default detector: ',rm_det
except: print 'list of detectors appeared to be empty...'

print('removed previous default detector: {}'.format(rm_det))
except:
print('list of detectors appeared to be empty...')

ascan.user_detectors.append(det) # add detector
new_def_det=ascan.user_detectors[0].name

print ''
print 'new default detector: ',new_def_det

new_def_det=ascan.user_detectors[0].name

print('\nnew default detector: {}'.format(new_def_det))


def cw_ascan(mot,xmin,xmax,npoints,acqt='default',pos_ret=True):
"""
Expand All @@ -43,30 +44,32 @@ def cw_ascan(mot,xmin,xmax,npoints,acqt='default',pos_ret=True):
WILL NOT WORK FOR A LIST OF DETECTORS!
"""
# gather beamline information prior to starting the scan:
ini_motpos=caget(mot.record+'.RBV')
ini_motpos = caget(mot.record+'.RBV')
# current detector:
acq_pv=session_mgr[ascan.user_detectors[0].name].pvname
acq_pv = session_mgr[ascan.user_detectors[0].name].pvname

# set different exposure time for the scan, if requested:
if acqt!='default':
if acqt != 'default':
try:
ini_expt=caget(acq_pv[2]) # initial exposure time
ini_expt = caget(acq_pv[2]) # initial exposure time
session_mgr[ascan.user_detectors[0].name].acquire_time = acqt
print 'successfully set exposure time to [s]: ',acqt
except: print 'could not set exposure time to ',acqt

print('successfully set exposure time to [s]: {}'.format(acqt))
except:
print('could not set exposure time to {}'.format(acqt))

# execute the scan
ascan(mot,xmin,xmax,npoints)

# put beamline back into initial state
if pos_ret==True:
if pos_ret is True:
caput(mot.record+'.VAL',ini_motpos)
print 'returned axes to: ',ini_motpos
if acqt!='default':
print('returned axes to: {}'.format(ini_motpos))
if acqt != 'default':
try:
session_mgr[ascan.user_detectors[0].name].acquire_time = ini_expt
print 'successfully reset exposure time to [s]: ',ini_expt
except: print 'could not reset exposure time to ',ini_expt
print('successfully reset exposure time to [s]: {}'.format(ini_expt))
except:
print('could not reset exposure time to {}'.format(ini_expt))

def cw_dscan(mot,mdx,pdx,npoints,acqt='default',pos_ret=True):
"""
Expand All @@ -78,25 +81,27 @@ def cw_dscan(mot,mdx,pdx,npoints,acqt='default',pos_ret=True):
WILL NOT WORK FOR A LIST OF DETECTORS!
"""
# current detector:
acq_pv=session_mgr[ascan.user_detectors[0].name].pvname
acq_pv = session_mgr[ascan.user_detectors[0].name].pvname

# set different exposure time for the scan, if requested:
if acqt!='default':
if acqt != 'default':
try:
ini_expt=caget(acq_pv[2]) # initial exposure time
session_mgr[ascan.user_detectors[0].name].acquire_time = acqt
print 'successfully set exposure time to [s]: ',acqt
except: print 'could not set exposure time to ',acqt

print('successfully set exposure time to [s]: {}'.format(acqt))
except:
print('could not set exposure time to {}'.format(acqt))

# execute the scan
dscan(mot,mdx,pdx,npoints)
#print 'finished scan'
if acqt!='default':
#print('finished scan')

if acqt != 'default':
try:
session_mgr[ascan.user_detectors[0].name].acquire_time = ini_expt
print 'successfully reset exposure time to [s]: ',ini_expt
except: print 'could not reset exposure time to ',ini_expt
print('successfully reset exposure time to [s]: {}'.format(ini_expt))
except:
print('could not reset exposure time to {}'.format(ini_expt))

def cw_CCDseries(folder,filename,detector,imnum='default',startn=1,acqt='default',acqperiod='default'):
"""
Expand All @@ -118,7 +123,7 @@ def cw_CCDseries(folder,filename,detector,imnum='default',startn=1,acqt='default
# get the detector name::
detector.pvname.split('}')[0]+'}'
#get folder interactively:

if folder == 'ia':
root=Tkinter.Tk()
root.withdraw()
Expand All @@ -127,116 +132,125 @@ def cw_CCDseries(folder,filename,detector,imnum='default',startn=1,acqt='default
# check whether target directory exists and create it, if it doesn't
if not os.path.exists(folder):
os.mkdir(folder)
os.chmod(folder,436) #make sure everybody can read an write
os.chmod(folder,436) #make sure everybody can read an write
os.chmod(folder,stat.S_IRWXO)
print 'successfully created new directory: ',folder


print('successfully created new directory: {}'.format(folder))

# put folder:
r=caput(detector.pvname.split('}')[0]+'}TIFF1:FilePath', folder) # have some problem with syntax here...
if r==1:
print 'changed saving directory to: ',folder
else: print 'error: could not change directory for saving data files.'
if r == 1:
print('changed saving directory to: {}'.format(folder))
else:
print('error: could not change directory for saving data files.')
# put the filename:
r=caput(detector.pvname.split('}')[0]+'}TIFF1:FileName', filename) # have some problem with syntax here...
if r==1:
print 'filename for saving: ',filename
else: print 'error: could not change file name for saving.'
if r == 1:
print('filename for saving: {}'.format(filename))
else:
print('error: could not change file name for saving.')


# put start number
caput(detector.pvname.split('}')[0]+'}TIFF1:FileNumber',startn)

#gather information about current camera settings
acq_pv=session_mgr[ascan.user_detectors[0].name].pvname
ini_acq=caget(acq_pv.split('}')[0]+'}cam1:Acquire') # initial state: started or stopped
ini_mode=caget(acq_pv.split('}')[0]+'}cam1:ImageMode') # initial image mode: single [0], multiple [1], continous [2]
ini_expt=caget(acq_pv.split('}')[0]+'}cam1:AcquireTime') # initial exposure time
ini_acqperiod=caget(acq_pv.split('}')[0]+'}cam1:AcquirePeriod') # initial acquiring period
ini_imnum=caget(acq_pv.split('}')[0]+'}cam1:NumImages') # initial image number
if acqt!='default':
acq_pv = session_mgr[ascan.user_detectors[0].name].pvname
ini_acq = caget(acq_pv.split('}')[0]+'}cam1:Acquire') # initial state: started or stopped
ini_mode = caget(acq_pv.split('}')[0]+'}cam1:ImageMode') # initial image mode: single [0], multiple [1], continous [2]
ini_expt = caget(acq_pv.split('}')[0]+'}cam1:AcquireTime') # initial exposure time
ini_acqperiod = caget(acq_pv.split('}')[0]+'}cam1:AcquirePeriod') # initial acquiring period
ini_imnum = caget(acq_pv.split('}')[0]+'}cam1:NumImages') # initial image number

if acqt != 'default':
try:
caput(acq_pv.split('}')[0]+'}cam1:AcquireTime',acqt)
print 'successfully set exposure time to [s]: ',acqt
except: print 'could not set exposure time to ',acqt
print('successfully set exposure time to [s]: {}'.format(acqt))
except:
print('could not set exposure time to {}'.format(acqt))
# stop camara:
try:
caput(acq_pv.split('}')[0]+'}cam1:Acquire',0)
print 'successfully stopped camera'
except: print 'could not stop camera'
print('successfully stopped camera')
except:
print('could not stop camera')
# try to set image mode to multiple
try:
caput(acq_pv.split('}')[0]+'}cam1:ImageMode',1)
print 'successfully set ImageMode to "multiple"'
except: print 'could not set ImageMode to "multiple"'
if acqperiod!='default':
try:
print('successfully set ImageMode to "multiple"')
except:
print('could not set ImageMode to "multiple"')
if acqperiod != 'default':
try:
caput(acq_pv.split('}')[0]+'}cam1:AcquirePeriod',acqperiod)
print 'successfully set acquiering period to: ',acqperiod
except: print 'could not set aquireing period to ',acqperiod
print('successfully set acquiering period to: {}'.format(acqperiod))
except:
print('could not set aquireing period to {}'.format(acqperiod))

# set number of images to be taken:
if imnum!='default':
if imnum != 'default':
try:
caput(acq_pv.split('}')[0]+'}cam1:NumImages',imnum)
print 'successfully set number of images to: ',imnum
except: print 'could not set number of images to ',imnum
print 'going to start the acquisition...'
caput(acq_pv.split('}')[0]+'}cam1:NumImages', imnum)
print('successfully set number of images to: {}'.format(imnum))
except:
print('could not set number of images to {}'.format(imnum))
print('going to start the acquisition...')
time.sleep(1)
# start the acquisition!!!
caput(acq_pv.split('}')[0]+'}cam1:ArrayCounter',0)#set Images acquired (image counter) to 0
caput(detector.pvname.split('}')[0]+'}TIFF1:AutoSave',1) # start auto save
caput(acq_pv.split('}')[0]+'}cam1:Acquire',1) # start series acquisition
caput(acq_pv.split('}')[0]+'}cam1:ArrayCounter',0) # set Images acquired (image counter) to 0

caput(detector.pvname.split('}')[0]+'}TIFF1:AutoSave',1) # start auto save
caput(acq_pv.split('}')[0]+'}cam1:Acquire',1) # start series acquisition

counting=0
current_file=caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True)
while counting==0:
time.sleep(.5)
if caget(acq_pv.split('}')[0]+'}cam1:ArrayCounter_RBV')==imnum: counting=1
if caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True)!=current_file:
current_file=caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True)
print 'file written: ',current_file
time.sleep(.1)

print 'going to stop the acquisition...'
time.sleep(.5)
if caget(acq_pv.split('}')[0]+'}cam1:ArrayCounter_RBV') == imnum:
counting=1
if caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True) != current_file:
current_file=caget(detector.pvname.split('}')[0]+'}TIFF1:FullFileName_RBV',as_string=True)
print('file written: {}'.format(current_file))
time.sleep(.1)

print('going to stop the acquisition...')
time.sleep(.5)
caput(acq_pv.split('}')[0]+'}cam1:Acquire',0) # stop series acquisition (redundent...should have stopped already
caput(detector.pvname.split('}')[0]+'}TIFF1:AutoSave',0) # stop auto save
print 'going to reset camera...'
caput(acq_pv.split('}')[0]+'}cam1:Acquire',0) # stop series acquisition (redundent...should have stopped already
caput(detector.pvname.split('}')[0]+'}TIFF1:AutoSave',0) # stop auto save

print('going to reset camera...')
#time.sleep(5)
# put camera back into initial state:
if acqt!='default':
if acqt != 'default':
try:
caput(acq_pv.split('}')[0]+'}cam1:AcquireTime',ini_expt)
print 'successfully reset exposure time to [s]: ',ini_expt
except: print 'could not reset exposure time to ',ini_expt
print('successfully reset exposure time to [s]: {}'.format(ini_expt))
except:
print('could not reset exposure time to {}'.format(ini_expt))
try:
caput(acq_pv.split('}')[0]+'}cam1:ImageMode',ini_mode)
print 'successfully reset ImageMode'
except: print 'could not reset ImageMode'
caput(acq_pv.split('}')[0]+'}cam1:ImageMode', ini_mode)
print('successfully reset ImageMode')
except:
print('could not reset ImageMode')
try:
caput(acq_pv.split('}')[0]+'}cam1:Acquire',ini_acq)
print 'successfully reset camera acqusition mode'
except: print 'could not reset camera acquisition mode'
caput(acq_pv.split('}')[0]+'}cam1:Acquire', ini_acq)
print('successfully reset camera acqusition mode')
except:
print('could not reset camera acquisition mode')
if acqperiod!='default':
try:
caput(acq_pv.split('}')[0]+'}cam1:AcquirePeriod',ini_acqperiod)
print 'successfully reset acquisition period to [s]: ',ini_acqperiod
except: print 'could not reset acquisition period to ',ini_acqperiod
caput(acq_pv.split('}')[0]+'}cam1:AcquirePeriod', ini_acqperiod)
print('successfully reset acquisition period to [s]: {}'.format(ini_acqperiod))
except:
print('could not reset acquisition period to {}'.format(ini_acqperiod))
if imnum!='default':
try:
caput(acq_pv.split('}')[0]+'}cam1:NumImages',ini_imnum)
print 'successfully reset image numbers to: ',ini_imnum
except: print 'could not reset image numbers to ',ini_imnum
caput(acq_pv.split('}')[0]+'}cam1:NumImages', ini_imnum)
print('successfully reset image numbers to: {}'.format(ini_imnum))
except:
print('could not reset image numbers to {}'.format(ini_imnum))
time.sleep(.5)
try:
caput(acq_pv.split('}')[0]+'}cam1:Acquire',ini_acq) # restart camera if it was running before taking the series
print 'restarted camera'
except: print 'could not restart camera...'





caput(acq_pv.split('}')[0]+'}cam1:Acquire', ini_acq) # restart camera if it was running before taking the series
print('restarted camera')
except:
print('could not restart camera...')

14 changes: 9 additions & 5 deletions chxtools/handlers2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
import h5py
from pims import FramesSequence, Frame
from databroker import db
from databroker import Broker
from .handlers import LazyEigerHandler


class EigerImages2(FramesSequence):
Expand Down Expand Up @@ -96,8 +97,11 @@ def __call__(self, seq_id):
# TODO Return a multi-dimensional PIMS seq.
return EigerImages2(master_path, self._images_per_file, md=md)

db.fs.deregister_handler('AD_EIGER')
db.fs.deregister_handler('AD_EIGER2')
db = Broker.named('chx')

db.reg.deregister_handler('AD_EIGER')
db.reg.deregister_handler('AD_EIGER2')

db.reg.register_handler('AD_EIGER2', EigerHandler2)
db.reg.register_handler('AD_EIGER', LazyEigerHandler)

db.fs.register_handler('AD_EIGER2', EigerHandler2)
db.fs.register_handler('AD_EIGER', LazyEigerHandler)
4 changes: 2 additions & 2 deletions chxtools/speckle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import numpy as np
import time

from skxray.core import roi
from skxray.core.utils import bin_edges_to_centers, geometric_series
from skbeam.core import roi
from skbeam.core.utils import bin_edges_to_centers, geometric_series

import logging
logger = logging.getLogger(__name__)
Expand Down
15 changes: 15 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
atom
cython
databroker
enaml
fabio
h5py
matplotlib
numpy
ophyd
pandas
pims
pyepics
scikit-beam
scipy

Loading

0 comments on commit 0261082

Please sign in to comment.