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

from builtins import next 

from builtins import str 

from builtins import range 

import sqlite3 

import numpy as np 

 

from lsst.sims.catalogs.db import CatalogDBObject 

 

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

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

 

 

def getOneChunk(results): 

try: 

chunk = next(results) 

except StopIteration: 

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

" test database") 

return chunk 

 

 

def writeResult(result, fname): 

fh = open(fname, 'w') 

first = True 

for chunk in result: 

if first: 

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

first = False 

for i in range(len(chunk)): 

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

fh.close() 

 

 

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

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

if rng is None: 

rng = np.random.RandomState(42) 

 

ra = rng.random_sample(size)*dra 

ra += ramin 

ra %= 2*np.pi 

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

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

return ra, dec 

 

 

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

""" 

Sample points in a focused field of view 

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

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

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

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

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

""" 

if rng is None: 

rng = np.random.RandomState(1453) 

 

theta = rng.random_sample(size) 

rc = np.radians(raCenter) 

dc = np.radians(decCenter) 

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

ra = np.empty(size) 

dec = np.empty(size) 

for i, th in enumerate(theta): 

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

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

 

return ra, dec 

 

 

class myTestGals(CatalogDBObject): 

objid = 'testgals' 

tableid = 'galaxies' 

idColKey = 'id' 

# Make this implausibly large? 

appendint = 1022 

objectTypeId = 45 

driver = 'sqlite' 

database = 'testDatabase.db' 

raColName = 'ra' 

decColName = 'decl' 

spatialModel = 'SERSIC2D' 

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

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

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

('umag', None), 

('gmag', None), 

('rmag', None), 

('imag', None), 

('zmag', None), 

('ymag', None), 

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

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

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

('redshift', None), 

('a_disk', None), 

('b_disk', None), 

('a_bulge', None), 

('b_bulge', None)] 

 

 

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

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

""" 

Make a test database to serve information to the myTestGals object 

@param size: Number of rows in the database 

@param seedVal: Random seed to use 

 

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

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

 

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

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

""" 

conn = sqlite3.connect(filename) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE galaxies 

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

imag real, zmag real, ymag real, 

mag_norm_agn real, mag_norm_bulge real, mag_norm_disk real, 

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

conn.commit() 

except: 

raise RuntimeError("Error creating database.") 

 

if seedVal is not None: 

rng = np.random.RandomState(seedVal) 

else: 

rng = np.random.RandomState(3321) 

 

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

 

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

else: 

rc = np.radians(raCenter) 

dc = np.radians(decCenter) 

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

# Typical colors for main sequece stars 

umg = 1.5 

gmr = 0.65 

rmi = 1.0 

imz = 0.45 

zmy = 0.3 

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

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

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

redshift = rng.random_sample(size)*2.5 

 

a_disk = rng.random_sample(size)*2. 

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

b_disk = a_disk*(1 - flatness) 

 

a_bulge = rng.random_sample(size)*1.5 

flatness = rng.random_sample(size)*0.5 

b_bulge = a_bulge*(1 - flatness) 

 

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

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

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

umag = mag_norm + umg 

gmag = mag_norm 

rmag = gmag - gmr 

imag = rmag - rmi 

zmag = imag - imz 

ymag = zmag - zmy 

for i in range(size): 

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

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

 

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

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

# via DBObject 

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

 

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

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

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

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

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

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

 

c.execute(qstr) 

 

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

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

conn.commit() 

conn.close() 

 

 

class myTestStars(CatalogDBObject): 

objid = 'teststars' 

tableid = 'stars' 

idColKey = 'id' 

# Make this implausibly large? 

appendint = 1023 

objectTypeId = 46 

driver = 'sqlite' 

database = 'testDatabase.db' 

raColName = 'ra' 

decColName = 'decl' 

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

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

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

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

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

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

('umag', None), 

('gmag', None), 

('rmag', None), 

('imag', None), 

('zmag', None), 

('ymag', None), 

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

 

 

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

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

""" 

Make a test database to serve information to the myTestStars object 

@param size: Number of rows in the database 

@param seedVal: Random seed to use 

 

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

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

 

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

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

""" 

conn = sqlite3.connect(filename) 

c = conn.cursor() 

try: 

c.execute('''CREATE TABLE stars 

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

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

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

varParamStr text, ebv real)''') 

conn.commit() 

except: 

raise RuntimeError("Error creating database.") 

 

if seedVal is not None: 

rng = np.random.RandomState(seedVal) 

else: 

rng = np.random.RandomState(88) 

 

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

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

else: 

rc = np.radians(raCenter) 

dc = np.radians(decCenter) 

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

 

# Typical colors 

umg = 1.5 

gmr = 0.65 

rmi = 1.0 

imz = 0.45 

zmy = 0.3 

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

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

umag = mag_norm + umg 

gmag = mag_norm 

rmag = gmag - gmr 

imag = rmag - rmi 

zmag = imag - imz 

ymag = zmag - zmy 

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

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

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

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

ebv = rng.random_sample(size)*3.0 

for i in range(size): 

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

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

 

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

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

# via DBObject 

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

 

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

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

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

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

paramStr, ebv[i]) 

 

c.execute(qstr) 

 

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

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

conn.commit() 

conn.close()