<commit_before># -*- coding: utf-8 -*-
"""
set up for 2D XRF scan for HF mode

Created on Fri Feb 19 12:16:52 2016
Modified on Wed Wed 02 14:14 to comment out the saturn detector which is not in use

@author: xf05id1
"""

#TO-DOs:
    #1. add suspender once it's fixed
    #2. put snake back to the scan once it's fixed
    #3. check the x/y are correct
    #4. put x/y axes onto the live plot
    #5. add i0 into the default figure

from bluesky.plans import OuterProductAbsScanPlan
from bluesky.callbacks import LiveRaster
import matplotlib
import time
import epics
import os
import numpy

#matplotlib.pyplot.ticklabel_format(style='plain')

def hf2dxrf(xstart=None, xnumstep=None, xstepsize=None, 
            ystart=None, ynumstep=None, ystepsize=None, 
            #wait=None, simulate=False, checkbeam = False, checkcryo = False, #need to add these features
            acqtime=None, numrois=1, i0map_show=True, itmap_show = True,
            energy = None, u_detune = None,
            ):

    '''
    input:
        xstart, xnumstep, xstepsize (float)
        ystart, ynumstep, ystepsize (float)
        acqtime (float): acqusition time to be set for both xspress3 and F460
        numrois (integer): number of ROIs set to display in the live raster scans. This is for display ONLY. 
                           The actualy number of ROIs saved depend on how many are enabled and set in the read_attr
                           However noramlly one cares only the raw XRF spectra which are all saved and will be used for fitting.
        i0map_show (boolean): When set to True, map of the i0 will be displayed in live raster, default is True
        itmap_show (boolean): When set to True, map of the trasnmission diode will be displayed in the live raster, default is True   
        energy (float): set energy, use with caution, hdcm might become misaligned
        u_detune (float): amount of undulator to detune in the unit of keV
    '''

    #make sure user provided correct input

    if xstart is None:
        raise Exception('xstart = None, must specify an xstart position')
    if xnumstep is None:
        raise Exception('xnumstep = None, must specify an xnumstep position')
    if xstepsize is None:
        raise Exception('xstepsize = None, must specify an xstepsize position')
    if ystart is None:
        raise Exception('ystart = None, must specify an ystart position')
    if ynumstep is None:
        raise Exception('ynumstep = None, must specify an ynumstep position')
    if ystepsize is None:
        raise Exception('ystepsize = None, must specify an ystepsize position')
    if acqtime is None:
        raise Exception('acqtime = None, must specify an acqtime position')

    #record relevant meta data in the Start document, defined in 90-usersetup.py
    metadata_record()

    #setup the detector
    current_preamp.exp_time.put(acqtime)
    xs.settings.acquire_time.put(acqtime)
    xs.total_points.put((xnumstep+1)*(ynumstep+1))
    
    #saturn.mca.preset_real_time.put(acqtime)
    #saturn.mca.preset_live_time.put(acqtime)

    #hfvlmAD.cam.acquire_time.put(acqtime)

    #for roi_idx in range(numrois):
    #    saturn.read_attrs.append('mca.rois.roi'+str(roi_idx)+'.net_count')
    #    saturn.read_attrs.append('mca.rois.roi'+str(roi_idx)+'.count')
       
    #det = [current_preamp, saturn]        
    det = [current_preamp, xs]        
    #gjw
    #det = [xs, hfvlmAD]        
    #gjw


    #setup the live callbacks
    livecallbacks = []
    
    livetableitem = [hf_stage.x, hf_stage.y, 'current_preamp_ch0', 'current_preamp_ch2']
    #gjw
    #livetableitem = [hf_stage.x, hf_stage.y, 'hfvlm_stats3_total']
    #gjw

    xstop = xstart + xnumstep*xstepsize
    ystop = ystart + ynumstep*ystepsize  
  
    print('xstop = '+str(xstop))  
    print('ystop = '+str(ystop)) 
    
    
    for roi_idx in range(numrois):
        roi_name = 'roi{:02}'.format(roi_idx+1)
        
        roi_key = getattr(xs.channel1.rois, roi_name).value.name
        livetableitem.append(roi_key)
        
    #    livetableitem.append('saturn_mca_rois_roi'+str(roi_idx)+'_net_count')
    #    livetableitem.append('saturn_mca_rois_roi'+str(roi_idx)+'_count')
    #    #roimap = LiveRaster((xnumstep, ynumstep), 'saturn_mca_rois_roi'+str(roi_idx)+'_net_count', clim=None, cmap='viridis', xlabel='x', ylabel='y', extent=None)
        colormap = 'jet' #previous set = 'viridis'
    #    roimap = LiveRaster((ynumstep, xnumstep), 'saturn_mca_rois_roi'+str(roi_idx)+'_count', clim=None, cmap='jet', 
    #                        xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])

        roimap = LiveRaster((ynumstep+1, xnumstep+1), roi_key, clim=None, cmap='jet', 
                            xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])
        livecallbacks.append(roimap)


    if i0map_show is True:
        i0map = LiveRaster((ynumstep+1, xnumstep+1), 'current_preamp_ch2', clim=None, cmap='jet', 
                        xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])
        livecallbacks.append(i0map)

    if itmap_show is True:
        itmap = LiveRaster((ynumstep+1, xnumstep+1), 'current_preamp_ch0', clim=None, cmap='jet', 
                        xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])
        livecallbacks.append(itmap)
    #gjw
    #vlmmap=LiveRaster((ynumstep+1, xnumstep+1), 'hfvlm_stats3_total', clim=None, cmap='inferno',\
    #    xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])
    #livecallbacks.append(vlmmap)
    #gjw

#    commented out liveTable in 2D scan for now until the prolonged time issue is resolved
    livecallbacks.append(LiveTable(livetableitem)) 

    
    #setup the plan  
    #OuterProductAbsScanPlan(detectors, *args, pre_run=None, post_run=None)
    #OuterProductAbsScanPlan(detectors, motor1, start1, stop1, num1, motor2, start2, stop2, num2, snake2, pre_run=None, post_run=None)

    if energy is not None:
        if u_detune is not None:
            energy.detune.put(u_detune)
        energy.set(energy)
        time.sleep(5)
    

    #TO-DO: implement fast shutter control (open)
    #TO-DO: implement suspender for all shutters in genral start up script
    
#    shut_b.open_cmd.put(1)
#    while (shut_b.close_status.get() == 1):
#        epics.poll(.5)
#        shut_b.open_cmd.put(1)    
    
    hf2dxrf_scanplan = OuterProductAbsScanPlan(det, hf_stage.y, ystart, ystop, ynumstep+1, hf_stage.x, xstart, xstop, xnumstep+1, True)
    scaninfo = gs.RE(hf2dxrf_scanplan, livecallbacks, raise_if_interrupted=True)

    #TO-DO: implement fast shutter control (close)    
#    shut_b.close_cmd.put(1)
#    while (shut_b.close_status.get() == 0):
#        epics.poll(.5)
#        shut_b.close_cmd.put(1)

    #write to scan log    
    logscan('2dxrf')    
    
    return scaninfo
    
    


def hf2dxrf_estack(batch_dir = None, batch_filename = None,
            erange = [], estep = [],
            energy_pt = None,  waittime = 5, energy_waittime = 5,
            harmonic = None, correct_c2_x=True, correct_c1_r = False, 
          #same parameters as in hd2dxrf
            xstart=None, xnumstep=None, xstepsize=None, 
            ystart=None, ynumstep=None, ystepsize=None, 
            #wait=None, simulate=False, checkbeam = False, checkcryo = False, #need to add these features
            acqtime=None, numrois=1, i0map_show=False, itmap_show = False):
    '''
    A function under development that will provide the ability to do xrf stack imaging.
    Warning: this function is not complete and has not been tested. 
    '''
    if erange is []:
        raise Exception('erange = [], must specify energy ranges')
    if estep is []:
        raise Exception('estep = [], must specify energy step sizes')
    if len(erange)-len(estep) is not 1:
        raise Exception('must specify erange and estep correctly.'\
                         +'e.g. erange = [7000, 7100, 7150, 7500], estep = [2, 0.5, 5] ')
                         
    if acqtime is None:
        raise Exception('acqtime = None, must specify an acqtime position')
        
    #initializing the batch log file
    batchlogfile = batch_dir+'/logfile_'+batch_filename
    batchlogf = open(batchlogfile, 'w')<commit_msg>start to convert self-running scans to plans<commit_after># -*- coding: utf-8 -*-
"""
set up for 2D XRF scan for HF mode

Created on Fri Feb 19 12:16:52 2016
Modified on Wed Wed 02 14:14 to comment out the saturn detector which is not in use

@author: xf05id1
"""

#TO-DOs:
    #1. add suspender once it's fixed
    #2. put snake back to the scan once it's fixed
    #3. check the x/y are correct
    #4. put x/y axes onto the live plot
    #5. add i0 into the default figure

from bluesky.plans import OuterProductAbsScanPlan
import bluesky.plans as bp
from bluesky.callbacks import LiveRaster
import matplotlib
import time
import epics
import os
import numpy

#matplotlib.pyplot.ticklabel_format(style='plain')
def get_stock_md():
    md = {}
    md['beamline_status']  = {'energy':  energy.energy.position 
                                #'slt_wb': str(slt_wb.position),
                                #'slt_ssa': str(slt_ssa.position)
                                }
                                
    md['initial_sample_position'] = {'hf_stage_x': hf_stage.x.position,
                                       'hf_stage_y': hf_stage.y.position,
                                       'hf_stage_z': hf_stage.z.position}
    md['wb_slits'] = {'v_gap' : slt_wb.v_gap.position,
                            'h_gap' : slt_wb.h_gap.position,
                            'v_cen' : slt_wb.v_cen.position,
                            'h_cen' : slt_wb.h_cen.position
                            }
    md['hfm'] = {'y' : hfm.y.position,
                               'bend' : hfm.bend.position} 
    md['ssa_slits'] = {'v_gap' : slt_ssa.v_gap.position,
                            'h_gap' : slt_ssa.h_gap.position,
                            'v_cen' : slt_ssa.v_cen.position,
                            'h_cen' : slt_ssa.h_cen.position                                      
                             }                                      
    return md
                                       

def hf2dxrf(*, xstart, xnumstep, xstepsize, 
            ystart, ynumstep, ystepsize, 
            #wait=None, simulate=False, checkbeam = False, checkcryo = False, #need to add these features
            acqtime, numrois=1, i0map_show=True, itmap_show=True,
            energy=None, u_detune=None):

    '''
    input:
        xstart, xnumstep, xstepsize (float)
        ystart, ynumstep, ystepsize (float)
        acqtime (float): acqusition time to be set for both xspress3 and F460
        numrois (integer): number of ROIs set to display in the live raster scans. This is for display ONLY. 
                           The actualy number of ROIs saved depend on how many are enabled and set in the read_attr
                           However noramlly one cares only the raw XRF spectra which are all saved and will be used for fitting.
        i0map_show (boolean): When set to True, map of the i0 will be displayed in live raster, default is True
        itmap_show (boolean): When set to True, map of the trasnmission diode will be displayed in the live raster, default is True   
        energy (float): set energy, use with caution, hdcm might become misaligned
        u_detune (float): amount of undulator to detune in the unit of keV
    '''

    #record relevant meta data in the Start document, defined in 90-usersetup.py
    md = get_stock_md()

    #setup the detector
    # TODO do this with configure
    current_preamp.exp_time.put(acqtime)
    xs.settings.acquire_time.put(acqtime)
    xs.total_points.put((xnumstep+1)*(ynumstep+1))
    
    #saturn.mca.preset_real_time.put(acqtime)
    #saturn.mca.preset_live_time.put(acqtime)

    #hfvlmAD.cam.acquire_time.put(acqtime)

    #for roi_idx in range(numrois):
    #    saturn.read_attrs.append('mca.rois.roi'+str(roi_idx)+'.net_count')
    #    saturn.read_attrs.append('mca.rois.roi'+str(roi_idx)+'.count')
       
    #det = [current_preamp, saturn]        
    det = [current_preamp, xs]        
    #gjw
    #det = [xs, hfvlmAD]        
    #gjw


    #setup the live callbacks
    livecallbacks = []
    
    livetableitem = [hf_stage.x, hf_stage.y, 'current_preamp_ch0', 'current_preamp_ch2']
    #gjw
    #livetableitem = [hf_stage.x, hf_stage.y, 'hfvlm_stats3_total']
    #gjw

    xstop = xstart + xnumstep*xstepsize
    ystop = ystart + ynumstep*ystepsize  
  
    print('xstop = '+str(xstop))  
    print('ystop = '+str(ystop)) 
    
    
    for roi_idx in range(numrois):
        roi_name = 'roi{:02}'.format(roi_idx+1)
        
        roi_key = getattr(xs.channel1.rois, roi_name).value.name
        livetableitem.append(roi_key)
        
    #    livetableitem.append('saturn_mca_rois_roi'+str(roi_idx)+'_net_count')
    #    livetableitem.append('saturn_mca_rois_roi'+str(roi_idx)+'_count')
    #    #roimap = LiveRaster((xnumstep, ynumstep), 'saturn_mca_rois_roi'+str(roi_idx)+'_net_count', clim=None, cmap='viridis', xlabel='x', ylabel='y', extent=None)
        colormap = 'jet' #previous set = 'viridis'
    #    roimap = LiveRaster((ynumstep, xnumstep), 'saturn_mca_rois_roi'+str(roi_idx)+'_count', clim=None, cmap='jet', 
    #                        xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])

        roimap = LiveRaster((ynumstep+1, xnumstep+1), roi_key, clim=None, cmap='jet', 
                            xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])
        livecallbacks.append(roimap)


    if i0map_show is True:
        i0map = LiveRaster((ynumstep+1, xnumstep+1), 'current_preamp_ch2', clim=None, cmap='jet', 
                        xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])
        livecallbacks.append(i0map)

    if itmap_show is True:
        itmap = LiveRaster((ynumstep+1, xnumstep+1), 'current_preamp_ch0', clim=None, cmap='jet', 
                        xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])
        livecallbacks.append(itmap)
    #gjw
    #vlmmap=LiveRaster((ynumstep+1, xnumstep+1), 'hfvlm_stats3_total', clim=None, cmap='inferno',\
    #    xlabel='x (mm)', ylabel='y (mm)', extent=[xstart, xstop, ystop, ystart])
    #livecallbacks.append(vlmmap)
    #gjw

#    commented out liveTable in 2D scan for now until the prolonged time issue is resolved
    livecallbacks.append(LiveTable(livetableitem)) 

    
    #setup the plan  
    #OuterProductAbsScanPlan(detectors, *args, pre_run=None, post_run=None)
    #OuterProductAbsScanPlan(detectors, motor1, start1, stop1, num1, motor2, start2, stop2, num2, snake2, pre_run=None, post_run=None)

    if energy is not None:
        if u_detune is not None:
            # TODO maybe do this with set
            energy.detune.put(u_detune)
        # TODO fix name shadowing
        yield from bp.abs_set(energy, energy, wait=True)
    

    #TO-DO: implement fast shutter control (open)
    #TO-DO: implement suspender for all shutters in genral start up script
    
#    shut_b.open_cmd.put(1)
#    while (shut_b.close_status.get() == 1):
#        epics.poll(.5)
#        shut_b.open_cmd.put(1)    
    
    hf2dxrf_scanplan = OuterProductAbsScanPlan(det, hf_stage.y, ystart, ystop, ynumstep+1, hf_stage.x, xstart, xstop, xnumstep+1, True, md=md)
    hf2dxrf_scanplan = bp.subs_wrapper( hf2dxrf_scanplan, livecallbacks)
    scaninfo = yield from hf2dxrf_scanplan

    #TO-DO: implement fast shutter control (close)    
#    shut_b.close_cmd.put(1)
#    while (shut_b.close_status.get() == 0):
#        epics.poll(.5)
#        shut_b.close_cmd.put(1)

    #write to scan log    
    logscan('2dxrf')    
    
    return scaninfo
    
    
def multi_region_h(regions, energy_list=None, **kwargs):
    ret = []
    
    for r in regions:
        inp = {}
        inp.update(kwargs)
        inp.update(r)
        rs_uid = yield from hf2dxrf(**inp)
        ret.extend(rs_uid)
    return ret


def hf2dxrf_estack(batch_dir = None, batch_filename = None,
            erange = [], estep = [],
            energy_pt = None,  waittime = 5, energy_waittime = 5,
            harmonic = None, correct_c2_x=True, correct_c1_r = False, 
          #same parameters as in hd2dxrf
            xstart=None, xnumstep=None, xstepsize=None, 
            ystart=None, ynumstep=None, ystepsize=None, 
            #wait=None, simulate=False, checkbeam = False, checkcryo = False, #need to add these features
            acqtime=None, numrois=1, i0map_show=False, itmap_show = False):
    '''
    A function under development that will provide the ability to do xrf stack imaging.
    Warning: this function is not complete and has not been tested. 
    '''
    if erange is []:
        raise Exception('erange = [], must specify energy ranges')
    if estep is []:
        raise Exception('estep = [], must specify energy step sizes')
    if len(erange)-len(estep) is not 1:
        raise Exception('must specify erange and estep correctly.'\
                         +'e.g. erange = [7000, 7100, 7150, 7500], estep = [2, 0.5, 5] ')
                         
    if acqtime is None:
        raise Exception('acqtime = None, must specify an acqtime position')
        
    #initializing the batch log file
    batchlogfile = batch_dir+'/logfile_'+batch_filename
    batchlogf = open(batchlogfile, 'w')
{'fraction_non_alphanumeric': 0.10796198281258892, 'fraction_numerical': 0.013317397985316715, 'mean_word_length': 2.562132576525441, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '807648', 'n_tokens_mistral': 6133, 'n_tokens_neox': 5901, 'n_words': 1524}