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

381

382

383

384

385

386

387

388

389

390

#!/usr/local/bin/python 

# encoding: utf-8 

""" 

*Generate a catalogue of fake transient sources for GW* 

""" 

################# GLOBAL IMPORTS #################### 

import sys 

import os 

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

from HMpTy import htm 

from fundamentals import tools, times 

from fundamentals.mysql import readquery, table_exists, writequery 

from HMpTy.mysql import add_htm_ids_to_mysql_database_table 

import numpy as np 

import math 

import csv 

 

 

class generate_faker_catalogue(): 

""" 

*Generate a text file with fake transients locations and magnitudes given a PS1 exposure ID and a gravitational wave ID* 

 

**Key Arguments:** 

- ``log`` -- logger 

- ``settings`` -- the settings dictionary 

- ``gwid`` -- the gravitational wave ID 

- ``ps1ExpId`` -- the PS1 footprint ID 

 

**Usage:** 

 

To populate the associated galaxies with fake sources and writing faker results to file run the code: 

 

.. code-block:: python  

 

from breaker import fakers 

fakecat = fakers.generate_faker_catalogue( 

log=log, 

settings=settings, 

ps1ExpId=73283973, 

gwid="G211117" 

).get()  

 

The location of the output faker file is determined from the ``output directory`` setting in the settings file: 

 

.. code-block:: yaml  

 

output directory: "/Users/Dave/Desktop" 

""" 

 

def __init__( 

self, 

log, 

gwid, 

settings=False, 

ps1ExpId=False 

): 

self.log = log 

log.debug("instansiating a new 'generate_faker_catalogue' object") 

self.settings = settings 

self.gwid = gwid 

self.ps1ExpId = ps1ExpId 

# xt-self-arg-tmpx 

 

# Initial Actions 

# CONNECT TO THE VARIOUS DATABASES REQUIRED 

from breaker import database 

db = database( 

log=self.log, 

settings=self.settings 

) 

self.ligo_virgo_wavesDbConn, self.ps1gwDbConn, self.cataloguesDbConn, self.atlasDbConn, self.ps13piDbConn = db.get() 

 

return None 

 

def get(self): 

""" 

*Generate the fake transient source file* 

 

**Return:** 

- None 

""" 

self.log.info('starting the ``get`` method') 

 

self.generate_faker_locations_within_galaxies_found_in_survey_footprint() 

self.write_fakers_to_file() 

 

self.log.info('completed the ``get`` method') 

return None 

 

def write_fakers_to_file( 

self): 

""" 

*Write out the associated fake transient sources to file, alongside 15% extra transients added blindly throughout the survey field* 

 

.. warning:: 

 

The code will fail if sherlock has not yet been run against the survey areas for the give GW ID. 

 

**Usage:** 

 

.. code-block:: python  

 

from breaker import fakers 

fakecat = fakers.generate_faker_catalogue( 

log=log, 

settings=settings, 

ps1ExpId=73283973, 

gwid="G211117" 

).write_fakers_to_file()  

""" 

self.log.info('starting the ``write_fakers_to_file`` method') 

 

mesh16 = htm.HTM(16) 

theseArrays = [] 

radius = 1.45 

ra = [] 

dec = [] 

 

gwid = self.gwid 

ps1ExpId = self.ps1ExpId 

 

if gwid: 

gwidClause = 'and gw_id = "%(gwid)s"' % locals() 

else: 

gwidClause = "" 

 

# SELECT THE BORE-SIGHT FOR THE PS1 EXPOSURE 

sqlQuery = u""" 

select raDeg, decDeg, gw_id from ps1_pointings where 1=1 %(gwidClause)s and ps1_exp_id = %(ps1ExpId)s 

""" % locals() 

rows = readquery( 

log=self.log, 

sqlQuery=sqlQuery, 

dbConn=self.ligo_virgo_wavesDbConn 

) 

raFP = rows[0]["raDeg"] 

decFP = rows[0]["decDeg"] 

gwid = rows[0]["gw_id"] 

 

# GENERATE AN ARRAY OF HTM IDs FOR THE FOV 

thisArray = mesh16.intersect( 

raFP, decFP, radius, inclusive=True) 

hmax = thisArray.max() 

hmin = thisArray.min() 

ratio = float(hmax - hmin + 1) / float(thisArray.size) 

if ratio < 100 or thisArray.size > 2000: 

htmWhereClause = "where htm16ID between %(hmin)s and %(hmax)s" % locals( 

) 

else: 

s = StringIO() 

np.savetxt(s, thisArray, fmt='%d', newline=",") 

thesHtmIds = s.getvalue()[:-1] 

htmWhereClause = "where htm16ID in (%(thesHtmIds)s)" % locals() 

 

# TEST TABLE EXIST 

thisTableExists = table_exists( 

dbConn=self.ligo_virgo_wavesDbConn, 

log=self.log, 

dbTableName="tcs_%(gwid)s_catalogued_sources" % locals() 

) 

if not thisTableExists: 

self.log.error( 

'tcs_%(gwid)s_catalogued_sources table does not exist in LV database - need to run sherlock to create this table' % locals()) 

return 

 

# ADD THE HTMIDs TO THE RELEVANT CATALOGUE SOURCES TABLE BEFORE 

# QUERYING 

add_htm_ids_to_mysql_database_table( 

raColName="catalogue_object_ra", 

declColName="catalogue_object_dec", 

tableName="tcs_%(gwid)s_catalogued_sources" % locals(), 

dbConn=self.ligo_virgo_wavesDbConn, 

log=self.log, 

primaryIdColumnName="id", 

batchSize=50000 

) 

 

# FINALLY SELECT THE FAKER DETAILS FROM FOV 

sqlQuery = """select faker_ra, faker_dec, z, catalogue_object_id, catalogue_object_subtype, 2mass_k_mag, 2mass_k_mag_error from tcs_%(gwid)s_catalogued_sources %(htmWhereClause)s and z is not null and z < 0.15 and (z_quality is null or z_quality not like 'PHOT%%') and (catalogue_object_subtype is null or catalogue_object_subtype not like "%%*%%") and major_axis_arcsec is not null """ % locals( 

) 

rows = readquery( 

log=self.log, 

sqlQuery=sqlQuery, 

dbConn=self.ligo_virgo_wavesDbConn 

) 

 

savedRows = [] 

raList = [] 

decList = [] 

for row in rows: 

raList.append(row["faker_ra"]) 

decList.append(row["faker_dec"]) 

 

tRa = np.array([raFP]) 

tDec = np.array([decFP]) 

raList = np.array(raList) 

decList = np.array(decList) 

indexList1, indexList2, separation = mesh16.match( 

tRa, tDec, raList, decList, radius, maxmatch=0) 

for i in range(indexList1.size): 

savedRows.append(rows[indexList2[i]]) 

 

# ADD IN AN EXTRA 15% FOR FAKERS FOUND IN THE FIELD BUT NOT NEAR GALAXY 

extraRows = [] 

extra15 = int(len(savedRows) * 0.176) 

pi = (4 * math.atan(1.0)) 

DEG_TO_RAD_FACTOR = pi / 180.0 

RAD_TO_DEG_FACTOR = 180.0 / pi 

for i in range(extra15): 

theta = 360. * np.random.rand() 

sep = (np.random.rand() * radius**2.)**(0.5) 

dec2 = decFP - sep * math.cos(theta * DEG_TO_RAD_FACTOR) 

ra2 = raFP - sep * math.sin(theta * DEG_TO_RAD_FACTOR) / math.cos( 

(2 * decFP - sep * math.cos(theta * DEG_TO_RAD_FACTOR)) * DEG_TO_RAD_FACTOR / 2) 

extraRows.append({ 

"faker_ra": ra2, 

"faker_dec": dec2, 

"z": 0.00000000, 

"catalogue_object_id": "ORPHAN", 

"2mass_k_mag": 0.00000000, 

"2mass_k_mag_error": 0.00000000 

}) 

 

rows = savedRows + extraRows 

e = 2.7182818284590452353602875 

writeRows = [] 

# CALCULATE THE FAKER RANDOM MAGNITUDES 

for row in rows: 

mag = (np.random.rand() * 3.) + 17. 

f0 = 10**(-(mag + 48.6) / (2.5)) 

ft = 0.25 * f0 * (e**2) * ((3.22 / 1.18)**2) * (e**(-3.22 / 1.18)) 

magt = (-2.5) * (math.log10(ft)) - 48.6 

if not row["2mass_k_mag"]: 

row["2mass_k_mag"] = 0.00 

if not row["2mass_k_mag_error"]: 

row["2mass_k_mag_error"] = 0.00 

writeRows.append({ 

"ra": row["faker_ra"], 

"dec": row["faker_dec"], 

"imag": magt, 

"z": row["z"], 

"catalogue_object_id": row["catalogue_object_id"], 

"2mass_k_mag": row["2mass_k_mag"], 

"2mass_k_mag_error": row["2mass_k_mag_error"] 

}) 

 

# WRITE THE RESULT TO FILE 

import codecs 

from datetime import datetime, date, time 

now = datetime.now() 

now = now.strftime("%Y%m%dt%H%M%S") 

 

# RECURSIVELY CREATE MISSING DIRECTORIES - PATH FROM SETTINGS FILE 

if not os.path.exists(self.settings["output directory"] + "/fakers"): 

os.makedirs(self.settings["output directory"] + "/fakers") 

 

writeFile = codecs.open( 

self.settings["output directory"] + "/fakers/G184098_fake_sources_%(now)s_trimmed.csv" % locals(), encoding='utf-8', mode='w') 

writeFile2 = codecs.open( 

self.settings["output directory"] + "/fakers/G184098_fake_sources_%(now)s_complete.csv" % locals(), encoding='utf-8', mode='w') 

writeFile.write("index,ra,dec,i-mag\n") 

writeFile2.write( 

"index,ra,dec,i-mag,redshift,galaxy-id,2mass-k-mag,2mass-k-mag-error\n") 

for r, d in enumerate(writeRows): 

d["r"] = r + 1 

one = "%(r)04d,%(ra)6.5f,%(dec)6.5f,%(imag)4.2f\n" % d 

writeFile.write(one.replace( 

",0.00,", ",null,").replace(",0.00\n", ",null\n")) 

two = '%(r)04d,%(ra)6.5f,%(dec)6.5f,%(imag)4.2f,%(z)4.3f,"%(catalogue_object_id)s",%(2mass_k_mag)4.2f,%(2mass_k_mag_error)4.2f\n' % d 

writeFile2.write(two.replace( 

",0.00,", ",null,").replace(",0.00\n", ",null\n")) 

writeFile.close() 

writeFile2.close() 

 

self.log.info('completed the ``write_fakers_to_file`` method') 

return None 

 

def generate_faker_locations_within_galaxies_found_in_survey_footprint( 

self): 

""" 

*For galaxies matched within the PS1 survey footprints, add faker sources at a random location within the galaxy* 

 

.. note:: 

 

the galaxies need to have a semi-major axis measurement before a faker is added 

 

**Return:** 

- None 

 

**Usage:** 

 

.. code-block:: python  

 

# ADD FAKERS TO GALAXIES 

from breaker import fakers 

fakecat = fakers.generate_faker_catalogue( 

log=log, 

settings=settings, 

ps1ExpId=73283973, 

gwid="G211117" 

).generate_faker_locations_within_galaxies_found_in_survey_footprint()  

""" 

self.log.info( 

'starting the ``generate_faker_locations_within_galaxies_found_in_survey_footprint`` method') 

 

import math 

pi = (4 * math.atan(1.0)) 

DEG_TO_RAD_FACTOR = pi / 180.0 

RAD_TO_DEG_FACTOR = 180.0 / pi 

 

# ITERATE THROUGH THE CATALOGUED SOURCES DATABASE TABLES 

for gwid in self.settings["gravitational waves"]: 

tableName = "tcs_%(gwid)s_catalogued_sources" % locals() 

thisTableExists = table_exists( 

dbConn=self.ligo_virgo_wavesDbConn, 

log=self.log, 

dbTableName=tableName 

) 

if thisTableExists: 

# SELECT GALAXY ROWS WITH SEMI-MAJOR AXIS MEASUREMENT BUT NO 

# FAKER COORDINATES 

sqlQuery = u""" 

select id, catalogue_object_ra, catalogue_object_dec, major_axis_arcsec from %(tableName)s where major_axis_arcsec is not null and faker_ra is null 

""" % locals() 

rows = readquery( 

log=self.log, 

sqlQuery=sqlQuery, 

dbConn=self.ligo_virgo_wavesDbConn 

) 

count = 0 

values = [] 

 

if len(rows) == 0: 

print "Faker sources have been added to all galaxies found within the PS1 footprint" 

 

# GENERATE A RANDOM FAKER LOCATION AND ADD TO THE DATABASE - 

# ADD INT BATCHES OF 5000 

for row in rows: 

count += 1 

thisId = int(row["id"]) 

major_axis_arcsec = row["major_axis_arcsec"] 

sep = (major_axis_arcsec / 

3660.) * np.random.rand() 

arcsecSep = sep * 3600. 

print np.random.rand() 

theta = 360. * np.random.rand() 

 

ra1 = row["catalogue_object_ra"] 

dec1 = row["catalogue_object_dec"] 

 

dec2 = dec1 - sep * math.cos(theta * DEG_TO_RAD_FACTOR) 

ra2 = ra1 - sep * math.sin(theta * DEG_TO_RAD_FACTOR) / math.cos( 

(2 * dec1 - sep * math.cos(theta * DEG_TO_RAD_FACTOR)) * DEG_TO_RAD_FACTOR / 2) 

 

values.append(str((thisId, ra2, dec2))) 

if count == 5000: 

values = ",".join(values) 

sqlQuery = u""" 

INSERT INTO %(tableName)s (id,faker_ra,faker_dec) VALUES %(values)s 

ON DUPLICATE KEY UPDATE faker_ra=VALUES(faker_ra),faker_dec=VALUES(faker_dec); 

""" % locals() 

writequery( 

log=self.log, 

sqlQuery=sqlQuery, 

dbConn=self.ligo_virgo_wavesDbConn 

) 

count = 0 

values = [] 

 

print "Another 5000 faker sources have been added to galaxies found within the PS1 footprint" 

 

values = ",".join(values) 

 

if len(values): 

sqlQuery = u""" 

INSERT INTO %(tableName)s (id,faker_ra,faker_dec) VALUES %(values)s 

ON DUPLICATE KEY UPDATE faker_ra=VALUES(faker_ra),faker_dec=VALUES(faker_dec); 

""" % locals() 

writequery( 

log=self.log, 

sqlQuery=sqlQuery, 

dbConn=self.ligo_virgo_wavesDbConn 

) 

 

count = len(values) 

print "Another %(count)s faker sources have been added to galaxies found within the PS1 footprint" % locals() 

 

self.log.info( 

'completed the ``generate_faker_locations_within_galaxies_found_in_survey_footprint`` method') 

return None