-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_cutouts_multiband.py
270 lines (200 loc) · 7.45 KB
/
make_cutouts_multiband.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
__version__="v0.0.1" #
"""
DES cutout demo
option to plot the weightmaps
to overplot the catalogue for an image.
cutout needs to be padded so that size is correct and the centre is
the centre requested. Maybe region file in the header.
also maybe place some lookup table informatio scaling info into header for ds9
also need to cache the fits image so that it is not read multiple times
pyfits might be doing the caching on the fly.
create pngs and a webpage.
A table and a page per source
webpage with free text input box and some tick boxes
see also fitsimage.py for making a jpeg
https://code.google.com/p/wcs2kml/
https://wcs2kml.googlecode.com/svn-history/r25/trunk/python/fitsimage.py
"""
def make_cutouts_multiband(bands='r', detband='detr',
imagefiles=None,
listfile=None, format='region', width_arcsecs=10.0,
catfiles=None):
"""
"""
import os, sys
import time
import pyfits
import pyregion
import cutout
#import fits2png
import matplotlib.pyplot as plt
#if not interactive: matplotlib.use('Agg')
import aplpy
import plotid
t0=time.time()
base=os.path.basename(listfile)
id=os.path.splitext(base)[0]
weightmap = False
# open ds9 region
if format == 'region':
region = pyregion.open(listfile)
irow=-1
# print out the cordinates in degrees
for rows in region: # iterate over table rows
irow=irow+1
print irow+1, region[irow].coord_list[0], region[irow].coord_list[1]
nrows=len(region)
if format == 'ozdes':
table= pyfits.getdata(listfile,1)
ralist = table['RA']
declist = table['Dec']
object = table['object']
comment = table['comment']
redshift = table['z_fin']
print 'ralist range: ', min(ralist), max(ralist)
print 'declist range: ', min(declist), max(declist)
nrows=len(ralist)
# Read extension 2 of the catalog file
ext=2
catalog = pyfits.getdata(catalog_path+catalog_filename,ext)
ra_catalog = catalog['alphawin_j2000']
dec_catalog = catalog['deltawin_j2000']
title=imagefiles
#plt.text(.5, .95, title, horizontalalignment='center')
# leave some space to add annotation at the top of the page
plt.rc('figure.subplot', left=0.05, right=0.95, bottom=0.05, top=0.95,
wspace=0.0, hspace=0.0)
fig=plt.figure(figsize=(12.0, 10.0))
fig.text(0.1, 0.97, title, horizontalalignment='left',
color='blue', transform = plt.gcf().transFigure)
plt.xticks=[]
plt.yticks=[]
label=os.path.basename(__file__)
plotid.plotid(label=label)
nxplots=5
nyplots=6
irow=-1
iplot=0
#print plt.get_gca()
#print plt.get_gcf()
isource=0
ifig=1
#for rows in region: # iterate over table rows
for rows in ralist: # iterate over table rows
print 'Elpased time: %.2f seconds' %(time.time() - t0)
irow=irow+1
isource=isource+1
print irow, ' of ', nrows
#if irow > nxplots*nyplots: break
if format == 'region':
ra=region[irow].coord_list[0]
dec=region[irow].coord_list[1]
if format == 'ozdes':
ra=ralist[irow]
dec=declist[irow]
iband=-1
for band in bands: # iterate over the bands
iband=iband+1
iplot=iplot+1
outfile='cutout' + str(irow+1) + band + '.fits'
outfile=None
verbose=True
xc=ra
yc=dec
imagefile=imagefiles[iband]
print 'input image: ', imagefile
# width in arcsecs
width_arcsecs=10.0
# convert to semi-width in degrees
xw=(0.5*width_arcsecs)/3600.0
yw=(0.5*width_arcsecs)/3600.0
# create cutout FITS file
imagefile=imagefiles[iband]
image=cutout.cutout(imagefile, xc, yc, xw=xw, yw=yw, units='wcs',
outfile=outfile, clobber=True,
useMontage=False, coordsys='celestial', verbose=verbose)
image = image.data
ax=fig.add_subplot(nxplots,nyplots,iplot,
frameon=True, xticks=[], yticks=[])
ax.imshow(image)
if iband == 0: ax.text(0.1, 0.80, str(isource),
transform=plt.gca().transAxes)
ax.text(0.9, 0.80, band, transform=plt.gca().transAxes)
if format == 'ozdes':
annotation=object[isource-1].strip()
if iband == 0: ax.text(0.1, 0.02, annotation,
verticalalignment='baseline',
transform=plt.gca().transAxes)
annotation=comment[isource-1].strip()
if iband == 0: ax.text(0.1, 0.1, annotation,
transform=plt.gca().transAxes)
annotation=str(redshift[isource-1]).strip()
if iband == 0: ax.text(0.5, 0.1, annotation,
transform=plt.gca().transAxes)
if iplot == nxplots*nyplots:
print 'nxplots, nyplots, iplot, isource: ', \
nxplots, nyplots, iplot, isource
iplot=0
strfigno= '%4.4d' % (ifig)
#plt.savefig(id+'_cutouts_multiband_' + str(ifig) + '.png', dpi=150)
#plt.savefig(id+'_cutouts_multiband_' + strfigno + '.png', dpi=150)
plt.savefig(id+'_cutouts_multiband_' + strfigno + '.png', dpi=100)
if ifig == 1: plt.show()
ifig=ifig+1
fig=plt.figure(ifig,figsize=(12.0, 10.0))
plotid.plotid(label=label)
print 'nxplots, nyplots, iplot, isource: ', \
nxplots, nyplots, iplot, isource
iplot=0
strfigno= '%4.4d' % (ifig)
#plt.savefig(id+'_cutouts_multiband_' + str(ifig) + '.png', dpi=150)
#plt.savefig(id+'_cutouts_multiband_' + strfigno + '.png', dpi=150)
plt.savefig(id+'_cutouts_multiband_' + strfigno + '.png', dpi=100)
if __name__ == '__main__':
from argparse import ArgumentParser
default_band='r'
default_detband='detr'
# First attempt at using ArgumentParser instead of OptionParser
parser = ArgumentParser(
description='Create a montage of DES cutouts for a single field')
parser.add_argument("-b", "--bands", dest="bands",
default=default_band,
help="input band names [u,g,r,i,z,Y]")
parser.add_argument("-d", "--detband", dest="detband",
default=default_detband,
help="input Sextractor detection band [u,g,r,i,z,Y]")
parser.add_argument("-v", "--version", action="store_true", dest="version",
default="", help="print version number and exit")
args = parser.parse_args()
if args.version:
print __version__
sys.exit(0)
bands=args.bands
detband=args.detband
bands=['u','g','r','i','z','Y']
nbands=len(bands)
imagefiles=['','','','','','','']
iband=-1
# iterate over the bands making arrays with all the filenames
for band in bands:
iband=iband+1
image_path='/data/des/SV/snx3_vvds02hr/v2/' + band + '/'
image_filename="vvds02hr_" + band + ".fits"
imagefile= image_path + image_filename
imagefiles[iband]=imagefile
weightmap_filename = 'vvds02hr_weight_Y.fits'
weightfile = image_path + weightmap_filename
catalog_path = image_path
catalog_filename="vvds02hr_" + band + "_" + detband + "_cat.fits"
list_path='./'
list_filename = "snx3_dr9qso.reg"
list_filename = "snx3_vvdsqso.reg"
format='region'
list_path='/data/des/OzDES/'
list_filename='X3_121216z_qso.fits'
format='ozdes'
listfile = list_path + list_filename
print 'listfile: ', listfile
make_cutouts_multiband(bands=bands, detband=detband,
listfile=listfile, format=format,
imagefiles=imagefiles)