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*Given a location in the sky, check SDSS to see if the survey covered that region.* 

5 

6:Author: 

7 David Young 

8 

9:Date Created: 

10 June 29, 2016 

11""" 

12from __future__ import print_function 

13################# GLOBAL IMPORTS #################### 

14from builtins import str 

15from builtins import object 

16import sys 

17import os 

18import time 

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

20import requests 

21from fundamentals import tools 

22from astrocalc.coords import unit_conversion 

23 

24 

25class check_coverage(object): 

26 """ 

27 *The worker class for the check_coverage module* 

28 

29 **Key Arguments:** 

30 - ``log`` -- python logger 

31 - ``raDeg`` -- ra in decimal degrees 

32 - ``decDeg`` -- dec in decimal degrees 

33 - ``url`` -- the SDSS URL to ping (DR12 is the default) 

34 

35 **Usage:** 

36 

37 To test whether or not a location in the sky has been covered by the SDSS survey: 

38 

39 .. code-block:: python  

40 

41 from sloancone import check_coverage 

42 # covered = True | False | 999 (i.e. not sure) 

43 covered = check_coverage( 

44 log=log, 

45 ra=122.3343, 

46 dec=45.34343 

47 ).get()  

48 

49 print(covered) 

50 

51 # OUTPUT: True 

52 

53 Coordinates can also be given in sexegesimal format: 

54 

55 .. code-block:: python  

56 

57 from sloancone import check_coverage 

58 covered = check_coverage( 

59 log=log, 

60 ra="12:45:4.45466", 

61 dec="-25:22:34.3434" 

62 ).get() 

63 

64 print(covered) 

65 

66 # OUTPUT: False 

67 """ 

68 # Initialisation 

69 

70 def __init__( 

71 self, 

72 log, 

73 ra, 

74 dec, 

75 url="http://skyserver.sdss.org/dr12/en/tools/search/x_sql.aspx" 

76 ): 

77 self.log = log 

78 log.debug("instansiating a new 'check_coverage' object") 

79 self.ra = ra 

80 self.dec = dec 

81 self.url = url 

82 # xt-self-arg-tmpx 

83 

84 # Initial Actions 

85 

86 converter = unit_conversion( 

87 log=self.log 

88 ) 

89 self.raDeg = converter.ra_sexegesimal_to_decimal( 

90 ra=ra 

91 ) 

92 self.decDeg = converter.dec_sexegesimal_to_decimal( 

93 dec=dec 

94 ) 

95 

96 return None 

97 

98 def get(self): 

99 """ 

100 *get the check_coverage object* 

101 

102 **Return:** 

103 - ``check_coverage`` 

104 """ 

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

106 

107 match = self._query_sdss() 

108 

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

110 return match 

111 

112 def _query_sdss( 

113 self): 

114 """* query sdss* 

115 """ 

116 self.log.debug('starting the ``_query_sdss`` method') 

117 

118 raDeg = float(self.raDeg) 

119 decDeg = float(self.decDeg) 

120 

121 raUpper = raDeg + 0.02 

122 raLower = raDeg - 0.02 

123 declUpper = decDeg + 0.02 

124 declLower = decDeg - 0.02 

125 

126 ################ >ACTION(S) ################ 

127 sqlQuery = "SELECT TOP 1 rerun, camcol, field FROM PhotoObj WHERE ra BETWEEN %s and %s AND dec BETWEEN %s and %s" % ( 

128 raLower, raUpper, declLower, declUpper,) 

129 self.log.debug('sqlQuery: %s' % (sqlQuery,)) 

130 try: 

131 self.log.debug( 

132 "attempting to determine whether object is in SDSS DR12 footprint") 

133 result = self._query( 

134 sql=sqlQuery, 

135 url=self.url, 

136 fmt="html", 

137 log=self.log) 

138 except Exception as e: 

139 self.log.error( 

140 "could not determine whether object is in SDSS DR12 footprint - failed with this error %s: " % 

141 (str(e),)) 

142 return -1 

143 

144 self.log.debug('result: %s' % (result,)) 

145 

146 strResult = str(result) 

147 

148 if "No objects have been found" in strResult or "No entries have been found" in strResult: 

149 match = False 

150 print( 

151 "This location %(raDeg)s, %(decDeg)s is NOT in the SDSS footprint" % locals()) 

152 elif "cornsilk" in strResult or "Your query output" in strResult: 

153 match = True 

154 print( 

155 "This location %(raDeg)s, %(decDeg)s IS in the SDSS footprint" % locals()) 

156 elif "minute" in strResult: 

157 match = 999 

158 print( 

159 "Not sure if location %(raDeg)s, %(decDeg)s in SDSS, try again shortly" % locals()) 

160 print(strResult) 

161 else: 

162 match = 999 

163 print( 

164 "Not sure if location %(raDeg)s, %(decDeg)s in SDSS, here's the resulting HTML:" % locals()) 

165 print(strResult) 

166 

167 print(" See for yourself: http://skyserver.sdss.org/dr12/en/tools/chart/image.aspx?ra=%(raDeg)s&dec=%(decDeg)s&scale=0.8&opt=G&PhotoObjs=off&width=500&height=500" % locals()) 

168 

169 time.sleep(1) 

170 

171 self.log.debug('sdss match: %s' % (match,)) 

172 

173 self.log.debug('completed the ``_query_sdss`` method') 

174 return match 

175 

176 def _query( 

177 self, 

178 sql, 

179 url, 

180 fmt, 

181 log 

182 ): 

183 """* query* 

184 """ 

185 self.log.debug('starting the ``_query`` method') 

186 

187 try: 

188 response = requests.get( 

189 url=url, 

190 params={ 

191 "cmd": self._filtercomment(sql), 

192 "format": fmt, 

193 }, 

194 headers={ 

195 "Cookie": "ASP.NET_SessionId=d0fiwrodvk4rdf21gh3jzr3t; SERVERID=dsa003", 

196 }, 

197 ) 

198 # print('Response HTTP Status Code: {status_code}'.format( 

199 # status_code=response.status_code)) 

200 # print('Response HTTP Response Body: {content}'.format( 

201 # content=response.content)) 

202 except requests.exceptions.RequestException: 

203 print('HTTP Request failed') 

204 

205 self.log.debug('completed the ``_query`` method') 

206 return response.content 

207 

208 def _filtercomment( 

209 self, 

210 sql): 

211 "Get rid of comments starting with --" 

212 import os 

213 fsql = '' 

214 for line in sql.split('\n'): 

215 fsql += line.split('--')[0] + ' ' + os.linesep 

216 return fsql 

217 

218 # use the tab-trigger below for new method 

219 # xt-class-method