def extract_pv_diagram(image, xy=(0., 0.), pa=0., length=100, width=1): ny, nx, nfreq, npol = image.image.shape # Create a data cube that is appropriately shaped. cube = numpy.empty((nfreq, ny, nx)) for i in range(nfreq): cube[i, :, :] = image.image[:, :, i, 0] # Create the Path object. x0, y0 = xy line = [(x0-length/2*numpy.sin(pa),y0-length/2*numpy.cos(pa)), \ (x0+length/2*numpy.sin(pa), y0+length/2*numpy.cos(pa))] path = pvextractor.Path(line, width=width) # Extract the PV diagram along the Path pv = pvextractor.extract_pv_slice(cube, path) # Convert back to my Image class. pvdiagram = numpy.empty((1, pv.data.shape[1], pv.data.shape[0], 1)) for i in range(pv.data.shape[0]): pvdiagram[0, :, i, 0] = pv.data[i, :] # Get the velocity. velocity = c * (image.header["RESTFRQ"] - image.freq) / \ image.header["RESTFRQ"] # Get the x coordinates. x0 = 0. dx = image.header["CDELT2"] * numpy.pi / 180 / arcsec nx0 = int(pv.data.shape[1] / 2) + 1 x = (numpy.arange(pv.data.shape[1]) - (nx0 - 1)) * dx + x0 return Image(pvdiagram, x=x, velocity=velocity, freq=image.freq)
medsub = cube - med medsub.write(medsubfn) # create full-scale PV diagram (all freqs) outfn = paths.dpath( os.path.join( 'pv', os.path.split( fn.replace( ".image.pbcor.fits", "_medsub_diskpv_{0}.fits".format(width)))[-1])) if not os.path.exists(outfn): # width = 0.05 arcsec encompasses the disk; however, most # of the actual line emission comes from just above/below... #extraction_path = pvextractor.Path(diskycoords, width=0.05*u.arcsec) extraction_path = pvextractor.Path(diskycoords, width=width * u.arcsec) log.info( "Beginning extraction of path with width {0} for {1}". format(extraction_path.width, outfn)) extracted = pvextractor.extract_pv_slice( medsub, extraction_path) log.info("Writing to {0}".format(outfn)) extracted.writeto(outfn, overwrite=True) for linename, linefreq in disk_lines.items(): print(linename, width, fnt, spw, time.time() - t0) pl.close('all') band = 'B' + bandre.search(fnt).groups()[0]
import matplotlib import pyregion import copy from paths import mpath, apath, fpath, molpath, hpath, rpath, h2copath from astropy import units as u from astropy import coordinates from astropy.io import ascii from astropy import log import paths import matplotlib matplotlib.rc_file(paths.pcpath('pubfiguresrc')) reg = pyregion.open(rpath('backstream.reg'))[0].coord_list glon, glat = np.array(reg[::2]), np.array(reg[1::2]) coords = coordinates.SkyCoord(glon * u.deg, glat * u.deg, frame='galactic') P = pvextractor.Path(coords, width=300 * u.arcsec) vlos = glon * 0 # TODO: replace this dl = (glon[1:] - glon[:-1]) db = (glat[1:] - glat[:-1]) dist = (dl**2 + db**2)**0.5 cdist = np.zeros(dist.size + 1) cdist[1:] = dist.cumsum() 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 ]
assert 'cutout' in fn basename = ".".join([ os.path.basename(namesplit[0]), namesplit[1], name + "_diskpv", "fits" ]) outfn = paths.dpath(os.path.join("12m/pv/", basename)) print("Extracting {0} {2}: {1}".format(fn, direction, name)) cube = SpectralCube.read(fn) cube.allow_huge_operations = True cube.beam_threshold = 5 med = cube.median(axis=0) medsub = cube - med extraction_path = pvextractor.Path(diskycoords, 0.2 * u.arcsec) extracted = pvextractor.extract_pv_slice(medsub, extraction_path) #extracted.writeto(outfn, clobber=True) ww = wcs.WCS(extracted.header) ww.wcs.cdelt[1] /= 1000.0 ww.wcs.crval[1] /= 1000.0 ww.wcs.cunit[1] = u.km / u.s ww.wcs.cdelt[0] *= 3600 ww.wcs.cunit[0] = u.arcsec fig = pl.figure(1) fig.clf() ax = fig.add_axes([0.15, 0.1, 0.8, 0.8], projection=ww) ax.imshow(extracted.data, cmap='viridis') ax.set_xlabel("Offset [\"]")
# constants used for centroid_planes and seifried_analysis source = 'sourceI' #diskycoord_list = pyregion.open(paths.rpath("{0}_disk_pvextract.reg" # .format(source)))[0].coord_list #diskycoords = coordinates.SkyCoord(["{0} {1}".format(diskycoord_list[jj], # diskycoord_list[jj+1]) # for jj in range(0, # len(diskycoord_list), # 2)], unit=(u.deg, # u.deg), # frame='fk5') diskycoord_list = regions.read_ds9(paths.rpath("{0}_disk_pvextract.reg" .format(source))) diskycoords = coordinates.SkyCoord([diskycoord_list[0].start, diskycoord_list[0].end]) #source = coordinates.SkyCoord(83.81048617*u.deg, -5.37516858*u.deg, frame='icrs') source = coordinates.SkyCoord(regions.read_ds9(paths.rpath('sourceI_center.reg'))[0].center) extraction_path = pvextractor.Path(diskycoords, width=0.01*u.arcsec) origin = offset_to_point(source.ra.deg, source.dec.deg, extraction_path)*u.deg # only approximate central_freqs = {'B6': 224*u.GHz, 'B7': 345*u.GHz, 'B3': 95*u.GHz}
limitedmin=[T, T, T, T, T], maxpars=[70, 5, 1500, 1e18, 10000], minpars=[40, 0.1, 50, 1e13, min_background], signal_cut=0, maskmap=absorption_mask, errmap=err.value, multicore=4) pcube_cont.write_fit('e2e_CH3CN_Absorption_fits.fits', clobber=True) from kinematic_analysis_pv_LB import diskycoords, outflowpath import pvextractor import pylab as pl pl.figure(5).clf() for width in (None, 0.05 * u.arcsec, 0.1 * u.arcsec, 0.15 * u.arcsec): diskypath = pvextractor.Path(diskycoords, width) extracted_disky = pvextractor.extract_pv_slice( pcube_cont.parcube[0:1, :, :], diskypath, wcs=pcube_cont.wcs) pl.plot(extracted_disky.data.squeeze(), label=str(width)) pl.xlabel("Offset (pixels)") pl.ylabel("Velocity (km/s)") pl.ylim(55, 60) pl.xlim(855, 890) pl.legend(loc='best') pl.savefig(paths.fpath("longbaseline/kinematics/velocity_vs_offset.png")) pl.figure(6).clf() diskypath = pvextractor.Path(diskycoords, width=None)
('e8', (45,75)), ('e2', (45,70)), ): diskycoords = diskycoorddict[name] for fn in glob.glob(paths.lbpath("W51{0}cax*ALL_medsub_cutout.fits".format(name))): print(fn) try: cube = SpectralCube.read(fn).with_spectral_unit(u.GHz) except TypeError as ex: print(ex) continue P = pvextractor.Path(diskycoords, 0.05*u.arcsec) for line, restfreq, velocity_res, spw in line_to_image_list: basename = line frq = float(restfreq.strip('GHz')) * u.GHz vcube = cube.with_spectral_unit(u.km/u.s, velocity_convention='radio', rest_value=frq) svcube = vcube.spectral_slab((vrange[0]-1)*u.km/u.s, (vrange[1]+1)*u.km/u.s) if svcube.shape[0] <= 1: continue print("Extracting {0}: {1}".format(line, restfreq))
e8flowxy = Path(endpoints, width=1.5 * u.arcsec) # e2 e2ereg = pyregion.open(paths.rpath('w51e2e.reg'))[0] w51e2e = coordinates.SkyCoord(e2ereg.coord_list[0] * u.deg, e2ereg.coord_list[1] * u.deg, frame='fk5') e2ediskpath = "19:23:44.197,+14:30:37.34,19:23:43.960,+14:30:34.55,19:23:43.882,+14:30:32.21,19:23:43.851,+14:30:31.26".split( ",") e2ediskcoords = coordinates.SkyCoord([ "{0} {1}".format(e2ediskpath[jj], e2ediskpath[jj + 1]) for jj in (0, 2, 4) ], unit=(u.hour, u.deg), frame='fk5') e2ediskpath = pvextractor.Path(e2ediskcoords, 0.2 * u.arcsec) e2eoutflow_coords = coordinates.SkyCoord( ["19:23:44.127 +14:30:32.30", "19:23:43.822 +14:30:36.64"], unit=(u.hour, u.deg), frame='fk5') e2eoutflowpath = pvextractor.Path(e2eoutflow_coords, 0.2 * u.arcsec) #lacyreg = pyregion.open(paths.rpath('lacyjet_segment_trace.reg')) lacypath = pvextractor.pvregions.paths_from_regfile( paths.rpath('lacyjet_segment_trace.reg'))[0] lacypath.width = 1.5 * u.arcsec lacyorigin = coordinates.SkyCoord(290.91564, 14.518122, frame='fk5', unit=(u.deg, u.deg))