-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCamHamThread.py
More file actions
76 lines (63 loc) · 2.5 KB
/
Copy pathCamHamThread.py
File metadata and controls
76 lines (63 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from camera import Camera
import subprocess
import numpy as np
import threading
from hamamatsu.dcam import dcam, Stream, copy_frame, ECCDMode
import sys
import time
class CamHamThread:
def __init__(self,exposure=1, CCDMode='Normal',SensitivityGain=1,BufferLength=100,RunUntil=20000,BufferPath="./buffer/"):
self.exposure=exposure
self.CCDMode=CCDMode
self.BufferLength=BufferLength
self.SensitivityGain=SensitivityGain
self.RunUntil=RunUntil
self.BufferPath=BufferPath
self.ImageID=0
self.sleeptime=0.2
self.thr=threading.Thread(target=self.ThreadFunction,args=())
self.thr.daemon = True
self.thr.start()
self.running=True
time.sleep(5)
def BufferID(self,n=0):
return (self.ImageID-n)%self.BufferLength
def ThreadFunction(self):
with dcam:
camera = dcam[0]
with camera:
camera["exposure_time"] = self.exposure
if(self.CCDMode=='Normal'):
camera['ccd_mode']=ECCDMode.NORMALCCD
elif(self.CCDMode=='EMCCD'):
camera['ccd_mode']=ECCDMode.EMCCD
camera['readout_speed'] = 1
camera['sensitivity']=self.SensitivityGain
with Stream(camera, self.RunUntil) as stream:
camera.start()
for i, frame_buffer in enumerate(stream):
frame = copy_frame(frame_buffer)
np.savetxt(self.BufferPath+"buffer"+str(self.BufferID())+'.txt',frame)
self.ImageID=self.ImageID+1
self.running=False
def Snap(self, n):
if(not self.running):
print("Camera not running! Restart it!")
return False
returnvec=[]
for i in range(0,n):
returnvec.append(self.SnapOne())
return returnvec
def SnapOne(self,waitforit=True):
presentbuffer=self.ImageID
if(waitforit):
while(self.ImageID==presentbuffer):
time.sleep(self.exposure)
BufferIDToLoad=self.BufferID()
time.sleep(self.exposure)
return np.loadtxt(self.BufferPath+"buffer"+str(BufferIDToLoad)+'.txt')
def GetWholeBuffer(self):
returnvec=[]
for i in range(0, self.BufferLength):
returnvec.append(np.loadtxt(self.BufferPath+"buffer"+str(self.BufferID(i))+'.txt'))
return returnvec