2 Create N random selected (RA, Dec) pairs within a desired region.
4 1. RA, Dec range: (minRa, maxRa, minDec, maxDec)
5 2. DataId for single frame image: (visit, ccd)
6 3. DataId for coadd image: (tract, patch, filter)
13 from numpy.random
import uniform
17 from shapely.wkt
import loads
19 wkbFile = open(wkbName,
'r')
20 polyWkb = wkbFile.read().decode(
'hex')
31 Get the Ra,Dec range for certain single frame or coadded image
38 exposure = butler.get(dataType, dataId)
40 bboxI = exposure.getBBox(afwImage.PARENT)
41 wcs = exposure.getWcs()
43 minPix = wcs.pixelToSky(bboxI.getMinX(), bboxI.getMinY())
44 maxPix = wcs.pixelToSky(bboxI.getMaxX(), bboxI.getMaxY())
46 ra1 = minPix.getLongitude().asDegrees()
47 ra2 = maxPix.getLongitude().asDegrees()
48 dec1 = minPix.getLatitude().asDegrees()
49 dec2 = maxPix.getLatitude().asDegrees()
51 minRa, maxRa =
min(ra1, ra2),
max(ra1, ra2)
52 minDec, maxDec =
min(dec1, dec2),
max(dec1, dec2)
54 return [minRa, maxRa, minDec, maxDec]
59 Randomly select Ra,Dec pairs from the input Ra,Dec range
61 if minRa > maxRa
or minDec > maxDec:
62 raise Exception(
'Please provide appropriate Ra,Dec range !')
65 raArr = uniform(low=minRa, high=maxRa, size=nRand)
66 decArr = uniform(low=minDec, high=maxDec, size=nRand)
67 return list(zip(raArr, decArr))
75 while len(raArr) < nRand:
77 raArr.append(uniform(low=minRa, high=maxRa))
78 decArr.append(uniform(low=minDec, high=maxDec))
81 raTry = uniform(low=minRa, high=maxRa)
82 decTry = uniform(low=minDec, high=maxDec)
83 coordTry = afwGeom.SpherePoint(raTry, decTry, afwGeom.degrees)
86 for ii
in range(nExist):
87 coordTest = afwGeom.SpherePoint(raArr[ii], decArr[ii], afwGeom.degrees)
88 sep = coordTry.separation(coordTest).asArcseconds()
95 return list(zip(raArr, decArr))
100 Plot the distribution of radom Ra, Dec for examination
102 import matplotlib.pyplot
as plt
104 plt.scatter(*
list(zip(*randomRaDec)))
105 plt.xlabel(
r'RA (J2000)', fontsize=20, labelpad=20)
106 plt.ylabel(
r'DEC (J2000)', fontsize=20, labelpad=20)
110 if rangeRaDec is not None:
111 if type(rangeRaDec) is dict:
112 raMin, raMax = rangeRaDec['raMin'], rangeRaDec['raMax']
113 decMin, decMax = rangeRaDec['raMin'], rangeRaDec['raMax']
115 raMin, raMax = rangeRaDec[0], rangeRaDec[1]
116 decMin, decMax = rangeRaDec[0], rangeRaDec[1]
117 raDec0 = (raMin, decMin)
118 raRange = (raMax - raMin)
119 decRange = (decMax - decMin)
122 plt.gcf().savefig(
'randomRaDec.png')
128 rootDir='/lustre/Subaru/SSP/rerun/yasuda/SSP3.8.5_20150725/',
129 inputCat=None, plot=False, acpMask=None, rejMask=None):
131 Generate nRand random RA,Dec pairs in a desired region of sky
132 The region can be defined by:
133 1) Single frame image
138 if dataId
is not None:
139 if 'visit' in list(dataId.keys())
and 'ccd' in list(dataId.keys()):
143 rangeRaDec[2], rangeRaDec[3], rad=rad)
144 elif (
'tract' in list(dataId.keys())
and
145 'patch' in list(dataId.keys())
and
146 'filter' in dataId.keys):
149 dataType=
'deepCoadd_calexp')
151 rangeRaDec[2], rangeRaDec[3], rad=rad)
153 raise KeyError(
'Please provide the correct dataId !')
154 elif rangeRaDec
is not None:
155 if type(rangeRaDec)
is dict:
159 rangeRaDec[
'minDec'],
160 rangeRaDec[
'maxDec'], rad=rad)
161 elif (
type(rangeRaDec)
is list
or type(rangeRaDec).__module__ ==
'numpy'):
162 if len(rangeRaDec) >= 4:
164 rangeRaDec[1], rangeRaDec[2],
165 rangeRaDec[3], rad=rad)
167 raise Exception(
'randomRaDec should have at least 4 elements!')
169 raise Exception(
'randomRaDec need to be Dict/List/Numpy.Array')
171 raise Exception(
"Need to provide either dataId or rangeRaDec")
174 Add by Song Huang 15-09-01
175 Filter the random catalog through two masks
177 if acpMask
is not None or rejMask
is not None:
179 from shapely.geometry
import Point
180 from shapely.prepared
import prep
182 raArr, decArr = np.array(
list(zip(*randomRaDec)))
183 if os.path.isfile(acpMask):
185 acpPrep = prep(acpRegs)
186 inside =
list(map(
lambda x, y: acpPrep.contains(Point(x, y)),
189 inside = np.isfinite(raArr)
190 if os.path.isfile(rejMask):
192 rejPrep = prep(rejRegs)
193 masked =
list(map(
lambda x, y: rejPrep.contains(Point(x, y)),
196 masked = np.isnan(raArr)
197 useful =
list(map(
lambda x, y: x
and (
not y), inside, masked))
198 randomUse =
list(zip(raArr[useful], decArr[useful]))
200 warnings.warn(
'Please install the Shapely')
201 randomUse = randomRaDec
203 randomUse = randomRaDec
208 if inputCat
is not None:
213 if os.path.exists(inputCat):
215 galCat = astropy.table.Table.read(inputCat, format=
'fits')
216 outCat = inputCat.strip().replace(
'.fits',
'_radec.fits')
221 raArr, decArr = np.array(
list(zip(*randomUse)))
222 raCol = astropy.table.Column(name=
'RA', data=raArr)
223 decCol = astropy.table.Column(name=
'Dec', data=decArr)
224 galCat.add_columns([raCol, decCol])
227 raArr, decArr = np.array(
list(zip(*random.sample(randomUse, nGal))))
228 raCol = astropy.table.Column(name=
'RA', data=raArr)
229 decCol = astropy.table.Column(name=
'Dec', data=decArr)
230 galCat.add_columns([raCol, decCol])
233 indGal = np.arange(nGal)
234 galCatRand = galCat[random.sample(indGal, nRand)]
235 raArr, decArr = np.array(
list(zip(*randomUse)))
236 raCol = astropy.table.Column(name=
'RA', data=raArr)
237 decCol = astropy.table.Column(name=
'Dec', data=decArr)
238 galCatRand.add_columns([raCol, decCol])
241 galCat.write(outCat, format=
'fits', overwrite=
True)
243 raise Exception(
'Can not find input catalog %s!' % inputCat)