1
2
3
4 """
5 Make JPEGS of pixel data. This will make compressed images for all pixel
6 data in the input transfer log.
7
8 @author: E. Sutorius
9 @org: WFAU, IfA, University of Edinburgh
10
11 @newfield contributors: Contributors, Contributors (Alphabetical Order)
12 @contributors: R.S. Collins
13
14 @requires: PyFITS
15 """
16
17 import getopt
18 import math
19 import os
20 import re
21 import shutil
22 import sys
23
24 from wsatools.DbConnect.IngIngester import IngestLogger as Logger
25 import wsatools.FitsUtils as fits
26 import wsatools.jpeg as jpeg
27 from wsatools.Logger import ForLoopMonitor
28 from wsatools.SystemConstants import SystemConstants
29 import wsatools.Utilities as utils
30
31
32 contrast = 0.25
33 quality = 90
34 scrunch = 2
35 re_datevers = re.compile(r'((\d{8})_v(\d)(.{0,1}\d+))')
36
37
39 """
40 Creates a JPEG thumbnail image of the given FITS file.
41
42 @param fileName: Full path to the FITS file.
43 @type fileName: str
44 @param compName: Full path for the JPEG thumbnail image.
45 @type compName: str
46 @param cu2Path: Path to the CU2 directory.
47 @type cu2Path: str
48 @param dateVersStr: Date-version string of the processed file batch.
49 @type dateVersStr: str
50
51 """
52 fitsFile = fits.open(fileName)
53
54
55 arrays = [hduNo for hduNo, hdu in enumerate(fitsFile)
56 if hdu.header["NAXIS"] is 2 and
57 (hdu.header.has_key("SIMPLE")
58 or hdu.header.get("XTENSION") == "IMAGE"
59 or hdu.header.get("XTENSION") == "BINTABLE" and
60 hdu.header.has_key("ZIMAGE"))]
61
62
63 compMap = dict((str(arrayNum + 1),
64 compName + "_%s.jpg" % fitsFile[arrayNum].header["CAMNUM"])
65 for arrayNum in arrays)
66
67
68 coordsFileName = "coords-%s.tbl" % dateVersStr
69 if "TAN" in fitsFile[1].header["CTYPE1"]:
70 shutil.copy2(os.path.join(cu2Path, "skycoords.tbl"),
71 os.path.join(cu2Path, coordsFileName))
72 rotation = math.pi
73 else:
74 shutil.copy2(os.path.join(cu2Path, "earthcoords.tbl"),
75 os.path.join(cu2Path, coordsFileName))
76 rotation = 0.0
77
78
79 isOverlay = not any(part in fileName for part in ["_p1_", "_p2_", "_p3_"])
80
81 Logger.setEchoOff()
82 try:
83 Logger.addMessage("MEF = %s %r" % (fileName, arrays))
84
85
86 info = jpeg.mef2jpeg(mef=fileName, coordfile=coordsFileName,
87 scrunch=scrunch, quality=quality,
88 contrast=contrast, hdumap=compMap,
89 neoverlay=isOverlay, theta=rotation)
90
91
92 for hdu in info:
93 Logger.addMessage(compMap[hdu] + " %s %s %s" % info[hdu])
94 finally:
95 Logger.setEchoOn()
96
97
98
100 '''Make JPEGS of pixel data. This will make compressed images for all
101 pixel data in the input transfer log.
102
103 curator is a string identifying the archive curation scientist
104 that instigated this invocation;
105 xferlog is the filename of the logged successful file transfers
106 from the data processing centre.
107 outpath path where products/jpgs is created
108 comment is a string for the archive curation scientist to
109 supply an optional note for recording in the curation
110 history;
111 '''
112
113
114 try:
115 opts, args = getopt.getopt(argv[1:], "o:", ["outpath="])
116
117 except getopt.GetoptError:
118
119 print argv
120 print "usage: cu2jpeg.py [-o/--outpath path] curator xferlog comment"
121 raise SystemExit
122
123 jpgPath = None
124 for o, a in opts:
125 if o in ("-o", "--outpath"):
126 jpgPath = a
127
128 if len(args) != 3:
129 print argv
130 print "usage: cu2jpeg.py [-o/--outpath path] curator xferlog comment"
131 raise SystemExit
132
133 _curator = args[0]
134 xferlog = args[1]
135 _comment = args[2]
136
137
138 sysc = SystemConstants("WSA")
139
140
141 cuNum = 2
142 cu2Path = os.path.join(sysc.curationCodePath(), "invocations/cu2")
143 curPath = os.curdir
144 os.chdir(cu2Path)
145
146 Logger.addMessage("Running Curation Use case %s on %s" % (cuNum, xferlog))
147 try:
148 dateVersStr = re_datevers.search(xferlog).group()
149 except AttributeError:
150 dateVersStr = "99991231_v0"
151
152
153 xferlines = utils.ParsedFile(xferlog).readlines()
154 progress = ForLoopMonitor(xferlines)
155 for filename in xferlines:
156 inDisk, outDir = os.path.split(os.path.dirname(filename))
157
158 if jpgPath:
159 outDisk = jpgPath
160 elif sysc.testOutputPath() in inDisk:
161 outDisk = sysc.testOutputPath()
162 else:
163 outDisk = utils.getNextDisk(sysc)
164
165 outPath = os.path.join(outDisk, sysc.compressImDir, outDir)
166 utils.ensureDirExist(outPath)
167
168 compName = os.path.join(outPath,
169 os.path.splitext(os.path.basename(filename))[0])
170
171
172
173
174
175 try:
176 createThumbnailImage(filename, compName, cu2Path, dateVersStr)
177 except IndexError:
178 Logger.addMessage(filename +" missing required keywords. Skipping")
179 except TypeError:
180
181
182 Logger.addMessage(filename +" probably not FITS. Skipping")
183 except KeyError:
184 for i in range(1,5):
185 shutil.copy("noImage.jpg", compName + "_%s.jpg" % i)
186
187 progress.testForOutput()
188
189
190
191 logPathName = os.path.join(cu2Path, "tmplog%s_%s.log" % (cuNum,
192 os.path.splitext(os.path.basename(xferlog))[0]))
193 counter = 0
194 while os.path.exists(logPathName):
195 counter += 1
196 logPathName = \
197 ''.join(logPathName.partition(".log")[0:2]) + str(counter)
198
199 Logger.dump(file(logPathName, 'w'))
200 os.chdir(curPath)
201
202
203
204 if __name__ == '__main__':
205 main(sys.argv)
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