def test_lsf_errors(): """ Test that LSF raises errors correctly """ with pytest.raises(RuntimeError): LSF() with pytest.raises(RuntimeError): LSF(filename='NOTAFILE.txt')
def test_gaussian_lsf(): """ Create an LSF object with a gaussian function and assure it works OK """ lsf = LSF(function='gaussian', width=5) assert isinstance(lsf.kernel, astropy.convolution.kernels.Gaussian1DKernel) assert len(lsf.kernel.array) == 41 assert lsf.width == 5 print(lsf)
def test_boxcar_lsf(): """ Create an LSF object with a boxcar function and assure it works OK """ lsf = LSF(function='boxcar', width=5) assert isinstance(lsf.kernel, np.ndarray) assert len(lsf.kernel) == 5 assert lsf.width == 5 print(lsf)
def test_lsf_from_filename(): """ Create an LSF object from filename and assure that it works OK """ lsf = LSF(filename='avg_COS.txt') assert isinstance(lsf.kernel, np.ndarray) assert len(lsf.kernel) > 0 assert lsf.width == len(lsf.kernel) print(lsf)
def apply_lsf(self, function=None, width=None, filename=None): """ Postprocess a spectrum to apply a line spread function. If the SpectrumGenerator already has an LSF_kernel set, it will be used when no keywords are supplied. Otherwise, the user can specify a filename of a user-defined kernel or a function+width for a kernel. Valid functions are: "boxcar" and "gaussian". For more information, see :class:`~trident.LSF` and :class:`~trident.Instrument`. **Parameters** :function: string, optional Desired functional form for the applied LSF kernel. Valid options are currently "boxcar" or "gaussian" Default: None :width: int, optional Width of the desired LSF kernel in bin elements Default: None :filename: string, optional The filename of the user-supplied kernel for applying the LSF Default: None **Example** Make a one zone ray and generate a COS spectrum for it. Apply the COS line spread function to it. >>> import trident >>> ray = trident.make_onezone_ray() >>> sg = trident.SpectrumGenerator('COS') >>> sg.make_spectrum(ray) >>> sg.apply_lsf() >>> sg.plot_spectrum('spec_lsf_corrected.png') Make a one zone ray and generate a spectrum with bin width = 1 angstrom. Apply a boxcar LSF to it with width 50 angstroms. >>> import trident >>> ray = trident.make_onezone_ray() >>> sg = trident.SpectrumGenerator(lambda_min=1100, lambda_max=1200, dlambda=1) >>> sg.make_spectrum(ray) >>> sg.apply_lsf(function='boxcar', width=50) >>> sg.plot_spectrum('spec_lsf_corrected.png') """ # if nothing is specified, then use the Instrument-defined kernel if function is None and width is None and filename is None: if self.instrument.lsf_kernel is None: raise RuntimeError("To apply a line spread function, you " "must specify one or use an instrument " "where one is defined.") else: mylog.info("Applying default line spread function for %s." % \ self.instrument.name) lsf = LSF(filename=self.instrument.lsf_kernel) else: mylog.info("Applying specified line spread function.") lsf = LSF(function=function, width=width, filename=filename) from astropy.convolution import convolve self.flux_field = convolve(self.flux_field, lsf.kernel) # Negative fluxes don't make sense, so clip np.clip(self.flux_field, 0, np.inf, out=self.flux_field)