本文整理汇总了Python中spectral_cube.SpectralCube.with_spectral_unit方法的典型用法代码示例。如果您正苦于以下问题:Python SpectralCube.with_spectral_unit方法的具体用法?Python SpectralCube.with_spectral_unit怎么用?Python SpectralCube.with_spectral_unit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spectral_cube.SpectralCube
的用法示例。
在下文中一共展示了SpectralCube.with_spectral_unit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rebaseline
# 需要导入模块: from spectral_cube import SpectralCube [as 别名]
# 或者: from spectral_cube.SpectralCube import with_spectral_unit [as 别名]
def rebaseline(filename, blorder=3,
baselineRegion=[slice(0, 262, 1), slice(-512, 0, 1)],
windowFunction=None, blankBaseline=False,
flagSpike=True, v0=None, **kwargs):
"""
Rebaseline a data cube using robust regression of Legendre polynomials.
Parameters
----------
filename : string
FITS filename of the data cube
blorder : int
Order of the polynomial to fit to the data
baselineRegion : list
List of slices defining the default region of the spectrum, in
channels, to be used for the baseline fitting.
windowFunction : function
Name of function to be used that will accept spectrum data, and
velocity axis and will return a binary mask of the channels to be used
in the baseline fitting. Extra **kwargs are passed to windowFunction
to do with as it must.
blankBaseline : boolean
Blank the baseline region on a per-spectrum basis
Returns
-------
Nothing. A new FITS file is written out with the suffix 'rebaseN' where N
is the baseline order
"""
cube = SpectralCube.read(filename)
originalUnit = cube.spectral_axis.unit
cube = cube.with_spectral_unit(u.km / u.s, velocity_convention='radio')
spaxis = cube.spectral_axis.to(u.km / u.s).value
goodposition = np.isfinite(cube.apply_numpy_function(np.max, axis=0))
y, x = np.where(goodposition)
outcube = np.zeros(cube.shape) * np.nan
RegionName = (filename.split('_'))[0]
if hasattr(windowFunction, '__call__'):
catalog = catalogs.GenerateRegions()
nuindex = np.arange(cube.shape[0])
runmin = nuindex[-1]
runmax = nuindex[0]
for thisy, thisx in console.ProgressBar(zip(y, x)):
spectrum = cube[:, thisy, thisx].value
if v0 is not None:
baselineIndex = windowFunction(spectrum, spaxis,
v0=v0, **kwargs)
elif hasattr(windowFunction, '__call__'):
_, Dec, RA = cube.world[0, thisy, thisx]
# This determines a v0 appropriate for the region
v0 = VlsrByCoord(RA.value, Dec.value, RegionName,
regionCatalog=catalog)
baselineIndex = windowFunction(spectrum, spaxis,
v0=v0, **kwargs)
else:
baselineIndex = np.zeros_like(spectrum,dtype=np.bool)
for ss in baselineRegion:
baselineIndex[ss] = True
runmin = np.min([nuindex[baselineIndex].min(), runmin])
runmax = np.max([nuindex[baselineIndex].max(), runmax])
# Use channel-to-channel difference as the noise value.
if flagSpike:
jumps = (spectrum - np.roll(spectrum, -1))
noise = mad1d(jumps) * 2**(-0.5)
baselineIndex *= (np.abs(jumps) < 5 * noise)
noise = mad1d((spectrum -
np.roll(spectrum, -2))[baselineIndex]) * 2**(-0.5)
else:
noise = mad1d((spectrum -
np.roll(spectrum, -2))[baselineIndex]) * 2**(-0.5)
if blankBaseline:
spectrum = robustBaseline(spectrum, baselineIndex,
blorder=blorder,
noiserms=noise)
spectrum[baselineIndex] = np.nan
outcube[:, thisy, thisx] = spectrum
else:
outcube[:, thisy, thisx] = robustBaseline(spectrum, baselineIndex,
blorder=blorder,
noiserms=noise)
outsc = SpectralCube(outcube, cube.wcs, header=cube.header)
outsc = outsc[runmin:runmax, :, :] # cut beyond baseline edges
# Return to original spectral unit
outsc = outsc.with_spectral_unit(originalUnit)
outsc.write(filename.replace('.fits', '_rebase{0}.fits'.format(blorder)),
overwrite=True)