from astropy.wcs import WCS from analysis.paths import fourteenB_HI_data_path, iram_co21_data_path from analysis.constants import moment0_name ''' Reproject the CO to match the HI. Not taking the beam difference into account here since they are quite close. Once this is merged https://github.com/astrofrog/reproject/pull/115/files, make a full regrid of the whole cube. ''' hi_header = fits.getheader(fourteenB_HI_data_path(moment0_name)) co_ico = fits.open(iram_co21_data_path("m33.ico.fits"))[0] proj = Projection(co_ico.data.squeeze(), wcs=WCS(co_ico.header).dropaxis(3).dropaxis(2)) rep_array, footprint = reproject_interp(proj.hdu, hi_header) new_header = hi_header.copy() # Need to change some of the properties before saving keys = ["BMAJ", "BMIN", "BPA", "BUNIT"] for key in keys: new_header[key] = co_ico.header[key]
from spectral_cube.lower_dimensional_structures import Projection import os import pyregion import astropy.units as u import numpy as np from astropy.io import fits from scipy.signal import medfilt from multiprocessing import Pool from itertools import izip from lmfit import minimize, Parameters from analysis.paths import fourteenB_HI_data_path from analysis.constants import cube_name, mask_name, pb_lim from analysis.galaxy_params import gal cube = SpectralCube.read(fourteenB_HI_data_path(cube_name)) mask = fits.open(fourteenB_HI_data_path(mask_name))[0] # Apply the source mask cube = cube.with_mask((mask.data > 0)) # Now cut to the elliptical region to remove all bkg regions # region = pyregion.open(c_hi_analysispath("rotation_curves/mom1_rotcurve_mask.reg")) # subcube = cube.subcube_from_ds9region(region) # Since the parameters of M33 are already well constrained, use a radius # cut-off based on previous values. The fitting is done out to 10 kpc. A # small bit is added here to account for small changes in the fit parameters. radius = gal.radius(header=cube.header) max_radius = 10.25 * u.kpc subcube = cube.with_mask(radius < max_radius).minimal_subcube()
from analysis.paths import (fourteenB_HI_data_path, paper1_figures_path, c_hi_analysispath) from analysis.constants import pb_lim make_plot = True make_rotmodel = True gal = Galaxy("M33") try: folder_name = sys.argv[1] except IndexError: diskfit_runs = [dir for dir in os.listdir(fourteenB_HI_data_path("")) if dir.startswith("diskfit")] print("Available diskfit runs: " + str(diskfit_runs)) folder_name = raw_input("Give folder name of the diskfit run: ") # My convention has been diskfit_params_output. Only keep the params parts # for the names of the output plots. params = os.path.basename(folder_name.rstrip("/")) # Load in moment 1 # mom1_name = fourteenB_HI_data_path("M33_14B-088_HI.clean.image.pbcov_gt_{}.ellip_mask.mom1.fits".format(pb_lim)) mom1_name = fourteenB_HI_data_path("M33_14B-088_HI.clean.image.pbcov_gt_{}.ellip_mask.mom1.fits".format(pb_lim)) mom1 = fits.open(mom1_name) mom1_wcs = wcs.WCS(mom1[0].header)
edges of the adap. thresh mask. To avoid a whole cube regrid, I use the rotation subtracted cubes and extract zeroth moments from spectral slabs. ''' np.random.seed(34678953) # Plot a bunch verbose = False # slicer = (slice(825, 1033), slice(360, 692)) slicer = (slice(None), slice(None)) # Load in the rotation subtracted cubes hi_cube = SpectralCube.read(fourteenB_HI_data_path(rotsub_cube_name)) co_cube = SpectralCube.read(iram_co21_data_path("m33.co21_iram.rotsub.fits")) start_vel = - 30 * u.km / u.s end_vel = 30 * u.km / u.s del_vel = 3. * u.km / u.s vels = np.arange(start_vel.value, end_vel.value + del_vel.value, del_vel.value) * u.km / u.s # Get the radius array so we can cut to where the CO data is valid radii = gal.radius(header=hi_cube[0].header) max_radius = 6.0 * u.kpc all_dists = [] all_radii = []
from astropy.visualization import AsinhStretch from astropy.visualization.mpl_normalize import ImageNormalize import warnings import matplotlib.animation as anim from analysis.paths import fourteenB_HI_data_path, paper1_figures_path from analysis.constants import hi_freq, rotsub_cube_name ''' Channel plots of the rotation subtracted HI cube combined into a movie! Borrowing code from @keflavich: https://github.com/keflavich/paper_w51_evla/blob/master/plot_codes/h77a_layers.py ''' cube_file = fourteenB_HI_data_path(rotsub_cube_name) cube = SpectralCube.read(cube_file) # Begin channel map code here # integrate over velocities to make channel maps of a set width vstart = 308 # channels vend = 788 vstep = 10 all_slabs = np.arange(vstart, vend + vstep, vstep, dtype=int) # Define the average beam beam = average_beams(cube.beams) layers = \
execfile(c_hi_analysispath("HI_moment_figures.py")) ################# # Rotation curves ################# # Find the rotation curve # Create a centroid map w/ a radial cutoff execfile(c_hi_analysispath("rotation_curves/make_moment1_for_rotcurve.py")) # Now run diskfit. # First using the centroids diskfit_script = c_hi_analysispath("rotation_curves/run_diskfit.py") diskfit_params = c_hi_analysispath("rotation_curves/diskfit_params/diskfit_params_nowarp_noradial_noasymm.inp") output_path = fourteenB_HI_data_path("", no_check=True) call("python {0} {1} {2} {3}".format(diskfit_script, diskfit_params, output_path, fourteenB_HI_data_path(moment1_name)), shell=True) # And then using peak velocity diskfit_params = c_hi_analysispath("rotation_curves/diskfit_params/diskfit_params_peakvels_nowarp_noradial_noasymm.inp") output_path = fourteenB_HI_data_path("", no_check=True) call("python {0} {1} {2} {3}".format(diskfit_script, diskfit_params, output_path, fourteenB_HI_data_path(moment1_name)), shell=True)
from turbustat.statistics.stats_utils import fourier_shift from analysis.paths import fourteenB_HI_data_path from analysis.constants import (rotsub_cube_name, cube_name, mask_name, rotsub_mask_name) ''' Subtract a rotation model from a cube. ''' # Load in my huge FITS creator execfile(os.path.expanduser("~/Dropbox/code_development/ewky_scripts/write_huge_fits.py")) cube = SpectralCube.read(fourteenB_HI_data_path(cube_name)) mask = fits.open(fourteenB_HI_data_path(mask_name))[0] model = fits.open(fourteenB_HI_data_path("diskfit_noasymm_noradial_nowarp_output/rad.fitmod.fits")) # Load in the fit results for the galaxy parameters to get vsys table_name = fourteenB_HI_data_path("diskfit_noasymm_noradial_nowarp_output/rad.out.params.csv") tab = Table.read(table_name) vsys = (float(tab["Vsys"]) * u.km / u.s).to(u.m / u.s) # Now calculate the spectral shifts needed for each pixel # Assuming that the array shapes for the same (which they are here) shifts = np.zeros(model[0].data.shape) posns = list(np.where(np.isfinite(model[0].data)))
from spectral_cube.cube_utils import average_beams import numpy as np import matplotlib.pyplot as p from astropy.io import fits from astropy.wcs import WCS import astropy.units as u from analysis.paths import fourteenB_HI_data_path, paper1_figures_path from constants import (hi_freq, cube_name, moment0_name, lwidth_name, skew_name, kurt_name, mask_name) ''' Investigating skewness and kurtosis in the 14B-088 cube. ''' cube = SpectralCube.read(fourteenB_HI_data_path(cube_name)) mask = fits.open(fourteenB_HI_data_path(mask_name))[0].data > 0 cube = cube.with_mask(mask) mom0_hdu = fits.open(fourteenB_HI_data_path(moment0_name))[0] mom0 = Projection(mom0_hdu.data, wcs=WCS(mom0_hdu.header), unit=u.Jy * u.m / u.s) lwidth_hdu = fits.open(fourteenB_HI_data_path(lwidth_name))[0] lwidth = Projection(lwidth_hdu.data, wcs=WCS(lwidth_hdu.header), unit=u.m / u.s) skew_hdu = fits.open(fourteenB_HI_data_path(skew_name))[0] skew = Projection(skew_hdu.data, wcs=WCS(skew_hdu.header), unit=u.Unit("")) kurt_hdu = fits.open(fourteenB_HI_data_path(kurt_name))[0]
from analysis.paths import (fourteenB_HI_data_path, iram_co21_data_path, paper1_figures_path, paper1_tables_path) from analysis.constants import (rotsub_cube_name, rotsub_mask_name, co21_mass_conversion, hi_freq) from analysis.galaxy_params import gal ''' Create profiles of HI and CO after rotation subtraction. ''' co_cube = SpectralCube.read(iram_co21_data_path("m33.co21_iram.rotsub.fits")) # mask = fits.getdata(iram_co21_data_path("m33.co21_new_assign_clfind.fits")) # co_cube = co_cube.with_mask(mask.astype(bool)) hi_cube = SpectralCube.read(fourteenB_HI_data_path(rotsub_cube_name)) hi_mask = fits.open(fourteenB_HI_data_path(rotsub_mask_name))[0] hi_cube = hi_cube.with_mask(hi_mask.data > 0) hi_radius = gal.radius(header=hi_cube.header) co_radius = gal.radius(header=co_cube.header) # Perform the same analysis split up into radial bins dr = 500 * u.pc max_radius = (6.0 * u.kpc).to(u.pc) nbins = np.int(np.floor(max_radius / dr)) inneredge = np.linspace(0, max_radius - dr, nbins) outeredge = np.linspace(dr, max_radius, nbins)
if fail: warn("Fail flag was raised. Check the output") # raise ValueError("Fit failed.") return fit, err, model, profile if __name__ == "__main__": from analysis.paths import (fourteenB_HI_data_path, paper1_figures_path, iram_co21_data_path) from analysis.constants import moment0_name from analysis.galaxy_params import gal mom0_fits = fits.open(fourteenB_HI_data_path(moment0_name))[0] beam = Beam.from_fits_header(mom0_fits.header) mom0 = Projection(mom0_fits.data * beam.jtok(hi_freq) / 1000. * u.km / u.s, wcs=WCS(mom0_fits.header)) mom0.meta['beam'] = beam # Create the bubble mask instead of letting FilFinder to do it. bub = BubbleFinder2D(mom0, sigma=80. * beam.jtok(hi_freq) / 1000.) # fils = fil_finder_2D(mom0.value, mom0.header, 10, distance=0.84e6) # fils.mask = ~(bub.mask.copy()) # fils.medskel() # fils.analyze_skeletons() # # So at least on of the radial profiles fails. BUT the second fit is to a # # skeleton that is essentially the entire disk, so plot without interactivity
''' Cut out noisy regions by imposing a mask of the primary beam coverage. ''' from astropy.io import fits from spectral_cube import SpectralCube from spectral_cube.cube_utils import beams_to_bintable from astropy.utils.console import ProgressBar import os from analysis.paths import fourteenB_HI_data_path from analysis.constants import pb_lim, cube_name # execfile(os.path.expanduser("~/Dropbox/code_development/ewky_scripts/write_huge_fits.py")) pbcov = fits.open(fourteenB_HI_data_path("M33_14B-088_pbcov.fits"))[0] cube = SpectralCube.read(fourteenB_HI_data_path("M33_14B-088_HI.clean.image.fits")) # Apply the mask, using a cut-off of 0.3. This retains all of the regions with # emission. masked_cube = cube.with_mask(pbcov.data > pb_lim) masked_cube = masked_cube.minimal_subcube() new_fitsname = fourteenB_HI_data_path(cube_name, no_check=True) masked_cube.write(new_fitsname, overwrite=True) # create_huge_fits(new_fitsname, cube.header)
from astropy.io import fits from analysis.paths import fourteenB_HI_data_path from analysis.constants import (cube_name, mask_name, moment0_name, moment1_name, lwidth_name, skew_name, kurt_name) ''' Make the first three moments with the pbcov masked cube. The higher moments (skewness & kurtosis) need a more aggressive mask, made by make_signal_mask.py, and computed in higher_moments.py. ''' cube = SpectralCube.read(fourteenB_HI_data_path(cube_name)) # Load in source mask source_mask = fits.getdata(fourteenB_HI_data_path(mask_name)) source_mask = source_mask.astype(np.bool) cube = cube.with_mask(source_mask) # Now create the moment 1 and save it. Make a linewidth one too. moment0 = cube.moment0() moment0.write(fourteenB_HI_data_path(moment0_name, no_check=True), overwrite=True) moment1 = cube.moment1().astype(np.float32) moment1.header["BITPIX"] = -32
def round_up_to_odd(f): return np.ceil(f) // 2 * 2 + 1 def sigma_rob(data, iterations=1, thresh=3.0, axis=None): """ Iterative m.a.d. based sigma with positive outlier rejection. """ noise = mad_std(data, axis=axis) for _ in range(iterations): ind = (np.abs(data) <= thresh * noise).nonzero() noise = mad_std(data[ind], axis=axis) return noise cube = SpectralCube.read(fourteenB_HI_data_path(cube_name)) # noise = Noise(cube) # scale = noise.scale # scale = sig_clip(cube[-1].value, nsig=10) pixscale = proj_plane_pixel_scales(cube.wcs)[0] # cube = cube.with_mask(cube > 3 * scale * u.Jy) # # Want to smooth the mask edges mask = cube.mask.include() # Perform smooth and mask on spectra