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

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

"""Tasks that comprise the alert orchestrator. 

 

The orchestrator is responsible for the vetting and annotation workflow to 

produce preliminary, initial, and update alerts for gravitational-wave event 

candidates. 

""" 

import json 

import re 

 

from celery import chain, group 

 

from ..import app 

from . import bayestar 

from . import circulars 

from .core import identity, ordered_group, ordered_group_first 

from . import detchar 

from . import em_bright 

from . import gcn 

from . import gracedb 

from . import inference 

from . import lvalert 

from . import p_astro 

from . import skymaps 

from . import superevents 

 

 

@lvalert.handler('superevent', 

'mdc_superevent', 

shared=False) 

def handle_superevent(alert): 

"""Schedule annotations for new superevents. 

 

After waiting for a time specified by the 

:obj:`~gwcelery.conf.orchestrator_timeout` configuration variable 

for the choice of preferred event to settle down, this task performs data 

quality checks with :meth:`gwcelery.tasks.detchar.check_vectors` and 

calls :meth:`~gwcelery.tasks.orchestrator.preliminary_alert` to send a 

preliminary GCN notice. 

""" 

superevent_id = alert['uid'] 

# launch PE and detchar based on new type superevents 

if alert['alert_type'] == 'new': 

( 

_get_preferred_event.si(superevent_id).set( 

countdown=app.conf['pe_timeout'] 

) 

| 

ordered_group( 

_get_lowest_far.si(superevent_id), 

gracedb.get_event.s() 

) 

| 

parameter_estimation.s(superevent_id) 

).apply_async() 

 

# run check_vectors. Create and upload omegascans 

group( 

detchar.omegascan.si( 

alert['object']['t_0'], 

superevent_id 

).set(countdown=10), 

 

gracedb.get_event.si(alert['object']['preferred_event']) 

| 

detchar.check_vectors.s( 

superevent_id, 

alert['object']['t_start'], 

alert['object']['t_end'] 

) 

).delay() 

 

elif alert['alert_type'] == 'label_added': 

label_name = alert['data']['name'] 

if label_name == superevents.FROZEN_LABEL: 

( 

gracedb.get_event.s(alert['object']['preferred_event']) 

| 

_leave_log_message_and_return_event_dict.s( 

superevent_id, 

"Automated DQ check before sending preliminary alert. " 

"New results supersede old results.", 

tags=['data_quality'] 

) 

| 

detchar.check_vectors.s( 

superevent_id, 

alert['object']['t_start'], 

alert['object']['t_end'] 

) 

| 

preliminary_alert.s(superevent_id) 

).apply_async() 

 

elif label_name == superevents.READY_LABEL: 

( 

_get_preferred_event.si(superevent_id).set( 

countdown=app.conf['subthreshold_annotation_timeout'] 

) 

| 

gracedb.get_event.s() 

| 

preliminary_alert.s(superevent_id, 

annotation_prefix='subthreshold.', 

initiate_voevent=False) 

).apply_async() 

 

# launch second preliminary on GCN_PRELIM_SENT 

elif label_name == 'GCN_PRELIM_SENT': 

query = f'superevent: {superevent_id}' 

if alert['object']['category'] == 'MDC': 

query += ' MDC' 

elif alert['object']['category'] == 'Test': 

query += ' Test' 

 

( 

gracedb.get_events.si(query).set( 

countdown=app.conf['superevent_clean_up_timeout'] 

) 

| 

superevents.select_preferred_event.s() 

| 

_update_superevent_and_return_event_dict.s(superevent_id) 

| 

_leave_log_message_and_return_event_dict.s( 

superevent_id, 

"Superevent cleaned up." 

) 

| 

preliminary_alert.s(superevent_id) 

).apply_async() 

# launch initial/retraction alert on ADVOK/ADVNO 

elif label_name == 'ADVOK': 

initial_alert((None, None, None), superevent_id, 

labels=alert['object']['labels']) 

elif label_name == 'ADVNO': 

retraction_alert(superevent_id) 

 

# check DQV label on superevent, run check_vectors if required 

elif alert['alert_type'] == 'event_added': 

new_event_id = alert['data']['preferred_event'] 

start = alert['data']['t_start'] 

end = alert['data']['t_end'] 

 

if 'DQV' in gracedb.get_labels(superevent_id): 

( 

gracedb.get_event.s(new_event_id) 

| 

detchar.check_vectors.s(superevent_id, start, end) 

| 

_update_if_dqok.si(superevent_id, new_event_id) 

).apply_async() 

 

 

@lvalert.handler('cbc_gstlal', 

'cbc_spiir', 

'cbc_pycbc', 

'cbc_mbtaonline', 

shared=False) 

def handle_cbc_event(alert): 

"""Perform annotations for CBC events that depend on pipeline-specific 

matched-filter parameter estimates. 

 

Notes 

----- 

This LVAlert message handler is triggered by updates that include the file 

``psd.xml.gz``. The table below lists which 

files are created as a result, and which tasks generate them. 

 

============================== ================================================ 

File Task 

============================== ================================================ 

``bayestar.multiorder.fits`` :meth:`gwcelery.tasks.bayestar.localize` 

``em_bright.json`` :meth:`gwcelery.tasks.em_bright.classifier` 

``p_astro.json.json`` :meth:`gwcelery.tasks.p_astro.compute_p_astro` 

============================== ================================================ 

 

""" # noqa: E501 

graceid = alert['uid'] 

# priority = 0 if superevents.should_publish(alert['object']) else 1 

priority = 0 # FIXME: should_publish always false for incomplete events! 

 

# em_bright and p_astro calculation 

if alert['alert_type'] == 'new': 

pipeline = alert['object']['pipeline'].lower() 

instruments = superevents.get_instruments_in_ranking_statistic( 

alert['object']) 

extra_attributes = alert['object']['extra_attributes'] 

snr = superevents.get_snr(alert['object']) 

far = alert['object']['far'] 

mass1 = extra_attributes['SingleInspiral'][0]['mass1'] 

mass2 = extra_attributes['SingleInspiral'][0]['mass2'] 

chi1 = extra_attributes['SingleInspiral'][0]['spin1z'] 

chi2 = extra_attributes['SingleInspiral'][0]['spin2z'] 

 

em_bright_task = em_bright.classifier_gstlal if pipeline == 'gstlal' \ 

else em_bright.classifier_other 

 

( 

em_bright_task.si((mass1, mass2, chi1, chi2, snr), graceid) 

| 

gracedb.upload.s( 

'em_bright.json', graceid, 

'em bright complete', ['em_bright', 'public'] 

) 

| 

gracedb.create_label.si('EMBRIGHT_READY', graceid) 

).apply_async(priority=priority) 

 

# p_astro calculation for other pipelines 

if pipeline != 'gstlal' or alert['object']['search'] == 'MDC': 

( 

p_astro.compute_p_astro.s(snr, 

far, 

mass1, 

mass2, 

pipeline, 

instruments) 

| 

gracedb.upload.s( 

'p_astro.json', graceid, 

'p_astro computation complete', ['p_astro', 'public'] 

) 

| 

gracedb.create_label.si('PASTRO_READY', graceid) 

).apply_async(priority=priority) 

 

if alert['alert_type'] != 'log': 

return 

 

filename = alert['data']['filename'] 

 

if filename == 'psd.xml.gz': 

( 

ordered_group( 

gracedb.download.s('coinc.xml', graceid), 

gracedb.download.s('psd.xml.gz', graceid) 

) 

| 

bayestar.localize.s(graceid) 

| 

gracedb.upload.s( 

'bayestar.multiorder.fits', graceid, 

'sky localization complete', ['sky_loc', 'public'] 

) 

| 

gracedb.create_label.si('SKYMAP_READY', graceid) 

).apply_async(priority=priority) 

 

 

@lvalert.handler('superevent', 

'mdc_superevent', 

shared=False) 

def handle_posterior_samples(alert): 

"""Generate multi-resolution and flat-resolution fits files and skymaps 

from an uploaded HDF5 file containing posterior samples. 

""" 

if alert['alert_type'] != 'log' or \ 

not alert['data']['filename'].endswith('.posterior_samples.hdf5'): 

return 

superevent_id = alert['uid'] 

filename = alert['data']['filename'] 

info = '{} {}'.format(alert['data']['comment'], filename) 

prefix, _ = filename.rsplit('.posterior_samples.') 

 

( 

gracedb.download.si(filename, superevent_id) 

| 

skymaps.skymap_from_samples.s() 

| 

group( 

skymaps.annotate_fits.s( 

'{}.fits.gz'.format(prefix), 

superevent_id, ['pe', 'sky_loc', 'public'] 

), 

 

gracedb.upload.s( 

'{}.multiorder.fits'.format(prefix), superevent_id, 

'Multiresolution fits file generated from "{}"'.format(info), 

['pe', 'sky_loc', 'public'] 

), 

 

skymaps.flatten.s('{}.fits.gz'.format(prefix)) 

| 

gracedb.upload.s( 

'{}.fits.gz'.format(prefix), superevent_id, 

'Flat-resolution fits file created from "{}"'.format(info), 

['pe', 'sky_loc', 'public'] 

) 

) 

).delay() 

 

# em_bright from LALInference posterior samples 

( 

gracedb.download.si(filename, superevent_id) 

| 

em_bright.em_bright_posterior_samples.s() 

| 

gracedb.upload.s( 

'{}.em_bright.json'.format(prefix), superevent_id, 

'em-bright computed from "{}"'.format(info) 

) 

).delay() 

 

 

@app.task(shared=False, ignore_result=True) 

def _update_if_dqok(superevent_id, event_id): 

"""Update `preferred_event` of `superevent_id` to `event_id` if `DQOK` 

label has been applied. 

""" 

if 'DQOK' in gracedb.get_labels(superevent_id): 

gracedb.update_superevent(superevent_id, preferred_event=event_id) 

gracedb.create_log( 

"DQOK applied based on new event %s" % (event_id), superevent_id) 

 

 

@gracedb.task(shared=False) 

def _get_preferred_event(superevent_id): 

"""Determine preferred event for a superevent by querying GraceDB. 

 

This works just like :func:`gwcelery.tasks.gracedb.get_superevent`, except 

that it returns only the preferred event, and not the entire GraceDB JSON 

response. 

""" 

# FIXME: remove ._orig_run when this bug is fixed: 

# https://github.com/getsentry/sentry-python/issues/370 

return gracedb.get_superevent._orig_run(superevent_id)['preferred_event'] 

 

 

@gracedb.task(shared=False) 

def _create_voevent(classification, *args, **kwargs): 

r"""Create a VOEvent record from an EM bright JSON file. 

 

Parameters 

---------- 

classification : tuple, None 

A collection of JSON strings, generated by 

:meth:`gwcelery.tasks.em_bright.classifier` and 

:meth:`gwcelery.tasks.p_astro.compute_p_astro` or 

content of ``p_astro.json`` uploaded by gstlal respectively; 

or None 

\*args 

Additional positional arguments passed to 

:meth:`gwcelery.tasks.gracedb.create_voevent`. 

\*\*kwargs 

Additional keyword arguments passed to 

:meth:`gwcelery.tasks.gracedb.create_voevent`. 

 

Returns 

------- 

str 

The filename of the newly created VOEvent. 

 

""" 

kwargs = dict(kwargs) 

 

if classification is not None: 

# Merge source classification and source properties into kwargs. 

for text in classification: 

if text is not None: 

kwargs.update(json.loads(text)) 

 

# FIXME: These keys have differ between em_bright.json 

# and the GraceDB REST API. 

try: 

kwargs['ProbHasNS'] = kwargs.pop('HasNS') 

except KeyError: 

pass 

 

try: 

kwargs['ProbHasRemnant'] = kwargs.pop('HasRemnant') 

except KeyError: 

pass 

 

skymap_filename = kwargs.get('skymap_filename') 

if skymap_filename is not None: 

skymap_type = re.sub(r'\.fits(\..+)?$', '', skymap_filename) 

kwargs.setdefault('skymap_type', skymap_type) 

 

# FIXME: remove ._orig_run when this bug is fixed: 

# https://github.com/getsentry/sentry-python/issues/370 

return gracedb.create_voevent._orig_run(*args, **kwargs) 

 

 

@gracedb.task(shared=False) 

def _create_label_and_return_filename(filename, label, graceid): 

gracedb.create_label(label, graceid) 

return filename 

 

 

@gracedb.task(shared=False) 

def _leave_log_message_and_return_event_dict(event, superevent_id, 

message, **kwargs): 

"""Wrapper around :meth:`gracedb.update_superevent` 

that returns the event dictionary. 

""" 

gracedb.upload(None, None, superevent_id, message, **kwargs) 

return event 

 

 

@gracedb.task(shared=False) 

def _update_superevent_and_return_event_dict(event, superevent_id): 

"""Wrapper around :meth:`gracedb.update_superevent` 

that returns the event dictionary. 

""" 

gracedb.update_superevent(superevent_id, 

preferred_event=event['graceid']) 

return event 

 

 

@gracedb.task(shared=False) 

def _proceed_if_no_advocate_action(filenames, superevent_id): 

"""Return filenames in case the superevent does not have labels 

indicating advocate action. 

""" 

superevent_labels = gracedb.get_labels(superevent_id) 

blocking_labels = {'ADVOK', 'ADVNO'}.intersection( 

superevent_labels) 

if blocking_labels: 

gracedb.upload( 

None, None, superevent_id, 

"Blocking automated notice due to labels %s" % (blocking_labels) 

) 

return None 

else: 

gracedb.upload(None, None, superevent_id, 

"Sending preliminary notice") 

return filenames 

 

 

@app.task(ignore_result=True, shared=False) 

def preliminary_alert(event, superevent_id, annotation_prefix='', 

initiate_voevent=True): 

"""Produce a preliminary alert by copying any sky maps. 

 

This consists of the following steps: 

 

1. Copy any sky maps and source classification from the preferred event 

to the superevent. 

2. Create standard annotations for sky maps including all-sky plots by 

calling :meth:`gwcelery.tasks.skymaps.annotate_fits`. 

3. Create a preliminary VOEvent. 

4. Send the VOEvent to GCN. 

5. Apply the GCN_PRELIM_SENT label to the superevent. 

6. Create and upload a GCN Circular draft. 

""" 

priority = 0 if superevents.should_publish(event) else 1 

preferred_event_id = event['graceid'] 

 

if event['group'] == 'CBC': 

skymap_filename = 'bayestar.multiorder.fits' 

elif event['pipeline'] == 'CWB': 

skymap_filename = 'cWB.fits.gz' 

elif event['pipeline'] == 'oLIB': 

skymap_filename = 'oLIB.fits.gz' 

else: 

skymap_filename = None 

 

original_skymap_filename = skymap_filename 

if skymap_filename.endswith('.multiorder.fits'): 

skymap_filename = skymap_filename.replace('.multiorder.fits', '.fits') 

if skymap_filename.endswith('.fits'): 

skymap_filename += '.gz' 

 

# Determine if the event should be made public. 

is_publishable = (superevents.should_publish(event) 

and {'DQV', 'INJ'}.isdisjoint(event['labels'])) 

 

canvas = ordered_group( 

( 

gracedb.download.si(original_skymap_filename, preferred_event_id) 

| 

ordered_group_first( 

skymaps.flatten.s(annotation_prefix + skymap_filename) 

| 

gracedb.upload.s( 

annotation_prefix + skymap_filename, 

superevent_id, 

message='Flattened from multiresolution file {}'.format( 

original_skymap_filename), 

tags=['sky_loc'] if annotation_prefix else [ 

'sky_loc', 'public'] 

) 

| 

_create_label_and_return_filename.s( 

'SKYMAP_READY', superevent_id 

), 

 

gracedb.upload.s( 

annotation_prefix + original_skymap_filename, 

superevent_id, 

message='Localization copied from {}'.format( 

preferred_event_id), 

tags=['sky_loc'] if annotation_prefix else [ 

'sky_loc', 'public'] 

), 

 

skymaps.annotate_fits.s( 

annotation_prefix + skymap_filename, 

superevent_id, 

['sky_loc'] if annotation_prefix else [ 

'sky_loc', 'public'] 

) 

) 

) if skymap_filename is not None else identity.s(None), 

 

( 

gracedb.download.si('em_bright.json', preferred_event_id) 

| 

gracedb.upload.s( 

annotation_prefix + 'em_bright.json', 

superevent_id, 

message='Source properties copied from {}'.format( 

preferred_event_id), 

tags=['em_bright'] if annotation_prefix else [ 

'em_bright', 'public'] 

) 

| 

_create_label_and_return_filename.s( 

'EMBRIGHT_READY', superevent_id 

) 

) if event['group'] == 'CBC' else identity.s(None), 

 

( 

gracedb.download.si('p_astro.json', preferred_event_id) 

| 

gracedb.upload.s( 

annotation_prefix + 'p_astro.json', 

superevent_id, 

message='Source classification copied from {}'.format( 

preferred_event_id), 

tags=['p_astro'] if annotation_prefix else [ 

'p_astro', 'public'] 

) 

| 

_create_label_and_return_filename.s( 

'PASTRO_READY', superevent_id 

) 

) if event['group'] == 'CBC' else identity.s(None) 

) 

 

# Send GCN notice and upload GCN circular draft for online events. 

if is_publishable and initiate_voevent: 

canvas |= ( 

_proceed_if_no_advocate_action.s(superevent_id) 

| 

preliminary_initial_update_alert.s( 

superevent_id, 'preliminary', labels=event['labels']) 

) 

 

canvas.apply_async(priority=priority) 

 

 

@gracedb.task(shared=False) 

def _get_lowest_far(superevent_id): 

"""Obtain the lowest FAR of the events in the target superevent.""" 

# FIXME: remove ._orig_run when this bug is fixed: 

# https://github.com/getsentry/sentry-python/issues/370 

return min(gracedb.get_event._orig_run(gid)['far'] for gid in 

gracedb.get_superevent._orig_run(superevent_id)["gw_events"]) 

 

 

@app.task(ignore_result=True, shared=False) 

def parameter_estimation(far_event, superevent_id): 

"""Tasks for Parameter Estimation Followup with LALInference or Bilby 

 

For LALInference, this consists of the following steps: 

 

1. Prepare and upload an ini file which is suitable for the target event. 

2. Start Parameter Estimation if FAR is smaller than the PE threshold. 

 

For Bilby, this consists of the following steps: 

 

1. Start Parameter Estimation if FAR is smaller than the PE threshold. 

2. Upload of ini file during Parameter Estimation 

""" 

far, event = far_event 

preferred_event_id = event['graceid'] 

# FIXME: it will be better to start parameter estimation for 'burst' 

# events. 

if event['group'] == 'CBC': 

canvas = inference.pre_pe_tasks(event, superevent_id) 

if far <= app.conf['pe_threshold']: 

canvas |= group( 

inference.start_pe.s( 

preferred_event_id, superevent_id, 'lalinference' 

), 

inference.start_pe.s( 

preferred_event_id, superevent_id, 'bilby' 

) 

) 

else: 

canvas |= gracedb.upload.si( 

filecontents=None, filename=None, 

graceid=superevent_id, 

message='FAR is larger than the PE threshold, ' 

'{} Hz. Parameter Estimation will not ' 

'start.'.format(app.conf['pe_threshold']), 

tags='pe' 

) 

 

canvas.apply_async() 

 

 

@gracedb.task(ignore_result=True, shared=False) 

def preliminary_initial_update_alert(filenames, superevent_id, alert_type, 

labels=[]): 

""" 

Create and send a preliminary, initial, or update GCN notice. 

 

Parameters 

---------- 

filenames : tuple 

A list of the sky map, em_bright, and p_astro filenames. 

superevent_id : str 

The superevent ID. 

alert_type : {'preliminary', 'initial', 'update'} 

The alert type. 

labels : list 

A list of labels applied to superevent. 

 

Notes 

----- 

This function is decorated with :obj:`gwcelery.tasks.gracedb.task` rather 

than :obj:`gwcelery.app.task` so that a synchronous call to 

:func:`gwcelery.tasks.gracedb.get_log` is retried in the event of GraceDB 

API failures. If `EM_COINC` is in labels will create a RAVEN circular. 

 

""" 

if filenames is None: 

return 

 

skymap_filename, em_bright_filename, p_astro_filename = filenames 

skymap_needed = (skymap_filename is None) 

em_bright_needed = (em_bright_filename is None) 

p_astro_needed = (p_astro_filename is None) 

if skymap_needed or em_bright_needed or p_astro_needed: 

for message in gracedb.get_log(superevent_id): 

t = message['tag_names'] 

f = message['filename'] 

v = message['file_version'] 

fv = '{},{}'.format(f, v) 

if not f: 

continue 

if skymap_needed \ 

and {'sky_loc', 'public'}.issubset(t) \ 

and f.endswith('.fits.gz'): 

skymap_filename = fv 

if em_bright_needed \ 

and 'em_bright' in t \ 

and f.endswith('.json'): 

em_bright_filename = fv 

if p_astro_needed \ 

and 'p_astro' in t \ 

and f.endswith('.json'): 

p_astro_filename = fv 

 

if alert_type in ['preliminary', 'initial']: 

if 'EM_COINC' in labels: 

circular_task = circulars.create_emcoinc_circular.si(superevent_id) 

circular_filename = '{}-emcoinc-circular.txt'.format(alert_type) 

tags = ['em_follow', 'ext_coinc'] 

 

else: 

circular_task = circulars.create_initial_circular.si(superevent_id) 

circular_filename = '{}-circular.txt'.format(alert_type) 

tags = ['em_follow'] 

 

circular_canvas = ( 

circular_task 

| 

gracedb.upload.s( 

circular_filename, 

superevent_id, 

'Template for {} GCN Circular'.format(alert_type), 

tags=tags) 

) 

else: 

circular_canvas = chain() 

 

canvas = ( 

group( 

gracedb.expose.s(superevent_id), 

*( 

gracedb.create_tag.s(filename, 'public', superevent_id) 

for filename in [ 

skymap_filename, em_bright_filename, p_astro_filename 

] 

if filename is not None 

) 

) 

| 

group( 

gracedb.download.si(em_bright_filename, superevent_id), 

gracedb.download.si(p_astro_filename, superevent_id), 

) 

| 

_create_voevent.s( 

superevent_id, 

alert_type, 

skymap_filename=skymap_filename, 

internal=False, 

open_alert=True, 

vetted=True 

) 

| 

group( 

gracedb.download.s(superevent_id) 

| 

gcn.send.s(), 

 

circular_canvas, 

 

gracedb.create_tag.s('public', superevent_id) 

) 

) 

 

if alert_type == 'preliminary': 

canvas |= gracedb.create_label.si('GCN_PRELIM_SENT', superevent_id) 

 

canvas.apply_async() 

 

 

@gracedb.task(ignore_result=True, shared=False) 

def initial_alert(filenames, superevent_id, labels=[]): 

"""Produce an initial alert. 

 

This does nothing more than call 

:meth:`~gwcelery.tasks.orchestrator.preliminary_initial_update_alert` with 

``alert_type='initial'``. 

 

Parameters 

---------- 

filenames : tuple 

A list of the sky map, em_bright, and p_astro filenames. 

superevent_id : str 

The superevent ID. 

labels : list 

A list of labels applied to superevent. 

 

Notes 

----- 

This function is decorated with :obj:`gwcelery.tasks.gracedb.task` rather 

than :obj:`gwcelery.app.task` so that a synchronous call to 

:func:`gwcelery.tasks.gracedb.get_log` is retried in the event of GraceDB 

API failures. 

 

""" 

preliminary_initial_update_alert(filenames, superevent_id, 'initial', 

labels=labels) 

 

 

@gracedb.task(ignore_result=True, shared=False) 

def update_alert(filenames, superevent_id): 

"""Produce an update alert. 

 

This does nothing more than call 

:meth:`~gwcelery.tasks.orchestrator.preliminary_initial_update_alert` with 

``alert_type='update'``. 

 

Parameters 

---------- 

filenames : tuple 

A list of the sky map, em_bright, and p_astro filenames. 

superevent_id : str 

The superevent ID. 

 

Notes 

----- 

This function is decorated with :obj:`gwcelery.tasks.gracedb.task` rather 

than :obj:`gwcelery.app.task` so that a synchronous call to 

:func:`gwcelery.tasks.gracedb.get_log` is retried in the event of GraceDB 

API failures. 

 

""" 

preliminary_initial_update_alert(filenames, superevent_id, 'update') 

 

 

@app.task(ignore_result=True, shared=False) 

def retraction_alert(superevent_id): 

"""Produce a retraction alert. This is currently just a stub and does 

nothing more than create and send a VOEvent. 

""" 

( 

gracedb.expose.si(superevent_id) 

| 

_create_voevent.si( 

None, superevent_id, 'retraction', 

internal=False, 

open_alert=True, 

vetted=True 

) 

| 

group( 

gracedb.download.s(superevent_id) 

| 

gcn.send.s(), 

 

circulars.create_retraction_circular.si(superevent_id) 

| 

gracedb.upload.s( 

'retraction-circular.txt', 

superevent_id, 

'Template for retraction GCN Circular', 

tags=['em_follow'] 

), 

 

gracedb.create_tag.s('public', superevent_id) 

) 

).apply_async()