cube_tpkrg = SpectralCube.read(tpkrg) cube_tpkrg.with_spectral_unit(u.km/u.s, rest_value=intcube.wcs.wcs.restfrq*u.Hz, velocity_convention='radio').write(tpoutfn, overwrite=True) print("Single dish beam = {0}".format(cube_tpkrg.beam)) # intermediate work: test that a single frame has been properly combined frq = intcube.wcs.wcs.restfrq*u.Hz * (1-13/3e5) # approximately 13 kms closestchan = intcube.closest_spectral_channel(frq) im = intcube[closestchan] * jtok[closestchan] sdim = cube_tpkrg[cube_tpkrg.closest_spectral_channel(frq)] im.write('singleframe_cubek_13kms.fits', overwrite=True) sdim.write('singleframe_tpcube_k_rg_13kms.fits', overwrite=True) combohdu, hdu2 = feather_simple(im.hdu, sdim.hdu, return_regridded_lores=True, return_hdu=True) combohdu.writeto('singleframe_TP_7m_12m_feather_65kms.fits', clobber=True) hdu2.writeto('singleframe_tpcube_k_spatialandspectralregrid_65kms.fits', clobber=True) # "feather" the cubes together based on the low-resolution beam size combhdu = fourier_combine_cubes(intcube_k, cube_tpkrg, return_hdu=True, lowresfwhm=cube_tpkrg.beam.major) log.info("Fourier combination completed.") # restore all of the NaN values from the interferometer data back to NaN combhdu.data[intcube.mask.exclude()] = np.nan combhdu.writeto(outfilename, clobber=True) log.info("Wrote file to {0}".format(outfilename)) # convert the cube spectral axis to km/s (...probably unnecessary now) combcube = SpectralCube.read(combhdu).with_spectral_unit(u.km/u.s,
tpcube_k_ds_rg = tpcube_k_ds.spectral_interpolate(cube_k.spectral_axis) #tpkrg = spectral_regrid(tpcube_k_ds, cube_k.spectral_axis) log.debug("done regridding") #tpkrg.writeto('HC3N_tp_freq_ds_interp.fits', clobber=True) tpcube_k_ds_rg.write('HC3N_tp_freq_ds_interp_r05.fits', overwrite=True) # #cube_tpkrg = SpectralCube.read('HC3N_tp_freq_ds_interp.fits') frq = 90.9662 * u.GHz im = cube_k[cube_k.closest_spectral_channel(frq)] sdim = tpcube_k_ds_rg[tpcube_k_ds_rg.closest_spectral_channel(frq)] im.write('cubek_ch126_r05.fits', overwrite=True) sdim.write('tpcube_k_rg_ch126_r05.fits', overwrite=True) combohdu, hdu2 = feather_simple(im.hdu, sdim.hdu, return_regridded_lores=True, return_hdu=True) combohdu.writeto('HC3N_TP_7m_12m_feather_126_r05.fits', clobber=True) # final goal combhdu = fourier_combine_cubes(cube_k, tpcube_k_ds_rg, return_hdu=True, lowresfwhm=tpcube_k_ds_rg.beam.major, maximum_cube_size=3e9) combhdu.header.update(cube_k.header) combhdu.header.update(avbm.to_header_keywords()) combcube = SpectralCube.read(combhdu).with_spectral_unit( u.km / u.s, velocity_convention='radio') combcube.write('HC3N_TP_7m_12m_feather_r05.fits', overwrite=True)
frq7m,pow7m = fft_psd_tools.psds.pspec(fft_psd_tools.psds.PSD2(slc7m.value*jtok_7m[ch_65kms_7m]), view=False) frq12m,pow12m = fft_psd_tools.psds.pspec(fft_psd_tools.psds.PSD2(slc12m.value*jtok_12m[ch_65kms_12m]), view=False) frq12m_conv,pow12m_conv = fft_psd_tools.psds.pspec(fft_psd_tools.psds.PSD2(slc12m_conv_to_7m*jtok_12m[ch_65kms_12m]), view=False) frqTP,powTP = fft_psd_tools.psds.pspec(fft_psd_tools.psds.PSD2(slcTP), view=False) frq7m12m,pow7m12m = fft_psd_tools.psds.pspec(fft_psd_tools.psds.PSD2(slc7m12m.value*jtok_7m12m[ch_65kms_7m12m]), view=False) # some UVcombine tests; useful to compare to the above. Need psds still import uvcombine hdu7m = slc7m.hdu.copy() hdu7m.data *= jtok_7m[ch_65kms_7m] hdu7m.header['BUNIT'] = 'K' hdu12m = slc12m.hdu.copy() hdu12m.data *= jtok_12m[ch_65kms_12m] hdu12m.header['BUNIT'] = 'K' im7mTP = uvcombine.feather_simple(hdu7m, tp_fn) hdu7mTP = hdu7m.copy() hdu7mTP.data = im7mTP.real im12m7mTP = uvcombine.feather_simple(hdu12m, hdu7mTP) hdu12m7mTP = hdu12m.copy() hdu12m7mTP.data = im12m7mTP.real frq12m7mTP,pow12m7mTP = fft_psd_tools.psds.pspec(fft_psd_tools.psds.PSD2(hdu12m7mTP.data), view=False) frq7mTP,pow7mTP = fft_psd_tools.psds.pspec(fft_psd_tools.psds.PSD2(hdu7mTP.data), view=False) import pylab as pl pl.figure(1).clf() pl.loglog(pixscale7m.to(u.arcsec).value/frq7m, pow7m, label='7m')
sdim = cube_tpkrg[cube_tpkrg.closest_spectral_channel(frq)] imJy.write('singleframes/{species}{suffix}_cubeJy_65kms.fits'.format(species=species, suffix=suffix), overwrite=True) imK.write('singleframes/{species}{suffix}_cubek_65kms.fits'.format(species=species, suffix=suffix), overwrite=True) sdim.write('singleframes/{species}{suffix}_tpcube_k_rg_65kms.fits'.format(species=species, suffix=suffix), overwrite=True) # for sanity checking purposes, write out the Jy version too sdim_Jy = sdim.to(u.Jy, tpcube.beam.jtok_equiv(frq)) sdim_Jy._unit = u.Jy/u.beam sdim_Jy.write('singleframes/{species}{suffix}_tpcube_Jy_rg_65kms.fits'.format(species=species, suffix=suffix), overwrite=True) imKhdu = imK.hdu #imKhdu.data[imKhdu.data<0] = 0 # DELETE THIS IT'S WRONG combohdu, hdu2 = feather_simple(imKhdu, sdim.hdu, lowresscalefactor=1.0, highresscalefactor=1.0, highpassfilterSD=False, replace_hires=False, deconvSD=False, return_regridded_lores=True, return_hdu=True) combohdu.writeto('singleframes/{species}{suffix}_TP_7m_12m_feather_65kms.fits'.format(species=species, suffix=suffix), clobber=True) hdu2.header['BMAJ'] = tpcube_k_ds_hdu.header['BMAJ'] hdu2.header['BMIN'] = tpcube_k_ds_hdu.header['BMIN'] hdu2.header['BPA'] = tpcube_k_ds_hdu.header['BPA'] hdu2.writeto('singleframes/{species}{suffix}_tpcube_k_spatialandspectralregrid_65kms.fits'.format(species=species, suffix=suffix), clobber=True) combohdu, hdu2 = feather_simple(imK.hdu, sdim.hdu, return_regridded_lores=True, return_hdu=True, replace_hires=0.5) combohdu.writeto('singleframes/{species}{suffix}_TP_7m_12m_replacefeather_65kms.fits'.format(species=species, suffix=suffix), clobber=True) if 'do_full_cube' not in locals() or do_full_cube: # final goal os.system('cp {0} {1}'.format(interferometer_fn, outfilename))
lowresfwhm=11 * u.arcsec, hires_threshold=0.0005, lores_threshold=0.001) pl.savefig('pspec_threshold.png') rslts_nothresh = uvcombine.feather_plot(alma_image, lores=mgps_image, lowresfwhm=11 * u.arcsec) pl.savefig('pspec_nothreshold.png') hdu_low, im_lowraw, header_low = file_in(lores) hdu_hi, im_hi, header_hi = file_in(hires) pl.figure(2).clf() combined = uvcombine.feather_simple(alma_image, mgps_image, lowresfwhm=10 * u.arcsec) pl.imshow(visualization.AsinhStretch()(combined.real), vmax=0.1, origin='lower', interpolation='none') hdr = fits.getheader(alma_image) combined_default = uvcombine.feather_simple(alma_image, mgps_image) fits.PrimaryHDU(data=np.abs(combined_default), header=hdr).writeto('feathered_MGPS_ALMATCTE7m.fits', overwrite=True) combined_deconvsd = uvcombine.feather_simple(alma_image, mgps_image,
loresfh.data > stats.mad_std(loresfh.data, ignore_nan=True) * 6) pl.plot(repr_alma.flat, loresfh.data.flat, ',') pl.plot(repr_alma.flat, unsharp_gb.flat, '.') pl.plot(repr_alma[ok], unsharp_gb[ok], '.') # TODO: do this using bayes_linear or ODR scalefactor = np.nanmedian(unsharp_gb[ok] / repr_alma[ok]) pl.plot([0, 7.5], [0, 7.5 * scalefactor]) pl.subplot(2, 2, 2).imshow(loresfh.data, vmin=-0.1, vmax=1) pl.subplot(2, 2, 4).imshow(loresfh.data - repr_alma * scalefactor, vmin=-0.1, vmax=1) combined = uvcombine.feather_simple(almafn, lores=loresfh, lowresfwhm=loresfwhm, highresscalefactor=1, lowresscalefactor=1 / scalefactor) hdr = fits.getheader(almafn) fits.PrimaryHDU(data=repr_alma, header=loresfh.header).writeto( paths.root('SgrB2/ALMAsmgridtoMGPS.fits'), overwrite=True) fits.PrimaryHDU(data=sm_alma, header=almafh.header).writeto( paths.root('SgrB2/ALMAsmtoMGPS.fits'), overwrite=True) loresfh.writeto(paths.root('SgrB2/MGPS_SgrB2_zoom_fixed.fits'), overwrite=True) fits.PrimaryHDU(data=combined.real, header=hdr).writeto( paths.root('SgrB2/feathered_MGPS_ALMATCTE7m.fits'), overwrite=True) pl.figure(1).clf()
# for sanity checking purposes, write out the Jy version too sdim_Jy = sdim.to(u.Jy, tpcube.beam.jtok_equiv(frq)) sdim_Jy._unit = u.Jy / u.beam sdim_Jy.write( 'singleframes/{species}{suffix}{robust}_tpcube_Jy_rg_65kms.fits'. format(species=species, suffix=suffix, robust=robust), overwrite=True) imKhdu = imK.hdu #imKhdu.data[imKhdu.data<0] = 0 # DELETE THIS IT'S WRONG combohdu, hdu2 = feather_simple(imKhdu, sdim.hdu, lowresscalefactor=1.0, highresscalefactor=1.0, highpassfilterSD=False, replace_hires=False, deconvSD=False, return_regridded_lores=True, return_hdu=True) combohdu.header.update(cube.header) combohdu.header.update(avgbeam.to_header_keywords()) combohdu.writeto( 'singleframes/{species}{suffix}{robust}_TP_7m_12m_feather_65kms.fits'. format(species=species, suffix=suffix, robust=robust), clobber=True) hdu2.header['BMAJ'] = tpcube_k_ds_hdu.header['BMAJ'] hdu2.header['BMIN'] = tpcube_k_ds_hdu.header['BMIN'] hdu2.header['BPA'] = tpcube_k_ds_hdu.header['BPA'] hdu2.writeto( 'singleframes/{species}{suffix}{robust}_tpcube_k_spatialandspectralregrid_65kms.fits'
planckfn = os.path.join(rootdir, 'planck/{0}_100GHz.fits'.format(regname)) planckfn_scaled = os.path.join(rootdir, 'planck/{0}_scaled_to_92.44GHz.fits'.format(regname)) if not os.path.exists(planckfn) or not os.path.exists(planckfn_scaled): planck_image = SkyView.get_images(center, 'Planck 100')[0][0] # bandpass information here: https://wiki.cosmos.esa.int/planckpla2015/index.php/The_RIMO#HFI_2 # adopt central frequency assuming spectral index = 3 giving nu_c for 100_avg = 104.225 reffreq_hz = 104.225e9 planck_image.header['REFFREQ'] = reffreq_hz planck_image.header['BMAJ'] = 9.65/60 planck_image.header['BMIN'] = 9.65/60 planck_image.header['BUNIT'] = 'K' planck_image.writeto(planckfn, overwrite=True) # scale planck data to match MGPS assuming alpha=3 mustang_reffreq = 92.44e9 planck_image.data = planck_image.data * (reffreq_hz / mustang_reffreq)**-3 planck_image.header['REFFREQ'] = mustang_reffreq planck_image.writeto(planckfn_scaled, overwrite=True) fh[0].header['BMAJ'] = mustang_beam_fwhm.to(u.deg).value fh[0].header['BMIN'] = mustang_beam_fwhm.to(u.deg).value fh[0].header['REFFREQ'] = mustang_central_frequency.to(u.Hz).value fh[0].header['BUNIT'] = 'Jy/beam' rslt = feather_simple(fh[0], planckfn_scaled) fh[0].data = rslt.real fh.writeto(outfile, overwrite=True)
overwrite=True) log.info("make Interferometric image") intf_data = utils.interferometrically_observe_image(image=input_hdu.data, pixel_scale=pixel_scale, largest_angular_scale=40*u.arcsec, smallest_angular_scale=2*u.arcsec)[0].real intf_hdu = fits.PrimaryHDU(data=intf_data, header=input_hdu.header ) intf_fn = "input_image_sz512as_pl1.5_fwhm2as_scale1as_intf2to40as.fits" convert_to_casa(intf_hdu).writeto(intf_fn, overwrite=True) log.info("make SD image") sd_header = input_hdu.header.copy() sd_header['BMAJ'] = sd_header['BMIN'] = (33*u.arcsec).to(u.deg).value sd_data = utils.singledish_observe_image(image=input_hdu.data, pixel_scale=pixel_scale, beam=radio_beam.Beam(33*u.arcsec)) sd_hdu = fits.PrimaryHDU(data=sd_data, header=sd_header) sd_fn = "input_image_sz512as_pl1.5_fwhm2as_scale1as_sd33as.fits" convert_to_casa(sd_hdu).writeto(sd_fn, overwrite=True) log.info("Feather data") feathered_hdu = feather_simple(hires=intf_hdu, lores=sd_hdu, return_hdu=True) feathered_hdu.writeto("input_image_sz512as_pl1.5_fwhm2as_scale1as_intf2to40as_sd33as_feathered_MJySr.fits", overwrite=True) feathered_hdu = feather_simple(hires=intf_fn, lores=sd_fn, return_hdu=True) feathered_hdu.writeto("input_image_sz512as_pl1.5_fwhm2as_scale1as_intf2to40as_sd33as_feathered_JyBeam.fits", overwrite=True)
lowres=sd_fn_image.as_posix(), sdfactor=sdfactor, lowpassfiltersd=lowpassfilterSD, ) # Right now read as spectralcube despite being a 2D image. casa_feather_proj = SpectralCube.read(output_name)[0] # casa_sd_proj = SpectralCube.read(sd_fn_image)[0] # exportfits(imagename=output, fitsimage=output+".fits", # overwrite=True, dropdeg=True) # Feathering with uvcombine feathered_hdu = feather_simple(hires=intf_proj, lores=sd_proj, lowresscalefactor=sdfactor, lowpassfilterSD=lowpassfilterSD, deconvSD=False, return_hdu=True) uvcomb_feather_proj = Projection.from_hdu(feathered_hdu) diff = (uvcomb_feather_proj - casa_feather_proj).value diff_orig_uvc = (input_proj.value - uvcomb_feather_proj.value) diff_orig_casa = (input_proj.value - casa_feather_proj.value) diffsq = (diff**2) / (casa_feather_proj.value**2) diff_frac_orig = diff / input_proj.value print("Proof that we have exactly reimplemented CASA's feather: ")
header['CRVAL2'] = 0.0 header['CRPIX2'] = 350.0 totalimg, weightimg = np.zeros([header['NAXIS2'], header['NAXIS1']]), np.zeros( [header['NAXIS2'], header['NAXIS1']]) for fn in urls: fh = fits.open(fn) reproj, weight = reproject.reproject_interp(fh, header) totalimg[weight.astype('bool')] += reproj[weight.astype('bool')] weightimg += weight header['BUNIT'] = 'Jy/beam' rslt = fits.PrimaryHDU(data=totalimg / weightimg, header=header) rslt.writeto( os.path.expanduser('~/Dropbox/SMA_CMZ_FITS_files/BGPS_Mosaic.fits'), overwrite=True) sma_mosaic = fits.open( os.path.expanduser( '~/Dropbox/CMZoom_Data/continuum_images/mosaic_JySr.fits')) #sma_mosaic[0].header['BUNIT'] = 'Jy/sr' comb = uvcombine.feather_simple(sma_mosaic[0], rslt) comb_hdu = fits.PrimaryHDU(data=comb.real, header=sma_mosaic[0].header) comb_hdu.writeto( os.path.expanduser( '~/Dropbox/CMZoom_Data/continuum_images/sma_bgps_feather.fits'))