Esempio n. 1
0
def make_psf_beam_function(kern_fpath):
    # Load in the pspec and use a spline fit of the pspec for the beam
    # model
    kern_pspec = PowerSpectrum.load_results(kern_fpath)

    largest_val = kern_pspec.ps1D[0]
    smallest_freq = kern_pspec.freqs.value[0]

    spl = InterpolatedUnivariateSpline(kern_pspec.freqs.value, kern_pspec.ps1D)

    def beam_model(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
        beam_vals[f >= smallest_freq] = spl(f[f >= smallest_freq])

        return beam_vals

    return beam_model
Esempio n. 2
0
def plot_pspec(pspec_file,
               save_fig="pspec.pdf",
               plot_fit_kwargs={
                   "fit_color": "black",
                   'show': False
               },
               refit=False,
               refit_kwargs={
                   "low_cut": 0.01 / u.pix,
                   "high_cut": 0.1 / u.pix,
                   'weighted_fit': False,
               }):
    try:
        pspec = PowerSpectrum.load_results(pspec_file)
    except:
        pass

    if refit:
        pspec.fit_pspec(**refit_kwargs)
        pspec.save_results(pspec_file, keep_data=True)
    pl = pspec.plot_fit(save_name=save_fig, **plot_fit_kwargs)
    plt.gcf().clear()
Esempio n. 3
0
        plot_folder = osjoin(plot_folder, 'pspec_bksub_plots')
        if not os.path.exists(plot_folder):
            os.mkdir(plot_folder)

        for name in fitinfo_dict[gal]:

            print("On {}".format(name))

            beamsize_str = f"{int(fitinfo_dict[gal][name]['beam'].major.value)}"

            filename = f"{gal.lower()}_{name}_gauss{beamsize_str}_bksub.fits"
            out_filename = "{}_cutout.fits".format(filename.rstrip(".fits"))
            pspec_save_name = f"{out_filename.rstrip('.fits')}.pspec.pkl"

            # Load pspec object
            pspec = PowerSpectrum.load_results(
                osjoin(data_path, gal, pspec_save_name))

            # Beam doesn't stay cached. Don't know why
            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))

            if fitinfo_dict[gal][name]['high_cut'] is not None:
                high_cut = fitinfo_dict[gal][name]['high_cut']

            else:
                high_cut = (1 / (beam_gauss_width * 3.))

            # Fit on scales > 3 pixels to avoid flattening from pixelization
        else:
            plt.ioff()

        for slice_name in lmc_mips24_slice:

            if res_type == 'orig':
                filename = "{0}_{1}_{2}_mjysr.pspec.pkl".format(gal.lower(),
                                                                name, slice_name)
            else:
                filename = "{0}_{1}_{2}_{3}_mjysr.pspec.pkl".format(gal.lower(),
                                                                    name,
                                                                    res_type,
                                                                    slice_name)

            # Load pspec object
            pspec = PowerSpectrum.load_results(osjoin(data_path, gal, filename))

            # Beam doesn't stay cached. Don't know why
            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))

            # Fit on scales > 3 pixels to avoid flattening from pixelization
            high_cut = 1 / (beam_gauss_width * 3.)
            fit_mask = pspec.freqs.value < high_cut

            # And cut out the largest scales due to expected deviations with
            # small stddev
            fit_mask[:2] = False
Esempio n. 5
0
    'no30dor': ["Without 30 Dor", ':']
}

name = 'mips24'

res_type = 'mod'

onecolumn_figure()

fig = plt.figure()

ax = fig.add_subplot(111)

filename = "{0}_{1}_{2}_mjysr.pspec.pkl".format(gal.lower(), name, res_type)

pspec = PowerSpectrum.load_results(osjoin(data_path, gal, filename))
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 * 1.5))
fit_mask = pspec.freqs.value < high_cut

# And cut out the largest scales due to expected deviations with
# small stddev
fit_mask[:2] = False

freqs = pspec.freqs.value[fit_mask]
beam_freqs = pspec.freqs.value[fit_mask]
df = pd.read_csv(osjoin(data_path, "pspec_spire500_deproj_fit_results.csv"), index_col=0)

fig, axes = plt.subplots(1, 2)

for i, (gal, ax) in enumerate(zip(gals, axes.ravel())):

    for ch in ['orig', 'dep']:

        fit_params = df.loc[f"{gal.lower()}_{ch}"]

        if ch == 'orig':
            filename = "{0}_spire500_mod_mjysr.pspec.pkl".format(gal.lower())
        else:
            filename = "{0}_spire500_mod_mjysr_deproj.pspec.pkl".format(gal.lower())

        pspec = PowerSpectrum.load_results(osjoin(data_path, gal, filename))
        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 * 1.5))
        fit_mask = pspec.freqs.value < high_cut

        # And cut out the largest scales due to expected deviations with
        # small stddev
        fit_mask[:2] = False

        freqs = pspec.freqs.value[fit_mask]
        beam_freqs = pspec.freqs.value[fit_mask]
gals = {'M33': 840 * u.kpc}

df = pd.read_csv(osjoin(data_path, "pspec_coldens_fit_results.csv"),
                 index_col=0)
df_hi = pd.read_csv(osjoin(data_path, "pspec_hi_conv_m33_fit_results.csv"),
                    index_col=0)

# Open the pspec files
hi_name = osjoin(
    data_path,
    "M33_14B-088_HI.clean.image.GBT_feathered.pbcov_gt_0.5_masked.moment0_Kkms.fits"
)

hi_pspec_name = f"{hi_name.rstrip('fits')}conv.pspec.pkl"

hi_pspec = PowerSpectrum.load_results(hi_pspec_name)
hi_pspec.load_beam()

dust_pspec_name = osjoin(data_path, 'M33', "m33_coldens.pspec.pkl")

dust_pspec = PowerSpectrum.load_results(dust_pspec_name)
dust_pspec.load_beam()

dust_norm = dust_pspec.ps1D.max()
hi_norm = hi_pspec.ps1D.max()

fig = plt.figure()

ax = fig.add_subplot(111)

gal = 'M33'
            print("Resolution {}".format(res_type))

            if res_type == 'orig':
                filename = "{0}_{1}_mjysr.pspec.pkl".format(gal.lower(), name)
                # filename = "{0}_{1}_mjysr.pspec_fullimg.pkl".format(gal.lower(), name)
            else:
                filename = "{0}_{1}_{2}_mjysr.pspec.pkl".format(
                    gal.lower(), name, res_type)

            if not os.path.exists(osjoin(data_path, 'raw', filename)):
                print("Could not find {}. Skipping".format(filename))
                continue

            # Load pspec object
            pspec = PowerSpectrum.load_results(
                osjoin(data_path, 'raw', filename))

            # Beam doesn't stay cached. Don't know why
            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))

            if fitinfo_dict[gal][name]['high_cut'] is not None:
                high_cut = fitinfo_dict[gal][name]['high_cut']

            else:
                high_cut = (1 / (beam_gauss_width * 3.))

            # Fit on scales > 3 pixels to avoid flattening from pixelization
    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

    # And cut out the largest scales due to expected deviations with
    # small stddev
    fit_mask[:2] = False

    freqs = pspec.freqs.value[fit_mask]
                pspec = PowerSpectrum(mom0_cutout.hdu,
                                      beam=mom0.beam,
                                      distance=fitinfo_dict[gal]['distance'])
                pspec.compute_pspec(
                    use_pyfftw=False,
                    threads=ncores,
                    apodize_kernel=fitinfo_dict[gal]['apod_kern'],
                    alpha=alpha)
                pspec.compute_radial_pspec()

                pspec.save_results(save_pspec_name)

            else:

                pspec = PowerSpectrum.load_results(save_pspec_name)
                pspec.load_beam()

            # pspec.run(high_cut=10**-1.29 / u.pix, low_cut=10**-2. / u.pix,
            #           fit_2D=True,
            #           verbose=False,
            #           # save_name=os.path.join(perposn_figfolder, "{}.png".format(out_name)),
            #           beam_correct=True,
            #           # xunit=u.pc**-1,
            #           apodize_kernel='tukey', alpha=alpha,
            #           radial_pspec_kwargs={'binsize': 2.0},
            #           fit_kwargs={'weighted_fit': True})

            # pspec.save_results(os.path.join(perposn_figfolder,
            #                                 "{}.pkl".format(out_name)))
Esempio n. 11
0
    nsamp = 6000

    row_names = []

    gal = 'M33'

    fit_results = {'logA': [], 'ind': [], 'logB': [], 'logC': [],
                   'logA_std': [], 'ind_std': [], 'logB_std': [],
                   'logC_std': []}

    # Load model functions
    repo_path = os.path.expanduser("~/ownCloud/project_code/DustyPowerSpectra/")
    code_name = os.path.join(repo_path, "models.py")
    exec(compile(open(code_name, "rb").read(), code_name, 'exec'))

    pspec = PowerSpectrum.load_results(hi_pspec_name)

    filename = hi_pspec_name_conv

    # Beam doesn't stay cached. Don't know why
    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

    # And cut out the largest scales due to expected deviations with