import numpy as np
import os
import shutil
import paths
import warnings
from astropy import wcs

url = 'http://grunt.bu.edu/grs-stitch/source/grs-{0}-cube.fits'
files = {lon:download_file(url.format(lon), cache=True)
         if not os.path.exists(os.path.split(url.format(lon))[1])
         else os.path.abspath(os.path.split(url.format(lon))[1])
         for lon in (48,49,50)}
for lon,fn in files.items():
    outfn = os.path.split(url.format(lon))[1]
    try:
        os.link(fn, paths.dpath2(outfn))
    except OSError as ex:
        if ex.errno == 17 and ex.strerror == 'File exists':
            # All is good.  Continue.
            pass
        else:
            warnings.warn("Data is downloaded onto a different filesystem;"
                          " using symlinks instead of hardlinks")
            os.symlink(fn, paths.dpath2(outfn))


f1 = fits.open('grs-48-cube.fits')
f2 = fits.open('grs-49-cube.fits')
f3 = fits.open('grs-50-cube.fits')

lonmin = wcs.WCS(f1[0].header).sub([wcs.WCSSUB_CELESTIAL]).wcs_pix2world((f1[0].header['NAXIS1']),[0],0)[0][0]
import spectral_cube
import pyspeckit
import paths
from common_constants import distance, center
import numpy as np

sc = spectral_cube.SpectralCube.read(paths.dpath2('grs_48and50_cube_supersampledh2cogrid.fits'))
cube13 = pyspeckit.Cube(paths.dpath2('grs_48and50_cube_supersampledh2cogrid.fits'))

pars = []
spectra = []

import pylab as pl
for ii in range(1,25):
    pl.close(ii)

aprs = np.arange(50, 600, 30)

for apradius in aprs:
#apradius = 100
#if True:
    sp = cube13.get_apspec([center.l.deg, center.b.deg, apradius], coordsys='galactic')
    sp.plotter(xmin=30,xmax=90)
    guesses = [0.5,50,5,
               1,60,5,
               0.5,70,5]
    limits = [(0.1,3.5),(40,80),(1,15)]*3
    limits[1] = (45,55)
    limits[4] = (55,65)
    limits[7] = (65,75)
    limits[5] = (2,15)
from build_mask import cubemask,includemask
import numpy as np
import paths
from astropy import units as u
from astropy import constants
from astropy import coordinates
from astropy.table import Table,Column
from astropy.io import fits
from astropy import wcs
from astropy import log
import copy
import os
import warnings
from allpaths import h2co11subfn, h2co22subfn

grsfilepath = paths.dpath2('grs_48and50_cube.fits')
grsSSfilepath = paths.dpath2('grs_48and50_cube_supersampledh2cogrid.fits')
if not (os.path.exists(grsfilepath) and os.path.exists(grsSSfilepath)):
    warnings.warn("{0} not found.  Running reduction/stitch_grs_cubes.py")
    execfile(os.path.join(paths.source_root,'reduction/stitch_grs_cubes.py'))

cube13 = spectral_cube.SpectralCube.read(grsfilepath)

cube13_slab1 = cube13.spectral_slab(vrange1[0],vrange1[1])
cube13_slab2 = cube13.spectral_slab(vrange2[0],vrange2[1])
cube13_slab3 = cube13.spectral_slab(vrange1[0],vrange2[1])

snmask1 = cube13_slab1 > 0.5
snmask2 =  cube13_slab2 > 0.5
snmask3 =  cube13_slab3 > 0.5
from astropy.utils.data import download_file
import paths
import FITS_tools
from astropy.io import fits

url = 'http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/data/pub/VGPS/MOS_049_contincluded.Tb'

dl = download_file(url, cache=True)

outfn = os.path.split(url)[1]

try:
    os.link(dl, paths.dpath2(outfn))
except OSError as ex:
    if ex.errno == 17 and ex.strerror == 'File exists':
        # All is good.  Continue.
        pass
    else:
        warnings.warn("Data is downloaded onto a different filesystem;"
                      " using symlinks instead of hardlinks")
        os.symlink(dl, paths.dpath2(outfn))

outhead = fits.getheader(paths.h2co11subfn)
hicubefn = paths.dpath('MOS_049_Tb_reprojsupersampled.fits')

FITS_tools.cube_regrid.regrid_fits_cube(outfn, outheader=outheader,
                                        outfilename=hicubefn, order=1)
Ejemplo n.º 5
0
"""

from common_constants import distance
from astropy import units as u
from astropy.io import fits
from astropy import wcs
import numpy as np
import paths


def integ_to_lum(fn):

    hdr = fits.getheader(fn)
    img = fits.getdata(fn) * u.Unit(hdr["BUNIT"])
    w = wcs.WCS(hdr)
    pixscale = np.abs(w.wcs.get_cdelt()[0]) * u.deg
    pixarea_cm = ((pixscale * distance) ** 2).to(u.cm ** 2, u.dimensionless_angles())

    lum = pixarea_cm * img

    return lum.to(u.L_sun) * 4 * np.pi


for pfn in ("HIGAL_W51_mosaic_fit_070to500_integral.fits", "HIGAL_W51_mosaic_fit_160to500_integral.fits"):
    fn = paths.dpath2(pfn)
    lum = integ_to_lum(fn)
    ff = fits.open(fn)
    ff[0].data = lum.value
    ff[0].header["BUNIT"] = "L_sun"
    ff.writeto(fn.replace("_integral", "_luminosity"), clobber=True)
import numpy as np
from astropy.io import fits
import pylab as pl
import paths
import matplotlib
import os
matplotlib.rc_file(paths.rcfilepath)

d = fits.getdata(paths.dpath2('HIGAL_W51_mosaic_fit_070to500_N.fits'))
f = fits.open(paths.dpath2('HIGAL_W51_mosaic_fit_070to500_N.fits'))
pl.clf()
X = pl.hist(np.log10(d[d==d]), bins=50, histtype='stepfilled', linewidth=3,
            edgecolor='k', facecolor=(0,0,0,0.5), log=True)
ax = pl.gca()
ax.set_ylim(1,ax.get_ylim()[1])
pl.draw()
pl.show()
pl.xlabel("Column Density N(H$_2$)")
pl.ylabel("Number of pixels")
ax.set_xlim(20.95, 23.22)
pl.savefig(paths.fpath("HiGal_ColumnDensityHistogram.pdf"))

import pyregion
regs = pyregion.open(paths.rpath('main_and_b_squares.reg'))

reg1 = pyregion.ShapeList([regs[0]])
reg2 = pyregion.ShapeList([regs[1]])
mask1 = reg1.get_mask(f[0])
mask2 = reg2.get_mask(f[0])

X = pl.hist(np.log10(d[mask1]), bins=50, histtype='stepfilled', linewidth=3,
import numpy as np
from astropy import coordinates
from astropy import wcs
from astropy.io import fits
from image_tools import radialprofile
from astropy import units as u
from astropy import constants
import pylab as pl
from common_constants import distance, center
import paths
import FITS_tools

pl.mpl.rc_file(paths.rcfilepath)

h2co_densfile = paths.dpath("H2CO_ParameterFits_weighted_mean_mean_density_stdmasked.fits")
column_image = FITS_tools.hcongrid.hcongrid_hdu(fits.open(paths.dpath2('HIGAL_W51_mosaic_fit_160to500_N.fits'))[0],
                                                fits.getheader(h2co_densfile))
luminosity_image160 = FITS_tools.hcongrid.hcongrid_hdu(fits.open(paths.dpath2('HIGAL_W51_mosaic_fit_160to500_luminosity.fits'))[0],
                                                       fits.getheader(h2co_densfile))
luminosity_image70 = FITS_tools.hcongrid.hcongrid_hdu(fits.open(paths.dpath2('HIGAL_W51_mosaic_fit_070to500_luminosity.fits'))[0],
                                                      fits.getheader(h2co_densfile))

w = wcs.WCS(column_image.header)
dens_image = fits.open(h2co_densfile)[0]
w2 = wcs.WCS(dens_image.header)

pixsize = np.abs(w.wcs.get_cdelt()[0]) * u.deg
h2co_pixsize = np.abs(w2.wcs.get_cdelt()[0]) * u.deg

# Formaldehyde: mask out NaNs
x,y = w2.wcs_world2pix([center.l.deg], [center.b.deg], 0)