def extract(spectrum_data, x_range): ''' Extracts a region from a spectrum. Paramaters ---------- spectrum_data: SpectrumData Contains the spectrum to be extracted. w_range: tuple A spectral coordinate range as in (wave1, wave2) Returns ------- SpectrumData with extracted region. ''' x = spectrum_data.x.data y = spectrum_data.y.data slice = (x >= x_range[0]) & (x < x_range[1]) result = SpectrumData() result.set_x(x[slice], unit=spectrum_data.x.unit) result.set_y(y[slice], unit=spectrum_data.y.unit) return result
def read_image(image, flux_unit=None, dispersion_unit=None, **kwargs): """Read 1D image Parameters ---------- image: FITS Image HDU Returns ------- SpectrumData Notes ----- Assumes ONLY 1D and that the WCS has the dispersion definition. If not, its just pixels. """ if len(image.data.shape) > 1: raise RuntimeError('Attempting to read an image with more than one ' 'dimension.') wcs = WCS(image.header) spectrum = SpectrumData() unit = flux_unit if flux_unit else DEFAULT_FLUX_UNIT spectrum.set_y(image.data, unit=unit) unit = wcs.wcs.cunit[0] if not dispersion_unit else dispersion_unit spectrum.set_x(wcs.all_pix2world(range(image.data.shape[0]), 1)[0], unit=unit) return spectrum
def read_table(table, flux='flux', dispersion='wavelength', flux_unit=None, dispersion_unit=None): """Read FITS table Parameters ---------- table: FITS table dispersion: str Name of column containing the dispersion axis. flux: str Name of column containing the flux axis. dispersion_unit: str Unit of dispersion flux_unit: str Unit of flux Returns ------- SpectrumData '""" if dispersion_unit is None: try: dispersion_unit = table.columns[table.names.index(dispersion.upper())].unit if dispersion_unit is None: raise ValueError except ValueError: dispersion_unit = DEFAULT_DISPERSION_UNIT if flux_unit is None: try: flux_unit = table.columns[table.names.index(flux.upper())].unit if flux_unit is None: raise ValueError except ValueError: flux_unit = DEFAULT_FLUX_UNIT spectrum = SpectrumData() spectrum.set_x(table[dispersion], unit=dispersion_unit) spectrum.set_y(table[flux], unit=flux_unit) return spectrum
def read_table(table, flux='flux', dispersion='wavelength', flux_unit=None, dispersion_unit=None): """Read FITS table Parameters ---------- table: FITS table dispersion: str Name of column containing the dispersion axis. flux: str Name of column containing the flux axis. dispersion_unit: str Unit of dispersion flux_unit: str Unit of flux Returns ------- SpectrumData '""" if dispersion_unit is None: try: dispersion_unit = table.columns[table.names.index( dispersion.upper())].unit if dispersion_unit is None: raise ValueError except ValueError: dispersion_unit = DEFAULT_DISPERSION_UNIT if flux_unit is None: try: flux_unit = table.columns[table.names.index(flux.upper())].unit if flux_unit is None: raise ValueError except ValueError: flux_unit = DEFAULT_FLUX_UNIT spectrum = SpectrumData() spectrum.set_x(table[dispersion], unit=dispersion_unit) spectrum.set_y(table[flux], unit=flux_unit) return spectrum
# Example usage def print_results(wave, flux, converted_wave, converted_flux): for k in range(4): print(wave[k], " ", converted_wave[k]) for k in range(4): print(flux[k], " ", converted_flux[k]) import test_data if __name__ == "__main__": # real emission line from UV spectrum of NGC3516 x, y, e = test_data.get_data() # test conversions from SpectrumData objects. sp_data = SpectrumData() sp_data.set_x(x, unit="Angstrom") sp_data.set_y(y, unit="erg.s-1.cm**-2.Angstrom-1") # flam converter = UnitsConverter(u.micron, u.Jy) converter.convertSpectrumData(sp_data) print(sp_data.x.unit, " ", sp_data.y.unit) for k in range(4): print(sp_data.x.data[k], " ", sp_data.y.data[k]) # test conversions from Quantity objects. wave = test_data.get_data()[0] * u.Unit('angstrom') flux = test_data.get_data()[1] * u.Unit("erg / (Angstrom cm2 s)") converter = UnitsConverter(u.micron, u.Jy)
# Example usage def print_results(wave, flux, converted_wave, converted_flux): for k in range(4): print(wave[k], " ", converted_wave[k]) for k in range(4): print(flux[k], " ", converted_flux[k]) import test_data if __name__ == "__main__": # real emission line from UV spectrum of NGC3516 x,y,e = test_data.get_data() # test conversions from SpectrumData objects. sp_data = SpectrumData() sp_data.set_x(x, unit="Angstrom") sp_data.set_y(y, unit="erg.s-1.cm**-2.Angstrom-1") # flam converter = UnitsConverter(u.micron, u.Jy) converter.convertSpectrumData(sp_data) print(sp_data.x.unit, " ", sp_data.y.unit) for k in range(4): print(sp_data.x.data[k], " ", sp_data.y.data[k]) # test conversions from Quantity objects. wave = test_data.get_data()[0] * u.Unit('angstrom') flux = test_data.get_data()[1] * u.Unit("erg / (Angstrom cm2 s)")