forked from drphilmarshall/HumVI
/
compose.py
executable file
·281 lines (218 loc) · 8.97 KB
/
compose.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
# =====================================================================
# Globally useful modules:
import numpy
import sys,getopt
from PIL import Image
import humvi
import os
# =====================================================================
def compose(argv):
"""
NAME
compose.py
PURPOSE
Make a color composite PNG image from 3 FITS images, using the Lupton
algorithm. Part of the HumVI package.
COMMENTS
Reads filter name and zero point from header and tries to set scales
automatically - unless scales are set on command line. Note
telescope/survey has to be recognised for this to work...
USAGE
compose.py [flags] red.fits green.fits blue.fits
FLAGS
-h Print this message
-v Verbose operation
-b --subtract-background
INPUTS
red.fits etc Names of FITS files containing image data
OPTIONAL INPUTS
-s --scales 3,2,4 Comma-separated scales for R,G,B channels [None]
-p --parameters 5,0.05 Non-linearity parameters Q,alpha
-o --output outfile Name of output filename [guessed]
-x --saturate-to style Saturation method, color or white [white]
-z --offset 0.1 Offset image level (+ve = gray background)
-m --mask -1.0 Mask images below level [don't]
OUTPUTS
stdout Useful information
outfile Output plot in png format
EXAMPLES
Note the different alpha required, to cope with survey depth!
CFHTLS:
compose.py -v -s 0.4,0.6,1.7 -z 0.0 -p 1.7,0.09 -m -1.0 \
-o examples/CFHTLS-test_gri.png \
examples/CFHTLS-test_i.fits \
examples/CFHTLS-test_r.fits \
examples/CFHTLS-test_g.fits
PS1 (Needs optimizing on a larger image.):
compose.py -v -s 0.6,0.6,1.7 -z 0.0 -p 1.7,0.00006 -m -1.0 \
-o examples/PS1-test_riz.png \
examples/PS1-test_z.fits \
examples/PS1-test_i.fits \
examples/PS1-test_r.fits
DES (Experimental. No images checked in.):
compose.py -v -s 1.0,1.2,2.8 -z 0.0 -p 1.0,0.03 -m -1.0 \
-o examples/DES-test_gri.png \
examples/DES-test_i.fits \
examples/DES-test_r.fits \
examples/DES-test_g.fits
VICS82:
compose.py -v -s 1.0,1.4,2.0 -z 0.0 -p 1.5,0.4 -m -1.0 \
-o examples/VICS82-test_iJKs.png \
examples/VICS82-test_Ks.fits \
examples/VICS82-test_J.fits \
examples/VICS82-test_i.fits
KiDS:
compose.py -v -s 0.4,0.6,1.7 -z 0.0 -p 1.5,0.02 -m -1.0 \
-o examples/KiDS-test_gri.png \
examples/KiDS-test_i.fits \
examples/KiDS-test_r.fits \
examples/KiDS-test_g.fits
BUGS
- Renormalized scales will not be appropriate if one image is in very
different units to another, or if images are in counts, not counts per
second or AB maggies.
- Code currently assumes one file per channel, whereas we might want to
use N>3 images in combination. The image to channel transformation
would be performed after scaling to flux units but before
stretching. Masking would need to be done at this point too.
- Should cope with just two, or even one, input image file(s).
HISTORY
2012-05-11 started Marshall (Oxford)
2012-07-?? integrated with wherry.py Sandford (NYU)
2012-12-19 defaults set for CFHTLS images Marshall (Oxford)
2013-02-21 defaults set for PS1 images Marshall (Oxford)
2013-02-26 experimenting with DES images Hirsch (UCL)
"""
# -------------------------------------------------------------------
try:
opts, args = getopt.getopt(argv, "hvp:s:n:o:x:z:blwm:",\
["help","verbose","scales","pars","output","saturate-to","offset","subtract-background","lupton","wherry","mask"])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
print compose.__doc__
return
vb = False
outfile = "color.png"
# Defaults optimized for CFHTLS...
pars = '1.7,0.09'
scales = '0.4,0.6,1.7'
# More general sensible choices:
backsub = False
saturation = 'white'
offset = 0.0
mask = False
masklevel = -1.0
for o,a in opts:
if o in ("-h", "--help"):
print compose.__doc__
return
elif o in ("-v", "--verbose"):
vb = True
elif o in ("-p","--parameters"):
pars = a
elif o in ("-s","--scales"):
scales = a
elif o in ("-x","--saturate-to"):
saturation = a
elif o in ("-z","--offset"):
offset = float(a)
elif o in ("-m","--mask"):
mask = True
masklevel = float(a)
elif o in ("-o","--output"):
outfile = a
elif o in ("-b","--subtract-background"):
backsub = True
else:
assert False, "Unhandled option"
# Check for datafiles in array args:
print len(args)
if len(args) == 3:
rfile = args[0]
gfile = args[1]
bfile = args[2]
if vb:
print "Making color composite image of data in following files:",rfile,gfile,bfile
print "Output will be written to",outfile
if mask: print "Masking stretched pixel values less than",masklevel
else:
print compose.__doc__
return
# Parse nonlinearity parameters:
Qs,alphas = pars.split(',')
Q = float(Qs)
alpha = float(alphas)
# Parse channel colour scales:
x,y,z = scales.split(',')
rscale,gscale,bscale = float(x),float(y),float(z)
# -------------------------------------------------------------------
# Read in images, calibrated into flux units:
band3 = humvi.channel(rfile)
band2 = humvi.channel(gfile)
band1 = humvi.channel(bfile)
# Check shapes are equal:
humvi.check_image_shapes(band1.image,band2.image,band3.image)
# Subtract backgrounds (median, optional):
if backsub:
band1.subtract_background()
band2.subtract_background()
band3.subtract_background()
# -------------------------------------------------------------------
# BUG: as it stands, this code assumes one file one channel, whereas
# in practice we might like to be able to make composites based on
# N bands. Need to overload + operator for channels? Calib etc will
# need altering as well as image.
red = band3
green = band2
blue = band1
# -------------------------------------------------------------------
# Set scales determining color balance in composite:
rscale,gscale,bscale = humvi.normalize_scales(rscale,gscale,bscale)
red.set_scale(manually=rscale)
green.set_scale(manually=gscale)
blue.set_scale(manually=bscale)
if vb: print 'Scales normalized to:',red.scale,green.scale,blue.scale
# Scale images - only do once:
red.apply_scale()
green.apply_scale()
blue.apply_scale()
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Stretch images to cope with high dynamic range:
if vb:
print "Stretch parameters Q,alpha:",Q,alpha
print "At low surface brightness levels, the channel images are further rescaled by alpha"
print "Nonlinearity sets in at about 1/Q*alpha in the scaled intensity image:",1.0/(Q*alpha)
# Compute total intensity image and the arcsinh of it:
I = humvi.lupton_intensity(red.image,green.image,blue.image,type='sum')
stretch = humvi.lupton_stretch(I,Q,alpha)
# Apply stretch to channel images:
r = stretch * red.image
g = stretch * green.image
b = stretch * blue.image
if mask:
# Mask problem areas - exact zeros or very negative patches should
# be set to zero.
# BUG: this should have been done after scaling but before conversion
# to channels, as its the individual images that have problems...
r,g,b = humvi.pjm_mask(r,g,b,masklevel)
# Offset the stretched images to make zero level appear dark gray.
# Negative offset makes background more black...
r,g,b = humvi.pjm_offset(r,g,b,offset)
if saturation == 'color':
# Saturate to colour at some level - might as well be 1, since
# Q redefines scale?:
threshold = 1.0
r,g,b = humvi.lupton_saturate(r,g,b,threshold)
# Otherwise, saturate to white.
# Package into a python Image, and write out to file:
image = humvi.pack_up(r,g,b)
image.save(outfile)
# ======================================================================
if vb: print "Image saved to:",outfile
return
# ======================================================================
if __name__ == '__main__':
compose(sys.argv[1:])
# ======================================================================