combine_catalogs.py 3.98 KB
Newer Older
LUSTIG Peter's avatar
LUSTIG Peter committed
1
from astropy.table import Table, vstack
2 3 4
from astropy.io.fits import PrimaryHDU, BinTableHDU, HDUList, open
from pathlib import Path
import astropy.units as u
LUSTIG Peter's avatar
LUSTIG Peter committed
5
import numpy as np
6
import os
7
import sys
8 9
print(os.getcwd())

LUSTIG Peter's avatar
LUSTIG Peter committed
10 11 12 13

def CombineMeasurements(sourceslist, fakesourceslist):
    fake_sources = Table()
    sources = Table()
LUSTIG Peter's avatar
ready  
LUSTIG Peter committed
14
    for _fake, _detected in zip(fakesourceslist, sourceslist):
LUSTIG Peter's avatar
LUSTIG Peter committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28
        n_fake = len(fake_sources)
        n_detected = len(sources)

        if _detected is not None:
            _detected['ID'] = _detected['ID'] + n_detected
            _detected['fake_sources'] = _detected['fake_sources'] + n_fake
            sources = vstack([sources, _detected])

        _fake['ID'] = _fake['ID'] + n_fake
        _fake['find_peak'] = _fake['find_peak'] + n_detected
        fake_sources = vstack([fake_sources, _fake])
    return sources, fake_sources


LUSTIG Peter's avatar
LUSTIG Peter committed
29 30 31
def LoadSingleFile(fname):
    _file = open(fname)
    flux = u.Quantity(_file[0].header['influx'])
32 33

    try:
LUSTIG Peter's avatar
LUSTIG Peter committed
34
        sources = Table.read(_file['DETECTED_SOURCES'])
35
    except KeyError:
LUSTIG Peter's avatar
LUSTIG Peter committed
36 37 38 39 40 41 42 43 44
        print('no sources detected for {}'.format(flux))
        sources = None

    fsources = Table.read(_file['FAKE_SOURCES'])

    dthresh = _file[0].header['DTHRESH']

    return flux, sources, fsources, dthresh

LUSTIG Peter's avatar
ready  
LUSTIG Peter committed
45 46

def Combine(directory, key, outfile, precision=0.001*u.mJy):
LUSTIG Peter's avatar
LUSTIG Peter committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    fnames = list(sorted(Path(directory).glob(key)))
    minthresh = None

    n = 0
    fluxlist = []
    sourceslist = []
    fsourceslist = []

    ''' load every single file '''
    for i, ifile in enumerate(fnames):
        _flux, _sources, _fsources, _dthresh = LoadSingleFile(ifile)
        fluxlist.append(_flux)
        sourceslist.append(_sources)
        fsourceslist.append(_fsources)
        if minthresh is None or minthresh < _dthresh:
            minthresh = _dthresh

    '''sort for flux comparison'''
    fluxlist = u.Quantity(fluxlist)

    idxsort = np.argsort(fluxlist.to_value(u.mJy))
    fluxlist = fluxlist[idxsort]
    sourceslist = [sourceslist[i] for i in idxsort]
    fsourceslist = [fsourceslist[i] for i in idxsort]
72

LUSTIG Peter's avatar
ready  
LUSTIG Peter committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    groupflux = []
    sourcegroup = []
    fsourcegroup = []

    '''
    _fgroup = [fluxlist[0]]
    _sgroup = [sourceslist[0]]
    _fsgroup = [fsourceslist[0]]
    '''
    _fgroup = []
    _sgroup = []
    _fsgroup = []

    for i in range(len(fluxlist)):
        _f = fluxlist[i]
        _s = sourceslist[i]
        _fs = fsourceslist[i]
        if _fgroup == [] or (_f - fluxlist[i-1]) < precision:
            _fgroup.append(_f)
            _sgroup.append(_s)
            _fsgroup.append(_fs)
        else:
            groupflux.append(np.mean(u.Quantity(_fgroup)))
            print('combining', _fgroup)
            _gs, _gfs = CombineMeasurements(_sgroup, _fsgroup)
            sourcegroup.append(_gs)
            fsourcegroup.append(_gfs)
            _fgroup = [_f]
            _sgroup = [_s]
            _fsgroup = [_fs]

    groupflux.append(np.mean(u.Quantity(_fgroup)))
    print('combining', _fgroup)
    _gs, _gfs = CombineMeasurements(_sgroup, _fsgroup)
    sourcegroup.append(_gs)
    fsourcegroup.append(_gfs)

    phdu = PrimaryHDU()
    phdu.header['dthresh'] = minthresh
    phdu.header['nfluxes'] = len(groupflux)
    hdul = [phdu]
    for i in range(len(groupflux)):
        hdul.append(BinTableHDU(sourcegroup[i],
                                name='detected_sources{}'.format(i)))
        hdul.append(BinTableHDU(fsourcegroup[i],
                                name='fake_sources{}'.format(i)))
        hdul[0].header['flux{}'.format(i)] = '{}'.format(groupflux[i])
    hdul = HDUList(hdul)
    hdul.writeto(outname, overwrite=True)
    print('{} different fluxes, tables written to {}'.format(len(groupflux),
                                                             outname))
LUSTIG Peter's avatar
LUSTIG Peter committed
124 125 126


directory = 'montecarlo_results/700/'
LUSTIG Peter's avatar
ready  
LUSTIG Peter committed
127 128
outname = 'NEW_combine_fct_result.fits'
key = '*700.fits'
LUSTIG Peter's avatar
LUSTIG Peter committed
129 130

Combine(directory, key, outname)
LUSTIG Peter's avatar
LUSTIG Peter committed
131 132

'''
LUSTIG Peter's avatar
LUSTIG Peter committed
133 134 135

'''

LUSTIG Peter's avatar
LUSTIG Peter committed
136
'''
137 138 139
hdul[0].header['nfluxes'] = n
hdul = HDUList(hdul)
hdul.writeto(outname, overwrite=True)
140
print('tables written to {}'.format(outname))
LUSTIG Peter's avatar
LUSTIG Peter committed
141
'''