-
Notifications
You must be signed in to change notification settings - Fork 0
/
moviemaker.py
executable file
·186 lines (147 loc) · 5.27 KB
/
moviemaker.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
#!/usr/bin/env python
import astropy, sys, time, os, subprocess, tempfile
import astropy.io.fits, scipy.interpolate
import numpy
from trm import ultracam
import rashley_utils as utils
import classes
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import Image,ImageDraw,ImageFont
def getNextFrame():
global rdat, frameCounter, frameMJD, nxmax, nymax, numWindows, frameInfo
try:
ccdFrame = rdat()
except:
print "There was an error reading the next frame."
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("(%d, %d), %d"%(nxmax, nymax, numWindows))
frameInfo.channel = 'r'
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(str(frameInfo))
timeInfo = rdat.time(frameCounter)
frameMJD = frameR.time.mjd
debug.write("Frame:" + str(frameCounter) + " MJD:" + str(frameMJD), level = 1)
frameWindows = []
for i in frameR._data:
frameWindows.append(i.data)
for i in frameG._data:
frameWindows.append(i.data)
for i in frameB._data:
frameWindows.append(i.data)
return frameWindows
config = utils.readConfigFile()
debug = classes.debugObject(1)
debug.toggleTimeLog()
if (len(sys.argv) < 2):
print "Please give me a run name."
sys.exit()
runName = sys.argv[1]
runFilename = utils.addPaths(config.ULTRACAMRAW, runName)
startFrame = 1
requestedNumFrames = -1
CCDside = 0
keepTmpFiles = False
frameInfo = classes.FrameObject()
tmpMoviePath = config.MOVIE_TMP_PATH
movieFilename = utils.addPaths(config.SITE_PATH, runName)
movieFilename+= "movie.mp4"
for i in sys.argv[2:]:
if i[:2]=="-n":
debug.write("command line: number of frames:" + i[2:])
framesStr = i[2:]
requestedNumFrames = int(framesStr)
if i[:2]=="-d":
debug.write("command line: debugLevel:" + i[2:])
debugLevel = int(i[2:])
debug.setLevel(debugLevel)
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]=="-c":
if (i[2:]=='r'):
CCDside = 1
print "CCD right side"
if i[:2]=="-k":
keepTmpFiles = True
date = utils.separateRunNameAndDate(runName)[0]
newFolder = utils.addPaths(config.MOVIE_TMP_PATH, date)
print "Creating folder:", newFolder
subprocess.call(["mkdir", newFolder])
rdat = ultracam.Rdata(runFilename, startFrame, server=False)
numFrames = rdat.ntotal()
frameMJD = 0
if requestedNumFrames==-1: requestedNumFrames=numFrames
debug.write("Total frames in %s: %d"%(runName,numFrames))
debug.write("nBlue: " + str(rdat.nblue))
frameCounter = 0
colours = ['r','g','b']
for i in range(requestedNumFrames):
if (frameCounter<numFrames):
frameCounter+= 1
else:
break
frameWindows = getNextFrame()
rgbFrames = []
for i, colour in enumerate(colours):
# Construct full frame from Windows
fullFrame = numpy.zeros((frameInfo.nxmax, frameInfo.nymax))
for j in range(frameInfo.numWindows):
frameImage = frameWindows[j + frameInfo.numWindows*i]
frameImage = numpy.rot90(frameImage)
# All windows need to be rotated by 180 degrees before using the Image library to write to png
xll = frameInfo.getWindow(j).xll
yll = frameInfo.getWindow(j).yll
xsize = frameInfo.getWindow(j).xsize
ysize = frameInfo.getWindow(j).ysize
rotatedImage = numpy.flipud(frameImage)
normalisedImage = utils.percentiles(rotatedImage, 10, 98)
fullFrame[xll:xll+xsize, yll:yll+ysize] = fullFrame[xll:xll+xsize, yll:yll+ysize] + normalisedImage
fullFrame = numpy.fliplr(fullFrame)
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)
if (colour=="b"):
if ((frameCounter % rdat.nblue)!= 0):
debug.write("Skipping an empty blue frame...")
if (frameCounter==1):
# This is the first frame, so it is ok to just use the first blue frame, which is readout noise
blueChannelHolder = img.copy()
else:
# Use last good blue image
img = blueChannelHolder
else:
# This was a 'real' blue image so use it and keep it
blueChannelHolder = img.copy()
if (keepTmpFiles==True):
outputFilename = utils.addPaths(config.MOVIE_TMP_PATH,runName) + str(frameCounter).zfill(5) + colour + "."
caption = str(frameCounter).zfill(5) + " : " + str(frameMJD) + " [" + colour + "]"
utils.writePNG(img, outputFilename + "png", caption)
rgbFrames.append(img)
img = Image.merge("RGB", (rgbFrames[0],rgbFrames[1],rgbFrames[2]))
caption = str(frameCounter+startFrame-1).zfill(5) + " : " + str(frameMJD) + " [rgb]"
outputFilename = utils.addPaths(config.MOVIE_TMP_PATH,runName) + str(frameCounter).zfill(5) + "rgb"
utils.writePNG(img, outputFilename, caption)
print "Written %d png files to %s folder"%(frameCounter, config.MOVIE_TMP_PATH)
pngPath = utils.addPaths(config.MOVIE_TMP_PATH, date)
pngFiles = pngPath + "/*.png"
subprocess.call(["mencoder", "mf://" + pngFiles, "-o", movieFilename, "-fps", "5", "-ovc", "lavc"])
subprocess.call(["rm", "-f", pngPath + "/*"])