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

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

import types 

import numpy as np 

 

from .utils import astropyHeaderFromDict, astropyHeaderToDict 

from .pfsConfig import TargetType 

 

__all__ = ("Target",) 

 

 

class Target(types.SimpleNamespace): 

"""A spectroscopic target 

 

Parameters 

---------- 

catId : `int` 

Catalog identifier of the object. 

tract : `int` 

Tract in which the object resides. 

patch : `str` 

Patch in which the object resides. 

objId : `objId` 

Object identifier of the object. 

ra : `float`, optional 

Right Ascension of the object. 

dec : `float`, optional 

Declination of the object. 

targetType : `TargetType`, optional 

Type of target (typically ``SCIENCE``). 

fiberFlux : `dict` mapping `str` to `float`, optional 

Filter names and corresponding fiber fluxes. 

""" 

_attributes = ("catId", "tract", "patch", "objId", "ra", "dec", "targetType") # Read from header 

"""Attributes to read from FITS header (iterable of `str`)""" 

 

def __init__(self, catId, tract, patch, objId, ra=np.nan, dec=np.nan, targetType=-1, fiberFlux=None): 

self.catId = catId 

self.tract = tract 

self.patch = patch 

self.objId = objId 

self.ra = ra 

self.dec = dec 

self.targetType = targetType 

self.fiberFlux = fiberFlux if fiberFlux is not None else {} 

self.identity = dict(catId=catId, tract=tract, patch=patch, objId=objId) 

 

def __str__(self): 

"""Stringify""" 

return "catId=%d tract=%d patch=%s objId=%d" % (self.catId, self.tract, self.patch, self.objId) 

 

@classmethod 

def fromFits(cls, fits): 

"""Construct from a FITS file 

 

Parameters 

---------- 

fits : `astropy.io.fits.HDUList` 

Opened FITS file. 

 

Returns 

------- 

self : `Target` 

Constructed `Target`. 

""" 

hdu = fits["TARGET"] 

header = astropyHeaderToDict(hdu.header) 

kwargs = {} 

for attr in cls._attributes: 

kwargs[attr] = header[attr.upper()] 

kwargs["fiberFlux"] = dict(zip(hdu.data["filterName"], hdu.data["fiberFlux"])) 

return cls(**kwargs) 

 

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 

maxLength = max(len(ff) for ff in self.fiberFlux.keys()) if self.fiberFlux else 1 

header = astropyHeaderFromDict({attr.upper(): getattr(self, attr) for attr in self._attributes}) 

header.update(TargetType.getFitsHeaders()) 

hdu = BinTableHDU.from_columns([ 

Column("filterName", "%dA" % maxLength, array=list(self.fiberFlux.keys())), 

Column("fiberFlux", "E", array=np.array(list(self.fiberFlux.values()))), 

], header=header, name="TARGET") 

fits.append(hdu) 

 

def __hash__(self): 

return hash((self.catId, self.tract, self.patch, self.objId)) 

 

@classmethod 

def fromPfsConfig(cls, pfsConfig, index): 

"""Construct from a PfsConfig 

 

Parameters 

---------- 

pfsConfig : `pfs.datamodel.PfsConfig` 

Top-end configuration. 

index : `int` 

Index into the ``pfsConfig`` arrays for the target of interest. 

 

Returns 

------- 

self : cls 

Constructed `Target`. 

""" 

catId = pfsConfig.catId[index] 

tract = pfsConfig.tract[index] 

patch = pfsConfig.patch[index] 

objId = pfsConfig.objId[index] 

ra = pfsConfig.ra[index] 

dec = pfsConfig.dec[index] 

fiberFlux = dict(zip(pfsConfig.filterNames[index], pfsConfig.fiberFlux[index])) 

targetType = pfsConfig.targetType[index] 

return cls(catId, tract, patch, objId, ra, dec, targetType, fiberFlux) 

 

def __reduce__(self): 

"""How to pickle""" 

return type(self), (self.catId, self.tract, self.patch, self.objId, self.ra, self.dec, 

self.targetType, self.fiberFlux)