# Reading flag columns

**URL:** <https://www.rubin.community/t/reading-flag-columns/3412>\
**Category:** Data Management\
**Tags:** catalogs, astropy\
**Created:** [November 14, 2018, 6:12pm UTC](https://www.rubin.community/t/reading-flag-columns/3412 "2018-11-14T18:12:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![price](https://sea2.discourse-cdn.com/flex002/user_avatar/www.rubin.community/price/32/488_2.png) [@price](https://www.rubin.community/u/price)\
**Post date:** [November 14, 2018, 6:12pm UTC](https://www.rubin.community/t/reading-flag-columns/3412/1 "2018-11-14T18:12:22Z")

</div>

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])
```
