Git Product home page Git Product logo

pygcn's Introduction

PyGCN

Python Package Index status Code coverage status

Anonymous VOEvent client for receiving GCN/TAN notices in XML format

The Gamma-ray Coordinates Network/Transient Astronomy Network (GCN/TAN) is a system for distributing astronomical alerts, largely focused on operations of and detections from high-energy satellite missions.

GCN/TAN disseminates both Notices (prompt, machine-readable alerts) and Circulars (human-readable correspondence) through a handful of delivery methods and formats.

This package implements a simple client that listens for VOEvent XML format notices over the custom TCP/IP VOEvent Transport Protocol. By default, it connects to one of the anonymous GCN/TAN server, so no sign-up or configuration is necessary to begin receiving alerts.

Installation

To install PyGCN, simply run:

$ pip install --user pygcn

Usage

PyGCN provides an example script called pygcn-listen that will simply write all VOEvents that it receives to files in the current directory. To try it out, simply run:

$ pygcn-listen

and then type Control-C to quit.

Writing a custom GCN handler

You can also write your own handler that performs a custom action for every GCN that is received. A handler function takes two arguments: payload, the raw content of the GCN, and root, the root element of the XML document as parsed by lxml.etree. Here is a basic example:

#!/usr/bin/env python
import gcn

# Define your custom handler here.
def handler(payload, root):
    # Get the IVORN, or unique VOEvent ID, and print it.
    print(root.attrib['ivorn'])

    # Print all of the event attributes.
    for param in root.findall('./What/Param'):
        name = param.attrib['name']
        value = param.attrib['value']
        print('{} = {}'.format(name, value))

# Listen for VOEvents until killed with Control-C.
gcn.listen(handler=handler)

Filtering

You can also filter events by notice type using gcn.include_notice_types or gcn.exclude_notice_types. Here is an example:

#!/usr/bin/env python
import gcn

# Define your custom handler here.
@gcn.include_notice_types(
    gcn.notice_types.FERMI_GBM_FLT_POS,  # Fermi GBM localization (flight)
    gcn.notice_types.FERMI_GBM_GND_POS,  # Fermi GBM localization (ground)
    gcn.notice_types.FERMI_GBM_FIN_POS)  # Fermi GBM localization (final)
def handler(payload, root):
    # Look up right ascension, declination, and error radius fields.
    pos2d = root.find('.//{*}Position2D')
    ra = float(pos2d.find('.//{*}C1').text)
    dec = float(pos2d.find('.//{*}C2').text)
    radius = float(pos2d.find('.//{*}Error2Radius').text)

    # Print.
    print('ra = {:g}, dec={:g}, radius={:g}'.format(ra, dec, radius))

# Listen for VOEvents until killed with Control-C.
gcn.listen(handler=handler)

pygcn's People

Contributors

axj336 avatar dependabot[bot] avatar dmopalmer avatar jracusin avatar lpsinger avatar volodymyrss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pygcn's Issues

Threading support

I need to do threading (e.g. one thread waits for the VO Event, other threads run the telescope).

I have submitted a pull request with my implementation for this. I have only tested it with the threading API, but the multiprocessing and asyncio APIs should work transparently with this code.

Test added for the queue handler; README.md gives a usage example; command line routine serves as more complete example; PEP-8; Travis gives a green check.

No handlers could be found for logger "gcn.listen"

Hello Leo,

I updated pygcn (great software btw) and it throws this error message as soon as it starts:

No handlers could be found for logger "gcn.listen"

I try it with this minimal example with pygcn freshly installed:

import gcn
def hello():
    print("Hello!")
if __name__ == "__main__":
    gcn.listen(port=1234, handler=hello)

Is it just me? Can you reproduce?
Thanks!

XML security

First of all, thanks for creating and sharing this Python package. While exploring the code and beginning to write our own handler function, I noticed that according to the Python docs, there are XML vulnerabilities that should probably be secured using something like defusedxml. Have you considered using this package to parse the incoming packets?

trigger processing after receiving an event

Hi. I have pygcn listening for (vo)events. Everything works fine. What I want to do is to trigger a script (parse the votable and so on) whenever I/pygcn receive an event. Would you have any tip for me on how to best accomplish that?
Thanks.

4pisky broker "iamalive"s are reported as "unrecognised root tag"

Listening to the 4pisky broker instead of the default NASA GCN/TAN server reports an error every time an iamalive comes in, and after 2 minutes the connection keeps getting reset.

Testing is simple, just listen to voevent.4pisky.org and wait for a few minutes:

$ pygcn-listen voevent.4pisky.org
INFO:gcn.listen:connected to voevent.4pisky.org:8099
ERROR:gcn.listen:received XML document with unrecognized root tag: {http://www.telescope-networks.org/xml/Transport/v1.1}Transport
INFO:gcn.listen:received VOEvent
INFO:gcn.handlers.archive:archived ivo://nasa.gsfc.gcn/Fermi#Point_Dir_2018-10-25T14:50:00.00_000000-0-311
ERROR:gcn.listen:received XML document with unrecognized root tag: {http://www.telescope-networks.org/xml/Transport/v1.1}Transport
ERROR:gcn.listen:socket error
Traceback (most recent call last):
  File "/home/martin/.local/lib/python3.5/site-packages/gcn/voeventclient.py", line 218, in listen
    _ingest_packet(sock, ivorn, handler, log)
  File "/home/martin/.local/lib/python3.5/site-packages/gcn/voeventclient.py", line 140, in _ingest_packet
    payload = _recv_packet(sock)
  File "/home/martin/.local/lib/python3.5/site-packages/gcn/voeventclient.py", line 107, in _recv_packet
    payload_len, = _size_struct.unpack_from(_recvall(sock, _size_len))
  File "/home/martin/.local/lib/python3.5/site-packages/gcn/voeventclient.py", line 96, in _recvall
    raise socket.error('connection closed by peer')
OSError: connection closed by peer
INFO:gcn.listen:closed socket
INFO:gcn.listen:connected to voevent.4pisky.org:8099
ERROR:gcn.listen:received XML document with unrecognized root tag: {http://www.telescope-networks.org/xml/Transport/v1.1}Transport
ERROR:gcn.listen:received XML document with unrecognized root tag: {http://www.telescope-networks.org/xml/Transport/v1.1}Transport
ERROR:gcn.listen:socket error
Traceback (most recent call last):
  File "/home/martin/.local/lib/python3.5/site-packages/gcn/voeventclient.py", line 218, in listen
    _ingest_packet(sock, ivorn, handler, log)
  File "/home/martin/.local/lib/python3.5/site-packages/gcn/voeventclient.py", line 140, in _ingest_packet
    payload = _recv_packet(sock)
  File "/home/martin/.local/lib/python3.5/site-packages/gcn/voeventclient.py", line 107, in _recv_packet
    payload_len, = _size_struct.unpack_from(_recvall(sock, _size_len))
  File "/home/martin/.local/lib/python3.5/site-packages/gcn/voeventclient.py", line 96, in _recvall
    raise socket.error('connection closed by peer')
OSError: connection closed by peer
INFO:gcn.listen:closed socket
...

Output from running with DEBUG logging:

INFO:test:connected to voevent.4pisky.org:8099
DEBUG:test:received packet of 464 bytes
DEBUG:test:payload is:
b'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<trn:Transport xmlns:trn="http://www.telescope-networks.org/xml/Transport/v1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://telescope-networks.org/schema/Transport/v1.1 http://www.telescope-networks.org/schema/Transport-v1.1.xsd" role="authenticate">\n  <Origin>ivo://voevent.4pisky.org/voevent-receive</Origin>\n  <TimeStamp>2018-10-25T14:50:39Z</TimeStamp>\n</trn:Transport>\n'
ERROR:test:received XML document with unrecognized root tag: {http://www.telescope-networks.org/xml/Transport/v1.1}Transport

This seems to arise due to the following check looking for a root tag of

{http://telescope-networks.org/schema/Transport/v1.1}Transport

but not 4pisky's

{http://telescope-networks.org/xml/Transport/v1.1}Transport

https://github.com/lpsinger/pygcn/blob/abd4454e21229439f9699eb3c9a836c857e05447/gcn/voeventclient.py#L157-L166

It should be a simple fix to allow either, although it's not a critical error (since the connection is restored and actual events are still handled) it would save having to reconnect and prevent filling up the logs with unnecessary error messages.

Importing gcn not working in script. Works in Jupyter notebook

Hi,

I was wondering why I get the following error when running my python script

    import gcn
ModuleNotFoundError: No module named 'gcn'.

There is no trouble when I import in a Jupyter notebook.
Please let me know if you have any idea why this is.

Thank you in advance!

Uzzie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.