-
Notifications
You must be signed in to change notification settings - Fork 2
/
combine_catalogs.py
96 lines (78 loc) · 3.32 KB
/
combine_catalogs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Script for combining the three individual catalogs of g, r, and u band stars
into one catalog.
"""
from fits_utils import *
def read_catalog(filename):
"""Function for reading in the data from a catalog.
Reads in .cat files as ndarrays. Rearranges the ndarrays so that the columns
are in a more suitable order.
Args:
filename (str): Filename and location of the specific catalog.
Returns:
catalog (ndarray): Ndarray of the catalog data.
"""
catalog = np.loadtxt(filename)
permutation = [0, 3, 4, 1, 2]
index = np.empty_like(permutation)
index[permutation] = np.arange(len(permutation))
catalog[:] = catalog[:, index]
return catalog
def create_catalog(catalog, filename, cat_dir):
"""Method for creating a new catalog file from an ndarray.
Takes an ndarray and writes it out to a new catalog file with a '.cat'
extension. Creates a header for this file with the indexes included.
Args:
catalog (ndarray): Merged catalog data.
filename (str): Filename of the new catalog. Do not include the
extension, this is added automatically.
"""
#: str: New header text for the output file.
header_txt = '\n'.join(['[0] : NUMBER',
'[1] : ALPHAPEAK_J2000',
'[2] : DELTAPEAK_J2000',
'[3] : FLUX_APER_G',
'[4] : FLUXERR_APER_G',
'[5] : FLUX_APER_R',
'[6] : FLUXERR_APER_R',
'[7] : FLUX_APER_U',
'[8] : FLUXERR_APER_U'])
np.savetxt(cat_dir+'{}.cat'.format(filename), catalog, header=header_txt)
def match_sources(catalog_1, catalog_2):
"""Function for matching sources in two catalogs.
Takes two catalogs. For each catalog, matches sources based on their RA and
DEC. Returns a new catalog.
Args:
catalog_1 (ndarray): First catalog to be matched.
catalog_2 (ndarray): Second catalog to be matched.
Returns:
new_catalog (ndarray): New catalog containing the RA, DEC, fluxes, and
flux errors of the two older catalogs.
"""
new_catalog = []
for source in catalog_1:
index = np.where((np.isclose(catalog_2[:,1],source[1],rtol=1e-05, atol=1e-08)) &
(np.isclose(catalog_2[:,2],source[2],rtol=1e-05, atol=1e-08)))[0]
if np.size(index) != 0:
new_catalog.append(np.append(source, catalog_2[index[0], 3:5]))
new_catalog = np.vstack(new_catalog)
return new_catalog
def main():
#: str: catlogue directory
cat_dir = "cat/cumulative_trim/"
#: tuple: Catalog bands. Should just be g, r, u.
bands = ('g', 'r', 'u')
#: dict: For storing the raw catalog ndarrays.
catalog = {}
for band in bands:
#: Store the catalogs.
catalog[band] = read_catalog(cat_dir+'{}.cat'.format(band))
#: ndarray: New merged catalog for the g and r bands.
gr_catalog = match_sources(catalog['g'], catalog['r'])
gr_catalog = remove_intervening_stars(gr_catalog)
#: ndarray: New merged catalog for all bands.
ugr_catalog = match_sources(gr_catalog, catalog['u'])
create_catalog(ugr_catalog, 'ugr', cat_dir)
create_catalog(gr_catalog, 'gr', cat_dir)
if __name__ == '__main__':
main()