-
Notifications
You must be signed in to change notification settings - Fork 2
/
MultiExposure.py
162 lines (129 loc) · 4.62 KB
/
MultiExposure.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
import numpy
import pylibapogee.pylibapogee as apg
import pylibapogee.pylibapogee_setup as SetupDevice
import pyfits
import re
import logging
import sys
import traceback
import datetime
import time
from multiprocessing import Pool
def GetCamConnections( ):
print "Trying to find and connect with camera"
#look for usb cameras first
devices = SetupDevice.GetUsbDevices()
# no usb cameras, then look for ethernet cameras
if( len(devices) == 0 ):
devices = SetupDevice.GetEthernetDevices()
# exception....no cameras anywhere....
if( len(devices) == 0 ):
raise RuntimeError( "No devices found on usb or ethernet" )
# connect to the first camera
cams=map( lambda i: SetupDevice.CreateAndConnectCam( devices[i] ), range(len(devices)))
for cam in cams:
print cam.GetSerialNumber()
return cams
def GetCamConnection( num ):
print "Trying to find and connect with camera"
#look for usb cameras first
devices = SetupDevice.GetUsbDevices()
# no usb cameras, then look for ethernet cameras
if( len(devices) == 0 ):
devices = SetupDevice.GetEthernetDevices()
# exception....no cameras anywhere....
if( len(devices) == 0 ):
raise RuntimeError( "No devices found on usb or ethernet" )
# connect to the first camera
return SetupDevice.CreateAndConnectCam( devices[num] )
def GetCameraInfo( obj ):
excludelist = [
"GetImage", # readout
"GetInfo", # contains duplicated infomation
"GetStatus", # contains duplicated infomation
"GetStatusStr", # contains duplicated infomation
"GetUsbFirmwareVersion", #
"GetMacAddress", #
"GetUsbVendorInfo", #
"GetAdcGain", #
"GetAdcOffset", #
"GetSerialBaudRate", #
"GetSerialFlowControl", #
"GetSerialParity", #
"GetUsbVendorInfo", #
]
wildcard=r"(^Get)|(^Is)"
p=re.compile(wildcard)
getmethod = filter(lambda x: True if p.match(x) is not None else False, dir(obj))
getmethod = filter(lambda x: False if x in excludelist else True, getmethod)
ccdinfo = []
for method in getmethod:
try:
ret = getattr(obj,method)()
logging.debug("%s: %s" % ( method, ret ) )
ccdinfo.append((re.sub(wildcard, "", method),ret))
except TypeError as e:
logging.warn(traceback.format_exc())
except RuntimeError as e:
logging.warn(traceback.format_exc())
except ValueError as e:
logging.warn(traceback.format_exc())
except:
raise
return ccdinfo
def camprocess( camid, filename, exposeTime, extraheader, shutter ):
# print caminfo
# (cam, camid) = caminfo
cam = camid
#print some basic info
print cam, camid
row = cam.GetMaxImgRows()
# col = cam.GetMaxImgCols()
col = cam.GetMaxImgCols() + cam.GetNumOverscanCols()
cam.SetRoiNumCols(cam.GetMaxImgCols() + cam.GetNumOverscanCols())
logging.info("Imaging rows = %d, columns = %d" % ( row, col ))
count = 1
cam.SetImageCount( count )
logging.info("Starting %f sec light exposure" % (exposeTime) )
expdatetime = datetime.datetime.utcnow()
cam.StartExposure( exposeTime, shutter )
# cam.StartExposure( exposeTime, False )
time.sleep( exposeTime )
status = None
while status != apg.Status_ImageReady:
status = cam.GetImagingStatus()
logging.info("cam.GetImagingStatus() = %d" % status)
if( apg.Status_ConnectionError == status or
apg.Status_DataError == status or
apg.Status_PatternError == status ):
msg = "Run %s: FAILED - error in camera status = %d" % (runStr, status)
raise RuntimeError( msg )
time.sleep(1)
logging.info("Getting image")
data = cam.GetImage()
logging.info("Saving image to file: %s" % ( filename ))
imgName = "object"
header = pyfits.Header( [('DATE-OBS', expdatetime.strftime("%Y-%m-%d"), "") ,
('UT', expdatetime.strftime("%H:%M:%S"), ""),
('EXPTIME', exposeTime, "Exposure Time") ] )
if extraheader is not None:
header.extend(extraheader)
header.extend(GetCameraInfo(cam))
pyfits.writeto( filename, data.reshape((row,col)), header=header )
logging.info( "mean and std = %lf, %lf" % ( data.mean(), data.std() ))
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',)
# datefmt='%a, %d %b %Y %H:%M:%S',
# filename='/temp/myapp.log',
# filemode='w')
cams = GetCamConnections()
for i in range(3):
time.sleep(5)
pool=Pool(len(cams))
# pool.apply(camprocess,zip(cams,range(len(cams))))
pool.map(camprocess,range(len(cams)))
# pool.map(test,range(len(cams)))
pool.close()
for i in range(3):
cams[i].CloseConnection()