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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

from builtins import range 

import numpy as np 

import os 

import sqlite3 

import json 

import traceback 

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

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

raDecFromAltAz, haversine, ObservationMetaData) 

 

 

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

 

 

def failedOnFatboy(tracebackList): 

""" 

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

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

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

was probably due to fatboy connectivity. 

""" 

22 ↛ 23line 22 didn't jump to line 23, because the condition on line 22 was never true if not isinstance(tracebackList, list): 

return False 

 

lastSimsDex = -1 

for ix, item in enumerate(tracebackList): 

27 ↛ 29line 27 didn't jump to line 29, because the condition on line 27 was never true if (not isinstance(item, tuple) and 

not isinstance(item, traceback.FrameSummary)): 

return False 

 

31 ↛ 26line 31 didn't jump to line 26, because the condition on line 31 was never false if 'sims' in item[0]: 

lastSimsDex = ix 

 

34 ↛ 35line 34 didn't jump to line 35, because the condition on line 34 was never true if lastSimsDex < 0: 

return False 

 

37 ↛ 40line 37 didn't jump to line 40, because the condition on line 37 was never false if '_connect_to_engine' in tracebackList[lastSimsDex][2]: 

return True 

 

return False 

 

 

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

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

""" 

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

 

Parameters 

---------- 

sedName is a string specifying the file name of the SED 

 

magNorm is the normalizing magnitude of the SED in the imsimBandpass 

 

redshift is the redshift of the SED 

 

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

 

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

 

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

 

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

 

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

 

Returns 

------- 

A float representing the number of ADUs measured in the bandpass 

""" 

 

imsimband = Bandpass() 

imsimband.imsimBandpass() 

sed = Sed() 

sed.readSED_flambda(sedName) 

fNorm = sed.calcFluxNorm(magNorm, imsimband) 

sed.multiplyFluxNorm(fNorm) 

if internalAv is not None and internalRv is not None: 

if internalAv != 0.0 and internalRv != 0.0: 

a_int, b_int = sed.setupCCMab() 

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

 

if redshift is not None and redshift != 0.0: 

sed.redshiftSED(redshift, dimming=True) 

 

a_int, b_int = sed.setupCCMab() 

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

 

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

 

return adu 

 

 

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

deltaRA=None, deltaDec=None, 

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

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

""" 

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

catalog to use. 

 

The method will return an ObservationMetaData object guaranteed to encompass the 

objects in this database. 

 

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

 

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

 

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

 

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

 

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

ObservationMetaData (optional) 

 

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

(optional) 

 

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

ObservationMetaData (optional) 

 

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

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

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

will be set randomly. These parameters are optional. 

 

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

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

 

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

""" 

 

132 ↛ 133line 132 didn't jump to line 133, because the condition on line 132 was never true if os.path.exists(filename): 

os.unlink(filename) 

 

# just an example of some valid SED file names 

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

agn_sed = 'agn.spec' 

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

 

rng = np.random.RandomState(seedVal) 

 

142 ↛ 143line 142 didn't jump to line 143, because the condition on line 142 was never true if deltaRA is not None and deltaDec is not None: 

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

raise RuntimeError("WARNING in makePhoSimTestDB deltaRA and " 

"deltaDec have different lengths") 

 

147 ↛ 148line 147 didn't jump to line 148, because the condition on line 147 was never true if deltaRA is not None: 

size = len(deltaRA) 

149 ↛ 150line 149 didn't jump to line 150, because the condition on line 149 was never true elif deltaDec is not None: 

size = len(deltaDec) 

 

# create the ObservationMetaData object 

mjd = 52000.0 

alt = np.pi/2.0 

az = 0.0 

 

testSite = Site(name='LSST') 

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

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

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

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

 

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

pointingDec=np.degrees(centerDec), 

rotSkyPos=np.degrees(rotSkyPos), 

bandpassName=bandpass, 

mjd=mjd, 

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

site=testSite, 

m5=m5, seeing=seeing) 

 

moon_alt = -90.0 

sun_alt = -90.0 

 

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

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

obs_metadata._pointingRA, obs_metadata._pointingDec) 

 

obs_metadata.OpsimMetaData = {'moonra': moon_ra, 

'moondec': moon_dec, 

'moonalt': moon_alt, 

'sunalt': sun_alt, 

'dist2moon': dist2moon, 

'rottelpos': np.degrees(rotTel)} 

 

# Now begin building the database. 

# First create the tables. 

conn = sqlite3.connect(filename) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE galaxy_bulge 

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

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

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

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

conn.commit() 

except: 

raise RuntimeError("Error creating galaxy_bulge table.") 

 

try: 

c.execute('''CREATE TABLE galaxy 

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

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

agnra real, agndec real, 

magnorm_bulge, magnorm_disk, magnorm_agn, 

sedname_bulge text, sedname_disk text, sedname_agn text, 

varParamStr text, 

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

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

ext_model_b text, av_b real, rv_b real, 

ext_model_d text, av_d real, rv_d real, 

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

z_ab real, y_ab real, 

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

 

conn.commit() 

except: 

raise RuntimeError("Error creating galaxy table.") 

 

try: 

c.execute('''CREATE TABLE galaxy_agn 

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

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

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

except: 

raise RuntimeError("Error creating galaxy_agn table.") 

 

try: 

c.execute('''CREATE TABLE StarAllForceseek 

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

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

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

except: 

raise RuntimeError("Error creating StarAllForceseek table.") 

 

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

 

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

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

 

241 ↛ 244line 241 didn't jump to line 244, because the condition on line 241 was never false if deltaRA is None: 

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

else: 

ra = np.degrees(centerRA) + deltaRA 

 

246 ↛ 249line 246 didn't jump to line 249, because the condition on line 246 was never false if deltaDec is None: 

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

else: 

dec = np.degrees(centerDec) + deltaDec 

 

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

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

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

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

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

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

 

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

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

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

b_b = rng.random_sample(size)*0.2 

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

b_d = rng.random_sample(size)*0.5 

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

 

BulgeHalfLightRadius = rng.random_sample(size)*0.2 

DiskHalfLightRadius = rng.random_sample(size)*0.5 

 

pa_bulge = rng.random_sample(size)*360.0 

pa_disk = rng.random_sample(size)*360.0 

 

av_b = rng.random_sample(size)*0.3 

av_d = rng.random_sample(size)*0.3 

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

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

 

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

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

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

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

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

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

redshift = rng.random_sample(size)*2.0 

 

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

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

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

agn_sfu = rng.random_sample(size) 

agn_sfg = rng.random_sample(size) 

agn_sfr = rng.random_sample(size) 

agn_sfi = rng.random_sample(size) 

agn_sfz = rng.random_sample(size) 

agn_sfy = rng.random_sample(size) 

 

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

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

 

298 ↛ 301line 298 didn't jump to line 301, because the condition on line 298 was never false if deltaRA is None: 

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

else: 

raStar = centerRA + np.radians(deltaRA) 

 

303 ↛ 306line 303 didn't jump to line 306, because the condition on line 303 was never false if deltaDec is None: 

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

else: 

decStar = centerDec + np.radians(deltaDec) 

 

raStar = np.degrees(raStar) 

decStar = np.degrees(decStar) 

 

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

mudecl = rng.random_sample(size)*0.0001 

mura = rng.random_sample(size)*0.0001 

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

vrad = rng.random_sample(size)*1.0 

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

period = rng.random_sample(size)*20.0 

amp = rng.random_sample(size)*5.0 

 

# write the data to the tables. 

for i in range(size): 

 

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

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

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

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

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

 

c.execute(cmd) 

 

varParam = {'varMethodName': 'applyAgn', 

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

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

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

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

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

 

paramStr = json.dumps(varParam) 

 

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

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

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

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

'%s', %f, %f, 

'%s', %f, %f, 

%f, %f, %f, %f, %f, %f, 

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

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

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

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

paramStr, 

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

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

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

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

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

BulgeHalfLightRadius[i], DiskHalfLightRadius[i]) 

c.execute(cmd) 

 

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

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

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

magnorm_agn[i], agn_sed, paramStr, 

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

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

 

c.execute(cmd) 

 

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

paramStr = json.dumps(varParam) 

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

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

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

galacticAv[i]/3.1) 

 

c.execute(cmd) 

 

conn.commit() 

conn.close() 

return obs_metadata