def measure_MGPS(snr): sourcedir = "/media/data/Dropbox/MWA/SNR_search/confirm_candidates/regrid_all/MGPS/" fitsfile = sourcedir + snr.name + "_MGPS.fits" final_flux, total_flux, bkg_flux, rms_flux, nbeam = find_fluxes( snr.polygon, snr.sources, snr.exclude, fitsfile) make_single_plot(snr.polygon, snr.sources, snr.exclude, fitsfile) print "final flux, total flux, background flux, rms flux, number of beams" print final_flux, total_flux, bkg_flux, rms_flux, nbeam # In units of Jy return final_flux, total_flux, bkg_flux, rms_flux, nbeam
def do_fit(colors, snr): freqs = [] fluxes = [] bkg = [] rms = [] nbeams = [] for color in colors: if color == "white": fitsfile = color + "/" + snr.name + ".fits" else: fitsfile = color + "/rpj/" + snr.name + ".fits" if os.path.exists(fitsfile): hdu = fits.open(fitsfile) freqs.append(hdu[0].header["FREQ"]) final_flux, total_flux, bkg_flux, rms_flux, nbeam = find_fluxes( snr.polygon, snr.sources, snr.exclude, fitsfile) fluxes.append(final_flux) bkg.append(bkg_flux) rms.append(rms_flux) nbeams.append(nbeam) else: print "Unable to fit flux for {0} in {1}".format(snr.name, color) fluxes = np.array(fluxes) freqs = np.array(freqs) logfreqs = np.log([f / normfreq for f in freqs]) logfluxes = np.log(fluxes) fluxerrors = [ np.sqrt((0.02 * s)**2 + n * (r**2)) for s, r, n in zip(fluxes, rms, nbeams) ] logfluxerrors = np.array([e / f for e, f in zip(fluxerrors, fluxes)]) alpha, err_alpha, amp, err_amp, chi2red = fit_spectrum( logfreqs[np.where(fluxes > 0)], logfluxes[np.where(fluxes > 0)], logfluxerrors[np.where(fluxes > 0)]) return snr, dict(zip(freqs, fluxes)), dict(zip(freqs, bkg)), dict( zip(freqs, rms)), dict(zip(freqs, nbeams)), alpha, err_alpha, amp, err_amp, chi2red
def measure_E11(snr): sourcedir = "/media/data/Dropbox/MWA/SNR_search/confirm_candidates/regrid_all/E11/" fitsfile = sourcedir + snr.name + "_E11.fits" final_flux, total_flux, bkg_flux, rms_flux, nbeam = find_fluxes( snr.polygon, snr.sources, snr.exclude, fitsfile) hdu = fits.open(fitsfile) T = mK2K(final_flux) nu = WL2freq(cm2m(11.)) # Effelsberg 11cm survey bmaj = hdu[0].header["BMAJ"] bmin = hdu[0].header["BMIN"] make_single_plot(snr.polygon, snr.sources, snr.exclude, fitsfile) print "final temperature, total temperature, background temperature, rms temperature (all in K), number of beams" print mK2K(final_flux), mK2K(total_flux), mK2K(bkg_flux), mK2K( rms_flux), nbeam # In units of Jy S_11 = T2PFD(T, nu, bmaj, bmin) tot_11 = T2PFD(mK2K(total_flux), nu, bmaj, bmin) bkg_11 = T2PFD(mK2K(bkg_flux), nu, bmaj, bmin) rms_11 = T2PFD(mK2K(rms_flux), nu, bmaj, bmin) print "final flux, total flux, background flux, rms flux, number of beams" print S_11, tot_11, bkg_11, rms_11, nbeam return S_11, tot_11, bkg_11, rms_11, nbeam
def poly_plot(fitsfile, makeplots): print "Fitting polygons to " + fitsfile hdu = fits.open(fitsfile) data = hdu[0].data xmax = hdu[0].header["NAXIS1"] ymax = hdu[0].header["NAXIS2"] w = wcs.WCS(hdu[0].header) try: pix2deg = hdu[0].header["CD2_2"] except KeyError: pix2deg = hdu[0].header["CDELT2"] def update(val): vmin = svmin.val vmax = svmax.val img.set_clim([svmin.val, svmax.val]) fig.canvas.draw() fig = plt.figure(figsize=(5, 5)) ax = fig.add_axes([0.05, 0.15, 0.8, 0.8]) img = ax.imshow(data, cmap="gray") cbaxes = fig.add_axes([0.85, 0.1, 0.03, 0.85]) cb = plt.colorbar(img, cax=cbaxes, orientation="vertical") axcolor = 'white' axmin = fig.add_axes([0.05, 0.05, 0.2, 0.02], axisbg=axcolor) axmax = fig.add_axes([0.44, 0.05, 0.2, 0.02], axisbg=axcolor) vmin0 = np.min(data) vmax0 = np.max(data) # include the slider: cribbed from https://stackoverflow.com/questions/5611805/using-matplotlib-slider-widget-to-change-clim-in-image svmin = Slider(axmin, "vmin", -5.0, 1.0, valinit=vmin0) svmax = Slider(axmax, "vmax", -1.0, 10.0, valinit=vmax0) svmin.on_changed(update) svmax.on_changed(update) fig.suptitle( 'Left-click = select source, middle-click = source subtract, right-click = select region to exclude from background calculation' ) # If you don't do this, it automatically zooms out when you first click on the plot ax.set_xlim(0, xmax) ax.set_ylim(0, ymax) polypick = PolyPick(ax) plt.show() # Export pixel co-ordinates as WCS co-ordinates in order to use on any arbitrary image, and save for later polygon = Coords() sources = Coords() exclude = Coords() if len(polypick.points.x): sources.x, sources.y = w.wcs_pix2world( zip(polypick.points.x, polypick.points.y), 0).transpose() if len(polypick.exclude.x): exclude.x, exclude.y = w.wcs_pix2world( zip(polypick.exclude.x, polypick.exclude.y), 0).transpose() # Now I have the co-ordinates... # Go measure the flux densities if len(polypick.coords.x): polygon.x, polygon.y = w.wcs_pix2world( zip(polypick.coords.x, polypick.coords.y), 0).transpose() find_fluxes(polygon, sources, exclude, fitsfile) # And make nice plots if makeplots is True: make_single_plot(polygon, sources, exclude, fitsfile) else: print "Failed to draw any points; exiting."