-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolve_beam.py
284 lines (247 loc) · 12.2 KB
/
convolve_beam.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
282
283
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2017 - Francesco de Gasperin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os, sys, argparse, logging
import numpy as np
from lib_linearfit import linear_fit_bootstrap
from lib_fits import flatten
from astropy.io import fits as pyfits
from astropy.wcs import WCS as pywcs
from astropy.coordinates import match_coordinates_sky
from astropy.coordinates import SkyCoord
import astropy.units as u
import pyregion
# https://github.com/astrofrog/reproject
from reproject import reproject_interp, reproject_exact
reproj = reproject_exact
logging.root.setLevel(logging.DEBUG)
parser = argparse.ArgumentParser(
description='Make spectral index maps, e.g. spidxmap.py --region ds9.reg --noise --sigma 5 --save *fits')
parser.add_argument('images', nargs='+', help='List of images to use for spidx')
parser.add_argument('--beam', dest='beam', nargs='+', type=float,
help='3 parameters final beam to convolve all images (BMAJ (arcsec), BMIN (arcsec), BPA (deg))')
parser.add_argument('--size', dest='size', type=float, help='Size (horizontal and vertical) of final image in degree')
parser.add_argument('--radec', dest='radec', nargs='+', type=float,
help='RA/DEC where to center final image in deg (if not given, center on first image)')
parser.add_argument('--shift', dest='shift', action='store_true', help='Shift images before calculating spidx')
parser.add_argument('--save', dest='save', action='store_true', help='Save intermediate results')
parser.add_argument('--force', dest='force', action='store_true', help='Force remake intermediate results')
parser.add_argument('--circbeam', dest='circbeam', action='store_true',
help='Force final beam to be circular (default: False, use minimum common beam area)')
args = parser.parse_args()
# check input
if len(args.images) < 2:
logging.error('Requires at lest 2 images.')
sys.exit(1)
if args.beam is not None and len(args.beam) != 3:
logging.error('Beam must be in the form of "BMAJ BMIN BPA" (3 floats).')
sys.exit(1)
if args.radec is not None and len(args.radec) != 2:
logging.error('--radec must be in the form of "RA DEC" (2 floats).')
sys.exit(1)
from lib_fits import Image, find_freq
class ImageSpidx(Image):
def __init__(self, imagefile):
Image.__init__(self, imagefile)
def regrid(self, regrid_hdr):
logging.debug('%s: regridding' % (self.imagefile))
self.img_data, __footprint = reproj((self.img_data, self.img_hdr), regrid_hdr, parallel=True)
beam = self.get_beam()
freq = find_freq(self.img_hdr)
self.img_hdr = regrid_hdr
self.img_hdr['FREQ'] = freq
self.set_beam(beam) # retain beam info if not present in regrd_hdr
def blank_noisy(self, nsigma):
"""
Set to nan pixels below nsigma*noise
"""
nans_before = np.sum(np.isnan(self.img_data))
self.img_data[np.isnan(self.img_data)] = 0 # temporary set nans to 0 to prevent error in "<"
self.img_data[np.where(self.img_data <= nsigma * image.noise)] = np.nan
nans_after = np.sum(np.isnan(self.img_data))
logging.debug('%s: Blanked pixels %i -> %i' % (self.imagefile, nans_before, nans_after))
def make_catalogue(self):
"""
Create catalogue for this image
"""
import bdsf
from astropy.table import Table
img_cat = self.imagefile + '.cat'
if not os.path.exists(img_cat) and not args.force:
bdsf_img = bdsf.process_image(self.imagefile, rms_box=(100, 30), \
thresh_pix=5, thresh_isl=3, atrous_do=False, \
adaptive_rms_box=True, adaptive_thresh=100, rms_box_bright=(30, 10),
quiet=True)
bdsf_img.write_catalog(outfile=img_cat, catalog_type='srl', format='fits', clobber=True)
else:
logging.warning('%s already exists, using it.' % img_cat)
self.cat = Table.read(img_cat)
logging.debug('%s: Number of sources detected: %i' % (self.imagefile, len(self.cat)))
if __name__ == '__main__':
########################################################
# prepare images and make catalogues if necessary
all_images = []
all_beams = []
for imagefile in args.images:
image = ImageSpidx(imagefile)
all_beams.append(image.get_beam())
all_images.append(image)
if args.shift:
image.make_catalogue()
#####################################################
# find the smallest common beam
if args.beam is None:
if args.circbeam:
maxmaj = np.max([b[0] for b in all_beams])
target_beam = [maxmaj * 1.01, maxmaj * 1.01, 0.] # add 1% to prevent crash in convolution
else:
from radio_beam import Beams
my_beams = Beams([b[0] for b in all_beams] * u.deg, [b[1] for b in all_beams] * u.deg,
[b[2] for b in all_beams] * u.deg)
common_beam = my_beams.common_beam()
target_beam = [common_beam.major.value, common_beam.minor.value, common_beam.pa.value]
else:
target_beam = [args.beam[0] / 3600., args.beam[1] / 3600., args.beam[2]]
logging.info('Final beam: %.1f" %.1f" (pa %.1f deg)' \
% (target_beam[0] * 3600., target_beam[1] * 3600., target_beam[2]))
#####################################################
# find+apply shift w.r.t. first image
if args.shift:
ref_cat = all_images[0].cat
# keep only point sources
print(ref_cat)
for image in all_images[1:]:
# cross match
idx_match, sep, _ = match_coordinates_sky(SkyCoord(ref_cat['RA'], ref_cat['DEC']), \
SkyCoord(image.cat['RA'], image.cat['DEC']))
idx_matched_ref = np.arange(0, len(ref_cat))[sep < target_beam[0] * u.degree]
idx_matched_img = idx_match[sep < target_beam[0] * u.degree]
# find & apply shift
if len(idx_match) < 3:
logging.warning('%s: Not enough matches found, assume no shift.' % image.imagefile)
continue
dra = ref_cat['RA'][idx_matched_ref] - image.cat['RA'][idx_matched_img]
dra[dra > 180] -= 360
dra[dra < -180] += 360
ddec = ref_cat['DEC'][idx_matched_ref] - image.cat['DEC'][idx_matched_img]
flux = ref_cat['Peak_flux'][idx_matched_ref]
image.apply_shift(np.average(dra, weights=flux), np.average(ddec, weights=flux))
# clean up
# for image in all_images:
# os.system(rm ...)
######################################################
# Generate regrid headers
rwcs = pywcs(naxis=2)
rwcs.wcs.ctype = all_images[0].get_wcs().wcs.ctype
cdelt = target_beam[1] / 5. # 1/5 of minor axes (deg)
logging.info('Pixel scale: %f"' % (cdelt * 3600.))
rwcs.wcs.cdelt = [-cdelt, cdelt]
if args.radec is not None:
mra = args.radec[0] # *np.pi/180
mdec = args.radec[1] # *np.pi/180
else:
mra = all_images[0].img_hdr['CRVAL1']
mdec = all_images[0].img_hdr['CRVAL2']
rwcs.wcs.crval = [mra, mdec]
# if size is not give is taken from the mask
if args.size is None:
if args.region is not None:
r = pyregion.open(args.region)
mask = r.get_mask(header=all_images[0].img_hdr, shape=all_images[0].img_data.shape)
intermediate = pyfits.PrimaryHDU(mask.astype(float), all_images[0].img_hdr)
intermediate.writeto('mask.fits', overwrite=True)
w = all_images[0].get_wcs()
y, x = mask.nonzero()
ra_max, dec_max = w.all_pix2world(np.max(x), np.max(y), 0, ra_dec_order=True)
ra_min, dec_min = w.all_pix2world(np.min(x), np.min(y), 0, ra_dec_order=True)
args.size = 2 * np.max([np.max([np.abs(ra_max - mra), np.abs(ra_min - mra)]),
np.max([np.abs(dec_max - mdec), np.abs(dec_min - mdec)])])
else:
logging.warning('No size or region provided, use entire size of first image.')
sys.exit('not implemented')
xsize = int(np.rint(args.size / cdelt))
ysize = int(np.rint(args.size / cdelt))
if xsize % 2 != 0: xsize += 1
if ysize % 2 != 0: ysize += 1
rwcs.wcs.crpix = [xsize / 2, ysize / 2]
regrid_hdr = rwcs.to_header()
regrid_hdr['NAXIS'] = 2
regrid_hdr['NAXIS1'] = xsize
regrid_hdr['NAXIS2'] = ysize
regrid_hdr['BMAJ'], regrid_hdr['BMIN'], regrid_hdr['BPA'] = image.get_beam()
logging.info('Image size: %f deg (%i %i pixels)' % (args.size, xsize, ysize))
#########################################################
# regrid, convolve and only after apply mask and find noise
for image in all_images:
if os.path.exists(image.imagefile + '-conv.fits') and not args.force:
data, hdr = pyfits.getdata(image.imagefile + '-conv.fits', 0, header=True)
image.img_data = data
image.img_hdr = hdr
image.set_beam([hdr['BMAJ'], hdr['BMIN'], hdr['BPA']])
else:
image.convolve(target_beam)
if args.save:
intermediate = pyfits.PrimaryHDU(image.img_data, image.img_hdr)
intermediate.writeto(image.imagefile + '-conv.fits', overwrite=True)
if os.path.exists(image.imagefile + '-regrid-conv.fits') and not args.force:
data, hdr = pyfits.getdata(image.imagefile + '-regrid-conv.fits', 0, header=True)
image.img_data = data
image.img_hdr = hdr
image.set_beam([hdr['BMAJ'], hdr['BMIN'], hdr['BPA']])
else:
image.regrid(regrid_hdr)
if args.save:
intermediate = pyfits.PrimaryHDU(image.img_data, image.img_hdr)
intermediate.writeto(image.imagefile + '-regrid-conv.fits', overwrite=True)
if args.noise:
if args.sigma is not None:
if args.bgreg:
image.calc_noise(sigma=args.sigma, bg_reg=args.bgreg) # after mask?/convolution
else:
image.calc_noise(sigma=args.sigma) # after mask?/convolution
image.blank_noisy(args.sigma)
else:
image.calc_noise() # after mask?/convolution
if args.region is not None:
image.apply_region(args.region, invert=True) # after convolution to minimise bad pixels
#########################################################
# do spdix and write output
frequencies = [image.freq for image in all_images]
regrid_hdr['FREQLOWER'] = np.min(frequencies)
regrid_hdr['FREQUPPER'] = np.max(frequencies)
if args.noise:
yerr = [image.noise for image in all_images]
else:
yerr = None
spidx_data = np.empty(shape=(xsize, ysize))
spidx_data[:] = np.nan
spidx_err_data = np.empty(shape=(xsize, ysize))
spidx_err_data[:] = np.nan
for i in range(xsize):
print('.', end=' ')
sys.stdout.flush()
for j in range(ysize):
val4reg = [image.img_data[i, j] for image in all_images]
if np.isnan(val4reg).any(): continue
(a, b, sa, sb) = linear_fit_bootstrap(x=frequencies, y=val4reg, yerr=yerr, tolog=True)
spidx_data[i, j] = a
spidx_err_data[i, j] = sa
spidx = pyfits.PrimaryHDU(spidx_data, regrid_hdr)
spidx_err = pyfits.PrimaryHDU(spidx_err_data, regrid_hdr)
spidx.writeto(args.output, overwrite=True)
spidx_err.writeto(args.output.replace('.fits', '-err.fits'), overwrite=True)