def run_pspec(cube, distance=414 * u.pc, xunit=u.pix**-1, pickle_file=None, keep_data=False, **kwargs): cube = SpectralCube.read(cube) mom0_hdu = cube.moment0().hdu pspec = PowerSpectrum(mom0_hdu, distance=distance) pspec.run(xunit=xunit, **kwargs) if pickle_file: pspec.save_results(pickle_file, keep_data=keep_data) return pspec
def pspec_noise(cube="../subcubes/c18o_jrf_l1641n.fits", vel_low=12 * u.km / u.s, vel_hi=16 * u.km / u.s, run_kwargs={}, pspec_file="pspec_noise_c18o_jrf_l1641n"): try: cube = SpectralCube.read(cube) except: pass noise_hdu = cube.spectral_slab(vel_low, vel_hi).moment0().hdu noise_pspec = PowerSpectrum(noise_hdu) noise_pspec.run(**run_kwargs) noise_pspec.save_results(pspec_file) return noise_pspec
save_name)) and skip_check: print("Already saved pspec for {}. Skipping".format(filename)) continue else: os.system("rm -f {}".format(osjoin(data_path, gal, save_name))) pspec = PowerSpectrum(proj, distance=dist) pspec.run(verbose=False, beam_correct=False, fit_2D=False, high_cut=0.1 / u.pix, use_pyfftw=True, threads=ncores, apodize_kernel=fitinfo_dict[gal][name]['apod_kern']) pspec.save_results(osjoin(data_path, gal, save_name), keep_data=False) del pspec, proj, hdu if run_fits: row_names = [] fit_results = { 'logA': [], 'ind': [], 'logB': [], 'logA_std': [], 'ind_std': [], 'logB_std': [] }
# Now open the kernel file kernfits_name = names[name][0] kernfits_ext = names[name][1] kernel_filename = osjoin(kern_path, kernfits_name) kern_proj = Projection.from_hdu(fits.open(osjoin(kern_path, kernel_filename))[kernfits_ext]) img_scale = np.abs(proj_plane_pixel_scales(proj.wcs))[0] kern_scale = np.abs(proj_plane_pixel_scales(kern_proj.wcs))[0] kernel = resize_psf(kern_proj.value, kern_scale, img_scale) # Normalize to make a kernel kernel /= kernel.sum() kern_pspec = PowerSpectrum((kernel, kern_proj.header)) kern_pspec.run(verbose=False, fit_2D=False) save_name = "{0}_kernel_{1}.pspec.pkl".format(name, gal.lower()) kern_pspec.save_results(osjoin(data_path, gal, save_name), keep_data=True) # plt.draw() # input("?") # # plt.clf() # plt.close()
pspec.compute_radial_pspec() # pspec.fit_pspec() # high_cut=0.1 / u.pix,) # pspec.run(verbose=False, beam_correct=False, fit_2D=False, # high_cut=0.1 / u.pix, # use_pyfftw=True, threads=ncores, # apodize_kernel=fitinfo_dict[gal]['apod_kern']) if make_interactive: print(pspec.slope) pspec.plot_fit(show_residual=False, show_2D=True) plt.draw() input(gal) plt.close() full_save_name = osjoin(data_path, gal, save_name) pspec.save_results(full_save_name, keep_data=False) if do_fitpspec: nsamp = 6000 row_names = [] fit_results = { 'logA': [], 'ind': [], 'logB': [], 'logC': [], 'logA_std': [], 'ind_std': [], 'logB_std': [],
print("Already saved pspec for {}. Skipping".format(filename)) continue else: os.system("rm -f {}".format(osjoin(data_path, 'raw', save_name))) pspec = PowerSpectrum(proj, distance=dist) pspec.run(verbose=False, beam_correct=False, fit_2D=False, high_cut=0.1 / u.pix, use_pyfftw=True, threads=ncores, apodize_kernel=fitinfo_dict[gal][name]['apod_kern']) pspec.save_results(osjoin(data_path, 'raw', save_name), keep_data=False) del pspec, proj, hdu # Make PSF power-spectra in with the same pix scale as the data if run_pspec_psfs: from glob import glob import numpy as np from astropy.io import fits from spectral_cube import Projection from radio_beam import Beam import astropy.units as u from astropy.wcs.utils import proj_plane_pixel_scales import matplotlib.pyplot as plt
do_makepspec_dust_smooth = False do_fitpspec_dust_smooth = True if do_makepspec: pspec_name = osjoin(data_path, 'M31_CO', 'm33_hi_co_dustSD.pspec.pkl') gas_sd = hi_proj * hi_mass_conversion + co10_mass_conversion * co_proj gas_sd[np.isnan(hi_proj)] = np.NaN pspec = PowerSpectrum(gas_sd, distance=720 * u.kpc) pspec.run(verbose=False, fit_2D=False, high_cut=10**-1.3 / u.pix) # pspec.plot_fit() pspec.save_results(pspec_name) # Fit the pspec. if do_fitpspec: pspec = PowerSpectrum.load_results(pspec_name) pspec.load_beam() beam_size = pspec._beam.major.to(u.deg) / pspec._ang_size.to(u.deg) beam_size = beam_size.value beam_gauss_width = beam_size / np.sqrt(8 * np.log(2)) high_cut = (1 / (beam_gauss_width * 3.)) fit_mask = pspec.freqs.value < high_cut
hi_pspec_name = f"{hi_name.rstrip('fits')}pspec.pkl" hi_pspec_name_conv = f"{hi_name.rstrip('fits')}conv.pspec.pkl" if do_makepspec: hdu = fits.open(hi_name) pspec = PowerSpectrum(hdu[0], distance=840 * u.kpc) pspec.run(verbose=False, fit_2D=False) pspec.save_results(hi_pspec_name) if do_fitpspec: # Fit the same as the dust column density model plot_folder = osjoin(data_path, "{}_plots".format(gal)) if not os.path.exists(plot_folder): os.mkdir(plot_folder) nsamp = 6000 row_names = [] gal = 'M33'
# Make an HDU for the beam so we can make a model of its power-spectrum deproj_beam_hdu = fits.PrimaryHDU(beam_arr) pspec_dep_beam = PowerSpectrum(deproj_beam_hdu) pspec_dep_beam.run(verbose=False, beam_correct=False, fit_2D=False, high_cut=0.1 / u.pix, use_pyfftw=True, threads=ncores) save_name = "{0}_{1}_{2}_mjysr_deproj_beam.pspec.pkl".format( gal.lower(), 'spire500', res_type) pspec_dep_beam.save_results(osjoin(data_path, gal, save_name)) # Make an interpolated model from the ps1D of the beam spl = InterpolatedUnivariateSpline(pspec_dep_beam.freqs.value, pspec_dep_beam.ps1D) largest_val = pspec_dep_beam.ps1D[0] smallest_freq = pspec_dep_beam.freqs.value[0] def beam_model_dep(f): beam_vals = np.empty_like(f) # beam_vals = T.zeros_like(f) # if on scales larger than the kernel image, return # value on largest scale beam_vals[f < smallest_freq] = largest_val