Usage

Once you have the required credentials and IndexDB installed, you’re ready to go.

If your IndexDB is at ./path/to/alerts.db, you can open and use the database like this:

import alertbase
with alertbase.Database.open("./path/to/alerts.db") as db:
    alerts = db.get_by_object_id("ZTF28abmodkj")
for a in alerts:
    print(a.raw_dict['candidate']['magpsf'])

Searching by ZTF Object ID

Use alertbase.Database.get_by_object_id():

import alertbase
with alertbase.Database.open("./path/to/alerts.db") as db:
    alerts = db.get_by_object_id("ZTF28abmodkj")
for a in alerts:
    print(a.raw_dict['candidate']['magpsf'])

Searching by Time Range

Use alertbase.Database.get_by_time_range(). You pass in astropy.time.Time values for the start and end of your desired range. You’ll get all alerts that come from exposures within that time range.

import alertbase
from astropy.time import Time

start = Time("2021-01-12 20:00:00")
end = Time("2021-01-12 20:15:00")

with alertbase.Database.open("./path/to/alerts.db") as db:
    alerts = db.get_by_time_range(start, end)
for a in alerts:
    print(a.raw_dict['candidate']['magpsf'])

Advanced: Using asyncio

If you’re processing a lot of large queries, or just feeling particularly brave, you can use the async APIs of alertbase.Database. Each get_ method has a streaming counterpart which returns an asynchronous generator.

So, for example, if you wanted to stream a query’s results into your terminal for some reason, you could do something like this:

import alertbase
from astropy.time import Time

start = Time("2021-01-12")
end = Time("2021-01-13")

async def stream_alerts():
    with alertbase.Database.open("./path/to/alerts.db"):
        stream = db.get_by_time_range_async(start, end)
        async for alert in stream:
            print(alert.candidate_id)

asyncio.run(stream_alerts())