Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

import numpy as np 

 

from .utils import astropyHeaderToDict, astropyHeaderFromDict 

from .masks import MaskHelper 

 

__all__ = ["FluxTable"] 

 

 

class FluxTable: 

"""Table of coadded fluxes at near-original sampling 

 

Merged and coadded spectra have been resampled to a standard wavelength 

sampling. This representation provides coadded fluxes at approximately the 

native wavelength sampling, for those that want the data with a minimum of 

resampling. This is mostly of use for single exposures and coadds made from 

back-to-back exposures with the same top-end configuration. For coadds made 

from exposures with different top-end configurations, the different 

wavelength samplings obtained from the different fibers means there's no 

single native wavelength sampling, and so this is less useful. 

 

This is like a `pfs.datamodel.PfsSimpleSpectrum`, except that it includes a 

variance array, and is written to a FITS HDU rather than a file (so it can 

be incorporated within a `pfs.datamodel.PfsSpectrum`). 

 

Parameters 

---------- 

wavelength : `numpy.ndarray` of `float` 

Array of wavelengths. 

flux : `numpy.ndarray` of `float` 

Array of fluxes. 

error : `numpy.ndarray` of `float` 

Array of flux errors. 

mask : `numpy.ndarray` of `int` 

Array of mask pixels. 

flags : `pfs.datamodel.MaskHelper` 

Helper for dealing with symbolic names for mask values. 

""" 

_hduName = "FLUX_TABLE" # HDU name to use 

 

def __init__(self, wavelength, flux, error, mask, flags): 

dims = np.array([len(wavelength.shape), len(flux.shape), len(error.shape), len(mask.shape)]) 

lengths = set([wavelength.shape, flux.shape, error.shape, mask.shape]) 

if np.any(dims != 1) or len(lengths) > 1: 

raise RuntimeError("Bad shapes for wavelength,flux,error,mask: %s,%s,%s,%s" % 

(wavelength.shape, flux.shape, error.shape, mask.shape)) 

self.wavelength = wavelength 

self.flux = flux 

self.error = error 

self.mask = mask 

self.flags = flags 

 

def __len__(self): 

"""Return number of elements""" 

return len(self.wavelength) 

 

def toFits(self, fits): 

"""Write to a FITS file 

 

Parameters 

---------- 

fits : `astropy.io.fits.HDUList` 

Opened FITS file. 

""" 

from astropy.io.fits import BinTableHDU, Column 

header = self.flags.toFitsHeader() 

hdu = BinTableHDU.from_columns([ 

Column("wavelength", "E", array=self.wavelength), 

Column("flux", "E", array=self.flux), 

Column("error", "E", array=self.error), 

Column("mask", "K", array=self.mask), 

], header=astropyHeaderFromDict(header), name=self._hduName) 

fits.append(hdu) 

 

@classmethod 

def fromFits(cls, fits): 

"""Construct from a FITS file 

 

Parameters 

---------- 

fits : `astropy.io.fits.HDUList` 

Opened FITS file. 

 

Returns 

------- 

self : `FluxTable` 

Constructed `FluxTable`. 

""" 

hdu = fits[cls._hduName] 

header = astropyHeaderToDict(hdu.header) 

flags = MaskHelper.fromFitsHeader(header) 

return cls(hdu.data["wavelength"], hdu.data["flux"], hdu.data["error"], hdu.data["mask"], flags)