Reading flag columns

astropy
catalogs
Tags: #<Tag:0x00007f7f78688a90> #<Tag:0x00007f7f78688888>

(Paul Price) #1

LSST-DM writes boolean flag columns using what I believe is a convention that we invented, so other FITS readers don’t have immediate access to the flags by symbolic name, as we do when using lsst.afw.table. In at least some circumstances, this has led to scientists writing code assuming a particular layout of the flags (e.g., bad = f.data["flags"][:, 42]), which is dangerous because the layout is subject to change between pipeline releases, and even within a release when the pipeline configuration changes.

In case someone finds it useful, I recently put together some simple code that reads the flags and makes them available by symbolic name:

import astropy.io.fits

class LsstTable:
    def __init__(self, filename):
        fits = astropy.io.fits.open(filename)
        self.phu = fits[0].header
        self.header = fits[1].header
        self.data = fits[1].data
        self.flags = {}
        ii = 1
        while ("TFLAG%d" % ii) in self.header:
            self.flags[self.header["TFLAG%d" % ii]] = self.data["flags"][:, ii - 1]
            ii += 1

Then you can do:

table = LsstTable(filename)
bad = table.flags["base_PsfFlux_flag"]
import numpy as np
mag = - 2.5*np.log10(table.data["base_PsfFlux_flux"])
print("Good values:", mag[~bad])