def mass_photometry(fname, outfile): filenames = glob('/lustre/aoc/students/bmcclell/w51/' + fname) objs = [] print('Loading files') pb = ProgressBar(len(filenames)) for f in filenames: rs = dendrocat.RadioSource(fits.open(f)) objs.append(rs) pb.update() #n = np.shape(objs[0].data)[0] #center = regions.PixCoord(n/2, n/2) #reg = regions.CirclePixelRegion(center, 3200) #mask = reg.to_mask() #img = mask.to_image((n, n)).astype(bool) #objs[0].data = np.where(img==True, objs[0].data, np.nan) # Debugging #plt.figure() #plt.imshow(objs[0].data) #plt.show() #objs[0].threshold = 4.5 #objs[0].min_value = 1.1e-4 #objs[0].min_delta = 1.2*objs[0].min_value #objs[0].to_catalog() #objs[0].autoreject() #objs[0].reject([44001, 44032]) #dendrocat.utils.save_regions(objs[0].catalog, '/users/bmcclell/nrao/reg/test_mass_photometry.reg') #print('Autorejection complete') t = Table.read('/users/bmcclell/nrao/cat/w51IRS2_photometered.dat', format='ascii') #for col in t.colnames: # if 'GHz' in col: # t.remove_column(col) mc = dendrocat.MasterCatalog(*objs, catalog=t) print('\nMaster Catalog made') start = time.time() mc.photometer(dendrocat.ellipse) stop = time.time() print('Ellipse apertures photometered. Time: {} s'.format(stop - start)) start = time.time() mc.photometer(dendrocat.annulus) stop = time.time() print('Annulus apertures photometered. Time: {} s'.format(stop - start)) start = time.time() mc.catalog.write(outfile, format='ascii', overwrite=True) stop = time.time() print('Catalog written. Time: {} s'.format(stop - start))
from collections import OrderedDict irs2 = Table.read('/users/bmcclell/nrao/cat/w51IRS2_photometered.dat', format='ascii') n1 = dendrocat.RadioSource( fits.open( '/users/bmcclell/Data/irs2/w51n_band6_226GHz_adjustedRADEC.fits')) n2 = dendrocat.RadioSource( fits.open( '/users/bmcclell/Data/irs2/w51north_sci.spw0_1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19.mfs.I.manual.image.tt0.pbcor.fits' )) n3 = dendrocat.RadioSource( fits.open('/users/bmcclell/Data/irs2/w51n_bandQ_45GHz_adjustedRADEC.fits')) n3.nu = 45.0 * u.GHz n3.freq_id = '45.0GHz' n3.set_metadata() mc = dendrocat.MasterCatalog(n1, n2, n3, catalog=irs2) accepted = mc.catalog[mc.catalog['rejected'] == 0] for i in range(len(accepted)): nus, fluxes, errs = mc.plotsedgrid( accepted[i], alphas=[1, 2, 3, 4], path='/users/bmcclell/nrao/documentation/SEDS_IRS2/') print(' nus', nus) print('fluxes', fluxes) print('errors', errs)
rs1.autoreject() rs2.autoreject() rs1.add_sources( Table.read('/media/connor/Lore/Summer2018/nrao/cat/special_source.dat', format='ascii')) rs1.reject( [226000, 226008, 226016, 226023, 226028, 226085, 226124, 226135, 226137]) rs1.accept([226024, 226043, 226123, '226111lowd']) rs2.accept([93005, 93006, 93007, 93012, 92021, 93054, 93053]) rs2.reject([93009, 93010, 93016, 93022, 93024, 93030, 93028, 93031, 93059]) mc = dendrocat.MasterCatalog(rs1, rs2, rs3, catalog=dendrocat.utils.match(rs1, rs2).catalog) mc.photometer(Ellipse, Circle, Annulus) mc.catalog['_name'][mc.catalog['_name'] == '226083'] = 'w51e2e' mc.catalog['_name'][mc.catalog['_name'] == '93045'] = 'w51e2w' mc.catalog['_name'][mc.catalog['_name'] == '226040'] = 'w51e8' dendrocat.utils.saveregions( mc.catalog, '/media/connor/Lore/Summer2018/nrao/reg/mc_regions.reg') mc.catalog.write( '/media/connor/Lore/Summer2018/nrao/cat/45-93-226GHz_photometered_adjustedRADEC.dat', format='ascii', overwrite=True)
t = Table(Table.read( '/users/bmcclell/nrao/cat/43-93-226GHz_photometered_ellipse.dat', format='ascii'), masked=True) rs1 = dendrocat.RadioSource( fits.open( '/lustre/aoc/students/bmcclell/w51/W51e2_cont_briggsSC_tclean.image.fits.gz' )) rs2 = dendrocat.RadioSource( fits.open( '/lustre/aoc/students/bmcclell/w51/w51e2_sci.spw0_1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19.mfs.I.manual.image.tt0.pbcor.fits.gz' )) rs3 = dendrocat.RadioSource( fits.open( '/lustre/aoc/students/bmcclell/w51/W51e2w_QbandAarray_cont_spws_continuum_cal_clean_2terms_robust0_incrementalselfcal8.image.tt0.pbcor.icrs.fits' )) rs3.nu = 45 * u.GHz rs3.freq_id = '45GHz' rs3.set_metadata() mc = dendrocat.MasterCatalog(rs1, rs2, rs3, catalog=t) nus, fluxes, errs = mc.plot_sed(mc.catalog[23], aperture='ellipse', alphas=[1, 2, 3]) print(nus) print(fluxes) print(errs) plt.show()
def dendrocatify(regname, fn, threshold=4, min_npix=100, min_delta=1, cutout=None, cutout_header=None, snr_threshold=5, noiselevel_table='noise_levels.json'): noisefilepath = f"{catalog_path}/{noiselevel_table}" if os.path.exists(noisefilepath): with open(noisefilepath, 'r') as fh: noiselevel_table = json.load(fh) else: noiselevel_table = {} print(f"Starting region {regname}: {fn}") if cutout_header is None or cutout is None: fh = fits.open(fn) data = fh[0].data header = fh[0].header # LONPOLE isn't very relevant and LATPOLE is not part of the coordinate # systems we're interested in. From Calabretta 2002: "LATPOLEa is never # required for zenithal projections" try: del header['LONPOLE'] del header['LATPOLE'] except KeyError: pass ww = wcs.WCS(header) pixscale = wcs.utils.proj_plane_pixel_area(ww)**0.5 * u.deg reg = cutout_regions[regname].to_pixel(ww) mask = reg.to_mask() data_sm = convolve_fft(data, Gaussian2DKernel( (45 * u.arcsec / pixscale).decompose()), allow_huge=True) data_filtered = data - data_sm err = mad_std(data_filtered) cutout = mask.multiply(data_filtered) frame = wcs.utils.wcs_to_celestial_frame(ww) # TODO: check that this is working - the regions output seem to be offset? ww_cutout = ww[mask.bbox.iymin:mask.bbox.iymax, mask.bbox.ixmin:mask.bbox.ixmax] # add beam parameters to header cutout_header = ww_cutout.to_header() cutout_header['BMAJ'] = mustang_beam_fwhm.to(u.deg).value cutout_header['BMIN'] = mustang_beam_fwhm.to(u.deg).value cutout_header['BUNIT'] = 'Jy/beam' cutout_header['TELESCOP'] = 'GBT' cutout_header['FREQ'] = mustang_central_frequency.to(u.Hz).value avg_noise = mad_std(data, ignore_nan=True) filt_avg_noise = mad_std(cutout, ignore_nan=True) noiselevel_table[regname] = { 'noise': avg_noise, 'filtered_noise': filt_avg_noise } with open(noisefilepath, 'w') as fh: json.dump(noiselevel_table, fh) print( f"Beginning cataloging. Noise is {avg_noise:0.4f} before and {filt_avg_noise:0.4f} after filtering" ) radiosource = dendrocat.RadioSource( [fits.PrimaryHDU(data=cutout, header=cutout_header)]) radiosource.nu = mustang_central_frequency radiosource.freq_id = 'MUSTANG' radiosource.set_metadata() print("Running to_dendrogram") radiosource.to_dendrogram( min_value=err * threshold, min_delta=err * min_delta, min_npix=min_npix, ) print("Making plot grid") radiosource.plot_grid( skip_rejects=False, outfile= f'{catalog_figure_path}/{regname}_dendrocat_thr{threshold}_minn{min_npix}_mind{min_delta}_prerejection.png', figurekwargs={'num': 1}, ) pl.figure(1).clf() #radiosource.autoreject(threshold=5.0) rejected = radiosource.catalog[ 'MUSTANG_dend_flux'] / filt_avg_noise < snr_threshold #radiosource.catalog.add_column(Column(rejected, name='rejected')) radiosource.catalog['rejected'] = rejected.astype('int') print("Rejected {0}, kept {1}, of {2} total sources".format( radiosource.catalog['rejected'].sum(), (1 - radiosource.catalog['rejected']).sum(), len(radiosource.catalog))) radiosource.plot_grid( skip_rejects=False, outfile= f'{catalog_figure_path}/{regname}_dendrocat_thr{threshold}_minn{min_npix}_mind{min_delta}_postrejection.png', figurekwargs={'num': 1}, ) pl.figure(1).clf() #dend = astrodendro.Dendrogram.compute(cutout, # min_value=err*threshold, # min_delta=err*min_delta, # verbose=True, min_npix=min_npix, # wcs=ww_cutout) dend = radiosource.dendrogram pl.figure(2).clf() ax = pl.gca() ax.cla() pl.imshow(cutout, cmap='gray_r', interpolation='none', origin='lower', vmax=0.01, vmin=-0.001) pltr = dend.plotter() print("Contouring dendrogram leaves") for struct in ProgressBar(dend.leaves): pltr.plot_contour(ax, structure=struct, colors=['r'], linewidths=[0.9], zorder=5) if struct.parent: while struct.parent: struct = struct.parent pltr.plot_contour(ax, structure=struct, colors=[(0, 1, 0, 1)], linewidths=[0.5]) cntr = pl.gca().collections pl.setp([x for x in cntr if x.get_color()[0, 0] == 1], linewidth=0.25) pl.setp([x for x in cntr if x.get_color()[0, 1] == 1], linewidth=0.25) pl.savefig( f'{catalog_figure_path}/{regname}_dend_contour_thr{threshold}_minn{min_npix}_mind{min_delta}.pdf' ) # zoom only for G31 if regname == 'G31': pl.axis((1125.4006254228616, 1670.3650637799306, 1291.6829155596627, 1871.8063499397681)) pl.setp([x for x in cntr if x.get_color()[0, 0] == 1], linewidth=0.75) # Red pl.setp([x for x in cntr if x.get_color()[0, 1] == 1], linewidth=0.5) # Green pl.savefig( f'{catalog_figure_path}/{regname}_dend_contour_thr{threshold}_minn{min_npix}_mind{min_delta}_zoom.pdf' ) #metadata = {'data_unit': u.Jy / u.beam, # 'beam_major': 8*u.arcsec, # 'beam_minor': 8*u.arcsec, # 'wcs': ww_cutout, # 'spatial_scale': wcs.utils.proj_plane_pixel_scales(ww_cutout).mean()*u.deg, # } #ppcat = astrodendro.pp_catalog(dend, metadata) ppcat = radiosource.catalog mastercatalog = dendrocat.MasterCatalog(radiosource, catalog=ppcat) aperture1 = Circle([0, 0], 10 * u.arcsec, name='10as', frame=frame.name) aperture2 = Circle([0, 0], 15 * u.arcsec, name='15as', frame=frame.name) background = Annulus([0, 0], inner=15 * u.arcsec, outer=20 * u.arcsec, name='background', frame=frame.name) print("Beginning photometry ...") mastercatalog.photometer(aperture1, aperture2, background) print() source_center = coordinates.SkyCoord(mastercatalog.catalog['x_cen'], mastercatalog.catalog['y_cen'], unit=(u.deg, u.deg), frame=frame.name) source_name = [ "G{0:0.3f}{1:+0.3f}".format(sc.galactic.l.deg, sc.galactic.b.deg) for sc in source_center ] mastercatalog.catalog.add_column( Column(name='SourceName', data=source_name)) mastercatalog.catalog.write( f'{catalog_path}/{regname}_dend_contour_thr{threshold}_minn{min_npix}_mind{min_delta}.ipac', format='ascii.ipac') regs = ppcat_to_regions(ppcat, frame.name) regions.write_ds9( regions=regs, filename= f'{catalog_path}/{regname}_dend_contour_thr{threshold}_minn{min_npix}_mind{min_delta}.reg' ) return radiosource, dend, mastercatalog, ppcat