Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
spectrum : `~specutils.Spectrum1D`
Output `~specutils.Spectrum1D` which is copy of the one passed in with the updated flux.
The uncertainty will not be copied as it is not necessarily the same.
"""
# If the input model has units then we will call it normally.
if getattr(model_input, model_input.param_names[0]).unit is not None:
flux = model_input(spectrum.spectral_axis)
# If the input model does not have units, then assume it is in
# the same units as the input spectrum.
else:
flux = model_input(spectrum.spectral_axis.value)*spectrum.flux.unit
return Spectrum1D(flux=flux,
spectral_axis=spectrum.spectral_axis,
wcs=spectrum.wcs,
velocity_convention=spectrum.velocity_convention,
rest_value=spectrum.rest_value)
def data_loader(label, identifier=None, dtype=Spectrum1D, extensions=None,
priority=0):
"""
Wraps a function that can be added to an `~astropy.io.registry` for custom
file reading.
Parameters
----------
label : str
The label given to the function inside the registry.
identifier : func
The identified function used to verify that a file is to use a
particular file.
dtype : class
A class reference for which the data loader should be store.
extensions : list
A list of file extensions this loader supports loading from. In the
# now figure out the frequency axis....
sp_axis = 3
naxis3 = header['NAXIS%d' % sp_axis]
cunit3 = wcs.wcs.cunit[sp_axis-1]
crval3 = wcs.wcs.crval[sp_axis-1]
cdelt3 = wcs.wcs.cdelt[sp_axis-1]
crpix3 = wcs.wcs.crpix[sp_axis-1]
freqs = np.arange(naxis3) + 1
freqs = (freqs - crpix3) * cdelt3 + crval3
freqs = freqs * cunit3
# should wcs be transformed to a 1D case ?
return Spectrum1D(flux=data, wcs=wcs, meta=meta, spectral_axis=freqs)
# return Spectrum1D(flux=data, wcs=wcs, meta=meta) # this does not work yet
else:
raise RuntimeError(f"Keyword SRCTYPE is {srctype}. It should "
"be 'POINT' or 'EXTENDED'. Can't decide between `flux` and "
"`surf_bright` columns.")
if np.min(uncertainty.array) <= 0.:
warnings.warn("Standard Deviation has values of 0 or less",
AstropyUserWarning)
# Merge primary and slit headers and dump into meta
slit_header = hdu.header
header = primary_header.copy()
header.extend(slit_header, strip=True, update=True)
meta = {k: v for k,v in header.items()}
spec = Spectrum1D(flux=flux, spectral_axis=wavelength,
uncertainty=uncertainty, meta=meta)
spectra.append(spec)
return SpectrumList(spectra)
def from_tree(cls, tree, ctx):
"""
Converts tree representation back into Spectrum1D object
"""
flux = tagged_tree_to_custom_tree(tree['flux'], ctx)
spectral_axis = tagged_tree_to_custom_tree(tree['spectral_axis'], ctx)
uncertainty = tree.get('uncertainty', None)
if uncertainty is not None:
klass = UNCERTAINTY_TYPE_MAPPING[uncertainty['uncertainty_type']]
data = tagged_tree_to_custom_tree(uncertainty['data'], ctx)
uncertainty = klass(data)
return Spectrum1D(flux=flux, spectral_axis=spectral_axis,
uncertainty=uncertainty)
@data_loader("JWST x1d", identifier=identify_jwst_x1d_fits, dtype=Spectrum1D,
extensions=['fits'])
def jwst_x1d_single_loader(filename, **kwargs):
"""
Loader for JWST x1d 1-D spectral data in FITS format
Parameters
----------
filename: str
The path to the FITS file
Returns
-------
Spectrum1D
The spectrum contained in the file.
"""
spectrum_list = _jwst_x1d_loader(filename, **kwargs)
# Convert
ivar_values = uncertainty.array
# Propagate
prop_ivar_values = convolution.convolve(ivar_values, kernel)
# Put back in
uncertainty.array = prop_ivar_values
else:
uncertainty = None
warnings.warn("Uncertainty is {} but convolutional error propagation is not defined for that type. Uncertainty will be dropped in the convolved spectrum.".format(type(uncertainty)),
AstropyUserWarning)
# Return a new object with the smoothed flux.
return Spectrum1D(flux=u.Quantity(smoothed_flux, spectrum.unit),
spectral_axis=u.Quantity(spectrum.spectral_axis,
spectrum.spectral_axis.unit),
wcs=spectrum.wcs,
uncertainty=uncertainty,
velocity_convention=spectrum.velocity_convention,
rest_value=spectrum.rest_value)
new_flux = np.delete(spectrum.flux, excise_indices)
new_spectral_axis = np.delete(spectrum.spectral_axis, excise_indices)
if spectrum.mask is not None:
new_mask = np.delete(spectrum.mask, excise_indices)
else:
new_mask = None
if spectrum.uncertainty is not None:
new_uncertainty = np.delete(spectrum.uncertainty, excise_indices)
else:
new_uncertainty = None
# Return a new object with the regions excised.
return Spectrum1D(flux=new_flux,
spectral_axis=new_spectral_axis,
uncertainty=new_uncertainty,
mask=new_mask,
wcs=spectrum.wcs,
velocity_convention=spectrum.velocity_convention,
rest_value=spectrum.rest_value if not isinstance(new_spectral_axis, SpectralCoord) else None,
radial_velocity=spectrum.radial_velocity if not isinstance(new_spectral_axis, SpectralCoord) else None)