-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloudCam.py
More file actions
executable file
·206 lines (176 loc) · 6.57 KB
/
Copy pathCloudCam.py
File metadata and controls
executable file
·206 lines (176 loc) · 6.57 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
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
169
170
171
172
173
174
175
176
177
178
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
205
206
#! /usr/bin/python
"""
CloudCam.py
Operates the CloudCam hardware and analysis software
TODO:
Usage:
Automatic camera operation and data analysis from command line:
python CloudCam.py
Live data is displayed by opening a web browser and looking at
the IP of the CloudCam
"""
import numpy as np
from graphCloud import *
from camera import *
import datetime
from clouduino_interface import ClouduinoInterface
import os
from CloudParams import *
import traceback
class CloudCam(object):
def __init__(self):
"""
Options:
self.min (minimum median value for exposure control)
self.max (maximum median value for exposure control)
self.step (What percent the exp value changes under exposure control)
self.expose (starting exposure length [s])
self.dir (where are the .fits images saved)
self.gain (camera gain setting)
self.filterpos (where is the filter arm? 0 = out, 1 = in)
"""
self.debug = False
self.min = min_median
self.max = max_median
self.step = step_size
self.expose = expose
self.dir = os.path.join(os.getcwd(),'images')
self.dayDir = None
self.gain = gain
self.gainmax = gainmax
self.maxExp = max_exp
self.backupFile = "backupParams.txt"
self.cg = CloudGraph()
self.c = CameraExpose()
self.ci = ClouduinoInterface()
def check_exposure(self, median):
"""
Adjusts the exposure timing and filter arm position
to try and keep the median between self.min and self.max
input:
median (median value from analysis)
"""
# Check and adjust exposure timing for low light
print "bounds (minMed, maxMed, maxGain, maxExp: ", self.min, self.max, self.gainmax, self.maxExp
if median < self.min:
if self.expose >=self.maxExp and self.gain >= 1:
self.gain += 1
if self.gain > self.gainmax:
self.gain = self.gainmax
print ("Gain Set To: "+str(self.gain))
if self.expose <= self.maxExp and self.gain == self.gainmax:
self.expose = self.expose*(1.0+self.step)
print ("Exposure too short, increasing to: "+str(self.expose)+" seconds")
# Check and adjust exposure and gain for high light
elif median > self.max:
if self.expose >=0.02 and self.gain > 1:
self.gain -= 1
print ("Gain Set To: "+ str(self.gain))
if self.expose >=0.02 and self.gain == 1:
self.expose = self.expose*(1.0-self.step)
print ("Exposure too long, decreasing to: "+str(self.expose)+" seconds")
else:
print ("Exposure within bounds")
if self.expose < 0.02:
self.expose = 0.02
if self.expose > self.maxExp:
self.expose = self.maxExp
try:
backupParams = str(self.expose)+", "+str(self.gain)
backup = open(self.backupFile, "w")
backup.write(backupParams)
backup.close()
except:
print("Could not write backup file")
return
def checkDir(self):
"""
This function checks for needed image storage directories
and creates them if necessary
"""
dayDir = time.strftime("%Y%m%d", time.gmtime())
#Check for fits image storage folder for today, make if needed
if not os.path.isdir(os.path.join(os.getcwd(), 'images', dayDir)):
os.mkdir(os.path.join(os.getcwd(), 'images', dayDir))
print ('directory made: ', os.path.join(os.getcwd(), 'images', dayDir))
#Check for analyzed image storage folder for today, make if needed
if not os.path.isdir(os.path.join(os.getcwd(), 'analyzed', dayDir)):
os.mkdir(os.path.join(os.getcwd(), 'analyzed', dayDir))
print ('directory made: ', os.path.join(os.getcwd(), 'analyzed', dayDir))
return dayDir
def run_camera(self):
"""
Take and analyze image, check exposure after analysis
"""
dayDir = os.path.join(os.getcwd(),'images',self.checkDir())
name = time.strftime("%Y%m%dT%H%M%S")+"_"+str('%.3f'%(self.expose))
#Remove the old image binary file
if os.path.isfile('binary'):
os.remove('binary')
try:
backupParams = np.genfromtxt(self.backupFile, delimiter=",")
self.expose = backupParams[0]
self.gain = backupParams[1]
#os.remove(self.backupFile)
except:
print("Could not load backup file")
#Try to take an image
try:
self.takeImage("cloud", name+".fits", self.expose, dayDir)
except:
traceback.print_exc()
if self.debug != True:
time.sleep(self.expose+2) # go to sleep while the image is taken
#Run the analysis and check the exposure timing
try:
median = cg.run_analysis(os.path.join(dayDir,name), self.expose, self.gain)
self.check_exposure(median)
except:
traceback.print_exc()
#self.expose = 1.0
if self.debug != True:
if self.expose < 60:
print (str(60-self.expose)+" seconds till next exposure")
time.sleep(60-self.expose)
return
def takeImage(self, imType = None, imgName = None, imExp = None, imDir = None):
"""Takes the class, a string keyword for image type, a string for image
name, an integer for exposure tiem, and a string for directory name.
The function checks if the variable fakeOut is equal to true first
-- if so, ends function and carries out rest of code using existing
images. If not, the function takes an image using the CameraExpose
object defined in the constructor, and checks that the image has
been taken and saved.
Args:
imType (str): the type of image (bias, dark, object)
imgName (str): the name of the image
imExp (str): the exposure length in seconds
imDir (str): the directory of the image to be saved
Returns:
int.
0 -- image was taken
1 -- image not taken
2 -- unknown state
Raises:
Exception
"""
self.fakeOut = False
im = False
if self.fakeOut != True:
print imExp, self.gain
im = self.c.runExpose(str(imgName), str(imExp), str(imDir), self.gain)
if im == True: # check on completion and save of image exposure
time.sleep(1)
return 0
else:
raise Exception("Image exposure not completed")
return 1
else:
return 3 # Simply returns if no exception raised
if __name__ == "__main__":
cg = CloudGraph()
cc = CloudCam()
cg.start_up_checks()
run = True
while run == True:
cc.run_camera()