def test_mag(): """Spectrum class: testing magnitude computations.""" Vega = Spectrum(get_data_file('obj', 'Vega.fits')) Vega.unit = u.Unit('2E-17 erg / (Angstrom cm2 s)') Vega.wave.wcs.wcs.cunit[0] = u.angstrom assert_almost_equal(Vega.abmag_filter_name('V')[0], 0, 1) mag = Vega.abmag_filter_name('Ic')[0] assert mag > 0.4 and mag < 0.5 mag = Vega.abmag_band(22500, 2500)[0] assert mag > 1.9 and mag < 2.0
def test_integrate(): """Spectrum class: testing integration""" wave = WaveCoord(crpix=2.0, cdelt=3.0, crval=0.5, cunit=u.nm) spectrum1 = Spectrum(data=np.array([0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9]), wave=wave, unit=u.Unit('ct/Angstrom')) # Integrate the whole spectrum, by not specifying starting or ending # wavelengths. This should be the sum of the pixel values multiplied # by cdelt in angstroms (because the flux units are per angstrom). result, err = spectrum1.integrate() expected = spectrum1.get_step(unit=u.angstrom) * spectrum1.sum()[0] assert_almost_equal(result.value, expected) assert result.unit == u.ct assert np.isinf(err) # The result should not change if we change the wavelength units of # the wavelength limits to nanometers. result = spectrum1.integrate(unit=u.nm)[0] expected = spectrum1.get_step(unit=u.angstrom) * spectrum1.sum()[0] assert_almost_equal(result.value, expected) assert result.unit == u.ct # new spectrum with variance spectrum1 = Spectrum(data=np.array([0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9]), var=np.ones(10), wave=wave, unit=u.Unit('ct/Angstrom')) # Integrate over a wavelength range 3.5 to 6.5 nm. The WCS # conversion equation from wavelength to pixel index is, # # index = crpix-1 + (lambda-crval)/cdelt # index = 1 + (lambda - 0.5) / 3.0 # # So wavelengths 3.5 and 6.5nm, correspond to pixel indexes # of 2.0 and 3.0. These are the centers of pixels 2 and 3. # Thus the integration should be the value of pixel 2 times # half of cdelt, plus the value of pixel 3 times half of cdelt. # This comes to 2*3.0/2 + 3*3.0/2 = 7.5 ct/Angstrom*nm, which # should be rescaled to 75 ct, since nm/Angstrom is 10.0. result, err = spectrum1.integrate(lmin=3.5, lmax=6.5, unit=u.nm) assert_almost_equal(result.value, 75) assert result.unit == u.ct datasum, var = spectrum1.sum(lmin=3.5, lmax=6.5, unit=u.nm) assert_almost_equal(result.value, datasum * 15) assert_almost_equal(err.value, var * 15) # Do the same test, but specify the wavelength limits in angstroms. # The result should be the same as before. result = spectrum1.integrate(lmin=35.0, lmax=65.0, unit=u.angstrom)[0] assert_almost_equal(result.value, 75) assert result.unit == u.ct assert_almost_equal(result.value, datasum * 15) assert_almost_equal(err.value, var * 15) # Do the same experiment yet again, but this time after changing # the flux units of the spectrum to simple counts, without any per # wavelength units. Since there are no wavelength units in the # flux units, the result should not be rescaled from the native # value of 7.5, and because we specified a wavelength range in # angstroms, the resulting units should be counts * nm. spectrum1.unit = u.ct result = spectrum1.integrate(lmin=3.5, lmax=6.5, unit=u.nm)[0] assert_almost_equal(result.value, 7.5) assert result.unit == u.ct * u.nm