Maia detector scan macro

Macro performing an scan using the Maia detector.

#!/bin/env python

import PyTango
import time
from sardana.macroserver.macro import macro, Type

__all__ = ["maia_scan"] 

 
counter = 0
 
class HookPars:
    pass

def hook_pre_move(self, hook_pars):
    global counter

    PetraCurrentDevice = PyTango.DeviceProxy("petra/globals/keyword")

    while PetraCurrentDevice.BeamCurrent.value < 15.:
        self.output("Petra Current below 15 mA. Current Value " + str(PetraCurrentDevice.BeamCurrent.value) +  ". Waiting...")
        time.sleep(1.)

    m = self.macros

    if counter%2==0:
        self.output("Axis 0 to right: " + str(hook_pars.mot0_right)) 
        m.mv(hook_pars.mot0,hook_pars.mot0_right)
    else:
        self.output("Axis 0 to left: " + str(hook_pars.mot0_left)) 
        m.mv(hook_pars.mot0,hook_pars.mot0_left)
    if counter!=0:
        self.output(str(float(counter)/float(hook_pars.pixel_size1)*100.) + "% done, runtime " + str((time.time()-hook_pars.starttime)/60.) + " minutes, remaining time" + str( (time.time()-hook_pars.starttime)/60.*(float(hook_pars.pixel_size1-counter)/float(counter)) ) + " minutes")

    counter = counter + 1
    
@macro( param_def = [
        ['mot0',  Type.Moveable,   None, 'Internal motor'],
        ['pixel_origin0',  Type.Float,   None, 'Pixel origin internal motor'],
        ['scan_range0',  Type.Float,   None, 'Scan range internal motor'],
        ['pixel_pitch0',  Type.Float, None, 'Pixel pitch internal motor'],
        ['scan_dwell', Type.Float,   None, 'Scan time per pixel'],
        ['mot1',  Type.Moveable,   None, 'External motor'],
        ['pixel_origin1',  Type.Float,   None, 'Pixel origin external motor'],
        ['scan_range1',  Type.Float,   None, 'Scan range external motor'],
        ['pixel_pitch1',  Type.Float, None, 'Pixel pitch external motor'],
        ['info_comment',  Type.String, " ", 'Comment to be written in the maia scan info'] ])  

def maia_scan( self, mot0, pixel_origin0, scan_range0, pixel_pitch0, scan_dwell,  mot1, pixel_origin1, scan_range1, pixel_pitch1, info_comment):
    """ scans with maia detector """

    maia_logger = self.getEnv('MaiaLoggerDevice')
    maia_scan = self.getEnv('MaiaScanDevice')
    maia_processing = self.getEnv('MaiaProcessingDevice')
    maia_axis0 = self.getEnv('MaiaAxis0Device')
    maia_axis1 = self.getEnv('MaiaAxis1Device')
    energy_device = self.getEnv('EnergyDevice')
    keithley_device = self.getEnv('KeithleyDevice')
    maia_sample = self.getEnv('MaiaSampleDevice')

    MaiaLogger=PyTango.DeviceProxy(maia_logger)
    MaiaScan=PyTango.DeviceProxy(maia_scan)
    MaiaProcessing=PyTango.DeviceProxy(maia_processing)
    MaiaAxis0=PyTango.DeviceProxy(maia_axis0)
    MaiaAxis1=PyTango.DeviceProxy(maia_axis1)

    EnergyDevice = PyTango.DeviceProxy(energy_device) 
    KeithleyDevice = PyTango.DeviceProxy(keithley_device)
    
    mono_energy=EnergyDevice.position/1000.

    keithley_gain=1.E9/10**(KeithleyDevice.Gain)

    # Move motors to the start positions

    startpos0 = pixel_origin0 - pixel_pitch0
    macro_mov_starpos0 , pars = self.createMacro('mv', mot0, startpos0)
    self.runMacro(macro_mov_starpos0)
    startpos1 = pixel_origin1
    macro_mov_starpos1 , pars = self.createMacro('mv', mot1, startpos1)
    self.runMacro(macro_mov_starpos1)
    
    # Send values to the maia detector

    ScanInfo=str('"{energy:'+str(mono_energy)+',IC_sensitivity:'+str(keithley_gain)+',sample:\\"'+info_comment+'\\"}"')

    pixel_size0 = (int)(scan_range0/pixel_pitch0)
    pixel_size1 = (int)(scan_range1/pixel_pitch1)
    
    MaiaAxis1.PositionScale = pixel_pitch1
    
    MaiaAxis0.position = pixel_origin0 - pixel_pitch0
    MaiaAxis1.position = pixel_origin1
    
    MaiaAxis0.ScanSize = pixel_size0
    MaiaAxis1.ScanSize = pixel_size1 
  
    MaiaAxis0.PixelPitch = pixel_pitch0
    MaiaAxis1.PixelPitch = pixel_pitch1 
 
    MaiaAxis0.PixelOrigin = pixel_origin0
    MaiaAxis1.PixelOrigin = pixel_origin1

    MaiaScan.ScanDwell = scan_dwell 
    MaiaScan.ScanInfo = ScanInfo

    # Set mot0 to the desired slew rate
    # SlewDouble is still not accesible in the sardana motor
    
    mot0_tango = PyTango.DeviceProxy(mot0.TangoDevice)

    mot0_old_slewrate=mot0_tango.SlewDouble 
    mot0_desired_slewrate = pixel_pitch0/scan_dwell
    mot0_tango.SlewDouble = mot0_desired_slewrate


    starttime=time.time()

    MaiaLogger.NewRun()
    MaiaProcessing.PixelEnable = 1
    MaiaLogger.NewRun()   
    MaiaScan.ScanNew = 1

    # Start the scan

    global counter
    counter = 0

    mot0_left = pixel_origin0 + pixel_size0*pixel_pitch0
    mot0_right = pixel_origin0

    start_pos = pixel_origin1
    final_pos = pixel_origin1 + scan_range1
    nr_interv = (int)(scan_range1/pixel_pitch1)
    integ_time = 1.

    macro,pars = self.createMacro('ascan', mot1, start_pos, final_pos, nr_interv, integ_time)


    hook_pars = HookPars()
    hook_pars.mot0 = mot0
    hook_pars.mot0_left = mot0_left
    hook_pars.mot0_right = mot0_right
    hook_pars.MaiaScan = MaiaScan
    hook_pars.pixel_size1 = pixel_size1
    hook_pars.starttime = starttime
    
    f = lambda : hook_pre_move( self, hook_pars)

    macro.hooks = [
        (f, ["pre-move"]),
        ]
    
    self.runMacro(macro)

    # After finishing the scan


    MaiaLogger.EndRun()
    MaiaProcessing.PixelEnable = 0
    mot0_tango.SlewDouble = mot0_old_slewrate



2019-11-13