-
Notifications
You must be signed in to change notification settings - Fork 3
/
fluxmatch.py
690 lines (635 loc) · 33.3 KB
/
fluxmatch.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import matplotlib
matplotlib.use('PDF')
from matplotlib import pyplot as plt
import numpy,os,sys,logging
from optparse import OptionParser
import astropy
from astropy import constants as c, units as u
from astropy.table import Table,Column
from astropy.coordinates import SkyCoord
from astropy.io import fits
from astropy.wcs import WCS
from AegeanTools.regions import Region
from AegeanTools import source_finder,BANE
import mwapy
from mwapy.pb import make_beam
from mwapy import metadata
# configure the logging
logging.basicConfig(format='# %(levelname)s:%(name)s: %(message)s')
logger=logging.getLogger('fluxmatch')
logger.setLevel(logging.WARNING)
######################################################################
def get_rms_background(imagename, cores=16):
outbase=os.path.splitext(imagename)[0]
rmsimage=outbase + '_rms.fits'
bgimage=outbase + '_bkg.fits'
if not (os.path.exists(rmsimage) and os.path.exists(bgimage)):
BANE.filter_image(imagename,
outbase,
mask=False,
cores=cores)
else:
logger.info('Using existing background and rms images %s, %s' % (bgimage,rmsimage))
return rmsimage, bgimage
######################################################################
def find_sources_in_image(imagename, max_summits=5, nsigma=10, usebane=True,
region=None, cores=16):
"""
sources,rmsimage,bgimage=find_sources_in_image(imagename, max_summits=5, nsigma=10, usebane=True, region=None, cores=16)
runs aegean.find_sources_in_image
but first runs BANE to get the BG/rms estimates
if region is supplied (.mim format) only sources inside that will be identified
"""
if usebane:
rmsimage, bgimage=get_rms_background(imagename, cores=cores)
else:
rmsimage,bgimage=None,None
sf=source_finder.SourceFinder(log=logging.getLogger('fluxmatch'))
sources=sf.find_sources_in_image(imagename,
max_summits=max_summits,
innerclip=nsigma,
rmsin=rmsimage,
bkgin=bgimage,
mask=region,
cores=cores)
return sources,rmsimage,bgimage
######################################################################
def aegean2table(sources):
"""
sourcesTable=aegean2table(sources)
return astropy table corresponding to aegean source list
"""
ra=Column([s.ra for s in sources],name='RA')
dec=Column([s.dec for s in sources],name='Dec')
peakflux=Column([s.peak_flux for s in sources],name='PeakFlux')
peakfluxerr=Column([s.err_peak_flux for s in sources],name='PeakFluxErr')
intflux=Column([s.int_flux for s in sources],name='IntFlux')
intfluxerr=Column([s.err_int_flux for s in sources],name='IntFluxErr')
rms=Column([s.local_rms for s in sources],name='RMS')
sourcesTable=Table([ra,dec,peakflux,peakfluxerr,intflux,intfluxerr,rms])
return sourcesTable
######################################################################
def find_beam(image):
f=fits.open(image)
if 'BEAM' in f[0].header.keys():
if os.path.exists(f[0].header['BEAM']):
logger.info('Found existing primary beam in header: %s' % f[0].header['BEAM'])
return f[0].header['BEAM']
if not 'DELAYS' in f[0].header.keys():
return None
delays=[int(x) for x in f[0].header['DELAYS'].split(',')]
logger.info('Creating primary beam for %s' % image)
out=make_beam.make_beam(image, ext=0,
delays=delays,
model='analytic',
jones=False,
precess=False)
if f[0].header['CRVAL4']==-5:
# XX
return out[0]
elif f[0].header['CRVAL4']==-6:
# YY
return out[1]
elif f[0].header['CRVAL4']==1:
# I
fXX=fits.open(out[0])
fYY=fits.open(out[1])
fXX[0].data=0.5*(fXX[0].data+fYY[0].data)
fXX[0].header['CRVAL4']=1
out=out[0].replace('_beamXX','_beamI')
if os.path.exists(out):
os.remove(out)
fXX.writeto(out)
return out
######################################################################
def fluxmatch(image,
catalog='GLEAMIDR3.fits',
fluxcolumn=None,
fluxerrcolumn=None,
racolumn='RAJ2000',
deccolumn='DECJ2000',
nsigma=10,
rmsfactor=3,
matchradius=120,
rejectsigma=3,
maxdistance=20,
minbeam=0.5,
psfextent=1.1,
limit=10,
refineposition=False,
update=False,
prefix=None,
otherimages=[],
updatepoln=False,
updatebane=False,
plot=True,
region=True,
cores=1):
"""
catalog='GLEAMIDR3.fits',
fluxcolumn=None,
fluxerrcolumn=None,
racolumn='RAJ2000',
deccolumn='DECJ2000'
signal-to-noise for source finding
nsigma=10,
ratio of local rms to minimum image RMS that is OK
rmsfactor=3,
distance between catalog source and source in image (arcsec)
matchradius=120,
rejection sigma for flux ratio outliers
rejectsigma=3,
distance from the image center that is OK (deg)
maxdistance=20,
minimum beam power compared to max
minbeam=0.5,
area of source/area of psf threshold
psfextent=1.1,
max ratio of new to old fluxes (or reciprocal)
limit=10,
"""
if not isinstance(matchradius,astropy.units.quantity.Quantity):
matchradius=matchradius*u.arcsec
if not isinstance(maxdistance,astropy.units.quantity.Quantity):
maxdistance=maxdistance*u.deg
if not os.path.exists(image):
logger.error('Cannot find input image %s' % image)
return None
if updatepoln:
for stokes in ['Q','U','V']:
if os.path.exists(image.replace('-I.fits','-%s.fits' % stokes)):
otherimages.append(image.replace('-I.fits','-%s.fits' % stokes))
logger.info('Will also scale %s' % otherimages[-1])
if updatebane:
for ext in ['rms','bkg']:
if os.path.exists(image.replace('-I.fits','-I_%s.fits' % ext)):
otherimages.append(image.replace('-I.fits','-I_%s.fits' % ext))
logger.info('Will also scale %s' % otherimages[-1])
if updatepoln:
for stokes in ['Q','U','V']:
if os.path.exists(image.replace('-I.fits','-%s_%s.fits' % (stokes,ext))):
otherimages.append(image.replace('-I.fits','-%s_%s.fits' % (stokes,ext)))
logger.info('Will also scale %s' % otherimages[-1])
if not os.path.exists(catalog):
logger.error('Cannot find GLEAM catalog %s' % catalog)
return None
beam=find_beam(image)
if beam is None:
logger.warning('Did not generate primary beam: will ignore')
minbeam=None
if beam is not None and not os.path.exists(beam):
logger.warning('Cannot find primary beam %s: will ignore' % beam)
minbeam=None
beam=None
outbase=os.path.splitext(image)[0]
sources, rmsimage, bgimage=find_sources_in_image(image,
nsigma=nsigma,
cores=cores)
logger.info('Found %d sources above %d sigma in %s' % (len(sources),
nsigma,
image))
logger.info('Wrote %s and %s' % (rmsimage, bgimage))
# convert to astropy table
sourcesTable=aegean2table(sources)
fimage=fits.open(image)
frequency=fimage[0].header['CRVAL3']
w=WCS(fimage[0].header,naxis=2)
frmsimage=fits.open(rmsimage)
minrms=numpy.nanmin(frmsimage[0].data)
logger.info('Minimum RMS in image is %.1f mJy' % (minrms*1e3))
if beam is not None:
fbeam=fits.open(beam)
x,y=w.wcs_world2pix(sourcesTable['RA'],sourcesTable['Dec'],0)
sourcesTable.add_column(Column(x,name='X'))
sourcesTable.add_column(Column(y,name='Y'))
if 'RA' in fimage[0].header.keys():
pointingcenter=SkyCoord(fimage[0].header['RA'],fimage[0].header['DEC'],
unit=('deg','deg'))
else:
# get the pointing center from the metadata
logger.warning('Pointing metadata not present in header; retrieving...')
try:
obs=metadata.MWA_Observation(fimage[0].header['GPSTIME'])
logger.info('Found pointing center %f,%f' % (obs.RA,obs.Dec))
pointingcenter=SkyCoord(obs.RA,obs.Dec,
unit=('deg','deg'))
except:
logger.warning('Using CRVAL1/CRVAL2 for pointing center')
pointingcenter=SkyCoord(fimage[0].header['CRVAL1'],fimage[0].header['CRVAL2'],
unit=('deg','deg'))
coords=SkyCoord(sourcesTable['RA'],sourcesTable['Dec'],unit=(u.deg,u.deg))
sourcesTable.add_column(Column(coords.separation(pointingcenter).to(u.deg),
name='SOURCEDIST'))
if beam is not None:
pixelx,pixely=numpy.int16(x),numpy.int16(y)
pixelx[pixelx<0]=0
pixely[pixely<0]=0
pixelx[pixelx>=fbeam[0].data.shape[-1]]=fbeam[0].data.shape[-1]-1
pixely[pixely>=fbeam[0].data.shape[-2]]=fbeam[0].data.shape[-2]-1
sourcesTable.add_column(Column(fbeam[0].data[0,0,pixelx,pixely],
name='BEAM'))
else:
sourcesTable.add_column(Column(0*x,
name='BEAM'))
if '.fits' in catalog:
# this seems to be faster than going straight to the Table.read()
try:
fcatalog=fits.open(catalog)
except:
logger.error('Unable to open FITS catalog %s' % catalog)
return None
catalogTable=Table(fcatalog[1].data)
else:
try:
catalogTable=Table.read(catalog)
except:
logger.error('Unable to read catalog %s' % catalog)
return None
try:
bandfrequencies=numpy.array([int(s.split('_')[-1]) for s in numpy.array(catalogTable.colnames)[numpy.nonzero(numpy.array([('int_flux' in c) and not ('deep' in c) and not ('wide' in c) ('fit' in c) for c in catalogTable.colnames]))[0]]])
except:
bandfrequencies=[]
if len(bandfrequencies)>0:
# find the indices of the bands just above and below the observation
# linearly weight the fluxes just above and below to match
# the observation frequency
indexplus=(bandfrequencies>=frequency/1e6).nonzero()[0].min()
indexminus=(bandfrequencies<frequency/1e6).nonzero()[0].max()
logger.info('Observation frequency of %.1f MHz: interpolating between %d MHz and %d MHz' % (frequency/1e6,bandfrequencies[indexminus],bandfrequencies[indexplus]))
weightplus=(frequency/1e6-bandfrequencies[indexminus])/(bandfrequencies[indexplus]-bandfrequencies[indexminus])
weightminus=1-weightplus
gleamflux=catalogTable['int_flux_%03d' % bandfrequencies[indexminus]]*weightminus+catalogTable['int_flux_%03d' % bandfrequencies[indexplus]]*weightplus
try:
gleamfluxerr=numpy.sqrt((catalogTable['err_fit_flux_%03d' % bandfrequencies[indexminus]]*weightminus)**2+(catalogTable['err_fit_flux_%03d' % bandfrequencies[indexplus]]*weightplus)**2)
except KeyError:
gleamfluxerr=numpy.sqrt((catalogTable['err_int_flux_%03d' % bandfrequencies[indexminus]]*weightminus)**2+(catalogTable['err_int_flux_%03d' % bandfrequencies[indexplus]]*weightplus)**2)
else:
logger.warning('Could not identify GLEAM band fluxes')
if fluxcolumn is None:
logger.error('Could not identify flux columns to use')
return None
if fluxcolumn in catalogTable.colnames and fluxerrcolumn in catalogTable.colnames:
logger.warning('Using %s and %s columns' % (fluxcolumn,fluxerrcolumn))
gleamflux=catalogTable[fluxcolumn]
gleamfluxerr=catalogTable[fluxerrcolumn]
else:
logger.error('Could not identify flux columns to use')
return None
try:
catalogcoords=SkyCoord(catalogTable[racolumn],
catalogTable[deccolumn],unit=(u.deg,u.deg))
except KeyError:
catalogcoords=SkyCoord(catalogTable['RAJ2000'],
catalogTable['DEJ2000'],unit=(u.deg,u.deg))
racolumn='RAJ2000'
deccolumn='DEJ2000'
# match the catalog to the data
idx,sep2d,sep3d=coords.match_to_catalog_sky(catalogcoords)
# add the matched columns to the soure table
try:
sourcesTable.add_column(Column(catalogTable['Name'][idx],
name='Name'))
except:
pass
sourcesTable.add_column(Column(catalogTable[racolumn][idx],
name='GLEAMRA'))
sourcesTable.add_column(Column(catalogTable[deccolumn][idx],
name='GLEAMDEC'))
sourcesTable.add_column(Column(sep2d.to(u.arcsec),
name='GLEAMSep'))
sourcesTable.add_column(Column(gleamflux[idx],
name='GLEAMFlux'))
sourcesTable.add_column(Column(gleamfluxerr[idx],
name='GLEAMFluxErr'))
try:
sourcesTable.add_column(Column(catalogTable['psf_a_%03d' % bandfrequencies[indexplus]][idx] * catalogTable['psf_b_%03d' % bandfrequencies[indexplus]][idx],
name='PSFAREA'))
sourcesTable.add_column(Column(catalogTable['a_%03d' % bandfrequencies[indexplus]][idx] * catalogTable['b_%03d' % bandfrequencies[indexplus]][idx],
name='SOURCEAREA'))
except:
pass
dRA=(sourcesTable['RA']-sourcesTable['GLEAMRA'])
dDEC=(sourcesTable['Dec']-sourcesTable['GLEAMDEC'])
iterations=1
if refineposition:
iterations=2
for iter in xrange(iterations):
# determine the good matches
# first criterion is separation
good=(sourcesTable['GLEAMSep']<matchradius)
logger.info('%04d/%04d sources are within %.1f arcsec' % (good.sum(),
len(good),
matchradius.to(u.arcsec).value))
# only point sources
if psfextent is not None and psfextent>0:
good=good & (sourcesTable['SOURCEAREA']<=psfextent*sourcesTable['PSFAREA'])
logger.info('%04d/%04d sources also have source a*b < %.1f * psf a*b' % (good.sum(),
len(good),
psfextent))
# cut on the local rms compared to the minimum in the image
if rmsfactor is not None and rmsfactor>0:
good=good & (sourcesTable['RMS']<=rmsfactor*minrms)
logger.info('%04d/%04d sources also have RMS < %.1f mJy' % (good.sum(),
len(good),
rmsfactor*minrms*1e3))
# distance from pointing center
if maxdistance is not None and maxdistance>0:
good=good & (sourcesTable['SOURCEDIST'] < maxdistance)
logger.info('%04d/%04d sources also are within %.1f deg of pointing center' % (good.sum(),
len(good),
maxdistance.to(u.deg).value))
# primary beam power
if minbeam is not None and minbeam>0:
good=good & (sourcesTable['BEAM']>minbeam*fbeam[0].data.max())
logger.info('%04d/%04d sources also are at primary beam power > %.2f' % (good.sum(),len(good),minbeam))
# require that all sources are > 5 sigma detections
# and that flux uncertainties are > 0
ignorefluxerrs=True
if numpy.all(sourcesTable['IntFluxErr']<0) or ignorefluxerrs:
logger.warning('All source uncertainties are < 0: will ignore')
else:
good=good & (sourcesTable['IntFluxErr']<0.2*sourcesTable['IntFlux']) & (sourcesTable['IntFluxErr']>0) & (sourcesTable['GLEAMFluxErr']>0) & (sourcesTable['GLEAMFluxErr']<0.2*sourcesTable['GLEAMFlux'])
try:
good=good & (sourcesTable['GLEAMFlux']>=sourcesTable['IntFlux'][good].min())
pass
except ValueError:
logger.warning('No good sources left')
good=numpy.array([False]*len(good))
logger.info('%04d/%04d sources match all cuts' % (good.sum(),
len(good)))
if good.sum()<5:
logger.error('Insufficient sources for flux scaling')
return None
fitres=numpy.polyfit(sourcesTable['GLEAMFlux'][good],
sourcesTable['IntFlux'][good],
deg=1,
w=1/sourcesTable['IntFluxErr'][good]**2)
ratio=sourcesTable['IntFlux']/sourcesTable['GLEAMFlux']
ratioerr=numpy.sqrt((sourcesTable['IntFluxErr']/sourcesTable['GLEAMFlux'])**2+(sourcesTable['IntFlux']*sourcesTable['GLEAMFluxErr']/sourcesTable['GLEAMFlux']**2)**2)
if rejectsigma is not None:
# do a bit of sigma clipping just in case
good=(good) & (numpy.abs(ratio-numpy.median(ratio[good]))<=ratioerr*rejectsigma)
fittedratio=(ratio[good]/ratioerr[good]**2).sum()/(1/ratioerr[good]**2).sum()
fittedratioerr=numpy.sqrt(1/(1/ratioerr[good]**2).sum())
chisq=(((ratio[good]-fittedratio)/ratioerr[good])**2).sum()
ndof=good.sum()-1
logger.info('Found ratio of %s / %s = %.3f +/- %.3f' % (image,
catalog,
fittedratio,
fittedratioerr))
if refineposition and iter==0:
sourcesTable['RA']-=dRA[good].mean()
sourcesTable['Dec']-=dDEC[good].mean()
logger.info('Applied shift of (%.1f sec, %.1f arcsec)' % (dRA[good].mean()*3600,
dDEC[good].mean()*3600))
coords=SkyCoord(sourcesTable['RA'],sourcesTable['Dec'],unit=(u.deg,u.deg))
idx,sep2d,sep3d=coords.match_to_catalog_sky(catalogcoords)
sourcesTable['GLEAMSep']=sep2d.to(u.arcsec)
sourcesTable.add_column(Column(good,name='GOOD'))
sourcesTable.meta['ratio']=fittedratio
sourcesTable.meta['ratio_err']=fittedratioerr
sourcesTable.meta['chisq']=chisq
sourcesTable.meta['ndof']=ndof
sourcesTable.meta['slope']=fitres[0]
sourcesTable.meta['intercept']=fitres[1]
if refineposition:
sourcesTable.meta['rashift']=dRA[good].mean()*3600
sourcesTable.meta['decshift']=dDEC[good].mean()*3600
if os.path.exists(outbase + '_fluxmatch.hdf5'):
os.remove(outbase + '_fluxmatch.hdf5')
sourcesTable.write(outbase + '_fluxmatch.hdf5',path='data')
logger.info('Wrote %s_fluxmatch.hdf5' % outbase)
if region:
outreg=outbase + '_fluxmatch.reg'
if os.path.exists(outreg):
os.remove(outreg)
foutreg=open(outreg,'w')
for i in xrange(len(sourcesTable)):
if sourcesTable[i]['GOOD']:
foutreg.write('icrs;circle(%f,%f,60") # text={%03d} color={green}\n' % (sourcesTable[i]['RA'],
sourcesTable[i]['Dec'],
i))
else:
foutreg.write('icrs;box(%f,%f,60",60",0) # text={%03d} color={red}\n' % (sourcesTable[i]['RA'],
sourcesTable[i]['Dec'],
i))
logger.info('Wrote %s' % outreg)
foutreg.close()
if update:
if fittedratio > limit or fittedratio < 1.0/limit:
logger.warning('Ratio exceeds reasonable limits; skipping...')
else:
fimage=fits.open(image,'update')
if not 'BEAM' in fimage[0].header.keys():
fimage[0].header['BEAM']=beam
fimage[0].data/=fittedratio
fimage[0].header['FLUXSCAL']=(fittedratio,'Flux scaling relative to catalog')
fimage[0].header['FLUX_ERR']=(fittedratioerr,'Flux scaling uncertainty relative to catalog')
fimage[0].header['FLUXCAT']=(catalog,'Flux scaling catalog')
fimage[0].header['NFLUXSRC']=(good.sum(),'Number of sources used for flux scaling')
fimage[0].header['FLUXCHI2']=(chisq,'Flux scaling chi-squared')
fimage[0].header['FLUXSLOP']=(fitres[0],'Flux scaling slope')
if refineposition:
fimage[0].header['RASHIFT']=(dRA[good].mean()*3600,'[s] RA Shift for catalog match')
fimage[0].header['DECSHIFT']=(dDEC[good].mean()*3600,'[arcsec] DEC Shift for catalog match')
fimage[0].header['CRVAL1']-=dRA[good].mean()
fimage[0].header['CRVAL2']-=dDEC[good].mean()
if 'IMAGERMS' in fimage[0].header.keys():
fimage[0].header['IMAGERMS']/=fittedratio
if prefix is None:
fimage.flush()
logger.info('Scaled %s by %.3f' % (image,fittedratio))
else:
p,f=os.path.split(image)
outfile=os.path.join(p,prefix + f)
if os.path.exists(outfile):
os.remove(outfile)
fimage.writeto(outfile)
logger.info('Scaled %s by %.3f and wrote to %s' % (image,fittedratio,outfile))
if otherimages is not None and len(otherimages)>0:
# also update some other images
for otherimage in otherimages:
foimage=fits.open(otherimage,'update')
foimage[0].data/=fittedratio
foimage[0].header['FLUXIMG']=(image, 'Image used for flux scaling')
foimage[0].header['FLUXSCAL']=(fittedratio,'Flux scaling relative to catalog')
foimage[0].header['FLUX_ERR']=(fittedratioerr,'Flux scaling uncertainty relative to catalog')
foimage[0].header['FLUXCAT']=(catalog,'Flux scaling catalog')
foimage[0].header['NFLUXSRC']=(good.sum(),'Number of sources used for flux scaling')
foimage[0].header['FLUXCHI2']=(chisq,'Flux scaling chi-squared')
foimage[0].header['FLUXSLOP']=(fitres[0],'Flux scaling slope')
if refineposition:
foimage[0].header['RASHIFT']=(dRA[good].mean()*3600,'[s] RA Shift for catalog match')
foimage[0].header['DECSHIFT']=(dDEC[good].mean()*3600,'[arcsec] DEC Shift for catalog match')
foimage[0].header['CRVAL1']-=dRA[good].mean()
foimage[0].header['CRVAL2']-=dDEC[good].mean()
if 'IMAGERMS' in fimage[0].header.keys():
foimage[0].header['IMAGERMS']/=fittedratio
if prefix is None:
foimage.flush()
logger.info('Scaled %s by %.3f' % (otherimage,fittedratio))
else:
p,f=os.path.split(otherimage)
outfile=os.path.join(p,prefix + f)
if os.path.exists(outfile):
os.remove(outfile)
fimage.writeto(outfile)
logger.info('Scaled %s by %.3f and wrote to %s' % (otherimage,fittedratio,outfile))
if plot:
imagename=image.replace('_','\_')
plt.clf()
xx=numpy.logspace(-2,10)
plt.loglog(xx,xx*fittedratio,'r')
plt.loglog(xx,numpy.polyval(fitres,xx),
'r--')
plt.errorbar(sourcesTable[good]['GLEAMFlux'],
sourcesTable[good]['IntFlux'],
xerr=sourcesTable[good]['GLEAMFluxErr'],
yerr=sourcesTable[good]['IntFluxErr'],
fmt='b.')
#plt.gca().set_xscale('log')
#plt.gca().set_yscale('log')
plt.axis([0.1,100,0.1,100])
plt.xlabel('Flux Density in %s (Jy)' % catalog.replace('_','\_')
,fontsize=16)
plt.ylabel('Flux Density in %s (Jy)' % imagename.replace('_','\_'),
fontsize=16)
plt.gca().tick_params(labelsize=16)
plt.savefig('%s_fluxflux.pdf' % outbase)
logger.info('Wrote %s_fluxflux.pdf' % outbase)
plt.clf()
plt.hist(ratio[good],30)
plt.xlabel('Flux Density in %s / Flux Density in %s' % (imagename.replace('_','\_'),
catalog.replace('_','\_')),
fontsize=16)
plt.ylabel('Number of Sources',fontsize=16)
plt.plot(fittedratio*numpy.array([1,1]),
plt.gca().get_ylim(),'r-')
plt.gca().tick_params(labelsize=16)
plt.savefig('%s_hist.pdf' % outbase)
logger.info('Wrote %s_hist.pdf' % outbase)
plt.clf()
plt.plot(x,y,'k.')
h=plt.scatter(x[good],y[good],s=60,
c=ratio[good],
norm=matplotlib.colors.LogNorm(vmin=0.5,vmax=2),
cmap=plt.cm.BrBG)
plt.xlabel('X',fontsize=16)
plt.ylabel('Y',fontsize=16)
cbar = plt.gcf().colorbar(h,ticks=[0.5,1,2])
plt.gca().tick_params(labelsize=16)
plt.savefig('%s_scatter.pdf' % outbase)
logger.info('Wrote %s_scatter.pdf' % outbase)
plt.clf()
plt.plot((sourcesTable['RA'][good]-sourcesTable['GLEAMRA'][good])*3600,
(sourcesTable['Dec'][good]-sourcesTable['GLEAMDEC'][good])*3600,
'ro')
plt.plot(plt.gca().get_xlim(),[0,0],'k--')
plt.plot([0,0],plt.gca().get_ylim(),'k--')
plt.xlabel('$\\alpha$(%s)-$\\alpha$(%s)' % (imagename.replace('_','\_'),
catalog.replace('_','\_')),fontsize=16)
plt.ylabel('$\\delta$(%s)-$\\delta$(%s)' % (imagename.replace('_','\_'),
catalog.replace('_','\_')),fontsize=16)
plt.gca().tick_params(labelsize=16)
plt.savefig('%s_position.pdf' % outbase)
logger.info('Wrote %s_position.pdf' % outbase)
plt.clf()
xx=numpy.linspace(0,300,50)
plt.hist(sourcesTable['GLEAMSep'].to(u.arcsec).value[~good],
xx,color='b',alpha=0.5)
plt.hist(sourcesTable['GLEAMSep'].to(u.arcsec).value[good],
xx,color='r',alpha=0.5)
plt.plot(matchradius.to(u.arcsec).value*numpy.array([1,1]),
plt.gca().get_ylim(),
'k--')
plt.xlabel('Separation %s vs. %s (arcsec)' % (imagename.replace('_','\_'),
catalog.replace('_','\_')),
fontsize=16)
plt.ylabel('Number of sources',fontsize=16)
plt.gca().tick_params(labelsize=16)
plt.savefig('%s_separation.pdf' % outbase)
logger.info('Wrote %s_separation.pdf' % outbase)
return fittedratio, fittedratioerr, chisq, ndof, fitres[0], fitres[1]
######################################################################
def main():
usage="Usage: %prog [options] <files>\n"
usage+="\tCompare MWA image against GLEAM catalog and determine flux scaling\n"
usage+="\tSelection of comparison sources depends on:\n"
usage+="\t\tSeparation\n\t\tPoint source extent\n\t\tLocal RMS\n"
usage+="\t\tPrimary beam power\n\t\tDistance from pointing center\n"
usage+="\tCan optionally refine positions, update image, make diagnostic plots\n"
parser = OptionParser(usage=usage,version=mwapy.__version__ + ' ' + mwapy.__date__)
parser.add_option('-c','--catalog',dest='catalog',default='GLEAMIDR3.fits',
help='Location of GLEAM catalog file [default=%default]')
parser.add_option('--fluxcol',dest='fluxcolumn',default='FLUX',
help='Column for flux density if not GLEAM standard [default=%default]')
parser.add_option('--fluxerrcol',dest='fluxerrcolumn',default='FLUXERR',
help='Column for flux density errors if not GLEAM standard [default=%default]')
parser.add_option('--racolumn',dest='racolumn',default='RAJ2000',
help='Column for RA if not GLEAM standard [default=%default]')
parser.add_option('--deccolumn',dest='deccolumn',default='DECJ2000',
help='Column for Dec if not GLEAM standard [default=%default]')
parser.add_option('--nsigma',dest='nsigma',default=10,type='float',
help='Threshold in sigma for source finding [default=%default]')
parser.add_option('--match',dest='matchradius',default=60,type='float',
help='Matching radius in arcsec [default=%default]')
parser.add_option('--rmsfactor',dest='rmsfactor',default=3,type='float',
help='Max ratio of local RMS to min RMS [default=%default]')
parser.add_option('--maxdistance',dest='maxdistance',default=20,type='float',
help='Max distance from pointing center in degrees [default=%default]')
parser.add_option('--minbeam',dest='minbeam',default=0.5,type='float',
help='Minimum primary beam power [default=%default]')
parser.add_option('--psfextent',dest='psfextent',default=1.1,type='float',
help='Max source / PSF extent [default=%default]')
parser.add_option('--rejectsigma',dest='rejectsigma',default=3,type='float',
help='Sigma clipping threshold [default=%default]')
parser.add_option('--limit',dest='ratiolimit',default=10,type='float',
help='Max ratio of new to old fluxes (or old to new) [default=%default]')
parser.add_option('--update',action="store_true",dest='update',default=False,
help="Update original FITS image?")
parser.add_option('--prefix',dest='prefix',default=None,type='str',
help='Prefix for writing to a new file [default=overwrite]')
parser.add_option('--updatepoln','--updatestokes',action="store_true",dest='updatepoln',default=False,
help="Update other polarizations?")
parser.add_option('--updatebane',action="store_true",dest='updatebane',default=False,
help="Update BANE output (_rms, _bks)?")
parser.add_option('--refineposition',dest='refineposition',default=False,action='store_true',
help="Refine positions?")
parser.add_option('--plot',action="store_true",dest="plot",default=False,
help="Save diagnostic plots?")
parser.add_option('--region',action="store_true",dest="region",default=False,
help="Save ds9 region?")
parser.add_option('-m','--cores',default=1,type='int',dest='cores',
help='Number of cores for Aegean [default=%default]')
parser.add_option('-v','--verbose',action="store_true",dest="verbose",default=False,
help="Increase verbosity of output")
(options, args) = parser.parse_args()
if (options.verbose):
logger.setLevel(logging.INFO)
for file in args:
out=fluxmatch(file,
catalog=options.catalog,
fluxcolumn=options.fluxcolumn,
fluxerrcolumn=options.fluxerrcolumn,
racolumn=options.racolumn,
deccolumn=options.deccolumn,
nsigma=options.nsigma,
matchradius=options.matchradius,
rmsfactor=options.rmsfactor,
maxdistance=options.maxdistance,
minbeam=options.minbeam,
psfextent=options.psfextent,
limit=options.ratiolimit,
rejectsigma=options.rejectsigma,
update=options.update,
prefix=options.prefix,
updatepoln=options.updatepoln,
updatebane=options.updatebane,
otherimages=[],
refineposition=options.refineposition,
plot=options.plot,
region=options.region)
sys.exit(0)
################################################################################
if __name__=="__main__":
main()