/
objecttracker.py
executable file
·246 lines (194 loc) · 6.72 KB
/
objecttracker.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python
import astropy, sys, time, os, subprocess, tempfile
import astropy.io.fits
from scipy import *
import numpy, json
from trm import ultracam
from trm.ultracam.UErrors import PowerOnOffError, UendError, UltracamError
import rashley_utils as utils
import classes
import Image,ImageDraw
def getNextFrame():
global rdat, frameCounter, actualFrame, frameMJD, nxmax, nymax, numWindows, frameInfo, goodTime
try:
ccdFrame = rdat()
except UltracamError:
print "There was an error reading the next frame."
print UltracamError
return
frameR = ccdFrame[0]
frameG = ccdFrame[1]
frameB = ccdFrame[2]
if (frameCounter == 1):
# Work out the window structure for this run...
nxmax = frameR.nxmax
nymax = frameR.nymax
numWindows = len(frameR)
debug.write("Frame dimensions: (%d, %d), Num windows %d"%(nxmax, nymax, numWindows), level = 2)
frameInfo.channel = channel
frameInfo.nxmax = nxmax
frameInfo.nymax = nymax
for nwin,win in enumerate(frameR._data):
frameInfo.addWindow(win.llx, win.lly, win.nx, win.ny)
debug.write(frameInfo, level = 2)
frameMJD = frameR.time.mjd
goodTime = frameR.time.good
#timeTest = rdat.time()
#timeTest = rtime()
#debug.write(timeTest)
debug.write("Frame: " + str(frameCounter) + "[" + str(actualFrame) + "]" + " MJD: " + str(frameMJD), level = 1)
frameWindows = []
if (channel == 'r'):
for i in frameR._data:
frameWindows.append(i.data)
if (channel == 'g'):
for i in frameG._data:
frameWindows.append(i.data)
if (channel == 'b'):
for i in frameB._data:
frameWindows.append(i.data)
return frameWindows
def updateMasterObjectList(objects, windowObject):
global frameMJD, masterObjectList
for ob in objects:
# add x, y offset for this window
debug.write("sextractor x, y (%d, %d)"%(int(ob['x']),int(ob['y'])), level = 3)
ob['x'] = ob['x'] + windowObject.xll
ob['y'] = ob['y'] + windowObject.yll
debug.write("actual x, y (%d, %d)"%(int(ob['x']),int(ob['y'])), level = 3)
# is this object in the master list yet?
objectRecognised = False
for eo in masterObjectList:
if (eo.isDistanceMatch(ob)!=-1):
eo.addExposureByObject(ob, frameMJD)
objectRecognised = True
break
if (not objectRecognised):
newIDNumber = utils.getUniqueID(masterObjectList)
debug.write("New object detected... assigning a new ID number: %d"%newIDNumber)
newObject = classes.ObservedObject(newIDNumber)
newObject.addExposureByObject(ob, frameMJD)
masterObjectList.append(newObject)
config = utils.readConfigFile()
debug = classes.debugObject(config.DEBUG)
if (len(sys.argv) < 2):
print "Please give me a run name."
sys.exit()
runName = sys.argv[1]
runFilename = utils.addPaths(config.ULTRACAMRAW, runName)
channel = 'r'
startFrame = 1
requestedNumFrames = 10
keepTmpFiles = False
frameInfo = classes.FrameObject()
for i in sys.argv[2:]:
if i[:2]=="-n":
print "number of frames:", i[2:]
framesStr = i[2:]
requestedNumFrames = int(framesStr)
if i[:2]=="-s":
print "starting at frame:", i[2:]
framesStr = i[2:]
startFrame = int(framesStr)
if i[:2]=="-o":
print "output file:", i[2:]
outputFilename = i[2:]
if i[:2]=="-k":
keepTmpFiles = True
if i[:2]=="-c":
channel = i[2:]
if i[:2]=="-d":
debugLevel = i[2:]
debug.setLevel(debugLevel)
outputFilename = utils.addPaths(config.SITE_PATH,runName) + "deepimage" + channel + "."
debug.toggleTimeLog()
rdat = ultracam.Rdata(runFilename, startFrame, server=False)
numFrames = rdat.ntotal()
if (requestedNumFrames==-1): requestedNumFrames = numFrames;
frameMJD = 0
debug.write("Total frames in %s: %d"%(runName,numFrames))
debug.write("nBlue: " + str(rdat.nblue))
masterObjectList = []
summedFrames = []
frameCounter = 0
firstFrame = True
goodTime = False
for i in range(requestedNumFrames):
actualFrame = frameCounter + startFrame
if (actualFrame<numFrames):
frameCounter+= 1
else:
break
frameWindows = getNextFrame()
if frameWindows == None:
print "We couldn't read the next frame... exiting the loop"
break
if goodTime == False:
print "Timing error.... skipping the frame"
continue
if (channel == 'b') & (frameCounter % rdat.nblue != 0): # This is an empty blue frame so skip it.
continue
if (firstFrame):
for j in range(frameInfo.numWindows):
width = frameInfo.getWindow(j).xsize
height = frameInfo.getWindow(j).ysize
summedFrame = numpy.zeros((width, height))
summedFrames.append(summedFrame)
firstFrame = False
for j in range(frameInfo.numWindows):
frameImage = frameWindows[j]
utils.createFITS(frameCounter, frameImage)
utils.runSex(frameCounter)
frameImage = numpy.rot90(frameImage)
summedFrame = summedFrames[j]
summedFrame = numpy.add(summedFrame, frameImage)
summedFrames[j] = summedFrame
if (int(config.KEEP_TMP_FILES)==0):utils.removeFITS(frameCounter);
objects = utils.readSexObjects(frameCounter)
if (int(config.KEEP_TMP_FILES)==0):utils.removeCAT(frameCounter)
objects = utils.rejectBadObjects(objects)
updateMasterObjectList(objects, frameInfo.getWindow(j))
debug.write("Total objects in this window:" + str(len(objects)))
averageFrames = []
normalisedImages = []
for j in range(frameInfo.numWindows):
averageFrame = numpy.divide(summedFrames[j], frameCounter)
averageFrames.append(averageFrame)
normalisedImage = utils.percentiles(averageFrame, 25, 99)
normalisedImages.append(normalisedImage)
# Construct full frame from Windows
fullFrame = numpy.zeros((frameInfo.nxmax, frameInfo.nymax))
for j in range(frameInfo.numWindows):
xll = frameInfo.getWindow(j).xll
yll = frameInfo.getWindow(j).yll
xsize = frameInfo.getWindow(j).xsize
ysize = frameInfo.getWindow(j).ysize
rotatedImage = numpy.flipud(normalisedImages[j])
fullFrame[xll:xll+xsize, yll:yll+ysize] = fullFrame[xll:xll+xsize, yll:yll+ysize] + rotatedImage
fullFrame = numpy.fliplr(fullFrame)
debug.write("Objects detected:")
for m in masterObjectList:
debug.write(m.toString())
imgData = fullFrame
imgSize = numpy.shape(imgData)
imgLength = imgSize[0] * imgSize[1]
testData = numpy.reshape(imgData, imgLength, order="F")
img = Image.new("L", imgSize)
img.putdata(testData)
debug.write("Writing PNG file: " + outputFilename + ".png")
img.save(outputFilename + "png", "PNG")
if (int(config.WRITE_FITS)==1):
FITSFilename = outputFilename + "fits"
debug.write("Writing FITS file: " + FITSFilename, level=1)
utils.saveFITSImage(fullFrame, FITSFilename)
if (int(config.WRITE_JSON)==1):
allObjects = []
for m in masterObjectList:
allObjects.append(m.toJSON())
print m
outputFilename = utils.addPaths(config.SITE_PATH,runName)
outputFilename+= channel + ".json"
debug.write("Writing JSON file: " + outputFilename)
outputfile = open( outputFilename, "w" )
json.dump(allObjects, outputfile)
outputfile.close()