Machine-readable output from Lasair

Once you have made a Lasair filter, you can get machine-readable output from it. The Python program below shows how to do this, and the discussion explains how to modfy this program for your own use. Each of the five lines A, B, C, D, E has its own discussion.

import json
from lasair import lasair_consumer                            # line A
kafka_server = 'lasair-lsst-kafka.lsst.ac.uk:9092'            # line B
my_topic     = 'lasair_2Lightweight'                          # line C
group_id     = 'test123'                                      # line D
consumer = lasair_consumer(kafka_server, group_id, my_topic)
import json
n = 0
while n < 10:
    msg = consumer.poll(timeout=20)
    if msg is None:
        break
    if msg.error():
        print(str(msg.error()))
        break
    result = json.loads(msg.value())                            # line E
    print(json.dumps(result, indent=2))
    n += 1
print(n, 'messages')

line A: Install the Lasair client

You can use pip for this:

pip3 install lasair

and please make sure you have the latest version

pip3 install lasair --upgrade

line B: Choose your Kafka service

The domain shown above is for Rubin/LSST alerts being filtered by Lasair. Substituting ztf for lsst gives gthe domain name for ZTF alerts. Remember to add the port number 9092. Hopefully there is no firewall between you and the service; you can check with the nc command, like this:

$ nc -zv lasair-lsst-kafka.lsst.ac.uk 9092 
Connection to lasair-lsst-kafka.lsst.ac.uk port 9092 [tcp/XmlIpcRegSvc] succeeded!

line C: The name of your active filter

Before you start to get machine-readable (kafka) results from your Lasair filter, you must build the filter using the Lasair web page, as explained in a previous blog post. When you click on “Save” for a new filter, or “Settings” for an existing filter, you should select “kafka stream” in the “Streaming” select box. Then when you look at the web page for your filter, yu see gthe formal name (or topic) in red text, like this: The filter is streamed via kafka with the topic name lasair_2SN-likecandidates. It’s that topic name which should be in line C of the above program.

line D: What is group_id?

Kafka is made for streams of messages. The Lasair server keeps 7 days of messages, after which they are deleted. When you ask for the next message of a specific topic, you specify a group_id as well, just an arbitrary string. If the server has not seen that group_id before, you get the first message it has, but if you use the same group_id again, you get the next message following. If you want all the messages, use a new group_id, but if you want those you have not yet seen, use the same group-id each time. If you expect to get messages and don’t, try changing the group_id.

See the video Topic and GroupID for a Lasair Kafka Stream for more information.

line E: Results

The nature of the results depends on what is in the SELECT clause of the origination filter. For the ‘lasair_2Lightweight’ filter (line C), this is:

objects.objectId, objects.decmean, objects.ramean, 
objects.jdmin, objects.jdmax, 
objects.gmag, objects.rmag, 
objects.ncand, sherlock_classifications.classification

and the results from the code above is a set of dictionaries, one for each alert that passes the filter. It can be printed prettily by converting to json with an indent:

{
  "objectId": "ZTF18abcozfc",
  "decmean": 37.00725359473685,
  "ramean": 294.45884451052626,
  "jdmin": 2460861.9242014,
  "jdmax": 2460891.7590856,
  "gmag": 17.7473,
  "rmag": 17.0934,
  "ncand": 19,
  "classification": "VS",
  "UTC": "2025-08-04 10:45:48"
}

Note that the creation time is also added as UTC.

This particular filter – lasair_2Lightweight – can be used to make a real-time display of the flow of ZTF alerts. Each alert os several hundred attibutes, and for this filter, Approximately 2.5% of the full alert rate are sent in this filter, reduced to just the 9 attributes each. The real-time display is madecat j from a Raspberry Pi and a LED matrix, and full details are here.