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 range 

2import numpy as np 

3import os 

4import sqlite3 

5import json 

6import traceback 

7from lsst.sims.photUtils import Bandpass, Sed, PhotometricParameters 

8from lsst.sims.utils import (_raDecFromAltAz, _getRotSkyPos, _getRotTelPos, Site, 

9 raDecFromAltAz, haversine, ObservationMetaData) 

10 

11 

12__all__ = ["calcADUwrapper", "makePhoSimTestDB", "failedOnFatboy"] 

13 

14 

15def failedOnFatboy(tracebackList): 

16 """ 

17 Accepts a list generated by traceback.extract_tb; determines if the last 

18 point in the sims code in the traceback is _connect_to_engine (from 

19 sims_catalogs_generation/../db/dbConnection.py), in which case, the failure 

20 was probably due to fatboy connectivity. 

21 """ 

22 if not isinstance(tracebackList, list): 

23 return False 

24 

25 lastSimsDex = -1 

26 for ix, item in enumerate(tracebackList): 

27 if (not isinstance(item, tuple) and 

28 not isinstance(item, traceback.FrameSummary)): 

29 return False 

30 

31 if 'sims' in item[0]: 

32 lastSimsDex = ix 

33 

34 if lastSimsDex < 0: 

35 return False 

36 

37 if '_connect_to_engine' in tracebackList[lastSimsDex][2]: 

38 return True 

39 

40 return False 

41 

42 

43def calcADUwrapper(sedName=None, magNorm=None, redshift=None, internalAv=None, internalRv=None, 

44 galacticAv=None, galacticRv=None, bandpass=None): 

45 """ 

46 Read in an SED and calculat the number of ADU produced by that SED in a specified bandpass 

47 

48 Parameters 

49 ---------- 

50 sedName is a string specifying the file name of the SED 

51 

52 magNorm is the normalizing magnitude of the SED in the imsimBandpass 

53 

54 redshift is the redshift of the SED 

55 

56 internalAv is the Av due to internal dust of the source (if a galaxy) 

57 

58 internalRv is the Rv due to internal dust of the source (if a galaxy) 

59 

60 galacticAv is the Av due to Milky Way dust between observer and source 

61 

62 galacticRv is the Rv due to Milky Way dust between observer and source 

63 

64 bandpass is an intantiation of Bandpass representing the band in which the ADUs are measured 

65 

66 Returns 

67 ------- 

68 A float representing the number of ADUs measured in the bandpass 

69 """ 

70 

71 imsimband = Bandpass() 

72 imsimband.imsimBandpass() 

73 sed = Sed() 

74 sed.readSED_flambda(sedName) 

75 fNorm = sed.calcFluxNorm(magNorm, imsimband) 

76 sed.multiplyFluxNorm(fNorm) 

77 if internalAv is not None and internalRv is not None: 

78 if internalAv != 0.0 and internalRv != 0.0: 

79 a_int, b_int = sed.setupCCM_ab() 

80 sed.addDust(a_int, b_int, A_v=internalAv, R_v=internalRv) 

81 

82 if redshift is not None and redshift != 0.0: 

83 sed.redshiftSED(redshift, dimming=True) 

84 

85 a_int, b_int = sed.setupCCM_ab() 

86 sed.addDust(a_int, b_int, A_v=galacticAv, R_v=galacticRv) 

87 

88 adu = sed.calcADU(bandpass, photParams=PhotometricParameters()) 

89 

90 return adu 

91 

92 

93def makePhoSimTestDB(filename='PhoSimTestDatabase.db', size=1000, seedVal=32, radius=0.1, 

94 deltaRA=None, deltaDec=None, 

95 bandpass='r', m5=None, seeing=None, 

96 magnorm_min=17.0, delta_magnorm=4.0, **kwargs): 

97 """ 

98 Make a test database to storing cartoon information for the test phoSim input 

99 catalog to use. 

100 

101 The method will return an ObservationMetaData object guaranteed to encompass the 

102 objects in this database. 

103 

104 @param [in] filename is a string indicating the name of the DB file to be created 

105 

106 @param [in] size is the number of objects int he database 

107 

108 @param [in] seedVal is the seed passed to the random number generator 

109 

110 @param [in] radius is the radius (in degrees) of the field of view to be returned 

111 

112 @param [in] bandpass is the bandpas(es) of the observation to be passed to 

113 ObservationMetaData (optional) 

114 

115 @param [in] m5 is the m5 value(s) to be passed to ObservationMetaData 

116 (optional) 

117 

118 @param [in] seeing is the seeing value(s) in arcseconds to be passed to 

119 ObservationMetaData (optional) 

120 

121 @param [in] deltaRA/Dec are numpy arrays that indicate where (in relation to the center 

122 of the field of view) objects should be placed. These coordinates are in degrees. Specifying 

123 either of these paramters will overwrite size. If you only specify one of these parameters, the other 

124 will be set randomly. These parameters are optional. 

125 

126 @param [in] magnorm_min is the min magnorm (magNorms for sources in the database will be 

127 distributed according to a random deviate drawn from magnorm_min + random*delta_magnorm) 

128 

129 @param [in] delta_magnorm (see documentation for magnorm_min) 

130 """ 

131 

132 if os.path.exists(filename): 

133 os.unlink(filename) 

134 

135 # just an example of some valid SED file names 

136 galaxy_seds = ['Const.80E07.02Z.spec', 'Inst.80E07.002Z.spec', 'Burst.19E07.0005Z.spec'] 

137 agn_sed = 'agn.spec' 

138 star_seds = ['km20_5750.fits_g40_5790', 'm2.0Full.dat', 'bergeron_6500_85.dat_6700'] 

139 

140 rng = np.random.RandomState(seedVal) 

141 

142 if deltaRA is not None and deltaDec is not None: 

143 if len(deltaRA) != len(deltaDec): 

144 raise RuntimeError("WARNING in makePhoSimTestDB deltaRA and " 

145 "deltaDec have different lengths") 

146 

147 if deltaRA is not None: 

148 size = len(deltaRA) 

149 elif deltaDec is not None: 

150 size = len(deltaDec) 

151 

152 # create the ObservationMetaData object 

153 mjd = 52000.0 

154 alt = np.pi/2.0 

155 az = 0.0 

156 

157 testSite = Site(name='LSST') 

158 obsTemp = ObservationMetaData(mjd=mjd, site=testSite) 

159 centerRA, centerDec = _raDecFromAltAz(alt, az, obsTemp) 

160 rotTel = _getRotTelPos(centerRA, centerDec, obsTemp, 0.0) 

161 rotSkyPos = _getRotSkyPos(centerRA, centerDec, obsTemp, rotTel) 

162 

163 obs_metadata = ObservationMetaData(pointingRA=np.degrees(centerRA), 

164 pointingDec=np.degrees(centerDec), 

165 rotSkyPos=np.degrees(rotSkyPos), 

166 bandpassName=bandpass, 

167 mjd=mjd, 

168 boundType = 'circle', boundLength = 2.0*radius, 

169 site=testSite, 

170 m5=m5, seeing=seeing) 

171 

172 moon_alt = -90.0 

173 sun_alt = -90.0 

174 

175 moon_ra, moon_dec = raDecFromAltAz(moon_alt, 0.0, obs_metadata) 

176 dist2moon = haversine(np.radians(moon_ra), np.radians(moon_dec), 

177 obs_metadata._pointingRA, obs_metadata._pointingDec) 

178 

179 obs_metadata.OpsimMetaData = {'moonra': moon_ra, 

180 'moondec': moon_dec, 

181 'moonalt': moon_alt, 

182 'sunalt': sun_alt, 

183 'dist2moon': dist2moon, 

184 'rottelpos': np.degrees(rotTel)} 

185 

186 # Now begin building the database. 

187 # First create the tables. 

188 conn = sqlite3.connect(filename) 

189 c = conn.cursor() 

190 try: 

191 c.execute('''CREATE TABLE galaxy_bulge 

192 (galtileid int, galid int, bra real, bdec real, ra real, dec real, magnorm_bulge real, 

193 sedname_bulge text, a_b real, b_b real, pa_bulge real, bulge_n int, 

194 ext_model_b text, av_b real, rv_b real, u_ab real, g_ab real, r_ab real, i_ab real, 

195 z_ab real, y_ab real, redshift real, BulgeHalfLightRadius real)''') 

196 conn.commit() 

197 except: 

198 raise RuntimeError("Error creating galaxy_bulge table.") 

199 

200 try: 

201 c.execute('''CREATE TABLE galaxy 

202 (galtileid int, galid int, ra real, dec real, 

203 bra real, bdec real, dra real, ddec real, 

204 agnra real, agndec real, 

205 magnorm_bulge, magnorm_disk, magnorm_agn, 

206 sedname_bulge text, sedname_disk text, sedname_agn text, 

207 varParamStr text, 

208 a_b real, b_b real, pa_bulge real, bulge_n int, 

209 a_d real, b_d real, pa_disk real, disk_n int, 

210 ext_model_b text, av_b real, rv_b real, 

211 ext_model_d text, av_d real, rv_d real, 

212 u_ab real, g_ab real, r_ab real, i_ab real, 

213 z_ab real, y_ab real, 

214 redshift real, BulgeHalfLightRadius real, DiskHalfLightRadius real)''') 

215 

216 conn.commit() 

217 except: 

218 raise RuntimeError("Error creating galaxy table.") 

219 

220 try: 

221 c.execute('''CREATE TABLE galaxy_agn 

222 (galtileid int, galid int, agnra real, agndec real, ra real, dec real, 

223 magnorm_agn real, sedname_agn text, varParamStr text, u_ab real, 

224 g_ab real, r_ab real, i_ab real, z_ab real, y_ab real, redshift real)''') 

225 except: 

226 raise RuntimeError("Error creating galaxy_agn table.") 

227 

228 try: 

229 c.execute('''CREATE TABLE StarAllForceseek 

230 (simobjid int, ra real, decl real, magNorm real, 

231 mudecl real, mura real, galacticAv real, vrad real, varParamStr text, 

232 sedFilename text, parallax real, ebv real)''') 

233 except: 

234 raise RuntimeError("Error creating StarAllForceseek table.") 

235 

236 # Now generate the data to be stored in the tables. 

237 

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

239 theta = rng.random_sample(size)*2.0*np.pi 

240 

241 if deltaRA is None: 

242 ra = np.degrees(centerRA + rr*np.cos(theta)) 

243 else: 

244 ra = np.degrees(centerRA) + deltaRA 

245 

246 if deltaDec is None: 

247 dec = np.degrees(centerDec + rr*np.sin(theta)) 

248 else: 

249 dec = np.degrees(centerDec) + deltaDec 

250 

251 bra = np.radians(ra+rng.random_sample(size)*0.01*radius) 

252 bdec = np.radians(dec+rng.random_sample(size)*0.01*radius) 

253 dra = np.radians(ra + rng.random_sample(size)*0.01*radius) 

254 ddec = np.radians(dec + rng.random_sample(size)*0.01*radius) 

255 agnra = np.radians(ra + rng.random_sample(size)*0.01*radius) 

256 agndec = np.radians(dec + rng.random_sample(size)*0.01*radius) 

257 

258 magnorm_bulge = rng.random_sample(size)*delta_magnorm + magnorm_min 

259 magnorm_disk = rng.random_sample(size)*delta_magnorm + magnorm_min 

260 magnorm_agn = rng.random_sample(size)*delta_magnorm + magnorm_min 

261 b_b = rng.random_sample(size)*0.2 

262 a_b = b_b+rng.random_sample(size)*0.05 

263 b_d = rng.random_sample(size)*0.5 

264 a_d = b_d+rng.random_sample(size)*0.1 

265 

266 BulgeHalfLightRadius = rng.random_sample(size)*0.2 

267 DiskHalfLightRadius = rng.random_sample(size)*0.5 

268 

269 pa_bulge = rng.random_sample(size)*360.0 

270 pa_disk = rng.random_sample(size)*360.0 

271 

272 av_b = rng.random_sample(size)*0.3 

273 av_d = rng.random_sample(size)*0.3 

274 rv_b = rng.random_sample(size)*0.1 + 2.0 

275 rv_d = rng.random_sample(size)*0.1 + 2.0 

276 

277 u_ab = rng.random_sample(size)*4.0 + 17.0 

278 g_ab = rng.random_sample(size)*4.0 + 17.0 

279 r_ab = rng.random_sample(size)*4.0 + 17.0 

280 i_ab = rng.random_sample(size)*4.0 + 17.0 

281 z_ab = rng.random_sample(size)*4.0 + 17.0 

282 y_ab = rng.random_sample(size)*4.0 + 17.0 

283 redshift = rng.random_sample(size)*2.0 

284 

285 t0_mjd = mjd - rng.random_sample(size)*1000.0 

286 agn_tau = rng.random_sample(size)*1000.0 + 1000.0 

287 agnSeed = rng.randint(2, 4001, size=size) 

288 agn_sfu = rng.random_sample(size) 

289 agn_sfg = rng.random_sample(size) 

290 agn_sfr = rng.random_sample(size) 

291 agn_sfi = rng.random_sample(size) 

292 agn_sfz = rng.random_sample(size) 

293 agn_sfy = rng.random_sample(size) 

294 

295 rrStar = rng.random_sample(size)*np.radians(radius) 

296 thetaStar = rng.random_sample(size)*2.0*np.pi 

297 

298 if deltaRA is None: 

299 raStar = centerRA + rrStar*np.cos(thetaStar) 

300 else: 

301 raStar = centerRA + np.radians(deltaRA) 

302 

303 if deltaDec is None: 

304 decStar = centerDec + rrStar*np.sin(thetaStar) 

305 else: 

306 decStar = centerDec + np.radians(deltaDec) 

307 

308 raStar = np.degrees(raStar) 

309 decStar = np.degrees(decStar) 

310 

311 magnormStar = rng.random_sample(size)*delta_magnorm + magnorm_min 

312 mudecl = rng.random_sample(size)*0.0001 

313 mura = rng.random_sample(size)*0.0001 

314 galacticAv = rng.random_sample(size)*0.05*3.1 

315 vrad = rng.random_sample(size)*1.0 

316 parallax = 0.00045+rng.random_sample(size)*0.00001 

317 period = rng.random_sample(size)*20.0 

318 amp = rng.random_sample(size)*5.0 

319 

320 # write the data to the tables. 

321 for i in range(size): 

322 

323 cmd = '''INSERT INTO galaxy_bulge VALUES (%i, %i, %f, %f, %f, %f, %f, 

324 '%s', %f, %f, %f, %i, '%s', %f, %f, %f, %f, %f, %f, %f, %f, %f, %f)''' % \ 

325 (i, i, bra[i], bdec[i], ra[i], dec[i], magnorm_bulge[i], galaxy_seds[i%len(galaxy_seds)], 

326 a_b[i], b_b[i], pa_bulge[i], 4, 'CCM', av_b[i], rv_b[i], u_ab[i], g_ab[i], 

327 r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], BulgeHalfLightRadius[i]) 

328 

329 c.execute(cmd) 

330 

331 varParam = {'varMethodName': 'applyAgn', 

332 'pars': {'agn_tau': round(agn_tau[i], 4), 't0_mjd': round(t0_mjd[i], 4), 

333 'agn_sfu': round(agn_sfu[i], 4), 'agn_sfg': round(agn_sfg[i], 4), 

334 'agn_sfr': round(agn_sfr[i], 4), 'agn_sfi': round(agn_sfi[i], 4), 

335 'agn_sfz': round(agn_sfz[i], 4), 'agn_sfy': round(agn_sfy[i], 4), 

336 'seed': int(agnSeed[i])}} 

337 

338 paramStr = json.dumps(varParam) 

339 

340 cmd = '''INSERT INTO galaxy VALUES (%i, %i, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, 

341 '%s', '%s', '%s', '%s', 

342 %f, %f, %f, %i, 

343 %f, %f, %f, %i, 

344 '%s', %f, %f, 

345 '%s', %f, %f, 

346 %f, %f, %f, %f, %f, %f, 

347 %f, %f, %f)''' % \ 

348 (i, i, ra[i], dec[i], bra[i], bdec[i], dra[i], ddec[i], agnra[i], agndec[i], 

349 magnorm_bulge[i], magnorm_disk[i], magnorm_agn[i], 

350 galaxy_seds[i%len(galaxy_seds)], galaxy_seds[i%len(galaxy_seds)], agn_sed, 

351 paramStr, 

352 a_b[i], b_b[i], pa_bulge[i], 4, 

353 a_d[i], b_d[i], pa_disk[i], 1, 

354 'CCM', av_b[i], rv_b[i], 

355 'CCM', av_d[i], rv_d[i], 

356 u_ab[i], g_ab[i], r_ab[i], i_ab[i], z_ab[i], y_ab[i], redshift[i], 

357 BulgeHalfLightRadius[i], DiskHalfLightRadius[i]) 

358 c.execute(cmd) 

359 

360 cmd = '''INSERT INTO galaxy_agn VALUES (%i, %i, %f, %f, %f, %f, %f, '%s', '%s', 

361 %f, %f, %f, %f, %f, %f, %f)''' % \ 

362 (i, i, agnra[i], agndec[i], ra[i], dec[i], 

363 magnorm_agn[i], agn_sed, paramStr, 

364 u_ab[i], g_ab[i], r_ab[i], i_ab[i], 

365 z_ab[i], y_ab[i], redshift[i]) 

366 

367 c.execute(cmd) 

368 

369 varParam = {'varMethodName': 'testVar', 'pars': {'period': period[i], 'amplitude': amp[i]}} 

370 paramStr = json.dumps(varParam) 

371 cmd = '''INSERT INTO StarAllForceseek VALUES (%i, %f, %f, %f, %f, %f, %f, %f, '%s', '%s', %f, %f)''' %\ 

372 (i, raStar[i], decStar[i], magnormStar[i], mudecl[i], mura[i], 

373 galacticAv[i], vrad[i], paramStr, star_seds[i%len(star_seds)], parallax[i], 

374 galacticAv[i]/3.1) 

375 

376 c.execute(cmd) 

377 

378 conn.commit() 

379 conn.close() 

380 return obs_metadata