lsst.synpipe  15.0-4-g9ee0f43+11
makeBlendedCat.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """Make a catalog of fakes that are highly blended."""
4 
5 from builtins import range
6 from __future__ import (division, print_function)
7 
8 import os
9 import random
10 import argparse
11 
12 import numpy as np
13 from astropy.table import Table, Column
14 
15 
16 def disturbRaDec(num, mu=0.6, sigma=0.6):
17  """Disturb the coordinate a little bit."""
18  return (np.random.normal(mu, sigma, num) / 3600.0)
19 
20 
21 def makeBlendedCat(fakeCat, realCat, raCol='ra', decCol='dec',
22  sigma=0.6, mu=0.6):
23  """Make a highly blended version of fake catalog."""
24  # Fake catalog
25  if not os.path.isfile(fakeCat):
26  raise Exception('# Can not find input fake catalog : %s' % fakeCat)
27  else:
28  fakeTab = Table.read(fakeCat, format='fits')
29  nFake = len(fakeTab)
30  print("# There are %d fake galaxies in the catalog" % nFake)
31  # Name of the output catalog
32  blendTab = fakeCat.replace('.fits', '_highb.fits')
33 
34  # Real catalog
35  if not os.path.isfile(realCat):
36  raise Exception('# Can not find input real catalog : %s' % realCat)
37  else:
38  realTab = Table.read(realCat, format='fits')
39  nReal = len(realTab)
40  print("# There are %d real galaxies in the catalog" % nReal)
41 
42  # Randomly select nFake galaxies from the realCat
43  indices = random.sample(list(range(nReal)), nFake)
44 
45  # Replace the RA, DEC with a small shift
46  fakeTab.add_column(Column(realTab[indices][raCol], name='RA_ori'))
47  fakeTab.add_column(Column(realTab[indices][decCol], name='Dec_ori'))
48 
49  fakeTab['RA'] = (realTab[indices][raCol] + disturbRaDec(nFake,
50  mu=mu,
51  sigma=sigma))
52  fakeTab['Dec'] = (realTab[indices][decCol] + disturbRaDec(nFake,
53  mu=mu,
54  sigma=sigma))
55 
56  # Save the new catalog
57  fakeTab.write(blendTab, format='fits', overwrite=True)
58 
59  return fakeTab
60 
61 
62 if __name__ == '__main__':
63 
64  parser = argparse.ArgumentParser()
65  parser.add_argument("fakeCat", help="Input catalog of fake objects")
66  parser.add_argument("realCat", help="Catalog of real objects")
67  parser.add_argument('--ra', '--raCol', dest='raCol',
68  help='Column for RA in realCat',
69  default='ra')
70  parser.add_argument('--dec', '--decCol', dest='decCol',
71  help='Column for DEC in realCat',
72  default='dec')
73  parser.add_argument('--mu', dest='mu',
74  help='mu of a normal distribution',
75  default=0.6, type=float)
76  parser.add_argument('--sigma', dest='sigma',
77  help='sigma of a normal distribution',
78  default=0.6, type=float)
79 
80  args = parser.parse_args()
81 
82  makeBlendedCat(args.fakeCat, args.realCat,
83  raCol=args.raCol, decCol=args.decCol,
84  sigma=args.sigma, mu=args.mu)
def makeBlendedCat(fakeCat, realCat, raCol='ra', decCol='dec', sigma=0.6, mu=0.6)
def disturbRaDec(num, mu=0.6, sigma=0.6)