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

1from __future__ import print_function 

2from builtins import range 

3import os 

4import numpy as np 

5import warnings 

6 

7import lsst.utils 

8from lsst.sims.catUtils.matchSED.matchUtils import matchStar 

9from lsst.sims.photUtils import BandpassDict 

10from lsst.sims.catUtils.dust import EBVbase as ebv 

11 

12__all__ = ["selectStarSED"] 

13 

14class selectStarSED(matchStar): 

15 

16 """ 

17 This class provides a way to match star catalog magntiudes to those of the approriate SED. 

18 """ 

19 

20 def findSED(self, sedList, catMags, catRA = None, catDec = None, mag_error = None, 

21 reddening = True, bandpassDict = None, colors = None, nullValues = None, 

22 extCoeffs = (4.239, 3.303, 2.285, 1.698, 1.263), makeCopy = False, verbose=True): 

23 

24 """ 

25 This will find the SEDs that are the closest match to the magnitudes of a star catalog. 

26 It can also correct for reddening from within the milky way. Objects without magnitudes in at least 

27 two adjacent bandpasses will return as none and print out a message. 

28 

29 @param [in] sedList is the set of spectral objects from the models SEDs provided by loaders in rgStar 

30 in rgUtils.py or other custom loader routine. 

31 

32 @param [in] catMags is an array of the magnitudes of catalog objects to be matched with a model SED. 

33 It should be organized so that there is one object's magnitudes along each row. 

34 

35 @param [in] catRA is an array of the RA positions for each catalog object. 

36 

37 @param [in] catDec is an array of the Dec position for each catalog object. 

38 

39 @param [in] mag_error are provided error values for magnitudes in objectMags. If none provided 

40 then this defaults to 1.0. This should be an array of the same length as objectMags. 

41 

42 @param [in] reddening is a boolean that determines whether to correct catalog magnitudes for 

43 dust in the milky way. By default, it is True. 

44 If true, this uses calculateEBV from EBV.py to find an EBV value for the object's 

45 ra and dec coordinates and then uses the coefficients provided by extCoeffs which should come 

46 from Schlafly and Finkbeiner (2011) for the correct filters and in the same order as provided 

47 in bandpassDict. 

48 If false, this means it will not run the dereddening procedure. 

49 

50 @param [in] bandpassDict is a BandpassDict with which to calculate magnitudes. If left 

51 equal to None it will by default load the SDSS [u,g,r,i,z] bandpasses and therefore agree with 

52 default extCoeffs. 

53 

54 @param [in] colors is None if you are just providing a list of SED objects to match, but is the 

55 array holding the colors of those SED models (each row should be the colors for one model in the 

56 same order as sedList) if you have already calculated the colors. 

57 

58 @param [in] nullValues is None by default. This means that your catalog is complete and there is no 

59 default value in your catalog for missing data. If you do have a number that indicates missing data 

60 then set this parameter to that value and these missing values will be ignored when matching colors. 

61 

62 @param [in] extCoeffs are the Schlafly and Finkbeiner (2011) (ApJ, 737, 103) coefficients for the 

63 given filters from bandpassDict and need to be in the same order as bandpassDict. The default given 

64 are the SDSS [u,g,r,i,z] values. 

65 

66 @param [in] makeCopy indicates whether or not to operate on copies of the SED objects in sedList 

67 since this method will change the wavelength grid. 

68 

69 @param [out] sedMatches is a list with the name of a model SED that matches most closely to each 

70 object in the catalog. 

71 

72 @param [out] magNormMatches are the magnitude normalizations for the given magnitudes and 

73 matched SED. 

74 

75 @param [out] matchErrors contains the Mean Squared Error between the colors of each object and 

76 the colors of the matched SED. 

77 """ 

78 

79 if bandpassDict is None: 

80 starPhot = BandpassDict.loadTotalBandpassesFromFiles(['u','g','r','i','z'], 

81 bandpassDir = os.path.join(lsst.utils.getPackageDir('throughputs'), 

82 'sdss'), 

83 bandpassRoot = 'sdss_') 

84 else: 

85 starPhot = bandpassDict 

86 

87 if colors is None: 

88 modelColors = self.calcBasicColors(sedList, starPhot, makeCopy=makeCopy) 

89 else: 

90 modelColors = colors 

91 #Transpose so that all values for one color are in one row as needed for the matching loop below 

92 modelColors = np.transpose(modelColors) 

93 

94 #Set null values to nan so that we will skip them below 

95 if nullValues is not None: 

96 catMags[np.where(catMags == nullValues)] = np.nan 

97 

98 if reddening == True: 

99 #Check that catRA and catDec are included 

100 if catRA is None or catDec is None: 

101 raise RuntimeError("Reddening is True, but catRA and catDec are not included.") 

102 calcEBV = ebv() 

103 raDec = np.array((catRA,catDec)) 

104 #If only matching one object need to reshape for calculateEbv 

105 if len(raDec.shape) == 1: 

106 raDec = raDec.reshape((2,1)) 

107 ebvVals = calcEBV.calculateEbv(equatorialCoordinates = raDec) 

108 objMags = self.deReddenMags(ebvVals, catMags, extCoeffs) 

109 else: 

110 objMags = catMags 

111 

112 objMags = np.array(objMags) 

113 matchColors = [] 

114 

115 for filtNum in range(0, len(starPhot)-1): 

116 matchColors.append(np.transpose(objMags)[filtNum] - np.transpose(objMags)[filtNum+1]) 

117 

118 matchColors = np.transpose(matchColors) 

119 

120 numCatMags = len(catMags) 

121 numOn = 0 

122 sedMatches = [] 

123 magNormMatches = [] 

124 notMatched = 0 

125 matchErrors = [] 

126 

127 for catObject in matchColors: 

128 #This is done to handle objects with incomplete magnitude data 

129 colorRange = np.arange(0, len(starPhot)-1) 

130 filtNums = np.arange(0, len(starPhot)) 

131 if np.isnan(np.amin(catObject))==True: 

132 colorRange = np.where(np.isnan(catObject)==False)[0] 

133 filtNums = np.unique([colorRange, colorRange+1]) #Pick right filters in calcMagNorm 

134 if len(colorRange) == 0: 

135 if verbose == True: 

136 print('Could not match object #%i. No magnitudes for two adjacent bandpasses.' % (numOn)) 

137 notMatched += 1 

138 sedMatches.append(None) 

139 magNormMatches.append(None) 

140 matchErrors.append(None) 

141 else: 

142 distanceArray = np.zeros(len(sedList)) 

143 for colorNum in colorRange: 

144 distanceArray += np.power((modelColors[colorNum] - catObject[colorNum]),2) 

145 matchedSEDNum = np.nanargmin(distanceArray) 

146 sedMatches.append(sedList[matchedSEDNum].name) 

147 magNorm = self.calcMagNorm(objMags[numOn], sedList[matchedSEDNum], 

148 starPhot, filtRange = filtNums) 

149 magNormMatches.append(magNorm) 

150 matchErrors.append(distanceArray[matchedSEDNum]/len(colorRange)) #Mean Squared Error 

151 numOn += 1 

152 if numOn % 10000 == 0: 

153 print('Matched %i of %i catalog objects to SEDs' % (numOn-notMatched, numCatMags)) 

154 if numCatMags > 1: 

155 print('Done Matching. Matched %i of %i catalog objects to SEDs' % (numCatMags-notMatched, 

156 numCatMags)) 

157 if notMatched > 0: 

158 print('%i objects did not get matched' % (notMatched)) 

159 

160 return sedMatches, magNormMatches, matchErrors