lsst.synpipe  15.0-3-g11fe1a0+10
makeRaDecCat.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 """
4 Create N random selected (RA, Dec) pairs within a desired region.
5 The inputs can be:
6  1. RA, Dec range: (minRa, maxRa, minDec, maxDec)
7  2. DataId for single frame image: (visit, ccd)
8  3. DataId for coadd image: (tract, patch, filter)
9 """
10 
11 from builtins import map
12 from builtins import zip
13 from builtins import range
14 import os
15 import warnings
16 
17 import numpy as np
18 from numpy.random import uniform
19 
20 
21 def polyReadWkb(wkbName, load=True):
22  from shapely.wkt import loads
23 
24  wkbFile = open(wkbName, 'r')
25  polyWkb = wkbFile.read().decode('hex')
26  wkbFile.close()
27 
28  if load is True:
29  return loads(polyWkb)
30  else:
31  return polyWkb
32 
33 
34 def getImageRaDecRange(rootDir, dataId, dataType='calexp'):
35  """
36  Get the Ra,Dec range for certain single frame or coadded image
37  """
38 
39  import lsst.daf.persistence as dafPersist
40  import lsst.afw.image as afwImage
41 
42  butler = dafPersist.Butler(rootDir)
43  exposure = butler.get(dataType, dataId)
44 
45  bboxI = exposure.getBBox(afwImage.PARENT)
46  wcs = exposure.getWcs()
47 
48  minPix = wcs.pixelToSky(bboxI.getMinX(), bboxI.getMinY())
49  maxPix = wcs.pixelToSky(bboxI.getMaxX(), bboxI.getMaxY())
50 
51  ra1 = minPix.getLongitude().asDegrees()
52  ra2 = maxPix.getLongitude().asDegrees()
53  dec1 = minPix.getLatitude().asDegrees()
54  dec2 = maxPix.getLatitude().asDegrees()
55 
56  minRa, maxRa = min(ra1, ra2), max(ra1, ra2)
57  minDec, maxDec = min(dec1, dec2), max(dec1, dec2)
58 
59  return [minRa, maxRa, minDec, maxDec]
60 
61 
62 def getRandomRaDec(nRand, minRa, maxRa, minDec, maxDec, rad=None):
63  """
64  Randomly select Ra,Dec pairs from the input Ra,Dec range
65  """
66  if minRa > maxRa or minDec > maxDec:
67  raise Exception('Please provide appropriate Ra,Dec range !')
68 
69  if rad is None:
70  raArr = uniform(low=minRa, high=maxRa, size=nRand)
71  decArr = uniform(low=minDec, high=maxDec, size=nRand)
72  return list(zip(raArr, decArr))
73  else:
74  import lsst.afw.geom as afwGeom
75 
76  minSep = float(rad)
77  raArr = []
78  decArr = []
79  numTry = 0
80  while len(raArr) < nRand:
81  if numTry == 0:
82  raArr.append(uniform(low=minRa, high=maxRa))
83  decArr.append(uniform(low=minDec, high=maxDec))
84  numTry += 1
85  else:
86  raTry = uniform(low=minRa, high=maxRa)
87  decTry = uniform(low=minDec, high=maxDec)
88  coordTry = afwGeom.SpherePoint(raTry, decTry, afwGeom.degrees)
89  nExist = len(raArr)
90  sepGood = True
91  for ii in range(nExist):
92  coordTest = afwGeom.SpherePoint(raArr[ii], decArr[ii], afwGeom.degrees)
93  sep = coordTry.separation(coordTest).asArcseconds()
94  if sep <= minSep:
95  sepGood = False
96  break
97  if sepGood:
98  raArr.append(raTry)
99  decArr.append(decTry)
100  return list(zip(raArr, decArr))
101 
102 
103 def plotRandomRaDec(randomRaDec, rangeRaDec=None):
104  """
105  Plot the distribution of radom Ra, Dec for examination
106  """
107  import matplotlib.pyplot as plt
108 
109  plt.scatter(*list(zip(*randomRaDec)))
110  plt.xlabel(r'RA (J2000)', fontsize=20, labelpad=20)
111  plt.ylabel(r'DEC (J2000)', fontsize=20, labelpad=20)
112 
113  # TODO : To be finished
114  """
115  if rangeRaDec is not None:
116  if type(rangeRaDec) is dict:
117  raMin, raMax = rangeRaDec['raMin'], rangeRaDec['raMax']
118  decMin, decMax = rangeRaDec['raMin'], rangeRaDec['raMax']
119  else:
120  raMin, raMax = rangeRaDec[0], rangeRaDec[1]
121  decMin, decMax = rangeRaDec[0], rangeRaDec[1]
122  raDec0 = (raMin, decMin)
123  raRange = (raMax - raMin)
124  decRange = (decMax - decMin)
125  """
126 
127  plt.gcf().savefig('randomRaDec.png')
128 
129  return None
130 
131 
132 def makeRaDecCat(nRand, dataId=None, rangeRaDec=None, rad=None,
133  rootDir='/lustre/Subaru/SSP/rerun/yasuda/SSP3.8.5_20150725/',
134  inputCat=None, plot=False, acpMask=None, rejMask=None):
135  """
136  Generate nRand random RA,Dec pairs in a desired region of sky
137  The region can be defined by:
138  1) Single frame image
139  2) Coadd image
140  3) RA, Dec range
141  """
142 
143  if dataId is not None:
144  if 'visit' in list(dataId.keys()) and 'ccd' in list(dataId.keys()):
145  # Input should be a single frame image
146  rangeRaDec = getImageRaDecRange(rootDir, dataId)
147  randomRaDec = getRandomRaDec(nRand, rangeRaDec[0], rangeRaDec[1],
148  rangeRaDec[2], rangeRaDec[3], rad=rad)
149  elif ('tract' in list(dataId.keys())
150  and 'patch' in list(dataId.keys())
151  and 'filter' in dataId.keys):
152  # Input should be a coadd image
153  rangeRaDec = getImageRaDecRange(rootDir, dataId,
154  dataType='deepCoadd_calexp')
155  randomRaDec = getRandomRaDec(nRand, rangeRaDec[0], rangeRaDec[1],
156  rangeRaDec[2], rangeRaDec[3], rad=rad)
157  else:
158  raise KeyError('Please provide the correct dataId !')
159  elif rangeRaDec is not None:
160  if type(rangeRaDec) is dict:
161  # rKeys = rangeRaDec.keys()
162  randomRaDec = getRandomRaDec(nRand, rangeRaDec['minRa'],
163  rangeRaDec['maxRa'],
164  rangeRaDec['minDec'],
165  rangeRaDec['maxDec'], rad=rad)
166  elif (type(rangeRaDec) is list
167  or type(rangeRaDec).__module__ == 'numpy'):
168  if len(rangeRaDec) >= 4:
169  randomRaDec = getRandomRaDec(nRand, rangeRaDec[0],
170  rangeRaDec[1], rangeRaDec[2],
171  rangeRaDec[3], rad=rad)
172  else:
173  raise Exception('randomRaDec should have at least 4 elements!')
174  else:
175  raise Exception('randomRaDec need to be Dict/List/Numpy.Array')
176  else:
177  raise Exception("Need to provide either dataId or rangeRaDec")
178 
179  """
180  Add by Song Huang 15-09-01
181  Filter the random catalog through two masks
182  """
183  if acpMask is not None or rejMask is not None:
184  try:
185  from shapely.geometry import Point
186  from shapely.prepared import prep
187 
188  raArr, decArr = np.array(list(zip(*randomRaDec)))
189  if os.path.isfile(acpMask):
190  acpRegs = polyReadWkb(acpMask)
191  acpPrep = prep(acpRegs)
192  inside = list(map(lambda x, y: acpPrep.contains(Point(x, y)),
193  raArr, decArr))
194  else:
195  inside = np.isfinite(raArr)
196  if os.path.isfile(rejMask):
197  rejRegs = polyReadWkb(rejMask)
198  rejPrep = prep(rejRegs)
199  masked = list(map(lambda x, y: rejPrep.contains(Point(x, y)),
200  raArr, decArr))
201  else:
202  masked = np.isnan(raArr)
203  useful = list(map(lambda x, y: x and (not y), inside, masked))
204  randomUse = list(zip(raArr[useful], decArr[useful]))
205  except ImportError:
206  warnings.warn('Please install the Shapely')
207  randomUse = randomRaDec
208  else:
209  randomUse = randomRaDec
210 
211  if plot:
212  plotRandomRaDec(randomUse, rangeRaDec=rangeRaDec)
213 
214  if inputCat is not None:
215 
216  import os
217  import astropy.table
218 
219  if os.path.exists(inputCat):
220 
221  galCat = astropy.table.Table.read(inputCat, format='fits')
222  outCat = inputCat.strip().replace('.fits', '_radec.fits')
223 
224  nGal = len(galCat)
225 
226  if nGal == nRand:
227  raArr, decArr = np.array(list(zip(*randomUse)))
228  raCol = astropy.table.Column(name='RA', data=raArr)
229  decCol = astropy.table.Column(name='Dec', data=decArr)
230  galCat.add_columns([raCol, decCol])
231  elif nGal < nRand:
232  import random
233  raArr, decArr = np.array(list(zip(*random.sample(randomUse, nGal))))
234  raCol = astropy.table.Column(name='RA', data=raArr)
235  decCol = astropy.table.Column(name='Dec', data=decArr)
236  galCat.add_columns([raCol, decCol])
237  else:
238  import random
239  indGal = np.arange(nGal)
240  galCatRand = galCat[random.sample(indGal, nRand)]
241  raArr, decArr = np.array(list(zip(*randomUse)))
242  raCol = astropy.table.Column(name='RA', data=raArr)
243  decCol = astropy.table.Column(name='Dec', data=decArr)
244  galCatRand.add_columns([raCol, decCol])
245  galCat = galCatRand
246 
247  galCat.write(outCat, format='fits', overwrite=True)
248  else:
249  raise Exception('Can not find input catalog %s!' % inputCat)
250 
251  return randomUse
def makeRaDecCat(nRand, dataId=None, rangeRaDec=None, rad=None, rootDir='/lustre/Subaru/SSP/rerun/yasuda/SSP3.8.5_20150725/', inputCat=None, plot=False, acpMask=None, rejMask=None)
def getRandomRaDec(nRand, minRa, maxRa, minDec, maxDec, rad=None)
Definition: makeRaDecCat.py:62
def plotRandomRaDec(randomRaDec, rangeRaDec=None)
def polyReadWkb(wkbName, load=True)
Definition: makeRaDecCat.py:21
def getImageRaDecRange(rootDir, dataId, dataType='calexp')
Definition: makeRaDecCat.py:34