WITecCOM.py 23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# -*- coding: utf-8 -*-
"""
GEPARD - Gepard-Enabled PARticle Detection
Copyright (C) 2018  Lars Bittrich and Josef Brandt, Leibniz-Institut für 
Polymerforschung Dresden e. V. <bittrich-lars@ipfdd.de>    

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program, see COPYING.  
If not, see <https://www.gnu.org/licenses/>.
"""

import os
import sys
Josef Brandt's avatar
Josef Brandt committed
24

25 26 27 28 29 30 31
try:
    import pythoncom
    import win32com.client
except ImportError:
    os.environ["NO_WITEC_CONTROL"] = "True"
    
from time import sleep, time
32
try:            #when running the witectesting, the paths have to be differently, as it is in the same directory as the other raman com modules
Raman's avatar
Raman committed
33 34
    from .ramanbase import RamanBase
    from .configRaman import RamanSettingParam
Josef Brandt's avatar
Josef Brandt committed
35
except ModuleNotFoundError:
36 37
    from ramanbase import RamanBase
    from configRaman import RamanSettingParam
38 39 40 41 42
from socket import gethostname


class WITecCOM(RamanBase):
    CLSID = "{C45E77CE-3D66-489A-B5E2-159F443BD1AA}"
43 44 45 46 47 48
    
    magn = 20
    
    ramanParameters = [RamanSettingParam('IntegrationTime (s)', 'double', default=0.5, minVal=0.01, maxVal=100),
                       RamanSettingParam('Accumulations', 'int', default=5, minVal=1, maxVal=100)]
    
49 50
    def __init__(self, hostname=None):
        super().__init__()
Josef Brandt's avatar
 
Josef Brandt committed
51
        self.name = 'WITecCOM'
52 53 54 55
        if hostname is None:
            hostname = gethostname()
        self.IBUCSAccess = win32com.client.DispatchEx(self.CLSID, machine=hostname, 
                                                      clsctx=pythoncom.CLSCTX_REMOTE_SERVER)
56 57 58
        self.advancedInterface = False
        self.doAutoFocus = False
            
59 60 61 62 63 64
    def connect(self):
        if not self.IBUCSAccess.RequestWriteAccess(True):
            self.connected = False
            return False
        
        IBUCSCore = win32com.client.CastTo(self.IBUCSAccess, 'IBUCSCore')
65
        self.VersionInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|Application|ProgramVersion")
Raman's avatar
Raman committed
66 67 68 69 70 71 72
        self.VersionMan = win32com.client.CastTo(self.VersionInterface, 'IBUCSStatusContainer')
        self.VersionMan.Update()
        version = self.VersionMan.GetSingleValueAsString()[1]
        try:
            vnr = version.split(",")[1].split(" Release")[0].strip()
        except:
            vnr = ""                
73 74 75 76 77
        try:
            vnr = float(vnr)
        except ValueError:
            print("WITec: unknown version format:", version)
            vnr = 0.0 # version format unknown        
Raman's avatar
Raman committed
78
        self.version = vnr
79
        
80 81 82 83 84 85 86 87
        self.PosXInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SamplePositioning|AbsolutePositionX")
        self.PosYInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SamplePositioning|AbsolutePositionY")
        self.PosZInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|ScanTable|PositionMicroscopeZ")
        self.GoToInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SamplePositioning|GoToPosition")
        self.PosXCurInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|SamplePositioner|CurrentPositionX")
        self.PosYCurInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|SamplePositioner|CurrentPositionY")
        self.PosZCurInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|SamplePositioner|CurrentPositionZ")
        self.PosZCurUserInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Hardware|Controller|DataChannels|StepperMotorPosition")
Lars Bittrich's avatar
Lars Bittrich committed
88
        
89 90 91 92 93 94 95 96 97
        self.PosXFloatMan = win32com.client.CastTo(self.PosXInterface, 'IBUCSFloat')
        self.PosYFloatMan = win32com.client.CastTo(self.PosYInterface, 'IBUCSFloat')
        self.PosZFloatMan = win32com.client.CastTo(self.PosZInterface, 'IBUCSFloat')
        self.PosXCurFloatMan = win32com.client.CastTo(self.PosXCurInterface, 'IBUCSStatusContainer')
        self.PosYCurFloatMan = win32com.client.CastTo(self.PosYCurInterface, 'IBUCSStatusContainer')
        self.PosZCurFloatMan = win32com.client.CastTo(self.PosZCurInterface, 'IBUCSStatusContainer')
        self.PosZCurUserFloatMan = win32com.client.CastTo(self.PosZCurUserInterface, 'IBUCSStatusContainer')
        self.GoToTrigger = win32com.client.CastTo(self.GoToInterface, 'IBUCSTrigger')
        
98 99 100 101 102 103 104 105 106 107 108 109 110
        if vnr<5.1:
            self.ImageNameInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MultiCommVideoSystem|BitmapFileName")
            self.ImageNameMan = win32com.client.CastTo(self.ImageNameInterface, 'IBUCSString')
            self.ImageSaveInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MultiCommVideoSystem|SaveColorBitmapToFile")
            self.ImageSaveMan = win32com.client.CastTo(self.ImageSaveInterface, 'IBUCSTrigger')
            
            self.ImageHeightInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|ImageHeightMicrons")
            self.ImageHeightMan = win32com.client.CastTo(self.ImageHeightInterface, 'IBUCSFloat')
            self.ImageWidthInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|ImageWidthMicrons")
            self.ImageWidthMan = win32com.client.CastTo(self.ImageWidthInterface, 'IBUCSFloat')
            self.ImageRotationInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|RotationDegrees")
            self.ImageRotationMan = win32com.client.CastTo(self.ImageRotationInterface, 'IBUCSFloat')
        else:
Raman's avatar
Raman committed
111
            self.ImageNameInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|VideoImageFileName")
112
            self.ImageNameMan = win32com.client.CastTo(self.ImageNameInterface, 'IBUCSString')
Raman's avatar
Raman committed
113
            self.ImageSaveInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|AcquireVideoImageToFile")
114 115
            self.ImageSaveMan = win32com.client.CastTo(self.ImageSaveInterface, 'IBUCSTrigger')
            
Raman's avatar
Raman committed
116
            self.ImageHeightInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|ImageHeightMicrons")
117
            self.ImageHeightMan = win32com.client.CastTo(self.ImageHeightInterface, 'IBUCSFloat')
Raman's avatar
Raman committed
118
            self.ImageWidthInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|ImageWidthMicrons")
119
            self.ImageWidthMan = win32com.client.CastTo(self.ImageWidthInterface, 'IBUCSFloat')
Raman's avatar
Raman committed
120
            self.ImageRotationInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|Calibration|RotationDegrees")
121
            self.ImageRotationMan = win32com.client.CastTo(self.ImageRotationInterface, 'IBUCSFloat')
122
        
Elisa Kanaki's avatar
Elisa Kanaki committed
123 124 125 126 127 128 129
        if vnr>=5.2:
            self.TimeSeriesSlowNameInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|Naming|SampleName")
            self.TimeSeriesSlowNumInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|Naming|Counter")
        else:
            self.TimeSeriesSlowNameInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|Naming|DataName")
            self.TimeSeriesSlowNumInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|Naming|DataNumber")
        
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        self.SequencerStartInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerSingleSpectrum|Start")
        self.SequencerStartTrigger = win32com.client.CastTo(self.SequencerStartInterface, 'IBUCSTrigger')
        
        self.SequencerBusyInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|Sequencers|IsASequencerActive")
        self.SequencerBusyStatus = win32com.client.CastTo(self.SequencerBusyInterface, 'IBUCSStatusContainer') 
        
        self.TimeSeriesSlowNumMeasurementsInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|AmountOfMeasurements")
        self.TimeSeriesSlowNumMeasurementsMan = win32com.client.CastTo(self.TimeSeriesSlowNumMeasurementsInterface, 'IBUCSInt')
        self.TimeSeriesSlowNameMan = win32com.client.CastTo(self.TimeSeriesSlowNameInterface, 'IBUCSString')
        self.TimeSeriesSlowNumAccumulationsInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|SpectrumAcquisition|Accumulations")
        self.TimeSeriesSlowNumAccumulationsMan = win32com.client.CastTo(self.TimeSeriesSlowNumAccumulationsInterface, 'IBUCSInt')
        self.TimeSeriesSlowIntTimeInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|SpectrumAcquisition|IntegrationTime")
        self.TimeSeriesSlowIntTimeMan = win32com.client.CastTo(self.TimeSeriesSlowIntTimeInterface, 'IBUCSFloat')
        self.TimeSeriesSlowNumMan = win32com.client.CastTo(self.TimeSeriesSlowNumInterface, 'IBUCSInt')
        self.TimeSeriesSlowModeInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|MeasurementMode")
        self.TimeSeriesSlowModeMan = win32com.client.CastTo(self.TimeSeriesSlowModeInterface, 'IBUCSEnum')
        self.TimeSeriesSlowNextInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|NextMeasurement")
        self.TimeSeriesSlowNextMan = win32com.client.CastTo(self.TimeSeriesSlowNextInterface, 'IBUCSTrigger')
        
        self.TimeSeriesSlowStartInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|Start")
        self.TimeSeriesSlowStartMan = win32com.client.CastTo(self.TimeSeriesSlowStartInterface, 'IBUCSTrigger')
        
        self.TimeSeriesSlowStopInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|StopSequencer")
        self.TimeSeriesSlowStopMan = win32com.client.CastTo(self.TimeSeriesSlowStopInterface, 'IBUCSTrigger')
        
        self.TimeSeriesSlowIndNextInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerTimeSeriesSlow|IndexOfNextMeasurement")
        self.TimeSeriesSlowIndNextMan = win32com.client.CastTo(self.TimeSeriesSlowIndNextInterface, 'IBUCSInt')
        
        self.TimeSeriesSlowActivityInterface = IBUCSCore.GetSubSystemDefaultInterface("Status|Software|Sequencers|ActiveSequencer|CurrentActivity")
        self.TimeSeriesSlowActivityMan = win32com.client.CastTo(self.TimeSeriesSlowActivityInterface, 'IBUCSStatusContainer')
        
        self.RamanRelativeXInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|ProbePosition|RelativeX")
        self.RamanRelativeXMan = win32com.client.CastTo(self.RamanRelativeXInterface, 'IBUCSFloat')
        self.RamanRelativeYInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|Video|ProbePosition|RelativeY")
        self.RamanRelativeYMan = win32com.client.CastTo(self.RamanRelativeYInterface, 'IBUCSFloat')
        
        self.BrightnessInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|WhiteLight|Top|BrightnessPercentage")
        self.BrightnessMan = win32com.client.CastTo(self.BrightnessInterface, 'IBUCSFloat')
        
Josef Brandt's avatar
Josef Brandt committed
169 170 171
        try:
            self.getAdvancedCOMFeatures(IBUCSCore)
            self.advancedInterface = True
172
            self.doAutoFocus = False
Josef Brandt's avatar
Josef Brandt committed
173 174 175
        except pythoncom.com_error:
            self.advancedInterface = False
            
176 177 178
        self.connected = True
        return True
    
Josef Brandt's avatar
Josef Brandt committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    def getAdvancedCOMFeatures(self, IBUCSCore):
        self.AutoFocusInterface = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerAutoFocus|Start")
        self.AutoFocusMan = win32com.client.CastTo(self.AutoFocusInterface, 'IBUCSTrigger')
        
        self.SilentSpecTextInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|SequencerSingleSpectrum|SilentSpectrumAsText")
        self.SilentSpecText = win32com.client.CastTo(self.SilentSpecTextInterface, 'IBUCSString')
                
        self.SilentSpecDoInterface = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|SequencerSingleSpectrum|DoSilentSpectrum")
        self.DoSilentSpectrum = win32com.client.CastTo(self.SilentSpecDoInterface, 'IBUCSBool')
        
        self.SingleSpecIntegrationTime = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerSingleSpectrum|IntegrationTime")
        self.SingleSpecIntegrationTimeMan = win32com.client.CastTo(self.SingleSpecIntegrationTime, 'IBUCSFloat')
        self.SingleSpecNumAccs = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|SequencerSingleSpectrum|NrOfAccumulations")
        self.SingleSpecNumAccsMan = win32com.client.CastTo(self.SingleSpecNumAccs, 'IBUCSInt')
        
        self.BeamPathState = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|BeamPath|State")
        self.BeamPathSetRamanState = IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|BeamPath|SetStateRaman")
        self.MicroscopeIdle = IBUCSCore.GetSubSystemDefaultInterface("UserParameters|MicroscopeStateOnIdle")
        
        self.BeamPathSetVideoStateInterface =IBUCSCore.GetSubSystemDefaultInterface("MultiComm|MicroscopeControl|BeamPath|SetStateVideo")
        self.BeamPathSetVideoState = win32com.client.CastTo(self.BeamPathSetVideoStateInterface, 'IBUCSTrigger')
        
        try:
            from .advancedWITec import AdvancedWITecSpectra 
        except ModuleNotFoundError:
            from advancedWITec import AdvancedWITecSpectra 
205 206 207
        self.advSpec = AdvancedWITecSpectra()
        if 'Autofocus' not in [param.name for param in self.ramanParameters]:
            self.ramanParameters.append(RamanSettingParam('Autofocus', 'checkBox', default=False))
Josef Brandt's avatar
Josef Brandt committed
208
    
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    def getBrightness(self):
        assert self.connected
        return self.BrightnessMan.GetValue()
    
    def setBrightness(self, newval):
        if newval<0: 
            newval = 0.0
        elif newval>100.:
            newval = 100.0
        self.BrightnessMan.SetValue(newval)
        sleep(.1)
    
    def getRamanPositionShift(self):
        rx = .5-self.RamanRelativeXMan.GetValue() # current assumption is, that image center is current position
        ry = self.RamanRelativeYMan.GetValue()-.5
        width, height = self.ImageWidthMan.GetValue(), self.ImageHeightMan.GetValue()
Lars Bittrich's avatar
Lars Bittrich committed
225
        return rx*width, ry*height        
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    
    def disconnect(self):
        if self.connected:
            self.IBUCSAccess.RequestWriteAccess(False)
        self.connected = False
    
    def getPosition(self):
        assert self.connected
        self.PosXCurFloatMan.Update()
        x = self.PosXCurFloatMan.GetSingleValueAsDouble()[1]
        self.PosYCurFloatMan.Update()
        y = self.PosYCurFloatMan.GetSingleValueAsDouble()[1]
        self.PosZCurFloatMan.Update()
        z = self.PosZCurFloatMan.GetSingleValueAsDouble()[1]
        return x, y, z
    
    def getSoftwareZ(self):
        self.PosZCurFloatMan.Update()
        z = self.PosZCurFloatMan.GetSingleValueAsDouble()[1]
        return z
    
    def getUserZ(self):
        assert self.connected
        self.PosZCurUserFloatMan.Update()
        z = self.PosZCurUserFloatMan.GetSingleValueAsDouble()[1]
        return z
        
253
    def moveToAbsolutePosition(self, x, y, z=None, epsxy=0.11, epsz=0.011):
254 255 256 257 258
        assert self.connected
        initpos = self.getPosition()
        # move only if new position is really different; repeat if new position is ignored (happens some times)
        while max(abs(initpos[0]-x), abs(initpos[1]-y))>epsxy:
            t0 = time()
Josef Brandt's avatar
 
Josef Brandt committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
            numFails = 0
            maxFails = 50
            positionSubmitted = False
            while numFails < maxFails and not positionSubmitted:
                try:
                    self.PosXFloatMan.SetValue(x)
                    self.PosYFloatMan.SetValue(y)
                    self.GoToTrigger.OperateTrigger()
                    positionSubmitted = True
                except pythoncom.com_error:
                    numFails += 1
                    sleep(.1)
            if numFails > 0:
                print(f'{numFails} of max. {maxFails} unsuccessfull position submits to Position: {x}, {y}', flush=True)
            if not positionSubmitted:
                print(f'Error setting Position: {x}, {y}\nExpecting \"signal ignored\" warning', flush=True)
275 276 277 278 279 280
            
            # wait till position is found within accuracy of epsxy; check if position changes at all
            distance = 2*epsxy
            while distance > epsxy:# and (lastpos is None or lastpos!=curpos):
                curpos = self.getPosition()
                distance = max(abs(curpos[0]-x), abs(curpos[1]-y))
281 282
                if ((time()-t0>0.5) and max(abs(curpos[0]-initpos[0]), abs(curpos[1]-initpos[1]))<epsxy) or (time()-t0>10.):
                    print("WARNING: signal ignored:", time()-t0, x, y, curpos, initpos)
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
                    sys.stdout.flush()
                    break
                sleep(.01)
            sleep(.1)
            initpos = self.getPosition()
        if z is not None:
            self.moveZto(z, epsz)
            
    def moveZto(self, z, epsz=0.011):
        assert self.connected
        #z = round(z,2)
        while abs(self.PosZCurFloatMan.GetSingleValueAsDouble()[1]-z)>epsz:
            initpos = self.PosZCurFloatMan.GetSingleValueAsDouble()[1]
            t0 = time()
            self.PosZFloatMan.SetValue(z)
            distance = 2*epsz
            while distance > epsz:
                self.PosZCurFloatMan.Update()
                curpos = self.PosZCurFloatMan.GetSingleValueAsDouble()[1]
                distance = abs(curpos-z)
                if ((time()-t0>0.5) and abs(curpos-initpos)<epsz)  or (time()-t0>10.):
                    print("WARNING: signal z ignored:", time()-t0)
                    sys.stdout.flush()
                    break
                sleep(.01)
            sleep(.1)
        
    def saveImage(self, fname):
        assert self.connected
        self.ImageNameMan.SetValue(fname)
        self.ImageSaveMan.OperateTrigger()
        sleep(.1)
Josef Brandt's avatar
Josef Brandt committed
315

316
        
317
    def getImageDimensions(self, mode = 'df'):
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        """ Get the image width and height in um and the orientation angle in degrees.
        """
        assert self.connected
        width, height = self.ImageWidthMan.GetValue(), self.ImageHeightMan.GetValue()
        angle = self.ImageRotationMan.GetValue()
        return width, height, angle
    
    def startSinglePointScan(self):
        assert self.connected
        self.SequencerStartTrigger.OperateTrigger()
        # Wait until sequencer has finished
        while True:
            self.SequencerBusyStatus.Update()
            Busy = self.SequencerBusyStatus.GetSingleValueAsInt()[1]
            if not Busy:
                break
            
335
    def initiateMeasurement(self, ramanSettings):
336
        assert self.connected
Josef Brandt's avatar
Josef Brandt committed
337 338 339 340 341 342 343 344 345
        if not self.advancedInterface:
            self.initiateTimeSeriesMeasurement(ramanSettings)
        else:
            self.initateSilentSpectrumAcquisition(ramanSettings)
        
    def triggerMeasurement(self, num):
        if not self.advancedInterface:
            self.triggerTimeSeriesMeasurement(num)
        else:
346 347
            if self.doAutoFocus:
                self.doSpectralAutoFocus()
Josef Brandt's avatar
Josef Brandt committed
348 349
            self.acquireSilentSpectrum(num)

Josef Brandt's avatar
Josef Brandt committed
350
    def finishMeasurement(self, aborted=False):
Josef Brandt's avatar
Josef Brandt committed
351 352 353 354 355
        if self.advancedInterface:
            state = self.BeamPathState.GetValue()
            if state == 'Raman':
                self.MicroscopeIdle.SetValue('BeamPath|SetStateVideo')
                self.BeamPathSetVideoState.OperateTrigger()
Josef Brandt's avatar
Josef Brandt committed
356 357
            if not aborted:
                self.advSpec.createSummarizedSpecFiles()
Josef Brandt's avatar
Josef Brandt committed
358 359
            
    def initiateTimeSeriesMeasurement(self, ramanSettings):
360 361 362 363 364
        self.timeseries = ramanSettings['numPoints']
        self.TimeSeriesSlowNameMan.SetValue(ramanSettings['filename'])
        self.TimeSeriesSlowNumMeasurementsMan.SetValue(ramanSettings['numPoints'])
        self.TimeSeriesSlowNumAccumulationsMan.SetValue(ramanSettings['Accumulations'])
        self.TimeSeriesSlowIntTimeMan.SetValue(ramanSettings['IntegrationTime (s)'])
365 366 367
        self.TimeSeriesSlowModeMan.SetValueNumeric(0)
        self.TimeSeriesSlowNumMan.SetValue(0)
        self.TimeSeriesSlowStartMan.OperateTrigger()
Josef Brandt's avatar
Josef Brandt committed
368
    
369 370 371 372 373 374 375 376 377 378 379 380
        sleep(0.1)
        t1 = time()
        while True:
            self.TimeSeriesSlowActivityMan.Update()
            valid, act = self.TimeSeriesSlowActivityMan.GetSingleValueAsString()
            if act=="Waiting for next Measurement":
                break
            else:
                sleep(0.02)
                if time()-t1>3.:
                    print("Waiting for measurement ready...")
                    t1 = time()
Josef Brandt's avatar
Josef Brandt committed
381 382 383
    
    def initateSilentSpectrumAcquisition(self, ramanSettings):
        self.advSpec.setDatasetPath(ramanSettings['path'])
384 385 386
        
        self.doAutoFocus = ramanSettings['Autofocus']
        
Josef Brandt's avatar
Josef Brandt committed
387 388 389 390 391 392 393 394 395
        self.advSpec.createTmpSpecFolder()
        state = self.BeamPathState.GetValue()
        if state == 'Video':
            self.MicroscopeIdle.SetValue('BeamPath|SetStateRaman')
            self.BeamPathSetRamanState.OperateTrigger()
        self.SingleSpecIntegrationTimeMan.SetValue(ramanSettings['IntegrationTime (s)'])
        self.SingleSpecNumAccsMan.SetValue(ramanSettings['Accumulations'])
    
    def triggerTimeSeriesMeasurement(self, num):
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
        assert self.timeseries
        self.TimeSeriesSlowNextMan.OperateTrigger()
        # Wait until sequencer has finished
        sleep(0.1)
        t1 = time()
        while True:
            ind = self.TimeSeriesSlowIndNextMan.GetValue()
            if ind>num:
                break
            else:
                sleep(0.02)
                if time()-t1>3.:
                    print("Waiting for next index...")
                    t1 = time()
        while True:
            valid, act = self.TimeSeriesSlowActivityMan.GetSingleValueAsString()
            if act=="Waiting for next Measurement":
                break
            else:
                sleep(0.02)
                if time()-t1>3.:
                    print("Waiting for measurement ready...")
                    t1 = time()
        if num==self.timeseries-1:
            self.timeseries = False
Josef Brandt's avatar
Josef Brandt committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    
    def doSpectralAutoFocus(self):
        assert self.advancedInterface
        self.AutoFocusMan.OperateTrigger()
        self.waitForSequencerToFinish()
    
    def acquireSilentSpectrum(self, num):
        assert self.advancedInterface
        self.DoSilentSpectrum.SetValue(True)
        self.SequencerStartTrigger.OperateTrigger()
        self.waitForSequencerToFinish()
        
        out_spec = self.SilentSpecText.GetValue()
        out_spec = out_spec.split('\n')
        self.advSpec.registerNewSpectrum(out_spec, num)
        return out_spec
    
    def waitForSequencerToFinish(self):
        while True:
            self.SequencerBusyStatus.Update()
            Busy = self.SequencerBusyStatus.GetSingleValueAsInt()[1]
            if not Busy:
                break