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 builtins import next 

2from builtins import str 

3from builtins import range 

4import sqlite3 

5import numpy as np 

6 

7from lsst.sims.catalogs.db import CatalogDBObject 

8 

9__all__ = ["getOneChunk", "writeResult", "sampleSphere", "myTestGals", 

10 "makeGalTestDB", "myTestStars", "makeStarTestDB"] 

11 

12 

13def getOneChunk(results): 

14 try: 

15 chunk = next(results) 

16 except StopIteration: 

17 raise RuntimeError("No results were returned. Cannot run tests. Try increasing the size of the" 

18 " test database") 

19 return chunk 

20 

21 

22def writeResult(result, fname): 

23 fh = open(fname, 'w') 

24 first = True 

25 for chunk in result: 

26 if first: 

27 fh.write(",".join([str(el) for el in chunk.dtype.names])+"\n") 

28 first = False 

29 for i in range(len(chunk)): 

30 fh.write(",".join([str(chunk[name][i]) for name in chunk.dtype.names])+"\n") 

31 fh.close() 

32 

33 

34def sampleSphere(size, ramin = 0., dra = 2.*np.pi, rng=None): 

35 # From Shao 1996: "Spherical Sampling by Archimedes' Theorem" 

36 if rng is None: 

37 rng = np.random.RandomState(42) 

38 

39 ra = rng.random_sample(size)*dra 

40 ra += ramin 

41 ra %= 2*np.pi 

42 z = rng.random_sample(size)*2. - 1. 

43 dec = np.arccos(z) - np.pi/2. 

44 return ra, dec 

45 

46 

47def sampleFocus(size, raCenter, decCenter, radius, rng=None): 

48 """ 

49 Sample points in a focused field of view 

50 @param [in] raCenter is the RA at the center of the field of view in radians 

51 @param [in] decCenter is the Dec at the center of the field of view in radians 

52 @param [in] radius is the radius of the field of view in radians 

53 @param [in] rng is a random number generator (an instance of np.random.RandomState) 

54 @param [out] returns numpy arrays of ra and decs in radians 

55 """ 

56 if rng is None: 

57 rng = np.random.RandomState(1453) 

58 

59 theta = rng.random_sample(size) 

60 rc = np.radians(raCenter) 

61 dc = np.radians(decCenter) 

62 rr = np.radians(radius)*rng.random_sample(size) 

63 ra = np.empty(size) 

64 dec = np.empty(size) 

65 for i, th in enumerate(theta): 

66 ra[i] = rc + rr*np.cos(th) 

67 dec[i] = dc + rr*np.sin(th) 

68 

69 return ra, dec 

70 

71 

72class myTestGals(CatalogDBObject): 

73 objid = 'testgals' 

74 tableid = 'galaxies' 

75 idColKey = 'id' 

76 # Make this implausibly large? 

77 appendint = 1022 

78 objectTypeId = 45 

79 driver = 'sqlite' 

80 database = 'testDatabase.db' 

81 raColName = 'ra' 

82 decColName = 'decl' 

83 spatialModel = 'SERSIC2D' 

84 columns = [('id', None, int), 

85 ('raJ2000', 'ra*%f'%(np.pi/180.)), 

86 ('decJ2000', 'decl*%f'%(np.pi/180.)), 

87 ('umag', None), 

88 ('gmag', None), 

89 ('rmag', None), 

90 ('imag', None), 

91 ('zmag', None), 

92 ('ymag', None), 

93 ('magNormAgn', 'mag_norm_agn', None), 

94 ('magNormDisk', 'mag_norm_disk', None), 

95 ('magNormBulge', 'mag_norm_bulge', None), 

96 ('redshift', None), 

97 ('a_disk', None), 

98 ('b_disk', None), 

99 ('a_bulge', None), 

100 ('b_bulge', None)] 

101 

102 

103def makeGalTestDB(filename='testDatabase.db', size=1000, seedVal=None, 

104 raCenter=None, decCenter=None, radius=None, **kwargs): 

105 """ 

106 Make a test database to serve information to the myTestGals object 

107 @param size: Number of rows in the database 

108 @param seedVal: Random seed to use 

109 

110 @param raCenter,decCenter: the center of the field of view in degrees (optional) 

111 @param radius: the radius of the field of view in degrees (optional) 

112 

113 These last optional parameters exist in the event that you want to make sure 

114 that the objects are clustered around the bore site for a unit test 

115 """ 

116 conn = sqlite3.connect(filename) 

117 c = conn.cursor() 

118 try: 

119 c.execute('''CREATE TABLE galaxies 

120 (id int, ra real, decl real, umag real, gmag real, rmag real, 

121 imag real, zmag real, ymag real, 

122 mag_norm_agn real, mag_norm_bulge real, mag_norm_disk real, 

123 redshift real, a_disk real, b_disk real, a_bulge real, b_bulge real, varParamStr text)''') 

124 conn.commit() 

125 except: 

126 raise RuntimeError("Error creating database.") 

127 

128 if seedVal is not None: 

129 rng = np.random.RandomState(seedVal) 

130 else: 

131 rng = np.random.RandomState(3321) 

132 

133 if raCenter is None or decCenter is None or radius is None: 

134 

135 ra, dec = sampleSphere(size, rng=rng, **kwargs) 

136 else: 

137 rc = np.radians(raCenter) 

138 dc = np.radians(decCenter) 

139 ra, dec = sampleFocus(size, rc, dc, radius, rng=rng) 

140 # Typical colors for main sequece stars 

141 umg = 1.5 

142 gmr = 0.65 

143 rmi = 1.0 

144 imz = 0.45 

145 zmy = 0.3 

146 mag_norm_disk = rng.random_sample(size)*6. + 18. 

147 mag_norm_bulge = rng.random_sample(size)*6. + 18. 

148 mag_norm_agn = rng.random_sample(size)*6. + 19. 

149 redshift = rng.random_sample(size)*2.5 

150 

151 a_disk = rng.random_sample(size)*2. 

152 flatness = rng.random_sample(size)*0.8 # To prevent linear galaxies 

153 b_disk = a_disk*(1 - flatness) 

154 

155 a_bulge = rng.random_sample(size)*1.5 

156 flatness = rng.random_sample(size)*0.5 

157 b_bulge = a_bulge*(1 - flatness) 

158 

159 # assume mag norm is g-band (which is close to true) 

160 mag_norm = -2.5*np.log10(np.power(10, mag_norm_disk/-2.5) + np.power(10, mag_norm_bulge/-2.5) + 

161 np.power(10, mag_norm_agn/-2.5)) 

162 umag = mag_norm + umg 

163 gmag = mag_norm 

164 rmag = gmag - gmr 

165 imag = rmag - rmi 

166 zmag = imag - imz 

167 ymag = zmag - zmy 

168 for i in range(size): 

169 period = rng.random_sample(1)[0]*490. + 10. 

170 amp = rng.random_sample(1)[0]*5. + 0.2 

171 

172 # note that we are storing period and amp at a fixed string length 

173 # to facilitate a consistent dtype when we read them back in 

174 # via DBObject 

175 paramStr = '{"varMethodName": "testVar", "pars": {"period": %.6e, "amplitude": %.6e}}' % (period, amp) 

176 

177 qstr = '''INSERT INTO galaxies VALUES (%i, %f, %f, %f, 

178 %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, 

179 %f, %f, '%s')''' % \ 

180 (i, np.degrees(ra[i]), np.degrees(dec[i]), umag[i], gmag[i], rmag[i], imag[i], 

181 zmag[i], ymag[i], mag_norm_agn[i], mag_norm_bulge[i], mag_norm_disk[i], redshift[i], 

182 a_disk[i], b_disk[i], a_bulge[i], b_bulge[i], paramStr) 

183 

184 c.execute(qstr) 

185 

186 c.execute('''CREATE INDEX gal_ra_idx ON galaxies (ra)''') 

187 c.execute('''CREATE INDEX gal_dec_idx ON galaxies (decl)''') 

188 conn.commit() 

189 conn.close() 

190 

191 

192class myTestStars(CatalogDBObject): 

193 objid = 'teststars' 

194 tableid = 'stars' 

195 idColKey = 'id' 

196 # Make this implausibly large? 

197 appendint = 1023 

198 objectTypeId = 46 

199 driver = 'sqlite' 

200 database = 'testDatabase.db' 

201 raColName = 'ra' 

202 decColName = 'decl' 

203 columns = [('id', None, int), 

204 ('raJ2000', 'ra*%f'%(np.pi/180.)), 

205 ('decJ2000', 'decl*%f'%(np.pi/180.)), 

206 ('parallax', 'parallax*%.15f'%(np.pi/(648000000.0))), 

207 ('properMotionRa', 'properMotionRa*%.15f'%(np.pi/180)), 

208 ('properMotionDec', 'properMotionDec*%.15f'%(np.pi/180.)), 

209 ('umag', None), 

210 ('gmag', None), 

211 ('rmag', None), 

212 ('imag', None), 

213 ('zmag', None), 

214 ('ymag', None), 

215 ('magNorm', 'mag_norm', float)] 

216 

217 

218def makeStarTestDB(filename='testDatabase.db', size=1000, seedVal=None, 

219 raCenter=None, decCenter=None, radius=None, **kwargs): 

220 """ 

221 Make a test database to serve information to the myTestStars object 

222 @param size: Number of rows in the database 

223 @param seedVal: Random seed to use 

224 

225 @param raCenter,decCenter: the center of the field of view in degrees (optional) 

226 @param radius: the radius of the field of view in degrees (optional) 

227 

228 These last optional parameters exist in the event that you want to make sure 

229 that the objects are clustered around the bore site for a unit test 

230 """ 

231 conn = sqlite3.connect(filename) 

232 c = conn.cursor() 

233 try: 

234 c.execute('''CREATE TABLE stars 

235 (id int, ra real, decl real, umag real, gmag real, rmag real, 

236 imag real, zmag real, ymag real, mag_norm real, 

237 radialVelocity real, properMotionDec real, properMotionRa real, parallax real, 

238 varParamStr text, ebv real)''') 

239 conn.commit() 

240 except: 

241 raise RuntimeError("Error creating database.") 

242 

243 if seedVal is not None: 

244 rng = np.random.RandomState(seedVal) 

245 else: 

246 rng = np.random.RandomState(88) 

247 

248 if raCenter is None or decCenter is None or radius is None: 

249 ra, dec = sampleSphere(size, rng=rng, **kwargs) 

250 else: 

251 rc = np.radians(raCenter) 

252 dc = np.radians(decCenter) 

253 ra, dec = sampleFocus(size, rc, dc, radius, rng=rng) 

254 

255 # Typical colors 

256 umg = 1.5 

257 gmr = 0.65 

258 rmi = 1.0 

259 imz = 0.45 

260 zmy = 0.3 

261 mag_norm = rng.random_sample(size)*6. + 18. 

262 # assume mag norm is g-band (which is close to true) 

263 umag = mag_norm + umg 

264 gmag = mag_norm 

265 rmag = gmag - gmr 

266 imag = rmag - rmi 

267 zmag = imag - imz 

268 ymag = zmag - zmy 

269 radVel = rng.random_sample(size)*50. - 25. 

270 pmRa = rng.random_sample(size)*4./(1000*3600.) # deg/yr 

271 pmDec = rng.random_sample(size)*4./(1000*3600.) # deg/yr 

272 parallax = rng.random_sample(size)*1.0 # milliarcseconds per year 

273 ebv = rng.random_sample(size)*3.0 

274 for i in range(size): 

275 period = rng.random_sample(1)[0]*490. + 10. 

276 amp = rng.random_sample(1)[0]*5. + 0.2 

277 

278 # note that we are storing period and amp at a fixed string length 

279 # to facilitate a consistent dtype when we read them back in 

280 # via DBObject 

281 paramStr = '{"varMethodName": "testVar", "pars": {"period": %.6e, "amplitude": %.6e}}' % (period, amp) 

282 

283 qstr = '''INSERT INTO stars VALUES (%i, %f, %f, %f, %f, %f, %f, 

284 %f, %f, %f, %f, %.15f, %.15f, %.15f, '%s', %f)''' % \ 

285 (i, np.degrees(ra[i]), np.degrees(dec[i]), umag[i], gmag[i], rmag[i], 

286 imag[i], zmag[i], ymag[i], mag_norm[i], radVel[i], pmRa[i], pmDec[i], parallax[i], 

287 paramStr, ebv[i]) 

288 

289 c.execute(qstr) 

290 

291 c.execute('''CREATE INDEX star_ra_idx ON stars (ra)''') 

292 c.execute('''CREATE INDEX star_dec_idx ON stars (decl)''') 

293 conn.commit() 

294 conn.close()