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#!/usr/local/bin/python 

2# encoding: utf-8 

3""" 

4*Perform a conesearch of SDSS at a given location with a specified search radius* 

5 

6:Author: 

7 David Young 

8 

9:Date Created: 

10 June 29, 2016 

11""" 

12################# GLOBAL IMPORTS #################### 

13from builtins import object 

14import sys 

15import os 

16from time import sleep 

17os.environ['TERM'] = 'vt100' 

18from fundamentals import tools, times 

19from fundamentals.renderer import list_of_dictionaries 

20 

21 

22class cone_search(object): 

23 """ 

24 *The worker class for the cone_search module* 

25 

26 **Key Arguments:** 

27 - ``log`` -- logger 

28 - ``ra`` -- ra in sexigesimal or decimal degrees 

29 - ``dec`` -- dec in sexigesimal or decimal degrees 

30 - ``searchRadius`` -- search radius in arcsecs 

31 - ``nearest`` -- show closest match only 

32 - ``outputFormat`` -- output format (table or csv) 

33 

34 **Usage:** 

35 

36 .. code-block:: python  

37 

38 from sloancone.cone_search import cone_search 

39 csResults = cone_search( 

40 log=log, 

41 ra="12:45:23.2323", 

42 dec="30.343122", 

43 searchRadius=60., 

44 nearest=False, 

45 outputFormat="table", 

46 galaxyType="all" 

47 ).get() 

48 

49 print(csResults ) 

50 

51 This code outputs the following: 

52 

53 .. code-block:: plain 

54 

55 +---------------------------+---------+-----------+----------+--------+------------+---------+-------------+--------------------+--------------------------+-------------------------+ 

56 | sdss_name | type | ra | dec | specz | specz_err | photoz | photoz_err | separation_arcsec | separation_north_arcsec | separation_east_arcsec | 

57 +---------------------------+---------+-----------+----------+--------+------------+---------+-------------+--------------------+--------------------------+-------------------------+ 

58 | SDSS J124521.85+302046.0 | galaxy | 191.3410 | 30.3461 | None | None | 0.3443 | 0.1007 | 20.8856 | 10.8005 | -17.8762 | 

59 | SDSS J124522.39+302100.4 | galaxy | 191.3433 | 30.3501 | None | None | 0.3172 | 0.0901 | 27.4400 | 25.2212 | -10.8094 | 

60 | SDSS J124522.08+302007.4 | galaxy | 191.3420 | 30.3354 | None | None | 0.3672 | 0.1133 | 31.4720 | -27.7701 | -14.8090 | 

61 | SDSS J124524.95+302105.7 | galaxy | 191.3540 | 30.3516 | None | None | 0.2721 | 0.0311 | 37.8154 | 30.5314 | 22.3124 | 

62 | SDSS J124524.57+302000.2 | galaxy | 191.3524 | 30.3334 | None | None | 0.4181 | 0.0965 | 39.1194 | -35.0377 | 17.3979 | 

63 | SDSS J124519.23+302042.5 | galaxy | 191.3302 | 30.3452 | None | None | 0.2347 | 0.0749 | 52.2402 | 7.3538 | -51.7200 | 

64 | SDSS J124521.36+301943.9 | galaxy | 191.3390 | 30.3289 | None | None | 0.1978 | 0.0699 | 56.7372 | -51.3133 | -24.2086 | 

65 | SDSS J124519.89+302115.9 | galaxy | 191.3329 | 30.3544 | None | None | 0.9105 | 0.0821 | 59.3096 | 40.6688 | -43.1703 | 

66 +---------------------------+---------+-----------+----------+--------+------------+---------+-------------+--------------------+--------------------------+-------------------------+ 

67 

68 To return results in a traditional CSV format: 

69 

70 .. code-block:: python  

71 

72 from sloancone.cone_search import cone_search 

73 csResults = cone_search( 

74 log=log, 

75 ra="112.233432", 

76 dec="15:34:31.22", 

77 searchRadius=60., 

78 nearest=True, 

79 outputFormat="csv", 

80 galaxyType="all" 

81 ).get() 

82 

83 print(csResults) 

84 

85 This code outputs the following: 

86 

87 .. code-block:: plain 

88 

89 sdss_name,type,ra,dec,specz,specz_err,photoz,photoz_err,separation_arcsec,separation_north_arcsec,separation_east_arcsec 

90 SDSS J072855.31+153454.6,galaxy,112.2305,15.5818,,,0.7211,0.0719,25.5528,23.4273,-10.2034 

91 

92 To filter the result be a redshift type (``specz`` or ``photoz``) 

93 

94 .. code-block:: python  

95 

96 from sloancone.cone_search import cone_search 

97 csResults = cone_search( 

98 log=log, 

99 ra="12:45:23.2323", 

100 dec="30.343122", 

101 searchRadius=600., 

102 nearest=False, 

103 outputFormat="table", 

104 galaxyType="specz" 

105 ).get() 

106 

107 print(csResults ) 

108 

109 This code outputs the following: 

110 

111 .. code-block:: plain 

112 

113 +---------------------------+---------+-----------+----------+---------+------------+--------------------+--------------------------+-------------------------+ 

114 | sdss_name | type | ra | dec | specz | specz_err | separation_arcsec | separation_north_arcsec | separation_east_arcsec | 

115 +---------------------------+---------+-----------+----------+---------+------------+--------------------+--------------------------+-------------------------+ 

116 | SDSS J124540.06+301923.0 | galaxy | 191.4169 | 30.3231 | 0.3629 | 0.0002 | 229.5373 | -72.2170 | 217.8809 | 

117 | SDSS J124534.33+301624.6 | galaxy | 191.3931 | 30.2735 | 0.2609 | 0.0000 | 288.8809 | -250.5509 | 143.7929 | 

118 | SDSS J124512.46+301502.3 | galaxy | 191.3019 | 30.2506 | 0.5810 | 0.0002 | 360.9520 | -332.9018 | -139.5091 | 

119 | SDSS J124512.46+301502.3 | galaxy | 191.3019 | 30.2506 | 0.5809 | 0.0002 | 360.9520 | -332.9018 | -139.5091 | 

120 | SDSS J124544.87+302435.6 | galaxy | 191.4370 | 30.4099 | 0.2254 | 0.0000 | 369.1276 | 240.3704 | 280.1380 | 

121 | SDSS J124547.42+302532.8 | galaxy | 191.4476 | 30.4258 | 0.4473 | 0.0001 | 431.9371 | 297.5669 | 313.0872 | 

122 | SDSS J124454.28+301700.6 | galaxy | 191.2262 | 30.2835 | 0.2599 | 0.0000 | 431.9447 | -214.5446 | -374.8959 | 

123 | SDSS J124558.38+301904.7 | galaxy | 191.4933 | 30.3180 | 0.1767 | 0.0000 | 463.9608 | -90.4407 | 455.0606 | 

124 | SDSS J124544.31+301417.7 | galaxy | 191.4347 | 30.2383 | 0.3632 | 0.0000 | 465.9112 | -377.4610 | 273.1236 | 

125 | SDSS J124543.24+301319.2 | galaxy | 191.4302 | 30.2220 | 0.0669 | 0.0000 | 507.2387 | -436.0132 | 259.1979 | 

126 | SDSS J124557.99+302437.8 | galaxy | 191.4916 | 30.4105 | 0.4469 | 0.0002 | 511.0694 | 242.6379 | 449.7987 | 

127 | SDSS J124602.33+302241.3 | galaxy | 191.5097 | 30.3782 | 0.4365 | 0.0001 | 521.5570 | 126.1410 | 506.0733 | 

128 | SDSS J124602.33+302241.3 | galaxy | 191.5097 | 30.3782 | 0.4365 | 0.0001 | 521.5570 | 126.1410 | 506.0733 | 

129 | SDSS J124605.96+301913.6 | galaxy | 191.5248 | 30.3205 | 0.1761 | 0.0000 | 559.2108 | -81.5693 | 553.2298 | 

130 | SDSS J124439.37+301949.6 | galaxy | 191.1640 | 30.3305 | 0.2219 | 0.0000 | 569.6536 | -45.5571 | -567.8291 | 

131 | SDSS J124439.37+301949.6 | galaxy | 191.1640 | 30.3305 | 0.2220 | 0.0000 | 569.6536 | -45.5571 | -567.8291 | 

132 | SDSS J124538.93+302945.0 | galaxy | 191.4122 | 30.4958 | 0.2252 | 0.0000 | 586.1380 | 549.8198 | 203.1158 | 

133 | SDSS J124450.57+301333.8 | galaxy | 191.2107 | 30.2261 | 0.4373 | 0.0001 | 597.1297 | -421.4088 | -423.0586 | 

134 | SDSS J124450.57+301333.8 | galaxy | 191.2107 | 30.2261 | 0.4377 | 0.0001 | 597.1297 | -421.4088 | -423.0586 | 

135 +---------------------------+---------+-----------+----------+---------+------------+--------------------+--------------------------+-------------------------+ 

136 

137 Finally, we can also search for stars and galaxies by selecting ``galaxyType=False``: 

138 

139 .. code-block:: python  

140 

141 from sloancone.cone_search import cone_search 

142 csResults = cone_search( 

143 log=log, 

144 ra="12:45:23.2323", 

145 dec="30.343122", 

146 searchRadius=60., 

147 nearest=False, 

148 outputFormat="table", 

149 galaxyType=False 

150 ).get() 

151 

152 print(csResults) 

153 

154 .. code-block:: plain 

155 

156 +---------------------------+---------+-----------+----------+----------+------------+---------+-------------+--------------------+--------------------------+-------------------------+ 

157 | sdss_name | type | ra | dec | specz | specz_err | photoz | photoz_err | separation_arcsec | separation_north_arcsec | separation_east_arcsec | 

158 +---------------------------+---------+-----------+----------+----------+------------+---------+-------------+--------------------+--------------------------+-------------------------+ 

159 | SDSS J124521.85+302046.0 | galaxy | 191.3410 | 30.3461 | None | None | 0.3443 | 0.1007 | 20.8856 | 10.8005 | -17.8762 | 

160 | SDSS J124524.92+302042.7 | star | 191.3539 | 30.3452 | None | None | None | None | 23.2234 | 7.5490 | 21.9622 | 

161 | SDSS J124521.68+302016.9 | star | 191.3403 | 30.3380 | -0.0001 | 0.0000 | None | None | 27.1558 | -18.2960 | -20.0672 | 

162 | SDSS J124522.39+302100.4 | galaxy | 191.3433 | 30.3501 | None | None | 0.3172 | 0.0901 | 27.4400 | 25.2212 | -10.8094 | 

163 | SDSS J124522.08+302007.4 | galaxy | 191.3420 | 30.3354 | None | None | 0.3672 | 0.1133 | 31.4720 | -27.7701 | -14.8090 | 

164 | SDSS J124524.95+302105.7 | galaxy | 191.3540 | 30.3516 | None | None | 0.2721 | 0.0311 | 37.8154 | 30.5314 | 22.3124 | 

165 | SDSS J124524.57+302000.2 | galaxy | 191.3524 | 30.3334 | None | None | 0.4181 | 0.0965 | 39.1194 | -35.0377 | 17.3979 | 

166 | SDSS J124521.67+301955.1 | star | 191.3403 | 30.3320 | None | None | None | None | 44.8763 | -40.0699 | -20.2060 | 

167 | SDSS J124526.25+302103.7 | star | 191.3594 | 30.3511 | None | None | None | None | 48.4191 | 28.5417 | 39.1124 | 

168 | SDSS J124519.23+302042.5 | galaxy | 191.3302 | 30.3452 | None | None | 0.2347 | 0.0749 | 52.2402 | 7.3538 | -51.7200 | 

169 | SDSS J124521.36+301943.9 | galaxy | 191.3390 | 30.3289 | None | None | 0.1978 | 0.0699 | 56.7372 | -51.3133 | -24.2086 | 

170 | SDSS J124522.15+301937.9 | star | 191.3423 | 30.3272 | None | None | None | None | 58.9703 | -57.2852 | -13.9962 | 

171 | SDSS J124519.89+302115.9 | galaxy | 191.3329 | 30.3544 | None | None | 0.9105 | 0.0821 | 59.3096 | 40.6688 | -43.1703 | 

172 | SDSS J124526.04+301947.9 | star | 191.3585 | 30.3300 | None | None | None | None | 59.6986 | -47.3115 | 36.4080 | 

173 | SDSS J124524.95+302130.6 | star | 191.3540 | 30.3585 | None | None | None | None | 59.7431 | 55.4049 | 22.3502 | 

174 +---------------------------+---------+-----------+----------+----------+------------+---------+-------------+--------------------+--------------------------+-------------------------+ 

175 """ 

176 

177 # Initialisation 

178 def __init__( 

179 self, 

180 log, 

181 ra, 

182 dec, 

183 searchRadius, 

184 nearest, 

185 outputFormat="table", 

186 galaxyType=False 

187 ): 

188 from . import sdss_square_search 

189 self.log = log 

190 log.debug("instansiating a new 'cone_search' object") 

191 self.ra = ra 

192 self.dec = dec 

193 self.searchRadius = searchRadius 

194 self.nearest = nearest 

195 self.outputFormat = outputFormat 

196 self.galaxyType = galaxyType 

197 # xt-self-arg-tmpx 

198 

199 # Initial Actions 

200 # perform the initail square search 

201 squareSearch = sdss_square_search( 

202 log=self.log, 

203 ra=self.ra, 

204 dec=self.dec, 

205 searchRadius=self.searchRadius, 

206 galaxyType=self.galaxyType 

207 ) 

208 self.squareResults = squareSearch.get() 

209 

210 return None 

211 

212 def get(self): 

213 """ 

214 *get the cone_search object* 

215 

216 **Return:** 

217 - ``results`` -- the results of the conesearch 

218 

219 """ 

220 self.log.debug('starting the ``get`` method') 

221 

222 # sort results by angular separation 

223 from operator import itemgetter 

224 results = list(self.squareResults) 

225 results = sorted( 

226 results, key=itemgetter('separation_arcsec'), reverse=True) 

227 

228 # order of results 

229 headers = ["sdss_name", "type", "ra", "dec", "specz", "specz_err", "photoz", 

230 "photoz_err", "separation_arcsec", "separation_north_arcsec", "separation_east_arcsec"] 

231 

232 import collections 

233 orderDict = collections.OrderedDict(sorted({}.items())) 

234 

235 # filter out results greater than the search radius 

236 filteredResults = [] 

237 for row in results: 

238 if float(row["separation_arcsec"]) < self.searchRadius: 

239 orderDict = collections.OrderedDict(sorted({}.items())) 

240 for h in headers: 

241 if h in list(row.keys()): 

242 orderDict[h] = row[h] 

243 filteredResults.append(orderDict) 

244 else: 

245 pass 

246 

247 if self.nearest and len(filteredResults): 

248 orderDict = collections.OrderedDict(sorted({}.items())) 

249 for h in headers: 

250 if h in list(filteredResults[0].keys()): 

251 orderDict[h] = row[h] 

252 filteredResults = [orderDict] 

253 # filteredResults = [filteredResults[0]] 

254 

255 if not len(filteredResults): 

256 orderDict = collections.OrderedDict(sorted({}.items())) 

257 for h in headers: 

258 if self.galaxyType == "all" or self.galaxyType == False or (self.galaxyType == "specz" and h not in ["photoz_err", "photoz"]) or (self.galaxyType == "photoz" and h not in ["specz", "specz_err"]): 

259 orderDict[h] = "" 

260 filteredResults = [orderDict] 

261 

262 # pretty format print 

263 dataSet = list_of_dictionaries( 

264 log=self.log, 

265 listOfDictionaries=list(reversed(filteredResults)) 

266 ) 

267 if self.outputFormat == "csv": 

268 results = dataSet.csv() 

269 else: 

270 results = dataSet.table() 

271 

272 # sdss only allows 60 hits per minute 

273 sleep(1) 

274 

275 self.log.debug('completed the ``get`` method') 

276 return results