-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.py
More file actions
executable file
·134 lines (108 loc) · 3.73 KB
/
camera.py
File metadata and controls
executable file
·134 lines (108 loc) · 3.73 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
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
#! /usr/bin/python
"""
camera.py
Camera imaging and fits routines using input from camera.cpp
"""
__author__ = "John Armstrong"
__copyright__ = "NA"
__credits__ = ["Joseph Huehnerhoff"]
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "NA"
__email__ = "NA"
__status__ = "Developement"
import numpy as np
import pyfits
import subprocess
import time
import os
import thread
import traceback
class CameraExpose(object):
def __init__(self):
self.wait = 1.0
self.status = None
self.ssag = os.getcwd()+"/camera"
self.statusDict = {1:'idle', 2:'expose', 3:'reading'}
self.gain = 1
def expose(self, name, exp, dir, gain):
thread.start_new_thread(self.runExpose, (name, exp, dir, gain))
def runExpose(self, name, exp, dir, gain):
"""
Connect to the OpenSSAG and take image
input a given file name and exposure
output whether the image was successful
Tells camera to take an image, it will output a binary file named "test" with 1000 ms exposure.
Can also use './camera test 0 0' to check camera.
"""
if dir == None:
dir = os.getcwd()
if '.fit' not in name:
name = name+'.fits'
name = dir+'/'+str(name)
#print dir, name, self.ssag, exp
expose = float(exp)*1000
if gain == None:
gain = self.gain
try:
subprocess.Popen([self.ssag, 'image', 'binary', str(expose), str(gain)])
self.status = 2
#Pause for the camera to run
time.sleep(self.wait+float(exp))
self.status = 1
binary=np.fromfile('binary',dtype='u1').reshape(1024,1280)
# --------------------------
# Used for testing array procedure, can remove once program is tested on-sky.
# print binary.shape
# print binary.dtype.name
# print binary
# ---------------------------
prihdr = self.createHeader(exp, gain) #create emtpy header information
hdu=pyfits.PrimaryHDU(binary, header = prihdr) #create a primary header file for the FITS image
hdulist=pyfits.HDUList([hdu])
prihdr['EXPTIME'] = str(exp)
prihdr['IMAGTYP'] = 'guide'
# Write the image and header to a FITS file using variable name.
name = self.checkFile(name)
hdulist.writeto(name, clobber=True)
#im = Image.fromarray(binary)
#im.save("tmp.jpg")
#self.l.logStr('SaveIm\t%s' % name)
return True
except Exception,e:
print ("failed")
print (str(e))
traceback.print_exc()
return False
def checkFile(self, fileName):
if os.path.exists(fileName):
name = fileName.replace('.fits','')+time.strftime('_%Y%m%dT%H%M%S.fits')
return name
else:
return fileName
def createHeader(self, exp, gain):
prihdr = pyfits.Header()
prihdr['COMMENT'] = 'MRO Guider Camera'
prihdr['COMMENT'] = 'Orion Star Shoot Auto Guider'
prihdr['IMAGTYP'] = None
prihdr['EXPTIME'] = exp
prihdr['CCDBIN1'] = 1
prihdr['CCDBIN2'] = 1
prihdr['GAIN'] = gain
prihdr['RN'] = None
return prihdr
def checkStatus(self):
print ("return some status message")
print (self.status, self.statusDict[self.status])
return self.status
def checkConnection(self):
try:
subprocess.Popen([self.ssag, '0', '0', '0'])
except Exception, e:
print (e)
def help(self):
print (__doc__)
return
if __name__=="__main__":
c = CameraExpose()
c.runExpose('test',0.1, None, 8)