def signif_cube(fluxcube, varcube,wave1,wave2,outfile=None): nbhdu, errhdu = kt.narrowband(fluxcube,wave1,wave2,varcube=varcube) signifdat = nbhdu.data/np.sqrt(errhdu.data) newhdu = nbhdu.copy() newhdu.data = signifdat if outfile is not None: fits.writeto(data=newhdu.data, header=newhdu.header, filename=outfile, overwrite=True) #newhdu.writeto(outfile,overwrite=True) return newhdu
def median_continuum_subtract(cubefile, contwave1, contwave2, linewave1, linewave2, outfile=None): """Find continuum by median flux value and subtract from line regiom """ from astropy.io import fits medhdu = kt.narrowband(cubefile, contwave1, contwave2, mode='median') linehdu = kt.slice_cube(cubefile, linewave1, linewave2) dat = linehdu.data - medhdu.data sumdat = np.sum(dat,axis=0) medhdu.data = sumdat if outfile is not None: try: fits.writeto(data=medhdu.data, header=medhdu.header, filename=outfile, overwrite=True) #medhdu.writeto(outfile, overwrite=True) except: import pdb; pdb.set_trace() return medhdu
def addWcsRtModel(inp, ref, outfile, spatial_scale=0.2, zgal=0.6942, center=('12 36 19.811', '+62 12 51.831'), PA=27): ### Load in the model file inpdat = fits.open(inp) rtcube = DataCube(inp, include_wcs=False) rtcube.wavelength = inpdat[2].data rtcube.data = inpdat[1].data wavearr = rtcube.wavelength ### Load in the reference file if necessary if not isinstance(ref, DataCube): refcube = DataCube(ref) else: refcube = inp refdat = refcube.data refwave = refcube.wavelength nbhdu = transform.narrowband(refcube, 4000., 5000.) refnb = nbhdu.data refwcs = WCS(nbhdu.header) ### Rearrange the cube newdat = np.transpose(rtcube.data, axes=[2, 1, 0]) ### Get the pixel scale in the spectral dimension diff = wavearr[1:] - wavearr[:-1] if not np.all(diff == diff[0]): raise ValueError('Wavelength array not uniformly sampled') spectral_scale = diff[0] ### Set scale and reference positions wave1 = wavearr[0] * (1. + zgal) spectral_scale *= (1. + zgal) ### Get the bright pixel from reference cube brightpix, brightcoords = utils.bright_pix_coords(refnb, refwcs) ### Create the new WCS newwcs = WCS(naxis=3) spatscaledeg = spatial_scale * cosmo.arcsec_per_kpc_proper(zgal) / 3600. print(spatscaledeg) if center is not None: cencoords = SkyCoord(center[0], center[1], unit=(u.hourangle, u.deg)) else: cencoords = brightcoords newwcs.wcs.crpix = np.array([150.5, 150.5, 1]) newwcs.wcs.crval = np.array([cencoords.ra.deg, cencoords.dec.deg, wave1]) newwcs.wcs.cdelt = np.array([8.0226e-6, 8.0226e-6, spectral_scale]) newwcs.wcs.cunit = ['deg', 'deg', 'Angstrom'] newwcs.wcs.ctype = ('RA---TAN', 'DEC--TAN', 'VAC') rotangle = np.radians(PA) # Assuming PA given in degrees psmat = np.array([[ newwcs.wcs.cdelt[0] * np.cos(rotangle), -newwcs.wcs.cdelt[1] * np.sin(rotangle), 0. ], [ newwcs.wcs.cdelt[0] * np.sin(rotangle), newwcs.wcs.cdelt[1] * np.cos(rotangle), 0. ], [0., 0., 1.]]) newwcs.wcs.cd = psmat newhdu = newwcs.to_fits() newhdu[0].header['PC3_3'] = spectral_scale newhdu[0].data = newdat newhdu.writeto(outfile, overwrite=True)
def addWcsRtModel(inp, ref, outfile, spatial_scale_xy=[0.679, 0.290], zgal=0.6942, center=('12 36 19.811', '+62 12 51.831'), PA=27): ### Load in the model file inpdat = fits.open(inp) rtcube = DataCube(inp, include_wcs=False) rtcube.wavelength = inpdat[2].data rtcube.data = inpdat[1].data wavearr = rtcube.wavelength ### Load in the reference file if necessary if not isinstance(ref, DataCube): refcube = DataCube(ref) else: refcube = inp refdat = refcube.data refwave = refcube.wavelength nbhdu = transform.narrowband(refcube, 4000., 5000.) refnb = nbhdu.data refwcs = WCS(nbhdu.header) ### Rearrange the cube and find brightest pixel newdat = np.transpose(rtcube.data, axes=[2, 1, 0]) sumdat = np.sum(newdat, axis=0) maxdat = np.max(sumdat) brightmodpix = np.where(sumdat == maxdat) print(brightmodpix) ### Get the pixel scale in the spectral dimension diff = wavearr[1:] - wavearr[:-1] if not np.all(np.abs(diff - diff[0]) < 1e-3): import pdb pdb.set_trace() raise ValueError('Wavelength array not uniformly sampled') spectral_scale = diff[0] ### Set scale and reference position in model wave1 = wavearr[0] * (1. + zgal) spectral_scale *= (1. + zgal) ### Get the bright pixel from reference cube brightpix, brightcoords = utils.bright_pix_coords(refnb, refwcs) ### Create the new WCS newwcs = WCS(naxis=3) if center is not None: cencoords = SkyCoord(center[0], center[1], unit=(u.hourangle, u.deg)) else: cencoords = brightcoords # coordinates are (y, x, lam) and '1' indexed in FITS files newwcs.wcs.crpix = np.array( [brightmodpix[1][0] + 1, brightmodpix[0][0] + 1, 1]) newwcs.wcs.crval = np.array([cencoords.ra.deg, cencoords.dec.deg, wave1]) newwcs.wcs.cdelt = np.array([ spatial_scale_xy[0] / 3600., spatial_scale_xy[1] / 3600., spectral_scale ]) newwcs.wcs.cunit = ['deg', 'deg', 'Angstrom'] newwcs.wcs.ctype = ('RA---TAN', 'DEC--TAN', 'VAC') rotangle = np.radians(PA) # Assuming PA given in degrees psmat = np.array([[ newwcs.wcs.cdelt[0] * np.cos(rotangle), -newwcs.wcs.cdelt[1] * np.sin(rotangle), 0. ], [ newwcs.wcs.cdelt[0] * np.sin(rotangle), newwcs.wcs.cdelt[1] * np.cos(rotangle), 0. ], [0., 0., 1.]]) newwcs.wcs.cd = psmat newhdu = newwcs.to_fits() newhdu[0].header['PC3_3'] = spectral_scale newhdu[0].data = newdat newhdu.writeto(outfile, overwrite=True)