Пример #1
0
def make_ratio_integ():
    """
    This is an alternate implementation to that already included in
    make_ratiotem_cubesims
    """
    npixflat = bmasksm_rs.include().sum(axis=0)
    sum321 = cube321.with_mask(bmasksm_rs).sum(axis=0)
    sum303 = cube303.with_mask(bmasksm_rs).sum(axis=0)
    integ303321b = sum321 / sum303
    error303 = cube303.with_mask(~bmasksm_rs).std(axis=0)
    error321 = cube321.with_mask(~bmasksm_rs).std(axis=0)

    eintegratio303321 = ((integ303321b.value**2 *
                          (error303.value**2 /
                           (sum303.value / npixflat)**2 + error321.value**2 /
                           (sum321.value / npixflat)**2))**0.5)

    ok = (integ303321b.value / eintegratio303321) > 3
    integ303321ok = integ303321b.value
    integ303321ok[~ok] = np.nan

    hdu = cube321[0, :, :].hdu
    hdu.data = integ303321ok
    hdu.writeto(paths.hpath('H2CO_321220_to_303202_integ_smoothmask.fits'),
                clobber=True)
    hdu.data = eintegratio303321
    hdu.writeto(paths.hpath('H2CO_321220_to_303202_einteg_smoothmask.fits'),
                clobber=True)
Пример #2
0
def make_dend(cube, noise, view=True, write=True,
              min_npix=100,
              min_nsig_value=3,
              min_nsig_delta=2,
              outfn="DendroMask_H2CO303202.hdf5"):
    """
    Given a cube and a 2D noise map, extract dendrograms.
    """

    # Use a little sigma-rejection to get a decently robust noise estimate
    noise_std = noise[noise==noise].std()
    noise_mean = noise[noise==noise].mean()
    err_estimate = noise[(noise > (noise_mean-noise_std)) &
                         (noise < (noise_mean+noise_std))].mean()
    bad_noise = np.isnan(noise)
    log.info("{1} Estimated error: {0}".format(err_estimate, outfn))

    dend = Dendrogram.compute(cube.filled_data[:].value,
                              min_value=min_nsig_value*err_estimate,
                              min_delta=min_nsig_delta*err_estimate,
                              min_npix=min_npix,
                              verbose=True, wcs=cube.wcs)

    if view:
        dend.viewer()

    if write:
        dend.save_to(hpath(outfn))

    return dend
def pars_to_maps(positions, parvalues, shape=pcube_merge_high.cube.shape[1:],
                 celwcs=cube_merge_high.wcs.celestial, suffix=""):

    maxlen = max(len(r) for r in parvalues if r is not None)

    if maxlen % 6 == 0:
        names = ['AMPLITUDE', 'VELOCITY', 'WIDTH', 'RATIO321303X',
                 'RATIO322321X', 'AMPCH3OH']
        n = 6
    elif maxlen % 5 == 0:
        names = ['AMPLITUDE', 'VELOCITY', 'WIDTH', 'RATIO321303X',
                 'AMPCH3OH']
        n = 5
    else:
        raise

    maps = []
    for jj in range(maxlen / n):
        for ii in xrange(n):
            arr = np.zeros(shape)
            for p,r in zip(positions,parvalues):
                ind = ii + jj*n
                if r is None:
                    continue
                elif len(r) > ind:
                    arr[p[1], p[0]] = r[ind]
            maps.append(arr)
            hdu = fits.PrimaryHDU(data=arr,
                                  header=celwcs.to_header())
            hdu.writeto(hpath("pyspeckit_{0}{1}{2}.fits".format(names[ii],
                                                                jj,
                                                                suffix)),
                        clobber=True)

    return maps
Пример #4
0
def make_dend(cube,
              noise,
              view=True,
              write=True,
              min_npix=100,
              min_nsig_value=3,
              min_nsig_delta=2,
              outfn="DendroMask_H2CO303202.hdf5"):
    """
    Given a cube and a 2D noise map, extract dendrograms.
    """

    # Use a little sigma-rejection to get a decently robust noise estimate
    noise_std = noise[noise == noise].std()
    noise_mean = noise[noise == noise].mean()
    err_estimate = noise[(noise > (noise_mean - noise_std))
                         & (noise < (noise_mean + noise_std))].mean()
    bad_noise = np.isnan(noise)
    log.info("{1} Estimated error: {0}".format(err_estimate, outfn))

    dend = Dendrogram.compute(cube.filled_data[:].value,
                              min_value=min_nsig_value * err_estimate,
                              min_delta=min_nsig_delta * err_estimate,
                              min_npix=min_npix,
                              verbose=True,
                              wcs=cube.wcs)

    if view:
        dend.viewer()

    if write:
        dend.save_to(hpath(outfn))

    return dend
Пример #5
0
def make_ratio_max():
    npixflat = bmasksm_rs.include().sum(axis=0)
    max321 = cube321.with_mask(bmasksm_rs).max(axis=0)
    max303 = cube303.with_mask(bmasksm_rs).max(axis=0)
    maxratio303321b = max321/max303
    error303 = cube303.with_mask(~bmasksm_rs).std(axis=0)
    error321 = cube321.with_mask(~bmasksm_rs).std(axis=0)

    eintegratio303321 = ((maxratio303321b.value**2 * (error303.value**2/(max303.value)**2 +
                                                      error321.value**2/(max321.value)**2))**0.5)

    ok = (maxratio303321b.value/eintegratio303321) > 3
    maxratio303321ok = maxratio303321b.value
    maxratio303321ok[~ok] = np.nan

    hdu = cube321[0,:,:].hdu
    hdu.data = maxratio303321ok
    hdu.writeto(paths.hpath('H2CO_321220_to_303202_maxratio_smoothmask.fits'),clobber=True)
    hdu.data = eintegratio303321
    hdu.writeto(paths.hpath('H2CO_321220_to_303202_emaxratio_smoothmask.fits'),clobber=True)
Пример #6
0
def do_321_dendro_temperatures_sharp():
    from dendrograms import dend321
    catalog, tcube = measure_dendrogram_properties(dend=dend321,
                                                   cube303=cube303,
                                                   cube321=cube321,
                                                   sncube=sncube, suffix="",
                                                   line='321',
                                                   write=False)
    catalog.write(tpath('PPV_H2CO_Temperature_321selected.ipac'), format='ascii.ipac')
    tcube.write(hpath('TemperatureCube_Dendrogram321Objects.fits'), overwrite=True)

    return catalog,tcube
def do_fitting(ncores=4):

    # smooth both
    results3 = fit_all_positions(dendrogram=dendsm,
                                 catalog=catalog_sm,
                                 pcube=pcube_merge_high_sm,
                                 second_ratio=True,
                                 outfilename=hpath('pyspeckit_fits_smsm.txt'),
                                 ncores=ncores)
    (positions_sm2, results_sm2,
     bad_positions_sm2) = read_pars(hpath('pyspeckit_fits_smsm.txt'))
    pars_to_maps(positions_sm2, results_sm2, suffix='_sm2')

    # sharp both
    results2 = fit_all_positions(dendrogram=dend,
                                 catalog=catalog,
                                 second_ratio=True,
                                 outfilename=hpath('pyspeckit_fits.txt'),
                                 ncores=ncores)
    (positions, results,
     bad_positions) = read_pars(hpath('pyspeckit_fits.txt'))
    pars_to_maps(positions, results, suffix='')

    # Smooth dendrograms, sharp image
    results = fit_all_positions(dendrogram=dendsm,
                                catalog=catalog_sm,
                                second_ratio=True,
                                outfilename=hpath('pyspeckit_fits_densm.txt'),
                                ncores=ncores)
    (positions_sm1, results_sm1,
     bad_positions_sm1) = read_pars(hpath('pyspeckit_fits_densm.txt'))
    pars_to_maps(positions_sm1, results_sm1, suffix='_sm1')
def do_fitting(ncores=4):

    # smooth both
    results3 = fit_all_positions(dendrogram=dendsm, catalog=catalog_sm,
                                            pcube=pcube_merge_high_sm,
                                            second_ratio=True,
                                            outfilename=hpath('pyspeckit_fits_smsm.txt'),
                                            ncores=ncores)
    (positions_sm2, results_sm2,
     bad_positions_sm2) = read_pars(hpath('pyspeckit_fits_smsm.txt'))
    pars_to_maps(positions_sm2, results_sm2, suffix='_sm2')

    # sharp both
    results2 = fit_all_positions(dendrogram=dend, catalog=catalog,
                                        second_ratio=True,
                                        outfilename=hpath('pyspeckit_fits.txt'),
                                        ncores=ncores)
    (positions, results,
     bad_positions) = read_pars(hpath('pyspeckit_fits.txt'))
    pars_to_maps(positions, results, suffix='')

    # Smooth dendrograms, sharp image
    results = fit_all_positions(dendrogram=dendsm, catalog=catalog_sm,
                                second_ratio=True,
                                outfilename=hpath('pyspeckit_fits_densm.txt'),
                                ncores=ncores)
    (positions_sm1, results_sm1,
     bad_positions_sm1) = read_pars(hpath('pyspeckit_fits_densm.txt'))
    pars_to_maps(positions_sm1, results_sm1, suffix='_sm1')
Пример #9
0
def make_sncube(write=True, smooth=False, line='303_202'):
    """
    Not entirely obsolete: the "total" signal to noise is recorded in the
    dendrogram catalog in dendro-temperature

    Obsolete: I make dendrograms directly on the data, not on the signal to
    noise, based on Erik's suggestion.  It makes sense, because a varying noise
    field makes structure impossible to interpret: it's better to just raise
    the noise floor.
    """

    suff = 'smooth_' if smooth else ''
    fn = 'APEX_H2CO_{1}_{0}bl.fits'.format(suff, line)
    n = sm_noise if smooth else noise
    sncube = fits.getdata(hpath(fn)) / n
    ff = fits.open(hpath(fn))
    ff[0].data = sncube

    if write:
        outfn = 'APEX_H2CO_{1}_{0}signal_to_noise_cube.fits'.format(suff, line)
        ff.writeto(hpath(outfn), clobber=True)

    return sncube
Пример #10
0
def make_sncube(write=True, smooth=False, line='303_202'):
    """
    Not entirely obsolete: the "total" signal to noise is recorded in the
    dendrogram catalog in dendro-temperature

    Obsolete: I make dendrograms directly on the data, not on the signal to
    noise, based on Erik's suggestion.  It makes sense, because a varying noise
    field makes structure impossible to interpret: it's better to just raise
    the noise floor.
    """

    suff = 'smooth_' if smooth else ''
    fn = 'APEX_H2CO_{1}_{0}bl.fits'.format(suff, line)
    n = sm_noise if smooth else noise
    sncube = fits.getdata(hpath(fn)) / n
    ff = fits.open(hpath(fn))
    ff[0].data = sncube

    if write:
        outfn = 'APEX_H2CO_{1}_{0}signal_to_noise_cube.fits'.format(suff, line)
        ff.writeto(hpath(outfn), clobber=True)

    return sncube
Пример #11
0
def make_ratio_max():
    npixflat = bmasksm_rs.include().sum(axis=0)
    max321 = cube321.with_mask(bmasksm_rs).max(axis=0)
    max303 = cube303.with_mask(bmasksm_rs).max(axis=0)
    maxratio303321b = max321 / max303
    error303 = cube303.with_mask(~bmasksm_rs).std(axis=0)
    error321 = cube321.with_mask(~bmasksm_rs).std(axis=0)

    eintegratio303321 = ((maxratio303321b.value**2 *
                          (error303.value**2 /
                           (max303.value)**2 + error321.value**2 /
                           (max321.value)**2))**0.5)

    ok = (maxratio303321b.value / eintegratio303321) > 3
    maxratio303321ok = maxratio303321b.value
    maxratio303321ok[~ok] = np.nan

    hdu = cube321[0, :, :].hdu
    hdu.data = maxratio303321ok
    hdu.writeto(paths.hpath('H2CO_321220_to_303202_maxratio_smoothmask.fits'),
                clobber=True)
    hdu.data = eintegratio303321
    hdu.writeto(paths.hpath('H2CO_321220_to_303202_emaxratio_smoothmask.fits'),
                clobber=True)
Пример #12
0
def do_321_dendro_temperatures_sharp():
    from dendrograms import dend321
    catalog, tcube = measure_dendrogram_properties(dend=dend321,
                                                   cube303=cube303,
                                                   cube321=cube321,
                                                   sncube=sncube,
                                                   suffix="",
                                                   line='321',
                                                   write=False)
    catalog.write(tpath('PPV_H2CO_Temperature_321selected.ipac'),
                  format='ascii.ipac')
    tcube.write(hpath('TemperatureCube_Dendrogram321Objects.fits'),
                overwrite=True)

    return catalog, tcube
Пример #13
0
def make_ratio_integ():
    """
    This is an alternate implementation to that already included in
    make_ratiotem_cubesims
    """
    npixflat = bmasksm_rs.include().sum(axis=0)
    sum321 = cube321.with_mask(bmasksm_rs).sum(axis=0)
    sum303 = cube303.with_mask(bmasksm_rs).sum(axis=0)
    integ303321b = sum321/sum303
    error303 = cube303.with_mask(~bmasksm_rs).std(axis=0)
    error321 = cube321.with_mask(~bmasksm_rs).std(axis=0)

    eintegratio303321 = ((integ303321b.value**2 * (error303.value**2/(sum303.value/npixflat)**2 +
                                                   error321.value**2/(sum321.value/npixflat)**2))**0.5)

    ok = (integ303321b.value/eintegratio303321) > 3
    integ303321ok = integ303321b.value
    integ303321ok[~ok] = np.nan

    hdu = cube321[0,:,:].hdu
    hdu.data = integ303321ok
    hdu.writeto(paths.hpath('H2CO_321220_to_303202_integ_smoothmask.fits'),clobber=True)
    hdu.data = eintegratio303321
    hdu.writeto(paths.hpath('H2CO_321220_to_303202_einteg_smoothmask.fits'),clobber=True)
Пример #14
0
def make_sn_dend(sncube, view=True, write=True,
                 outfn="DendroMask_H2CO303202_signal_to_noise.hdf5"):
    """
    Obsolete: not used
    """
    raise DeprecationWarning("Obsolete")

    dend = Dendrogram.compute(sncube, min_value=3, min_delta=2, min_npix=50,
                              verbose=True)

    if view:
        dend.viewer

    if write:
        dend.save_to(hpath(outfn))

    return dend
Пример #15
0
def do_321_dendro_temperatures_smooth():
    from dendrograms import dend321sm
    assert sncubesm._wcs is cube303sm._wcs
    catalog, tcube = measure_dendrogram_properties(dend=dend321sm,
                                                   cube303=cube303sm,
                                                   cube321=cube321sm,
                                                   cube13co=cube13cosm,
                                                   cube18co=cube18cosm,
                                                   noise_cube=sm_noise_cube,
                                                   sncube=sncubesm,
                                                   suffix="_smooth",
                                                   line='321',
                                                   write=False)

    catalog.write(tpath('PPV_H2CO_Temperature_321selected_smooth.ipac'), format='ascii.ipac')
    tcube.write(hpath('TemperatureCube_Dendrogram321Objects_smooth.fits'), overwrite=True)

    return catalog, tcube
Пример #16
0
def do_321_dendro_temperatures_smooth():
    from dendrograms import dend321sm
    assert sncubesm._wcs is cube303sm._wcs
    catalog, tcube = measure_dendrogram_properties(dend=dend321sm,
                                                   cube303=cube303sm,
                                                   cube321=cube321sm,
                                                   cube13co=cube13cosm,
                                                   cube18co=cube18cosm,
                                                   noise_cube=sm_noise_cube,
                                                   sncube=sncubesm,
                                                   suffix="_smooth",
                                                   line='321',
                                                   write=False)

    catalog.write(tpath('PPV_H2CO_Temperature_321selected_smooth.ipac'),
                  format='ascii.ipac')
    tcube.write(hpath('TemperatureCube_Dendrogram321Objects_smooth.fits'),
                overwrite=True)

    return catalog, tcube
Пример #17
0
def make_sn_dend(sncube,
                 view=True,
                 write=True,
                 outfn="DendroMask_H2CO303202_signal_to_noise.hdf5"):
    """
    Obsolete: not used
    """
    raise DeprecationWarning("Obsolete")

    dend = Dendrogram.compute(sncube,
                              min_value=3,
                              min_delta=2,
                              min_npix=50,
                              verbose=True)

    if view:
        dend.viewer

    if write:
        dend.save_to(hpath(outfn))

    return dend
def pars_to_maps(positions,
                 parvalues,
                 shape=pcube_merge_high.cube.shape[1:],
                 celwcs=cube_merge_high.wcs.celestial,
                 suffix=""):

    maxlen = max(len(r) for r in parvalues if r is not None)

    if maxlen % 6 == 0:
        names = [
            'AMPLITUDE', 'VELOCITY', 'WIDTH', 'RATIO321303X', 'RATIO322321X',
            'AMPCH3OH'
        ]
        n = 6
    elif maxlen % 5 == 0:
        names = ['AMPLITUDE', 'VELOCITY', 'WIDTH', 'RATIO321303X', 'AMPCH3OH']
        n = 5
    else:
        raise

    maps = []
    for jj in range(maxlen / n):
        for ii in xrange(n):
            arr = np.zeros(shape)
            for p, r in zip(positions, parvalues):
                ind = ii + jj * n
                if r is None:
                    continue
                elif len(r) > ind:
                    arr[p[1], p[0]] = r[ind]
            maps.append(arr)
            hdu = fits.PrimaryHDU(data=arr, header=celwcs.to_header())
            hdu.writeto(hpath("pyspeckit_{0}{1}{2}.fits".format(
                names[ii], jj, suffix)),
                        clobber=True)

    return maps
import numpy as np
from paths import hpath, mpath, fpath
from astropy import log
from spectral_cube import SpectralCube, BooleanArrayMask
from masked_cubes import (cube303m, cube321m, cube303msm, cube321msm, cube303,
                          cube321, cube303sm, cube321sm, sncube, sncubesm)

for suffix in ("", "_smooth"):

    outpath = 'TemperatureCube_DendrogramObjects{0}.fits'
    tcube = SpectralCube.read(hpath(outpath.format(suffix)))

    outpath_leaf = 'TemperatureCube_DendrogramObjects{0}_leaves.fits'
    tcubeleaf = SpectralCube.read(hpath(outpath_leaf.format(suffix)))

    integ = tcube.mean(axis=0)
    integ.hdu.writeto(hpath(outpath.format(suffix)).replace(
        ".fits", "_integ.fits"),
                      clobber=True)
    integleaf = tcubeleaf.mean(axis=0)
    integleaf.hdu.writeto(hpath(outpath_leaf.format(suffix)).replace(
        ".fits", "_integ.fits"),
                          clobber=True)

    hdu_template = integ.hdu

    log.info("Writing Weighted Integrated TemperatureCube")
    tcubed = tcube.filled_data[:].value
    weight_cube = cube303sm if 'smooth' in suffix else cube303
    weights = weight_cube.filled_data[:].value
    weights[weights < 0] = 0
Пример #20
0
              'pv_H2CO_321220_to_303202{0}_bl_integ_weighted_temperature_dens3e4.fits',
              'pv_H2CO_321220_to_303202{0}_bl_integ_masked_weighted_temperature_dens1e4.fits',
              'pv_H2CO_321220_to_303202{0}_bl_integ_masked_weighted_temperature_dens1e5.fits',
              'pv_H2CO_321220_to_303202{0}_bl_integ_masked_weighted_temperature_dens3e4.fits',),
             ('', # used as pv_tmap_weight_masked
              '_dendro',
              '_dendro_leaves',
              '_directpv1e4',
              '_directpv1e5','_directpv3e4',
              '_directpv1e4_masked','_directpv1e5_masked','_directpv3e4_masked',))

#for vmax,vmax_str in zip((100,200),("_vmax100","")):
for vmax,vmax_str in zip((200,),("",)):
    for weight in ("_weight",""):
        for smooth in ("",):#("_smooth",""):#"_vsmooth"):
            sncubefile = hpath('APEX_H2CO_303_202{0}_signal_to_noise_cube.fits'.format(smooth))
            sncube = SpectralCube.read(sncubefile)
            snproj = sncube.max(axis=1)
            snproj.wcs.wcs.ctype[0] = 'OFFSET'
            snhdu = snproj.hdu
            snhdu.header['CTYPE1'] = 'GLON'

            fig5 = pl.figure(5, figsize=figsize)
            fig5.clf()
            Fsn = aplpy.FITSFigure(snhdu, convention='calabretta', figure=fig5)
            Fsn.show_grayscale(vmin=0, vmax=10, stretch='linear', invert=True)
            Fsn.add_colorbar()
            Fsn.colorbar.set_axis_label_text('Peak S/N')
            Fsn.recenter(**recen)
            Fsn.axis_labels.set_xtext(r'Galactic Longitude')
            Fsn.tick_labels.set_xformat('d.dd')
import numpy as np
from paths import hpath,mpath,fpath
from astropy import log
from spectral_cube import SpectralCube, BooleanArrayMask
from masked_cubes import (cube303m,cube321m,cube303msm,cube321msm,
                          cube303,cube321,cube303sm,cube321sm,
                          sncube, sncubesm)


for suffix in ("","_smooth"):

    outpath = 'TemperatureCube_DendrogramObjects{0}.fits'
    tcube = SpectralCube.read(hpath(outpath.format(suffix)))

    outpath_leaf = 'TemperatureCube_DendrogramObjects{0}_leaves.fits'
    tcubeleaf = SpectralCube.read(hpath(outpath_leaf.format(suffix)))

    integ = tcube.mean(axis=0)
    integ.hdu.writeto(hpath(outpath.format(suffix)).replace(".fits","_integ.fits"),
                      clobber=True)
    integleaf = tcubeleaf.mean(axis=0)
    integleaf.hdu.writeto(hpath(outpath_leaf.format(suffix)).replace(".fits","_integ.fits"),
                          clobber=True)

    hdu_template = integ.hdu

    log.info("Writing Weighted Integrated TemperatureCube")
    tcubed = tcube.filled_data[:].value
    weight_cube = cube303sm if 'smooth' in suffix else cube303
    weights = weight_cube.filled_data[:].value
    weights[weights < 0] = 0
Пример #22
0
                'big_lores{0}_tmap_greyed_{1}to{2}_slabratio.png'.format(
                    smooth, int(vrange[0]), int(vrange[1]))))

fig = pl.figure(1, figsize=figsize)
for cube, sn, smooth in zip((ratiocube_303321, ratiocubesm_303321),
                            (sncube, sncubesm), (
                                "",
                                "_smooth",
                            )):  #"_vsmooth"):
    for vrange in zip(vcuts[:-1], vcuts[1:]):
        fig.clf()
        proj = cube.spectral_slab(*(vrange * u.km / u.s)).mean(axis=0)
        tproj = np.copy(proj)
        tproj[np.isfinite(proj)] = pwtem(proj[np.isfinite(proj)].value)
        hdu = fits.PrimaryHDU(tproj, proj.hdu.header)
        hdu.writeto(hpath("tmap{0}_{1}to{2}".format(smooth, int(vrange[0]),
                                                    int(vrange[1]))),
                    clobber=True)
        F = aplpy.FITSFigure(hdu, convention='calabretta', figure=fig)

        #cm = copy.copy(pl.cm.rainbow)
        #cm.set_bad((0.5,0.5,0.5,0.5))
        cm = copy.copy(pl.cm.RdYlBu_r)
        cm.set_bad('#888888')
        F.show_colorscale(cmap=cm, vmin=15, vmax=200)
        F.set_tick_labels_format('d.dd', 'd.dd')
        peaksn = sn.spectral_slab(*(vrange * u.km / u.s)).max(axis=0)
        peaksn[(peaksn < 0) | np.isnan(peaksn)] = 0
        color = (0.5, ) * 3  # should be same as background #888
        nlevs = 50
        F.show_contour(peaksn.hdu,
                       levels=[0] + np.logspace(0.20, 1, nlevs).tolist(),
Пример #23
0
"""
import numpy as np
import time
import warnings
from astropy import log
from astrodendro import Dendrogram, ppv_catalog
from astropy.table import Table, Column
from paths import hpath, tpath
from masked_cubes import cube303, cube303sm, cube321, cube321sm
import flag_other_lines
from astropy.utils.console import ProgressBar

if 'dend' not in locals():
    t0 = time.time()
    log.info("Loading dendrogram from file.")
    dend = Dendrogram.load_from(hpath("DendroMask_H2CO303202.hdf5"))
    log.info(
        "Loaded dendrogram from file in {0:0.1f} seconds.".format(time.time() -
                                                                  t0))
    dend.wcs = cube303.wcs

if 'dendsm' not in locals():
    t0 = time.time()
    log.info("Loading dendrogram from file.")
    dendsm = Dendrogram.load_from(hpath("DendroMask_H2CO303202_smooth.hdf5"))
    log.info(
        "Loaded dendrogram from file in {0:0.1f} seconds.".format(time.time() -
                                                                  t0))
    dendsm.wcs = cube303sm.wcs

# Removed: the 321 signal extraction approach never worked nicely
Пример #24
0
def mask_out_region(mask_array, cube, regions=hc3n_regions):

    for region in regions:
        z = [
            cube.closest_spectral_channel(v * u.km / u.s) for v in region['v']
        ]
        view = [slice(*z), slice(*region['y']), slice(*region['x'])]

        mask_array[view] = False

    return mask_array


cube303 = SpectralCube.read(
    hpath('APEX_H2CO_303_202_bl.fits')).with_spectral_unit(
        u.km / u.s, velocity_convention='radio')
cube321 = SpectralCube.read(
    hpath('APEX_H2CO_321_220_bl.fits')).with_spectral_unit(
        u.km / u.s, velocity_convention='radio')
maskarr = mask_out_region(
    fits.getdata(hpath('APEX_H2CO_303_202_bl_mask.fits')).astype('bool'),
    cube303)
mask = (maskarr & cube303.mask.include(cube303, cube303.wcs)
        & cube321.mask.include(cube321, cube321.wcs))
bmask = BooleanArrayMask(mask, cube303.wcs)
cube303m = cube303.with_mask(bmask)
cube321m = cube321.with_mask(bmask)

cube303sm = SpectralCube.read(
    hpath('APEX_H2CO_303_202_smooth_bl.fits')).with_spectral_unit(
            logabundance=logabundance,
            elogabundance=elogabundance,
            taline303=ta303.value,
            etaline303=err,
            taline321=ta321.value,
            etaline321=err,
            linewidth=linewidth)
        row_data = mf.get_parconstraints()
        tcube[z, y, x] = row_data['temperature_chi2']
        row_data['ratio303321'] = rat
        row_data['eratio303321'] = erat

        if ii % 100 == 0 or ii < 50:
            log.info(
                "T: [{tmin1sig_chi2:7.2f},{temperature_chi2:7.2f},{tmax1sig_chi2:7.2f}]  R={ratio303321:6.2f}+/-{eratio303321:6.2f}"
                .format(**row_data))
        else:
            pb.update(ii)
        tcube.flush()
    else:
        pb.update(ii)

tcube[tcube == 0] = np.nan
tCube = SpectralCube(tcube,
                     cube303.wcs,
                     mask=BooleanArrayMask(np.isfinite(tcube),
                                           wcs=cube303.wcs))
tCube.write(hpath('chi2_temperature_cube.fits'), overwrite=True)

print()
Пример #26
0
def measure_dendrogram_properties(dend=None,
                                  cube303=cube303,
                                  cube321=cube321,
                                  cube13co=cube13co,
                                  cube18co=cube18co,
                                  noise_cube=noise_cube,
                                  sncube=sncube,
                                  suffix="",
                                  last_index=None,
                                  plot_some=True,
                                  line='303',
                                  write=True):

    assert (cube321.shape == cube303.shape == noise_cube.shape ==
            cube13co.shape == cube18co.shape == sncube.shape)
    assert sncube.wcs is cube303.wcs is sncube.mask._wcs

    metadata = {}
    metadata['data_unit'] = u.K
    metadata['spatial_scale'] = 7.2 * u.arcsec
    metadata['beam_major'] = 30 * u.arcsec
    metadata['beam_minor'] = 30 * u.arcsec
    metadata['wavelength'] = 218.22219 * u.GHz
    metadata['velocity_scale'] = u.km / u.s
    metadata['wcs'] = cube303.wcs

    keys = [
        'density_chi2',
        'expected_density',
        'dmin1sig_chi2',
        'dmax1sig_chi2',
        'column_chi2',
        'expected_column',
        'cmin1sig_chi2',
        'cmax1sig_chi2',
        'temperature_chi2',
        'expected_temperature',
        'tmin1sig_chi2',
        'tmax1sig_chi2',
        'eratio321303',
        'ratio321303',
        'logh2column',
        'elogh2column',
        'logabundance',
        'elogabundance',
    ]
    obs_keys = [
        'Stot303',
        'Smin303',
        'Smax303',
        'Stot321',
        'Smean303',
        'Smean321',
        'npix',
        'e303',
        'e321',
        'r321303',
        'er321303',
        '13cosum',
        'c18osum',
        '13comean',
        'c18omean',
        's_ntotal',
        'index',
        'is_leaf',
        'parent',
        'root',
        'lon',
        'lat',
        'vcen',
        'higaldusttem',
        'reff',
        'dustmass',
        'dustmindens',
        'bad',
        #'tkin_turb',
    ]
    columns = {k: [] for k in (keys + obs_keys)}

    log.debug("Initializing dendrogram temperature fitting loop")

    # FORCE wcs to match
    # (technically should reproject here)
    cube13co._wcs = cube18co._wcs = cube303.wcs
    cube13co.mask._wcs = cube18co.mask._wcs = cube303.wcs

    if line == '303':
        maincube = cube303
    elif line == '321':
        maincube = cube321
    else:
        raise ValueError("Unrecognized line: {0}".format(line))

    # Prepare an array to hold the fitted temperatures
    tcubedata = np.empty(maincube.shape, dtype='float32')
    tcubedata[:] = np.nan
    tcubeleafdata = np.empty(maincube.shape, dtype='float32')
    tcubeleafdata[:] = np.nan

    nbad = 0

    catalog = ppv_catalog(dend, metadata)
    pb = ProgressBar(len(catalog))
    for ii, row in enumerate(catalog):
        structure = dend[row['_idx']]
        assert structure.idx == row['_idx'] == ii
        dend_obj_mask = BooleanArrayMask(structure.get_mask(), wcs=cube303.wcs)
        dend_inds = structure.indices()

        view = (
            slice(dend_inds[0].min(), dend_inds[0].max() + 1),
            slice(dend_inds[1].min(), dend_inds[1].max() + 1),
            slice(dend_inds[2].min(), dend_inds[2].max() + 1),
        )
        #view2 = cube303.subcube_slices_from_mask(dend_obj_mask)
        submask = dend_obj_mask[view]
        #assert np.count_nonzero(submask.include()) == np.count_nonzero(dend_obj_mask.include())

        sn = sncube[view].with_mask(submask)
        sntot = sn.sum().value
        #np.testing.assert_almost_equal(sntot, structure.values().sum(), decimal=0)

        c303 = cube303[view].with_mask(submask)
        c321 = cube321[view].with_mask(submask)
        co13sum = cube13co[view].with_mask(submask).sum().value
        co18sum = cube18co[view].with_mask(submask).sum().value
        if hasattr(co13sum, '__len__'):
            raise TypeError(
                ".sum() applied to an array has yielded a non scalar.")

        npix = submask.include().sum()
        assert npix == structure.get_npix()
        Stot303 = c303.sum().value
        if np.isnan(Stot303):
            raise ValueError("NaN in cube.  This can't happen: the data from "
                             "which the dendrogram was derived can't have "
                             "NaN pixels.")
        Smax303 = c303.max().value
        Smin303 = c303.min().value

        Stot321 = c321.sum().value
        if npix == 0:
            raise ValueError("npix=0. This is impossible.")
        Smean303 = Stot303 / npix
        if Stot303 <= 0 and line == '303':
            raise ValueError(
                "The 303 flux is <=0.  This isn't possible because "
                "the dendrogram was derived from the 303 data with a "
                "non-zero threshold.")
        elif Stot303 <= 0 and line == '321':
            Stot303 = 0
            Smean303 = 0
        elif Stot321 <= 0 and line == '321':
            raise ValueError(
                "The 321 flux is <=0.  This isn't possible because "
                "the dendrogram was derived from the 321 data with a "
                "non-zero threshold.")
        if np.isnan(Stot321):
            raise ValueError("NaN in 321 line")
        Smean321 = Stot321 / npix

        #error = (noise_cube[view][submask.include()]).sum() / submask.include().sum()**0.5
        var = ((noise_cube[dend_obj_mask.include()]**2).sum() / npix**2)
        error = var**0.5
        if np.isnan(error):
            raise ValueError("error is nan: this is impossible by definition.")

        if line == '321' and Stot303 == 0:
            r321303 = np.nan
            er321303 = np.nan
        elif Stot321 < 0:
            r321303 = error / Smean303
            er321303 = (r321303**2 * (var / Smean303**2 + 1))**0.5
        else:
            r321303 = Stot321 / Stot303
            er321303 = (r321303**2 *
                        (var / Smean303**2 + var / Smean321**2))**0.5

        for c in columns:
            assert len(columns[c]) == ii

        columns['index'].append(row['_idx'])
        columns['s_ntotal'].append(sntot)
        columns['Stot303'].append(Stot303)
        columns['Smax303'].append(Smax303)
        columns['Smin303'].append(Smin303)
        columns['Stot321'].append(Stot321)
        columns['Smean303'].append(Smean303)
        columns['Smean321'].append(Smean321)
        columns['npix'].append(npix)
        columns['e303'].append(error)
        columns['e321'].append(error)
        columns['r321303'].append(r321303)
        columns['er321303'].append(er321303)
        columns['13cosum'].append(co13sum)
        columns['c18osum'].append(co18sum)
        columns['13comean'].append(co13sum / npix)
        columns['c18omean'].append(co18sum / npix)
        columns['is_leaf'].append(structure.is_leaf)
        columns['parent'].append(
            structure.parent.idx if structure.parent else -1)
        columns['root'].append(get_root(structure).idx)
        s_main = maincube._data[dend_inds]
        x, y, z = maincube.world[dend_inds]
        lon = ((z.value - (360 *
                           (z.value > 180))) * s_main).sum() / s_main.sum()
        lat = (y * s_main).sum() / s_main.sum()
        vel = (x * s_main).sum() / s_main.sum()
        columns['lon'].append(lon)
        columns['lat'].append(lat.value)
        columns['vcen'].append(vel.value)

        mask2d = dend_obj_mask.include().max(axis=0)[view[1:]]
        logh2column = np.log10(
            np.nanmean(column_regridded.data[view[1:]][mask2d]) * 1e22)
        if np.isnan(logh2column):
            log.info("Source #{0} has NaNs".format(ii))
            logh2column = 24
        elogh2column = elogabundance
        columns['higaldusttem'].append(
            np.nanmean(dusttem_regridded.data[view[1:]][mask2d]))

        r_arcsec = row['radius'] * u.arcsec
        reff = (r_arcsec * (8.5 * u.kpc)).to(u.pc, u.dimensionless_angles())
        mass = ((10**logh2column * u.cm**-2) * np.pi * reff**2 * 2.8 *
                constants.m_p).to(u.M_sun)
        density = (mass / (4 / 3. * np.pi * reff**3) / constants.m_p / 2.8).to(
            u.cm**-3)

        columns['reff'].append(reff.value)
        columns['dustmass'].append(mass.value)
        columns['dustmindens'].append(density.value)
        mindens = np.log10(density.value)
        if mindens < 3:
            mindens = 3

        if (r321303 < 0 or np.isnan(r321303)) and line != '321':
            raise ValueError("Ratio <0: This can't happen any more because "
                             "if either num/denom is <0, an exception is "
                             "raised earlier")
            #for k in columns:
            #    if k not in obs_keys:
            #        columns[k].append(np.nan)
        elif (r321303 < 0 or np.isnan(r321303)) and line == '321':
            for k in keys:
                columns[k].append(np.nan)
        else:
            # Replace negatives for fitting
            if Smean321 <= 0:
                Smean321 = error
            mf.set_constraints(
                ratio321303=r321303,
                eratio321303=er321303,
                #ratio321322=ratio2, eratio321322=eratio2,
                logh2column=logh2column,
                elogh2column=elogh2column,
                logabundance=logabundance,
                elogabundance=elogabundance,
                taline303=Smean303,
                etaline303=error,
                taline321=Smean321,
                etaline321=error,
                mindens=mindens,
                linewidth=10)
            row_data = mf.get_parconstraints()
            row_data['ratio321303'] = r321303
            row_data['eratio321303'] = er321303

            for k in row_data:
                columns[k].append(row_data[k])

            # Exclude bad velocities from cubes
            if row['v_cen'] < -80e3 or row['v_cen'] > 180e3:
                # Skip: there is no real structure down here
                nbad += 1
                is_bad = True
            else:
                is_bad = False
                tcubedata[
                    dend_obj_mask.include()] = row_data['expected_temperature']
                if structure.is_leaf:
                    tcubeleafdata[dend_obj_mask.include(
                    )] = row_data['expected_temperature']

            columns['bad'].append(is_bad)

            width = row['v_rms'] * u.km / u.s
            lengthscale = reff

            #REMOVED in favor of despotic version done in dendrograms.py
            # we use the analytic version here; the despotic version is
            # computed elsewhere (with appropriate gcor factors)
            #columns['tkin_turb'].append(heating.tkin_all(10**row_data['density_chi2']*u.cm**-3,
            #                                             width,
            #                                             lengthscale,
            #                                             width/lengthscale,
            #                                             columns['higaldusttem'][-1]*u.K,
            #                                             crir=0./u.s))

        if len(set(len(c) for k, c in columns.items())) != 1:
            print("Columns are different lengths.  This is not allowed.")
            import ipdb
            ipdb.set_trace()

        for c in columns:
            assert len(columns[c]) == ii + 1

        if plot_some and not is_bad and (ii - nbad % 100 == 0
                                         or ii - nbad < 50):
            try:
                log.info(
                    "T: [{tmin1sig_chi2:7.2f},{expected_temperature:7.2f},{tmax1sig_chi2:7.2f}]"
                    "  R={ratio321303:8.4f}+/-{eratio321303:8.4f}"
                    "  Smean303={Smean303:8.4f} +/- {e303:8.4f}"
                    "  Stot303={Stot303:8.2e}  npix={npix:6d}".format(
                        Smean303=Smean303,
                        Stot303=Stot303,
                        npix=npix,
                        e303=error,
                        **row_data))

                pl.figure(1)
                pl.clf()
                mf.denstemplot()
                pl.savefig(
                    fpath("dendrotem/diagnostics/{0}_{1}.png".format(
                        suffix, ii)))
                pl.figure(2).clf()
                mf.parplot1d_all(levels=[0.68268949213708585])
                pl.savefig(
                    fpath("dendrotem/diagnostics/1dplot{0}_{1}.png".format(
                        suffix, ii)))
                pl.draw()
                pl.show()
            except Exception as ex:
                print ex
                pass
        else:
            pb.update(ii + 1)

        if last_index is not None and ii >= last_index:
            break

    if last_index is not None:
        catalog = catalog[:last_index + 1]

    for k in columns:
        if k not in catalog.keys():
            catalog.add_column(table.Column(name=k, data=columns[k]))

    for mid, lo, hi, letter in (('expected_temperature', 'tmin1sig_chi2',
                                 'tmax1sig_chi2', 't'),
                                ('expected_density', 'dmin1sig_chi2',
                                 'dmax1sig_chi2', 'd'),
                                ('expected_column', 'cmin1sig_chi2',
                                 'cmax1sig_chi2', 'c')):
        catalog.add_column(
            table.Column(name='elo_' + letter,
                         data=catalog[mid] - catalog[lo]))
        catalog.add_column(
            table.Column(name='ehi_' + letter,
                         data=catalog[hi] - catalog[mid]))

    if write:
        catalog.write(tpath('PPV_H2CO_Temperature{0}.ipac'.format(suffix)),
                      format='ascii.ipac')

    # Note that there are overlaps in the catalog, which means that ORDER MATTERS
    # in the above loop.  I haven't yet checked whether large scale overwrites
    # small or vice-versa; it may be that both views of the data are interesting.
    tcube = SpectralCube(
        data=tcubedata,
        wcs=cube303.wcs,
        mask=cube303.mask,
        meta={'unit': 'K'},
        header=cube303.header,
    )
    tcubeleaf = SpectralCube(
        data=tcubeleafdata,
        wcs=cube303.wcs,
        mask=cube303.mask,
        meta={'unit': 'K'},
        header=cube303.header,
    )

    if write:
        log.info("Writing TemperatureCube")
        outpath = 'TemperatureCube_DendrogramObjects{0}.fits'
        tcube.write(hpath(outpath.format(suffix)), overwrite=True)

        outpath_leaf = 'TemperatureCube_DendrogramObjects{0}_leaves.fits'
        tcubeleaf.write(hpath(outpath_leaf.format(suffix)), overwrite=True)

    return catalog, tcube
                                                               column_flat, utline303,
                                                               utline321, unoise)):
    if tcube[z,y,x] == 0:
        logh2column = np.log10(col)+22

        mf.set_constraints(ratio303321=rat, eratio303321=erat,
                           #ratio321322=ratio2, eratio321322=eratio2,
                           logh2column=logh2column, elogh2column=elogh2column,
                           logabundance=logabundance, elogabundance=elogabundance,
                           taline303=ta303.value, etaline303=err,
                           taline321=ta321.value, etaline321=err,
                           linewidth=linewidth)
        row_data = mf.get_parconstraints()
        tcube[z,y,x] = row_data['temperature_chi2']
        row_data['ratio303321'] = rat
        row_data['eratio303321'] = erat

        if ii % 100 == 0 or ii < 50:
            log.info("T: [{tmin1sig_chi2:7.2f},{temperature_chi2:7.2f},{tmax1sig_chi2:7.2f}]  R={ratio303321:6.2f}+/-{eratio303321:6.2f}".format(**row_data))
        else:
            pb.update(ii)
        tcube.flush()
    else:
        pb.update(ii)

tcube[tcube==0] = np.nan
tCube = SpectralCube(tcube, cube303.wcs, mask=BooleanArrayMask(np.isfinite(tcube), wcs=cube303.wcs))
tCube.write(hpath('chi2_temperature_cube.fits'), overwrite=True)

print()
from astropy import log
from spectral_cube import SpectralCube, BooleanArrayMask
from paths import hpath, apath
from astropy.table import Table, Column
from ratio_cubes import (ratio303321, eratio303321, noise_flat, mask, ratioOK,
                         ratio303321sm, ratioOKsm, masksm)
from masked_cubes import (cube303m, cube321m, cube303msm, cube321msm, cube303,
                          cube321, cube303sm, cube321sm, sncube, sncubesm)
from astropy.utils.console import ProgressBar
from astrodendro import Dendrogram, ppv_catalog
from astropy.io import fits
from dendrograms import dend, dendsm
from piecewise_rtotem import pwtem
from temperature_cubes import tcubesm_direct, tcube_direct

tcube_direct.write(hpath('TemperatureCube_PiecewiseFromRatio.fits'),
                   overwrite=True)

tcubesm_direct.write(hpath('TemperatureCube_smooth_PiecewiseFromRatio.fits'),
                     overwrite=True)

# Do the integrated version
for smooth in ('', '_smooth'):
    top = hpath('APEX_H2CO_321_220{0}_bl_mask_integ.fits'.format(smooth))
    bot = hpath('APEX_H2CO_303_202{0}_bl_mask_integ.fits'.format(smooth))
    ff = fits.open(top)
    rr = ff[0].data / fits.getdata(bot)
    tem = pwtem(rr.ravel())
    ff[0].data = tem.reshape(rr.shape)
    ff.writeto(hpath(
        'IntegratedRatioPiecewiseTemperature{0}_303to321.fits'.format(smooth)),
Пример #29
0
import matplotlib
import numpy as np
from astropy import units as u
from astropy import coordinates
from astropy import wcs
from astropy.table import Table
from astropy.io import ascii
try:
    from paths import mpath,apath,fpath,molpath,hpath
except ImportError:
    hpath = lambda x:x


#load 2 datasets from files
dendrogram = load_dendro(hpath('DendroMask_H2CO303202_smooth.hdf5'))
dendro,sncube = dendrogram
sncube.label='S/N Cube'
cube = load_data(hpath('APEX_H2CO_303_202_smooth_bl.fits'))
table = ascii.read(hpath('PPV_H2CO_Temperature_smooth.ipac'), format='ipac')
table['glon'] = table['lon'] - 360*(table['lon'] > 180)
table['xpix'] = table['x_cen'] # Glue "eats" these
table['ypix'] = table['y_cen'] # Glue "eats" these

catalog=Data(parent=table['parent'], label='Fitted Catalog')
#catalog=Data()
for column_name in table.columns:
    cc = table[column_name]
    uu = cc.unit if hasattr(cc, 'unit') else cc.units
    if cc.name == 'parent':
        cc.name = 'cat_parent'
Пример #30
0
                          sncube, sncubesm)
from piecewise_rtotem import pwtem


tcubedata = np.empty(cube303m.shape)
tcubedata[~mask] = np.nan
tcubedata[mask] = pwtem(ratio303321[ratioOK])

tcube = SpectralCube(data=tcubedata, wcs=cube303m.wcs,
                     mask=cube303m.mask, meta={'unit':'K'},
                     header=cube303m.header,
                    )
assert tcube.header['CUNIT3'] == 'km s-1'
tcube_direct = tcube

# smoothed version
tcubedatasm = np.empty(cube303msm.shape)
tcubedatasm[~masksm] = np.nan
tcubedatasm[masksm] = pwtem(ratio303321sm[ratioOKsm])

tcubesm = SpectralCube(data=tcubedatasm, wcs=cube303msm.wcs,
                       mask=cube303msm.mask, meta={'unit':'K'},
                       header=cube303msm.header,)
assert tcubesm.header['CUNIT3'] == 'km s-1'
tcubesm_direct = tcubesm

for sm in ("","_smooth",):#'_321','_321smooth'):
    outpath = 'TemperatureCube_DendrogramObjects{0}_Piecewise.fits'.format(sm)
    tcube = SpectralCube.read(hpath(outpath))
    locals()['tcube_dend{0}'.format(sm)] = tcube
Пример #31
0
"""
Copy data from the appropriate directories to the CDS directory for upload to A&A
"""
import os
from paths import h2copath,figurepath,hpath,rpath,fpath,mpath,molpath,tpath
import tarfile

integrated_files = [hpath(x) for x in ('APEX_H2CO_303_202_masked_moment0.fits',
                                       'APEX_H2CO_303_202_masked_smooth_moment0.fits',
                                       'APEX_H2CO_321_220_masked_moment0.fits',
                                       'APEX_H2CO_321_220_masked_smooth_moment0.fits',
                                       "H2CO_321220_to_303202_bl_integ.fits",
                                       "H2CO_321220_to_303202_bl_integ_weighted.fits",
                                       "H2CO_321220_to_303202_bl_integ_masked_weighted.fits",
                                       "H2CO_321220_to_303202_bl_integ_temperature_dens1e4.fits",
                                       "H2CO_321220_to_303202_bl_integ_temperature_dens1e4_abund1e-10.fits",
                                       "H2CO_321220_to_303202_bl_integ_temperature_dens1e4_abund1e-8.fits",
                                       "H2CO_321220_to_303202_bl_integ_temperature_dens1e5.fits",
                                       "H2CO_321220_to_303202_bl_integ_temperature_dens3e4.fits",
                                       "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4.fits",
                                       "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4_abund1e-10.fits",
                                       "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4_abund1e-8.fits",
                                       "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e5.fits",
                                       "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens3e4.fits",
                                       "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens1e4.fits",
                                       "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens1e4_abund1e-10.fits",
                                       "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens1e4_abund1e-8.fits",
                                       "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens1e5.fits",
                                       "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens3e4.fits",
                                       "pv_H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4.fits",
                                       "pv_H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4_abund1e-10.fits",
Пример #32
0
import pylab as pl
import matplotlib
from astropy.io import fits
from masked_cubes import cube303, cube303m, cube321m, cube303msm, cube321msm, sncube, sncubesm
from astropy import log
import wcsaxes
from aplpy.regions import ds9
from astropy.wcs import WCS
from agpy import asinh_norm

pl.ioff()

cm = matplotlib.cm.RdYlBu_r
cm.set_bad('#888888')

if not os.path.exists(hpath("APEX_H2CO_303_202_integrated.fits")):
    im = cube303.sum(axis=0).hdu
    im.writeto(hpath("APEX_H2CO_303_202_integrated.fits"))
else:
    im = fits.open(hpath("APEX_H2CO_303_202_integrated.fits"))[0]

wcs = WCS(im.header)

fig1 = pl.figure(1)
fig1.clf()
ax = wcsaxes.WCSAxesSubplot(fig1, 1,1,1, wcs=wcs)
fig1.add_axes(ax)

cm = pl.cm.gray_r
im.data -= np.nanmin(im.data) - 0.00001
vmin = np.percentile(im.data[np.isfinite(im.data)], 00.05)
Пример #33
0
import pylab as pl
import matplotlib
from astropy.io import fits
from masked_cubes import cube303, cube303m, cube321m, cube303msm, cube321msm, sncube, sncubesm
from astropy import log
import wcsaxes
from aplpy.regions import ds9
from astropy.wcs import WCS
from agpy import asinh_norm

pl.ioff()

cm = matplotlib.cm.RdYlBu_r
cm.set_bad('#888888')

if not os.path.exists(hpath("APEX_H2CO_303_202_integrated.fits")):
    im = cube303.sum(axis=0).hdu
    im.writeto(hpath("APEX_H2CO_303_202_integrated.fits"))
else:
    im = fits.open(hpath("APEX_H2CO_303_202_integrated.fits"))[0]

wcs = WCS(im.header)

fig1 = pl.figure(1)
fig1.clf()
ax = wcsaxes.WCSAxesSubplot(fig1, 1, 1, 1, wcs=wcs)
fig1.add_axes(ax)

cm = pl.cm.gray_r
im.data -= np.nanmin(im.data) - 0.00001
vmin = np.percentile(im.data[np.isfinite(im.data)], 00.05)
dustcolumn = fits.open('/Users/adam/work/gc/gcmosaic_column_conv36.fits')
dustcoldata = dustcolumn[0].data
dustcolwcs = WCS(dustcolumn[0].header)

# Direct from cubes
for method in ('max', 'mean', 'moment0'):
    for cube, name, masked, smooth in zip(
        (cube303, cube303m, cube303sm, cube303msm, cube321, cube321m,
         cube321sm, cube321msm), ['303_202'] * 4 + ['321_220'] * 4,
        ['', '_masked'] * 4, ['', '', '_smooth', '_smooth'] * 2):

        outname = "APEX_H2CO_{0}{1}{2}_{3}.fits".format(
            name, masked, smooth, method)
        log.info(outname)
        if not os.path.exists(hpath(outname)):
            im = getattr(cube, method)(axis=0).hdu
            im.writeto(hpath(outname), clobber=True)
            log.info("{0} written.".format(outname))
        else:
            im = fits.open(hpath(outname))[0]
            log.info("{0} read.".format(outname))

        wcs = WCS(im.header)

        fig1 = pl.figure(1, figsize=figsize)
        fig1.clf()
        ax = wcsaxes.WCSAxesSubplot(fig1, 1, 1, 1, wcs=wcs)
        fig1.add_axes(ax)

        cm = pl.cm.gray_r
Пример #35
0
from glue import qglue

import matplotlib
import numpy as np
from astropy import units as u
from astropy import coordinates
from astropy import wcs
from astropy.table import Table
from astropy.io import ascii
try:
    from paths import mpath, apath, fpath, molpath, hpath
except ImportError:
    hpath = lambda x: x

#load 2 datasets from files
dendrogram = load_dendro(hpath('DendroMask_H2CO303202_smooth.hdf5'))
dendro, sncube = dendrogram
sncube.label = 'S/N Cube'
cube = load_data(hpath('APEX_H2CO_303_202_smooth_bl.fits'))
table = ascii.read(hpath('PPV_H2CO_Temperature_smooth.ipac'), format='ipac')
table['glon'] = table['lon'] - 360 * (table['lon'] > 180)
table['xpix'] = table['x_cen']  # Glue "eats" these
table['ypix'] = table['y_cen']  # Glue "eats" these

catalog = Data(parent=table['parent'], label='Fitted Catalog')
#catalog=Data()
for column_name in table.columns:
    cc = table[column_name]
    uu = cc.unit if hasattr(cc, 'unit') else cc.units
    if cc.name == 'parent':
        cc.name = 'cat_parent'
from paths import hpath, apath
from astropy.table import Table, Column
from ratio_cubes import (ratio303321, eratio303321, noise_flat, mask, ratioOK,
                         ratio303321sm, ratioOKsm, masksm)
from masked_cubes import (cube303m,cube321m,cube303msm,cube321msm,
                          cube303,cube321,cube303sm,cube321sm,
                          sncube, sncubesm)
from astropy.utils.console import ProgressBar
from astrodendro import Dendrogram,ppv_catalog
from astropy.io import fits
from dendrograms import dend, dendsm
from piecewise_rtotem import pwtem
from temperature_cubes import tcubesm_direct, tcube_direct


tcube_direct.write(hpath('TemperatureCube_PiecewiseFromRatio.fits'), overwrite=True)

tcubesm_direct.write(hpath('TemperatureCube_smooth_PiecewiseFromRatio.fits'), overwrite=True)

# Do the integrated version
for smooth in ('', '_smooth'):
    top = hpath('APEX_H2CO_321_220{0}_bl_mask_integ.fits'.format(smooth))
    bot = hpath('APEX_H2CO_303_202{0}_bl_mask_integ.fits'.format(smooth))
    ff = fits.open(top)
    rr = ff[0].data/fits.getdata(bot)
    tem = pwtem(rr.ravel())
    ff[0].data = tem.reshape(rr.shape)
    ff.writeto(hpath('IntegratedRatioPiecewiseTemperature{0}_303to321.fits'.format(smooth)),
               clobber=True)

# Try the same thing but on the dendrogrammed data.  Basically, this is 
dustcolumn = fits.open('/Users/adam/work/gc/gcmosaic_column_conv36.fits')
dustcoldata = dustcolumn[0].data
dustcolwcs = WCS(dustcolumn[0].header)

# Direct from cubes
for method in ('max','mean','moment0'):
    for cube,name,masked,smooth in zip((cube303, cube303m, cube303sm, cube303msm,
                                        cube321, cube321m,  cube321sm, cube321msm),
                                       ['303_202']*4 + ['321_220']*4,
                                       ['','_masked']*4,
                                       ['','','_smooth','_smooth']*2):

        outname = "APEX_H2CO_{0}{1}{2}_{3}.fits".format(name,masked,smooth,method)
        log.info(outname)
        if not os.path.exists(hpath(outname)):
            im = getattr(cube,method)(axis=0).hdu
            im.writeto(hpath(outname), clobber=True)
            log.info("{0} written.".format(outname))
        else:
            im = fits.open(hpath(outname))[0]
            log.info("{0} read.".format(outname))

        wcs = WCS(im.header)

        fig1 = pl.figure(1, figsize=figsize)
        fig1.clf()
        ax = wcsaxes.WCSAxesSubplot(fig1, 1,1,1, wcs=wcs)
        fig1.add_axes(ax)

        cm = pl.cm.gray_r
Пример #38
0
"""
import numpy as np
import time
import warnings
from astropy import log
from astrodendro import Dendrogram,ppv_catalog
from astropy.table import Table, Column
from paths import hpath,tpath
from masked_cubes import cube303,cube303sm,cube321,cube321sm
import flag_other_lines
from astropy.utils.console import ProgressBar

if 'dend' not in locals():
    t0 = time.time()
    log.info("Loading dendrogram from file.")
    dend = Dendrogram.load_from(hpath("DendroMask_H2CO303202.hdf5"))
    log.info("Loaded dendrogram from file in {0:0.1f} seconds.".format(time.time()-t0))
    dend.wcs = cube303.wcs

if 'dendsm' not in locals():
    t0 = time.time()
    log.info("Loading dendrogram from file.")
    dendsm = Dendrogram.load_from(hpath("DendroMask_H2CO303202_smooth.hdf5"))
    log.info("Loaded dendrogram from file in {0:0.1f} seconds.".format(time.time()-t0))
    dendsm.wcs = cube303sm.wcs

# Removed: the 321 signal extraction approach never worked nicely
# if 'dend321' not in locals():
#     t0 = time.time()
#     log.info("Loading dendrogram from file.")
#     dend321 = Dendrogram.load_from(hpath("DendroMask_H2CO321220.hdf5"))
Пример #39
0
import matplotlib
import numpy as np
from astropy import units as u
from astropy import coordinates
from astropy import wcs
from astropy.io import ascii
from paths import mpath,apath,fpath,molpath,hpath
import pvextractor

catalog = astropy_tabular_data(apath('fitted_line_parameters_Chi2Constraints.ipac'),
                               format='ascii.ipac')
catalog.label='FittedLineParameters'
catalog.style.color = 'green'
catalog.style.marker = 'o'
cube = load_data(hpath('APEX_H2CO_303_202_bl.fits'))
cube.label='H2CO 303/202'
cube2 = load_data(molpath('APEX_SiO_54.fits'))
cube2.label='SiO'
cube3 = load_data(hpath('APEX_13CO_matched_H2CO.fits'))
cube3.label='13CO'
higaltem = load_data('/Users/adam/work/gc/gcmosaic_temp_conv36.fits')

dc = DataCollection([cube, catalog, cube2, cube3, higaltem])
dc.merge(cube,cube2,cube3)

dc.add_link(LinkSame(cube.id['Galactic Longitude'], catalog.id['GLON']))
dc.add_link(LinkSame(cube.id['Galactic Latitude'], catalog.id['GLAT']))

def ms_to_kms(x): return x/1e3
def kms_to_ms(x): return x*1e3
Пример #40
0
    wcs=cube303m.wcs,
    mask=cube303m.mask,
    meta={'unit': 'K'},
    header=cube303m.header,
)
assert tcube.header['CUNIT3'] == 'km s-1'
tcube_direct = tcube

# smoothed version
tcubedatasm = np.empty(cube303msm.shape)
tcubedatasm[~masksm] = np.nan
tcubedatasm[masksm] = pwtem(ratio303321sm[ratioOKsm])

tcubesm = SpectralCube(
    data=tcubedatasm,
    wcs=cube303msm.wcs,
    mask=cube303msm.mask,
    meta={'unit': 'K'},
    header=cube303msm.header,
)
assert tcubesm.header['CUNIT3'] == 'km s-1'
tcubesm_direct = tcubesm

for sm in (
        "",
        "_smooth",
):  #'_321','_321smooth'):
    outpath = 'TemperatureCube_DendrogramObjects{0}_Piecewise.fits'.format(sm)
    tcube = SpectralCube.read(hpath(outpath))
    locals()['tcube_dend{0}'.format(sm)] = tcube
Пример #41
0
import matplotlib
import numpy as np
from astropy import units as u
from astropy import coordinates
from astropy import wcs
from astropy.io import ascii
from paths import mpath, apath, fpath, molpath, hpath
import pvextractor

catalog = astropy_tabular_data(
    apath('fitted_line_parameters_Chi2Constraints.ipac'), format='ascii.ipac')
catalog.label = 'FittedLineParameters'
catalog.style.color = 'green'
catalog.style.marker = 'o'
cube = load_data(hpath('APEX_H2CO_303_202_bl.fits'))
cube.label = 'H2CO 303/202'
cube2 = load_data(molpath('APEX_SiO_54.fits'))
cube2.label = 'SiO'
cube3 = load_data(hpath('APEX_13CO_matched_H2CO.fits'))
cube3.label = '13CO'
higaltem = load_data('/Users/adam/work/gc/gcmosaic_temp_conv36.fits')

dc = DataCollection([cube, catalog, cube2, cube3, higaltem])
dc.merge(cube, cube2, cube3)

dc.add_link(LinkSame(cube.id['Galactic Longitude'], catalog.id['GLON']))
dc.add_link(LinkSame(cube.id['Galactic Latitude'], catalog.id['GLAT']))


def ms_to_kms(x):
Пример #42
0
                                                                             int(vrange[0]),
                                                                             int(vrange[1]))))


fig = pl.figure(1, figsize=figsize)
for cube,sn,smooth in zip((ratiocube_303321, ratiocubesm_303321),
                          (sncube, sncubesm),
                          ("","_smooth",)):#"_vsmooth"):
    for vrange in zip(vcuts[:-1], vcuts[1:]):
        fig.clf()
        proj = cube.spectral_slab(*(vrange*u.km/u.s)).mean(axis=0)
        tproj = np.copy(proj)
        tproj[np.isfinite(proj)] = pwtem(proj[np.isfinite(proj)].value)
        hdu = fits.PrimaryHDU(tproj, proj.hdu.header)
        hdu.writeto(hpath("tmap{0}_{1}to{2}".format(smooth,
                                                    int(vrange[0]),
                                                    int(vrange[1]))),
                    clobber=True)
        F = aplpy.FITSFigure(hdu,
                             convention='calabretta',
                             figure=fig)

        #cm = copy.copy(pl.cm.rainbow)
        #cm.set_bad((0.5,0.5,0.5,0.5))
        cm = copy.copy(pl.cm.RdYlBu_r)
        cm.set_bad('#888888')
        F.show_colorscale(cmap=cm,vmin=15,vmax=200)
        F.set_tick_labels_format('d.dd','d.dd')
        peaksn = sn.spectral_slab(*(vrange*u.km/u.s)).max(axis=0)
        peaksn[(peaksn<0) | np.isnan(peaksn)] = 0
        color = (0.5,)*3 # should be same as background #888
Пример #43
0
molecules = ('13CO_2014_merge', 'C18O_2014_merge', 'H2CO_303_202_bl', 'SiO_54_bl')

filenames = [molpath('APEX_{0}.fits'.format(molecule))
            for molecule in molecules]

molecules = molecules + ('H2CO_TemperatureFromRatio',
                         'H2CO_TemperatureFromRatio_smooth',
                         'H2CO_Ratio',
                         'H2CO_Ratio_smooth',
                         #'H2CO_DendrogramTemperature',
                         #'H2CO_DendrogramTemperature_smooth',
                         #'H2CO_DendrogramTemperature_Leaves',
                         #'H2CO_DendrogramTemperature_Leaves_smooth',
                        )
filenames.append(hpath('TemperatureCube_PiecewiseFromRatio.fits'))
filenames.append(hpath('TemperatureCube_smooth_PiecewiseFromRatio.fits'))
filenames.append(hpath('H2CO_321220_to_303202_cube_bl.fits'))
filenames.append(hpath('H2CO_321220_to_303202_cube_smooth_bl.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_smooth.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_leaves.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_smooth_leaves.fits'))

def offset_to_point(ll, bb):
    """
    Determine the offset along the orbit to the nearest point on an orbit to
    the specified point
    """
    import shapely.geometry as geom
Пример #44
0
import matplotlib
import numpy as np
from astropy import log
from astropy import units as u
from astropy import coordinates
from astropy import wcs
from astropy.table import Table
from astropy.io import ascii
try:
    from paths import mpath,apath,fpath,molpath,hpath
except ImportError:
    hpath = lambda x:x


dendrogram = load_dendro(hpath('DendroMask_H2CO303202.hdf5'))
dendro,dendcube = dendrogram
dendcube.label='Dendrogram Cube' # this label is redundant, it will be deleted upon merge
# cube contains real WCS information; dendcube does not
h2cocube = load_data(hpath('APEX_H2CO_303_202_bl.fits'))
h2cocube.label='H2CO 303202 Cube'
catalog = astropy_tabular_data(hpath('PPV_H2CO_Temperature.ipac'), format='ipac')
catalog.label='Fitted Catalog'


h2cocube.add_component(dendcube.get_component('structure'), 'structure')

dc = DataCollection(dendrogram)
dc.append(h2cocube)
dc.append(catalog)
dc.append(dendcube)
Пример #45
0
        '_dendro',
        '_dendro_leaves',
        '_directpv1e4',
        '_directpv1e5',
        '_directpv3e4',
        '_directpv1e4_masked',
        '_directpv1e5_masked',
        '_directpv3e4_masked',
    ))

#for vmax,vmax_str in zip((100,200),("_vmax100","")):
for vmax, vmax_str in zip((200, ), ("", )):
    for weight in ("_weight", ""):
        for smooth in ("", ):  #("_smooth",""):#"_vsmooth"):
            sncubefile = hpath(
                'APEX_H2CO_303_202{0}_signal_to_noise_cube.fits'.format(
                    smooth))
            sncube = SpectralCube.read(sncubefile)
            snproj = sncube.max(axis=1)
            snproj.wcs.wcs.ctype[0] = 'OFFSET'
            snhdu = snproj.hdu
            snhdu.header['CTYPE1'] = 'GLON'

            fig5 = pl.figure(5, figsize=figsize)
            fig5.clf()
            Fsn = aplpy.FITSFigure(snhdu, convention='calabretta', figure=fig5)
            Fsn.show_grayscale(vmin=0, vmax=10, stretch='linear', invert=True)
            Fsn.add_colorbar()
            Fsn.colorbar.set_axis_label_text('Peak S/N')
            Fsn.recenter(**recen)
            Fsn.axis_labels.set_xtext(r'Galactic Longitude')
Пример #46
0
            for molecule in molecules]

molecules = molecules + (
                         'H2CO_Ratio',
                         #'H2CO_Ratio_smooth',
                         'H2CO_TemperatureFromRatio',
                         #'H2CO_TemperatureFromRatio_smooth',
                         #'H2CO_DendrogramTemperature',
                         #'H2CO_DendrogramTemperature_smooth',
                         #'H2CO_DendrogramTemperature_Leaves',
                         #'H2CO_DendrogramTemperature_Leaves_smooth'
                        )
# make_piecewise_temcube:
#filenames.append(hpath('TemperatureCube_PiecewiseFromRatio.fits'))
#filenames.append(hpath('TemperatureCube_smooth_PiecewiseFromRatio.fits'))
filenames.append(hpath('H2CO_321220_to_303202_cube_bl.fits'))
#filenames.append(hpath('H2CO_321220_to_303202_cube_smooth_bl.fits'))
filenames.append(hpath('H2CO_321220_to_303202_cube_bl.fits'))
#filenames.append(hpath('H2CO_321220_to_303202_cube_smooth_bl.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_smooth.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_leaves.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_smooth_leaves.fits'))


cmap = copy.copy(pl.cm.RdYlBu_r)
cmap.set_under((0.9,0.9,0.9,0.5))

for weight in ("","_weighted"):
    #for molecule,fn in zip(molecules[-2:],filenames[-2:]):
    for molecule,fn in zip(molecules,filenames):
Пример #47
0
def measure_dendrogram_properties(dend=None, cube303=cube303,
                                  cube321=cube321, cube13co=cube13co,
                                  cube18co=cube18co, noise_cube=noise_cube,
                                  sncube=sncube,
                                  suffix="",
                                  last_index=None,
                                  plot_some=True,
                                  line='303',
                                  write=True):

    assert (cube321.shape == cube303.shape == noise_cube.shape ==
            cube13co.shape == cube18co.shape == sncube.shape)
    assert sncube.wcs is cube303.wcs is sncube.mask._wcs

    metadata = {}
    metadata['data_unit'] = u.K
    metadata['spatial_scale'] =  7.2 * u.arcsec
    metadata['beam_major'] =  30 * u.arcsec
    metadata['beam_minor'] =  30 * u.arcsec
    metadata['wavelength'] =  218.22219*u.GHz
    metadata['velocity_scale'] = u.km/u.s
    metadata['wcs'] = cube303.wcs

    keys = [
            'density_chi2',
            'expected_density',
            'dmin1sig_chi2',
            'dmax1sig_chi2',
            'column_chi2',
            'expected_column',
            'cmin1sig_chi2',
            'cmax1sig_chi2',
            'temperature_chi2',
            'expected_temperature',
            'tmin1sig_chi2',
            'tmax1sig_chi2',
            'eratio321303',
            'ratio321303',
            'logh2column',
            'elogh2column',
            'logabundance',
            'elogabundance',
           ]
    obs_keys = [
            'Stot303',
            'Smin303',
            'Smax303',
            'Stot321',
            'Smean303',
            'Smean321',
            'npix',
            'e303',
            'e321',
            'r321303',
            'er321303',
            '13cosum',
            'c18osum',
            '13comean',
            'c18omean',
            's_ntotal',
            'index',
            'is_leaf',
            'parent',
            'root',
            'lon',
            'lat',
            'vcen',
            'higaldusttem',
            'reff',
            'dustmass',
            'dustmindens',
            'bad',
            #'tkin_turb',
    ]
    columns = {k:[] for k in (keys+obs_keys)}

    log.debug("Initializing dendrogram temperature fitting loop")

    # FORCE wcs to match
    # (technically should reproject here)
    cube13co._wcs = cube18co._wcs = cube303.wcs
    cube13co.mask._wcs = cube18co.mask._wcs = cube303.wcs

    if line == '303':
        maincube = cube303
    elif line == '321':
        maincube = cube321
    else:
        raise ValueError("Unrecognized line: {0}".format(line))

    # Prepare an array to hold the fitted temperatures
    tcubedata = np.empty(maincube.shape, dtype='float32')
    tcubedata[:] = np.nan
    tcubeleafdata = np.empty(maincube.shape, dtype='float32')
    tcubeleafdata[:] = np.nan


    nbad = 0

    catalog = ppv_catalog(dend, metadata)
    pb = ProgressBar(len(catalog))
    for ii,row in enumerate(catalog):
        structure = dend[row['_idx']]
        assert structure.idx == row['_idx'] == ii
        dend_obj_mask = BooleanArrayMask(structure.get_mask(), wcs=cube303.wcs)
        dend_inds = structure.indices()

        view = (slice(dend_inds[0].min(), dend_inds[0].max()+1),
                slice(dend_inds[1].min(), dend_inds[1].max()+1),
                slice(dend_inds[2].min(), dend_inds[2].max()+1),)
        #view2 = cube303.subcube_slices_from_mask(dend_obj_mask)
        submask = dend_obj_mask[view]
        #assert np.count_nonzero(submask.include()) == np.count_nonzero(dend_obj_mask.include())

        sn = sncube[view].with_mask(submask)
        sntot = sn.sum().value
        #np.testing.assert_almost_equal(sntot, structure.values().sum(), decimal=0)

        c303 = cube303[view].with_mask(submask)
        c321 = cube321[view].with_mask(submask)
        co13sum = cube13co[view].with_mask(submask).sum().value
        co18sum = cube18co[view].with_mask(submask).sum().value
        if hasattr(co13sum,'__len__'):
            raise TypeError(".sum() applied to an array has yielded a non scalar.")

        npix = submask.include().sum()
        assert npix == structure.get_npix()
        Stot303 = c303.sum().value
        if np.isnan(Stot303):
            raise ValueError("NaN in cube.  This can't happen: the data from "
                             "which the dendrogram was derived can't have "
                             "NaN pixels.")
        Smax303 = c303.max().value
        Smin303 = c303.min().value

        Stot321 = c321.sum().value
        if npix == 0:
            raise ValueError("npix=0. This is impossible.")
        Smean303 = Stot303/npix
        if Stot303 <= 0 and line=='303':
            raise ValueError("The 303 flux is <=0.  This isn't possible because "
                             "the dendrogram was derived from the 303 data with a "
                             "non-zero threshold.")
        elif Stot303 <= 0 and line=='321':
            Stot303 = 0
            Smean303 = 0
        elif Stot321 <= 0 and line=='321':
            raise ValueError("The 321 flux is <=0.  This isn't possible because "
                             "the dendrogram was derived from the 321 data with a "
                             "non-zero threshold.")
        if np.isnan(Stot321):
            raise ValueError("NaN in 321 line")
        Smean321 = Stot321/npix

        #error = (noise_cube[view][submask.include()]).sum() / submask.include().sum()**0.5
        var = ((noise_cube[dend_obj_mask.include()]**2).sum() / npix**2)
        error = var**0.5
        if np.isnan(error):
            raise ValueError("error is nan: this is impossible by definition.")

        if line == '321' and Stot303 == 0:
            r321303 = np.nan
            er321303 = np.nan
        elif Stot321 < 0:
            r321303 = error / Smean303
            er321303 = (r321303**2 * (var/Smean303**2 + 1))**0.5
        else:
            r321303 = Stot321 / Stot303
            er321303 = (r321303**2 * (var/Smean303**2 + var/Smean321**2))**0.5

        for c in columns:
            assert len(columns[c]) == ii

        columns['index'].append(row['_idx'])
        columns['s_ntotal'].append(sntot)
        columns['Stot303'].append(Stot303)
        columns['Smax303'].append(Smax303)
        columns['Smin303'].append(Smin303)
        columns['Stot321'].append(Stot321)
        columns['Smean303'].append(Smean303)
        columns['Smean321'].append(Smean321)
        columns['npix'].append(npix)
        columns['e303'].append(error)
        columns['e321'].append(error)
        columns['r321303'].append(r321303)
        columns['er321303'].append(er321303)
        columns['13cosum'].append(co13sum)
        columns['c18osum'].append(co18sum)
        columns['13comean'].append(co13sum/npix)
        columns['c18omean'].append(co18sum/npix)
        columns['is_leaf'].append(structure.is_leaf)
        columns['parent'].append(structure.parent.idx if structure.parent else -1)
        columns['root'].append(get_root(structure).idx)
        s_main = maincube._data[dend_inds]
        x,y,z = maincube.world[dend_inds]
        lon = ((z.value-(360*(z.value>180)))*s_main).sum()/s_main.sum()
        lat = (y*s_main).sum()/s_main.sum()
        vel = (x*s_main).sum()/s_main.sum()
        columns['lon'].append(lon)
        columns['lat'].append(lat.value)
        columns['vcen'].append(vel.value)

        mask2d = dend_obj_mask.include().max(axis=0)[view[1:]]
        logh2column = np.log10(np.nanmean(column_regridded.data[view[1:]][mask2d]) * 1e22)
        if np.isnan(logh2column):
            log.info("Source #{0} has NaNs".format(ii))
            logh2column = 24
        elogh2column = elogabundance
        columns['higaldusttem'].append(np.nanmean(dusttem_regridded.data[view[1:]][mask2d]))

        r_arcsec = row['radius']*u.arcsec
        reff = (r_arcsec*(8.5*u.kpc)).to(u.pc, u.dimensionless_angles())
        mass = ((10**logh2column*u.cm**-2)*np.pi*reff**2*2.8*constants.m_p).to(u.M_sun)
        density = (mass/(4/3.*np.pi*reff**3)/constants.m_p/2.8).to(u.cm**-3)

        columns['reff'].append(reff.value)
        columns['dustmass'].append(mass.value)
        columns['dustmindens'].append(density.value)
        mindens = np.log10(density.value)
        if mindens < 3:
            mindens = 3

        if (r321303 < 0 or np.isnan(r321303)) and line != '321':
            raise ValueError("Ratio <0: This can't happen any more because "
                             "if either num/denom is <0, an exception is "
                             "raised earlier")
            #for k in columns:
            #    if k not in obs_keys:
            #        columns[k].append(np.nan)
        elif (r321303 < 0 or np.isnan(r321303)) and line == '321':
            for k in keys:
                columns[k].append(np.nan)
        else:
            # Replace negatives for fitting
            if Smean321 <= 0:
                Smean321 = error
            mf.set_constraints(ratio321303=r321303, eratio321303=er321303,
                               #ratio321322=ratio2, eratio321322=eratio2,
                               logh2column=logh2column, elogh2column=elogh2column,
                               logabundance=logabundance, elogabundance=elogabundance,
                               taline303=Smean303, etaline303=error,
                               taline321=Smean321, etaline321=error,
                               mindens=mindens,
                               linewidth=10)
            row_data = mf.get_parconstraints()
            row_data['ratio321303'] = r321303
            row_data['eratio321303'] = er321303

            for k in row_data:
                columns[k].append(row_data[k])

            # Exclude bad velocities from cubes
            if row['v_cen'] < -80e3 or row['v_cen'] > 180e3:
                # Skip: there is no real structure down here
                nbad += 1
                is_bad = True
            else:
                is_bad = False
                tcubedata[dend_obj_mask.include()] = row_data['expected_temperature']
                if structure.is_leaf:
                    tcubeleafdata[dend_obj_mask.include()] = row_data['expected_temperature']

            columns['bad'].append(is_bad)

            width = row['v_rms']*u.km/u.s
            lengthscale = reff

            #REMOVED in favor of despotic version done in dendrograms.py
            # we use the analytic version here; the despotic version is
            # computed elsewhere (with appropriate gcor factors)
            #columns['tkin_turb'].append(heating.tkin_all(10**row_data['density_chi2']*u.cm**-3,
            #                                             width,
            #                                             lengthscale,
            #                                             width/lengthscale,
            #                                             columns['higaldusttem'][-1]*u.K,
            #                                             crir=0./u.s))

        if len(set(len(c) for k,c in columns.items())) != 1:
            print("Columns are different lengths.  This is not allowed.")
            import ipdb; ipdb.set_trace()

        for c in columns:
            assert len(columns[c]) == ii+1

        if plot_some and not is_bad and (ii-nbad % 100 == 0 or ii-nbad < 50):
            try:
                log.info("T: [{tmin1sig_chi2:7.2f},{expected_temperature:7.2f},{tmax1sig_chi2:7.2f}]"
                         "  R={ratio321303:8.4f}+/-{eratio321303:8.4f}"
                         "  Smean303={Smean303:8.4f} +/- {e303:8.4f}"
                         "  Stot303={Stot303:8.2e}  npix={npix:6d}"
                         .format(Smean303=Smean303, Stot303=Stot303,
                                 npix=npix, e303=error, **row_data))

                pl.figure(1)
                pl.clf()
                mf.denstemplot()
                pl.savefig(fpath("dendrotem/diagnostics/{0}_{1}.png".format(suffix,ii)))
                pl.figure(2).clf()
                mf.parplot1d_all(levels=[0.68268949213708585])
                pl.savefig(fpath("dendrotem/diagnostics/1dplot{0}_{1}.png".format(suffix,ii)))
                pl.draw()
                pl.show()
            except Exception as ex:
                print ex
                pass
        else:
            pb.update(ii+1)

        if last_index is not None and ii >= last_index:
            break

    if last_index is not None:
        catalog = catalog[:last_index+1]

    for k in columns:
        if k not in catalog.keys():
            catalog.add_column(table.Column(name=k, data=columns[k]))

    for mid,lo,hi,letter in (('expected_temperature','tmin1sig_chi2','tmax1sig_chi2','t'),
                             ('expected_density','dmin1sig_chi2','dmax1sig_chi2','d'),
                             ('expected_column','cmin1sig_chi2','cmax1sig_chi2','c')):
        catalog.add_column(table.Column(name='elo_'+letter,
                                        data=catalog[mid]-catalog[lo]))
        catalog.add_column(table.Column(name='ehi_'+letter,
                                        data=catalog[hi]-catalog[mid]))

    if write:
        catalog.write(tpath('PPV_H2CO_Temperature{0}.ipac'.format(suffix)), format='ascii.ipac')

    # Note that there are overlaps in the catalog, which means that ORDER MATTERS
    # in the above loop.  I haven't yet checked whether large scale overwrites
    # small or vice-versa; it may be that both views of the data are interesting.
    tcube = SpectralCube(data=tcubedata, wcs=cube303.wcs,
                         mask=cube303.mask, meta={'unit':'K'},
                         header=cube303.header,
                        )
    tcubeleaf = SpectralCube(data=tcubeleafdata, wcs=cube303.wcs,
                         mask=cube303.mask, meta={'unit':'K'},
                         header=cube303.header,
                        )

    if write:
        log.info("Writing TemperatureCube")
        outpath = 'TemperatureCube_DendrogramObjects{0}.fits'
        tcube.write(hpath(outpath.format(suffix)),
                    overwrite=True)

        outpath_leaf = 'TemperatureCube_DendrogramObjects{0}_leaves.fits'
        tcubeleaf.write(hpath(outpath_leaf.format(suffix)),
                    overwrite=True)


    return catalog, tcube
Пример #48
0
F.show_markers(tbl[cooler_than_turb]['x_cen'] - 360 *
               (tbl[cooler_than_turb]['x_cen'] > 180),
               tbl[cooler_than_turb]['y_cen'],
               marker='+',
               color='k',
               facecolor='k',
               edgecolor='k')

# PV
recen = dict(x=0.57, y=20e3, width=2.26, height=250e3)
vmin = 10
vmax = 200
cmap = pl.cm.RdYlBu_r
figsize = (20, 10)
ftemplate = 'pv_H2CO_321220_to_303202{0}_bl_integ_weighted_temperature_dens1e4.fits'
hdu = fits.open(paths.hpath(ftemplate.format(smooth)))[0]
hdu.header['CTYPE1'] = 'GLON'

fig = pl.figure(5, figsize=figsize)
fig.clf()

F = aplpy.FITSFigure(hdu, figure=fig)

cm = copy.copy(cmap)
cm.set_bad((0.5, ) * 3)
F.show_colorscale(cmap=cm,
                  vmin=vmin,
                  vmax=vmax,
                  aspect=1 if smooth == '' else 2)
F.recenter(**recen)
F.tick_labels.set_xformat('d.dd')
Пример #49
0
from spectral_cube import SpectralCube
from paths import hpath

cube13co = SpectralCube.read(hpath('APEX_13CO_matched_H2CO.fits'))
cube18co = SpectralCube.read(hpath('APEX_C18O_matched_H2CO.fits'))
cube13cosm = SpectralCube.read(hpath('APEX_13CO_matched_H2CO_smooth.fits'))
cube18cosm = SpectralCube.read(hpath('APEX_C18O_matched_H2CO_smooth.fits'))
Пример #50
0
            for molecule in molecules]

molecules = molecules + (
                         'H2CO_Ratio',
                         #'H2CO_Ratio_smooth',
                         'H2CO_TemperatureFromRatio',
                         #'H2CO_TemperatureFromRatio_smooth',
                         #'H2CO_DendrogramTemperature',
                         #'H2CO_DendrogramTemperature_smooth',
                         #'H2CO_DendrogramTemperature_Leaves',
                         #'H2CO_DendrogramTemperature_Leaves_smooth'
                        )
# make_piecewise_temcube:
#filenames.append(hpath('TemperatureCube_PiecewiseFromRatio.fits'))
#filenames.append(hpath('TemperatureCube_smooth_PiecewiseFromRatio.fits'))
filenames.append(hpath('H2CO_321220_to_303202_cube_bl.fits'))
#filenames.append(hpath('H2CO_321220_to_303202_cube_smooth_bl.fits'))
filenames.append(hpath('H2CO_321220_to_303202_cube_bl.fits'))
#filenames.append(hpath('H2CO_321220_to_303202_cube_smooth_bl.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_smooth.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_leaves.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_smooth_leaves.fits'))


cmap = copy.copy(pl.cm.RdYlBu_r)
cmap.set_under((0.9,0.9,0.9,0.5))

for weight in ("","_weighted"):
    for molecule,fn in zip(molecules[-2:],filenames[-2:]):
        log.info(molecule)
Пример #51
0
filenames = [
    molpath('APEX_{0}.fits'.format(molecule)) for molecule in molecules
]

molecules = molecules + (
    'H2CO_TemperatureFromRatio',
    'H2CO_TemperatureFromRatio_smooth',
    'H2CO_Ratio',
    'H2CO_Ratio_smooth',
    #'H2CO_DendrogramTemperature',
    #'H2CO_DendrogramTemperature_smooth',
    #'H2CO_DendrogramTemperature_Leaves',
    #'H2CO_DendrogramTemperature_Leaves_smooth',
)
filenames.append(hpath('TemperatureCube_PiecewiseFromRatio.fits'))
filenames.append(hpath('TemperatureCube_smooth_PiecewiseFromRatio.fits'))
filenames.append(hpath('H2CO_321220_to_303202_cube_bl.fits'))
filenames.append(hpath('H2CO_321220_to_303202_cube_smooth_bl.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_smooth.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_leaves.fits'))
#filenames.append(hpath('TemperatureCube_DendrogramObjects_smooth_leaves.fits'))


def offset_to_point(ll, bb):
    """
    Determine the offset along the orbit to the nearest point on an orbit to
    the specified point
    """
    import shapely.geometry as geom
Пример #52
0
"""
Compare the various temperature maps to ensure that they are identical
"""
import numpy as np
from spectral_cube import SpectralCube, BooleanArrayMask
from temperature_cubes import tcubesm_direct, tcube_direct
from astropy.io import fits
import paths
from paths import hpath
import copy

""" Generated in make_ratiotem_cubesims.py """
im1 = fits.getdata(hpath('H2CO_321220_to_303202_bl_integ_temperature_dens1e4_masked.fits'))
im2 = fits.getdata(hpath('H2CO_321220_to_303202_bl_integ_temperature_dens1e4.fits'))
im3 = fits.getdata(hpath('H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4_masked.fits'))

tcube = tcube_direct
tcube_mean = tcube.mean(axis=0)

wcube = SpectralCube.read(hpath('APEX_H2CO_303_202_bl.fits'))
mcube = SpectralCube.read(hpath('APEX_H2CO_303_202_bl_mask.fits'))

if tcube.shape != wcube.shape:
    raise

weighted = copy.copy(tcube)
weighted._data = wcube._data * tcube._data
mask = (wcube.mask & tcube.mask &
        BooleanArrayMask(weighted.filled_data[...] != 0, weighted.wcs) &
        BooleanArrayMask(wcube.filled_data[...] != 0, wcube.wcs) &
        BooleanArrayMask(mcube._data==1, mcube.wcs)
Пример #53
0
"""
Copy data from the appropriate directories to the CDS directory for upload to A&A
"""
import os
from paths import h2copath, figurepath, hpath, rpath, fpath, mpath, molpath, tpath
import tarfile

integrated_files = [
    hpath(x) for x in (
        'APEX_H2CO_303_202_masked_moment0.fits',
        'APEX_H2CO_303_202_masked_smooth_moment0.fits',
        'APEX_H2CO_321_220_masked_moment0.fits',
        'APEX_H2CO_321_220_masked_smooth_moment0.fits',
        "H2CO_321220_to_303202_bl_integ.fits",
        "H2CO_321220_to_303202_bl_integ_weighted.fits",
        "H2CO_321220_to_303202_bl_integ_masked_weighted.fits",
        "H2CO_321220_to_303202_bl_integ_temperature_dens1e4.fits",
        "H2CO_321220_to_303202_bl_integ_temperature_dens1e4_abund1e-10.fits",
        "H2CO_321220_to_303202_bl_integ_temperature_dens1e4_abund1e-8.fits",
        "H2CO_321220_to_303202_bl_integ_temperature_dens1e5.fits",
        "H2CO_321220_to_303202_bl_integ_temperature_dens3e4.fits",
        "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4.fits",
        "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4_abund1e-10.fits",
        "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e4_abund1e-8.fits",
        "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens1e5.fits",
        "H2CO_321220_to_303202_bl_integ_weighted_temperature_dens3e4.fits",
        "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens1e4.fits",
        "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens1e4_abund1e-10.fits",
        "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens1e4_abund1e-8.fits",
        "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens1e5.fits",
        "pv_H2CO_321220_to_303202_bl_integ_masked_weighted_temperature_dens3e4.fits",
Пример #54
0
def mask_out_region(mask_array, cube, regions=hc3n_regions):

    for region in regions:
        z = [cube.closest_spectral_channel(v*u.km/u.s)
             for v in region['v']]
        view = [slice(*z),
                slice(*region['y']),
                slice(*region['x'])
               ]

        mask_array[view] = False

    return mask_array

cube303 = SpectralCube.read(hpath('APEX_H2CO_303_202_bl.fits')).with_spectral_unit(u.km/u.s, velocity_convention='radio')
cube321 = SpectralCube.read(hpath('APEX_H2CO_321_220_bl.fits')).with_spectral_unit(u.km/u.s, velocity_convention='radio')
maskarr = mask_out_region(fits.getdata(hpath('APEX_H2CO_303_202_bl_mask.fits')).astype('bool'), cube303)
mask = (maskarr &
        cube303.mask.include(cube303, cube303.wcs) &
        cube321.mask.include(cube321, cube321.wcs))
bmask = BooleanArrayMask(mask, cube303.wcs)
cube303m = cube303.with_mask(bmask)
cube321m = cube321.with_mask(bmask)

cube303sm = SpectralCube.read(hpath('APEX_H2CO_303_202_smooth_bl.fits')).with_spectral_unit(u.km/u.s, velocity_convention='radio')
cube321sm = SpectralCube.read(hpath('APEX_H2CO_321_220_smooth_bl.fits')).with_spectral_unit(u.km/u.s, velocity_convention='radio')
smmaskarr = mask_out_region(fits.getdata(hpath('APEX_H2CO_303_202_smooth_bl_mask.fits')).astype('bool'), cube303sm)
masksm = (smmaskarr &
          cube303sm.mask.include(cube303sm, cube303sm.wcs) &
          cube321sm.mask.include(cube321sm, cube321sm.wcs))