4

I have an astropy table that looks like this:

$ print(astro_table)
 id    xcentroid     ycentroid     sharpness        roundness1    ... npix sky      peak          flux            mag       
---- ------------- ------------- -------------- ----------------- ... ---- --- ------------- ------------- -----------------
6603 346.836613078 4089.22051381 0.292000724835   0.0734556783249 ... 49.0 0.0 69302.3984375 245.224909511    -5.97391145737
6177 919.415933548 3859.57301712 0.406784343306   -0.277953216167 ... 49.0 0.0 68524.5234375 220.268294418    -5.85737997245
6602 345.532899395 4088.87923557 0.401278628932   -0.450002792676 ... 49.0 0.0 70189.1953125 210.018583984    -5.80564431499
 ...           ...           ...            ...               ... ...  ... ...           ...           ...               ...
5626 3095.76998822 3522.08198969 0.393572474564   -0.543965319616 ... 49.0 0.0 3037.37036133 1.00374231333 -0.00405558116745
3577  824.59454487 2245.85801066 0.578026446726 -0.00166964746818 ... 49.0 0.0 3150.42285156 1.00347471149 -0.00376608082606
 612 3971.99991783 391.836698859 0.363131852861  -0.0206680542966 ... 49.0 0.0 3087.11572266 1.00319616544 -0.00346465867044
Length = 6603 rows

I want to create a new table, filtering out all those stars with peak values above a certain p_max threshold.

I've bee playing with the filter method, and this is what I could come up with:

def not_saturated(table, key_colnames):
    """Filter out saturated stars"""
    if table['peak'] < 60000.:
        return True
    return False

# 'sources' is the Astropy table, generated previously by the code.
# Group the table by 'id' column.
tg = sources.group_by('id')
# Apply filter.
sour_peak_filt = tg.groups.filter(not_saturated)

This works, but it feels unnecesarily convoluted. Also, I'd like to pass the p_max parameter to the non_saturated() function, but I can't since it only takes two arguments. This forces me to hardcode a value within the function (60000.), which I don't want to do.

Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • 1
    Doesn't **filter** require that you group the table first? I wonder if the ability discussed at http://docs.astropy.org/en/stable/io/fits/usage/table.html#selecting-records-in-a-table isn't more like what you need? – Bill Bell Nov 09 '16 at 17:27
  • Indeed it does. I managed to get it working, but it is far from ideal. I'll add what I came up with to my question. – Gabriel Nov 09 '16 at 17:28
  • 1
    @BillBell your link contained the answer. I just need to do `mask = sources['peak'] < p_max & sour_peak_filt = sources[mask]`. Would you like to add this as an answer so I can mark it accepted? Thank you! – Gabriel Nov 09 '16 at 17:36

1 Answers1

2

As mentioned in my comment, this answer is based on information from selecting records from a table. I use the fits file provided in the astropy distribution.

>>> from astropy.io import fits
>>> import os
>>> os.chdir("C:\Python34\Lib\site-packages\astropy\io\fits\tests\data")
>>> tableData=fits.open("table.fits")[1].data
>>> print(tableData)
[('NGC1001', 11.1) ('NGC1002', 12.3) ('NGC1003', 15.2)]
>>> tableData.names
['target', 'V_mag']
>>> mask = tableData['V_mag'] < 13.0 
>>> mask
array([ True,  True, False], dtype=bool)
>>> tableData[mask]
FITS_rec([('NGC1001', 11.1), ('NGC1002', 12.3)], 
      dtype=(numpy.record, [('target', 'S20'), ('V_mag', '>f4')]))
Bill Bell
  • 21,021
  • 5
  • 43
  • 58