def setUp(self): ##Load sample input from disk path = os.path.join(eups.productDir("meas_pipeline"), "tests") srcSet = readSourceSet(os.path.join(path, "v695833-e0-c000.xy.txt")) #Make a copy, with different fluxes. #The exact choice doesn't matter ,we just want to make sure the code returns an answer #Also need to specify that each source is a star catSet = [] flags = malgUtil.getDetectionFlags() goodFlag = flags['BINNED1'] | flags['STAR'] for s in srcSet: s1 = afwDet.Source(s) s1.setPsfFlux(s1.getPsfFlux() * .281) catSet.append(s1) s.setFlagForDetection(goodFlag) #Make a SourceMatch object maxDist = 1 / 3600. #matches must be this close together srcMatchSet = afwDet.matchXy(catSet, srcSet, maxDist) #Put them on the clipboard filename = pexPolicy.DefaultPolicyFile("meas_pipeline", "PhotoCalStageDictionary.paf", "policy") self.policy = pexPolicy.Policy.createPolicy(filename) self.clipboard = pexClipboard.Clipboard() self.clipboard.put(self.policy.get("sourceMatchSetKey"), srcMatchSet) self.clipboard.put(self.policy.get("inputExposureKey"), afwImage.ExposureF(afwGeom.Extent2I(10, 10)))
def setUp(self): ##Load sample input from disk path = os.path.join(eups.productDir("meas_pipeline"), "tests") srcSet = readSourceSet(os.path.join(path, "v695833-e0-c000.xy.txt")) #Make a copy, with different fluxes. #The exact choice doesn't matter ,we just want to make sure the code returns an answer #Also need to specify that each source is a star catSet = [] flags = malgUtil.getDetectionFlags() goodFlag = flags['BINNED1'] | flags['STAR'] for s in srcSet: s1 = afwDet.Source(s) s1.setPsfFlux(s1.getPsfFlux()*.281) catSet.append(s1) s.setFlagForDetection(goodFlag) #Make a SourceMatch object maxDist = 1/3600. #matches must be this close together srcMatchSet = afwDet.matchXy(catSet, srcSet, maxDist) #Put them on the clipboard filename = pexPolicy.DefaultPolicyFile("meas_pipeline", "PhotoCalStageDictionary.paf", "policy") self.policy = pexPolicy.Policy.createPolicy(filename) self.clipboard = pexClipboard.Clipboard() self.clipboard.put(self.policy.get("sourceMatchSetKey"), srcMatchSet) self.clipboard.put(self.policy.get("inputExposureKey"), afwImage.ExposureF(afwGeom.Extent2I(10, 10)))
def getCalibObjects(butler, filterName, dataId): """ A version of getCalibObjectsImpl that isn't a class method, for use by other code @param useOutputSrc # use fluxes from the "src", not "icSrc" """ ################################# # color calibration # # this stuff stolen from pipette. # - numbers from suprimecam.paf, method in Calibrate.py ##################### primaryLookup = { #"B" : "g", #"V" : "g", #"R" : "r", #"I" : "i", "g" : "g", "g" : "g", "r" : "r", "i" : "i", } # The below colour terms are from the last page of # http://www.naoj.org/staff/nakata/suprime/illustration/colorterm_report_ver3.pdf secondaryLookup = { "g" : ["r", [-0.00569, -0.0427]], "r" : ["g", [0.00261, 0.0304]], "i" : ["r", [0.00586, 0.0827, -0.0118]], "z" : ['i', [0.000329, 0.0608, 0.0219]], "y" : ['i', [0.000329, 0.0608, 0.0219]], } # figure out which filters we need for the color term primaryFilter = primaryLookup[filterName] # Primary band for correction secondaryFilter = secondaryLookup[primaryFilter][0] # Secondary band for correction log = pexLog.Log.getDefaultLog() psources = butler.get('icSrc', dataId) pmatches = butler.get('icMatch', dataId) calexp_md = butler.get('calexp_md', dataId) wcs = afwImage.makeWcs(calexp_md) W, H = calexp_md.get("NAXIS1"), calexp_md.get("NAXIS2") calib = afwImage.Calib(calexp_md) matches = pmatches.getSourceMatches() setMatchListBlobsNone(matches) sources = psources.getSources() ## fails setSourceSetBlobsNone(sources) anid = pmatches.getSourceMatchMetadata().getInt('ANINDID') del psources; del pmatches; del calexp_md # cleanup useOutputSrc = False if useOutputSrc: srcs = butler.get('src', dataId).getSources() setSourceSetBlobsNone(srcs) import lsst.afw.detection as afwDetect pmMatch = afwDetect.matchXy(sources, srcs, 1.0, True) for icSrc, src, d in pmMatch: icSrc.setPsfFlux(src.getPsfFlux()) # ref sources xc, yc = 0.5*W, 0.5*H radec = wcs.pixelToSky(xc, yc) ra = radec.getLongitude(afwCoord.DEGREES) dec = radec.getLatitude(afwCoord.DEGREES) radius = wcs.pixelScale().asDegrees()*math.hypot(xc, yc)*1.1 pol = pexPolicy.Policy() pol.set('matchThreshold', 30) solver = measAstrom.createSolver(pol, log) idName = 'id' X = solver.getCatalogue(ra, dec, radius, primaryFilter, idName, anid) refsources = X.refsources setSourceSetBlobsNone(refsources) inds = X.inds referrs, stargal = None, None cols = solver.getTagAlongColumns(anid) colnames = [c.name for c in cols] col = 'starnotgal' if col in colnames: stargal1 = solver.getTagAlongBool(anid, col, inds) stargal = [] for i in range(len(stargal1)): stargal.append(stargal1[i]) fdict = maUtils.getDetectionFlags() # get the data for the secondary XX = solver.getCatalogue(ra, dec, radius, secondaryFilter, idName, anid) secondaryRefsources = XX.refsources setSourceSetBlobsNone(secondaryRefsources) polyData = secondaryLookup[primaryFilter][1] # Polynomial correction polyData.reverse() # Numpy wants decreasing powers polynomial = numpy.poly1d(polyData) # We already have the 'primary' magnitudes in the matches secondariesDict = dict() for s in secondaryRefsources: secondariesDict[s.getId()] = (s.getPsfFlux(), s.getPsfFluxErr()) del secondaryRefsources keepref = [] keepi = [] for i in xrange(len(refsources)): ra, dec = refsources[i].getRa(), refsources[i].getDec() # ra,dec in Rads x, y = wcs.skyToPixel(afwCoord.Coord(afwGeom.PointD(numpy.degrees(ra), numpy.degrees(dec)))) if x < 0 or y < 0 or x > W or y > H: continue refsources[i].setXAstrom(x) refsources[i].setYAstrom(y) if stargal[i]: refsources[i].setFlagForDetection(refsources[i].getFlagForDetection() | fdict["STAR"]) # color term primaryMag = -2.5*numpy.log10(refsources[i].getPsfFlux()) secondaryMag = -2.5*numpy.log10(secondariesDict[refsources[i].getId()][0]) diff = polynomial(primaryMag - secondaryMag) refsources[i].setPsfFlux(10.0**(-0.4*(primaryMag+diff))) keepref.append(refsources[i]) keepi.append(i) refsources = keepref if referrs is not None: referrs = [referrs[i] for i in keepi] if stargal is not None: stargal = [stargal[i] for i in keepi] measAstrom.joinMatchList(matches, refsources, first=True, log=log) measAstrom.joinMatchList(matches, sources, first=False, log=log) return matches, calib, refsources
def plotsForField(inButler, keys, fixup, plots=None, prefix=''): if plots is None: plots = ['photom', 'matches', 'corr', 'distortion'] filters = inButler.queryMetadata('raw', 'filter', **keys) print('Filters:', filters) filterName = filters[0] try: psources = inButler.get('icSrc', **keys) print('Got sources', psources) except Exception: print('"icSrc" not found. Trying "src" instead.') psources = inButler.get('src', **keys) print('Got sources', psources) pmatches = inButler.get('icMatch', **keys) sources = psources.getSources() calexp = inButler.get('calexp', **keys) wcs = calexp.getWcs() photocal = calexp.getCalib() zp = photocal.getMagnitude(1.) print('Zeropoint is', zp) # ref sources W, H = calexp.getWidth(), calexp.getHeight() log = Log.getDefaultLogger() log.setLevel(Log.DEBUG) kwargs = {} if fixup: # ugh, mask and offset req'd because source ids are assigned at write-time # and match list code made a deep copy before that. # (see svn+ssh://svn.lsstcorp.org/DMS/meas/astrom/tickets/1491-b r18027) kwargs['sourceIdMask'] = 0xffff kwargs['sourceIdOffset'] = -1 (matches, ref) = measAstrom.generateMatchesFromMatchList( pmatches, sources, wcs, W, H, returnRefs=True, log=log, **kwargs) print('Got', len(ref), 'reference catalog sources') # pull 'stargal' and 'referrs' arrays out of the reference sources fdict = maUtils.getDetectionFlags() starflag = int(fdict["STAR"]) stargal = [bool((r.getFlagForDetection() & starflag) > 0) for r in ref] referrs = [float(r.getPsfInstFluxErr() / r.getPsfInstFlux() * 2.5 / -np.log(10)) for r in ref] nstars = sum([1 for s in stargal if s]) print('Number of sources with STAR set:', nstars) visit = keys['visit'] raft = keys['raft'] sensor = keys['sensor'] prefix += 'imsim-v%i-r%s-s%s' % (visit, raft.replace(',', ''), sensor.replace(',', '')) if 'photom' in plots: print('photometry plots...') tt = 'LSST ImSim v%i r%s s%s' % (visit, raft.replace(',', ''), sensor.replace(',', '')) wcsPlots.plotPhotometry(sources, ref, matches, prefix, band=filterName, zp=zp, referrs=referrs, refstargal=stargal, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix, band=filterName, zp=zp, delta=True, referrs=referrs, refstargal=stargal, title=tt) # test w/ and w/o referrs and stargal. if False: wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'A', band=filterName, zp=zp, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'B', band=filterName, zp=zp, referrs=referrs, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'C', band=filterName, zp=zp, refstargal=stargal, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'A', band=filterName, zp=zp, delta=True, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'B', band=filterName, zp=zp, delta=True, referrs=referrs, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'C', band=filterName, zp=zp, delta=True, refstargal=stargal, title=tt) if 'matches' in plots: print('matches...') wcsPlots.plotMatches(sources, ref, matches, wcs, W, H, prefix) if 'distortion' in plots: print('distortion...') wcsPlots.plotDistortion(wcs, W, H, 400, prefix, 'SIP Distortion (exaggerated x 10)', exaggerate=10.) print('distortion...') wcsPlots.plotDistortion(wcs, W, H, 400, prefix, 'SIP Distortion (exaggerated x 100)', exaggerate=100., suffix='-distort2.')
def plotsForField(inButler, keys, fixup, plots=None, prefix=''): if plots is None: plots = ['photom', 'matches', 'corr', 'distortion'] filters = inButler.queryMetadata('raw', 'filter', **keys) print('Filters:', filters) filterName = filters[0] psources = inButler.get('icSrc', **keys) # since the butler does lazy evaluation, we don't know if it fails until... try: print('Got sources', psources) except: print('"icSrc" not found. Trying "src" instead.') psources = inButler.get('src', **keys) print('Got sources', psources) pmatches = inButler.get('icMatch', **keys) sources = psources.getSources() calexp = inButler.get('calexp', **keys) wcs = calexp.getWcs() photocal = calexp.getCalib() zp = photocal.getMagnitude(1.) print('Zeropoint is', zp) # ref sources W, H = calexp.getWidth(), calexp.getHeight() log = Log.getDefaultLogger() log.setLevel(Log.DEBUG) kwargs = {} if fixup: # ugh, mask and offset req'd because source ids are assigned at write-time # and match list code made a deep copy before that. # (see svn+ssh://svn.lsstcorp.org/DMS/meas/astrom/tickets/1491-b r18027) kwargs['sourceIdMask'] = 0xffff kwargs['sourceIdOffset'] = -1 (matches, ref) = measAstrom.generateMatchesFromMatchList( pmatches, sources, wcs, W, H, returnRefs=True, log=log, **kwargs) print('Got', len(ref), 'reference catalog sources') # pull 'stargal' and 'referrs' arrays out of the reference sources fdict = maUtils.getDetectionFlags() starflag = int(fdict["STAR"]) stargal = [bool((r.getFlagForDetection() & starflag) > 0) for r in ref] referrs = [float(r.getPsfFluxErr() / r.getPsfFlux() * 2.5 / -np.log(10)) for r in ref] nstars = sum([1 for s in stargal if s]) print('Number of sources with STAR set:', nstars) visit = keys['visit'] raft = keys['raft'] sensor = keys['sensor'] prefix += 'imsim-v%i-r%s-s%s' % (visit, raft.replace(',', ''), sensor.replace(',', '')) if 'photom' in plots: print('photometry plots...') tt = 'LSST ImSim v%i r%s s%s' % (visit, raft.replace(',', ''), sensor.replace(',', '')) wcsPlots.plotPhotometry(sources, ref, matches, prefix, band=filterName, zp=zp, referrs=referrs, refstargal=stargal, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix, band=filterName, zp=zp, delta=True, referrs=referrs, refstargal=stargal, title=tt) # test w/ and w/o referrs and stargal. if False: wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'A', band=filterName, zp=zp, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'B', band=filterName, zp=zp, referrs=referrs, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'C', band=filterName, zp=zp, refstargal=stargal, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'A', band=filterName, zp=zp, delta=True, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'B', band=filterName, zp=zp, delta=True, referrs=referrs, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'C', band=filterName, zp=zp, delta=True, refstargal=stargal, title=tt) if 'matches' in plots: print('matches...') wcsPlots.plotMatches(sources, ref, matches, wcs, W, H, prefix) if 'distortion' in plots: print('distortion...') wcsPlots.plotDistortion(wcs, W, H, 400, prefix, 'SIP Distortion (exaggerated x 10)', exaggerate=10.) print('distortion...') wcsPlots.plotDistortion(wcs, W, H, 400, prefix, 'SIP Distortion (exaggerated x 100)', exaggerate=100., suffix='-distort2.')
def getCalibObjects(butler, filterName, dataId): """ A version of getCalibObjectsImpl that isn't a class method, for use by other code @param useOutputSrc # use fluxes from the "src", not "icSrc" """ ################################# # color calibration # # this stuff stolen from pipette. # - numbers from suprimecam.paf, method in Calibrate.py ##################### primaryLookup = { #"B" : "g", #"V" : "g", #"R" : "r", #"I" : "i", "g": "g", "g": "g", "r": "r", "i": "i", } # The below colour terms are from the last page of # http://www.naoj.org/staff/nakata/suprime/illustration/colorterm_report_ver3.pdf secondaryLookup = { "g": ["r", [-0.00569, -0.0427]], "r": ["g", [0.00261, 0.0304]], "i": ["r", [0.00586, 0.0827, -0.0118]], "z": ['i', [0.000329, 0.0608, 0.0219]], "y": ['i', [0.000329, 0.0608, 0.0219]], } # figure out which filters we need for the color term primaryFilter = primaryLookup[filterName] # Primary band for correction secondaryFilter = secondaryLookup[primaryFilter][ 0] # Secondary band for correction log = pexLog.Log.getDefaultLog() psources = butler.get('icSrc', dataId) pmatches = butler.get('icMatch', dataId) calexp_md = butler.get('calexp_md', dataId) wcs = afwImage.makeWcs(calexp_md) W, H = calexp_md.get("NAXIS1"), calexp_md.get("NAXIS2") calib = afwImage.Calib(calexp_md) matches = pmatches.getSourceMatches() setMatchListBlobsNone(matches) sources = psources.getSources() ## fails setSourceSetBlobsNone(sources) anid = pmatches.getSourceMatchMetadata().getInt('ANINDID') del psources del pmatches del calexp_md # cleanup useOutputSrc = False if useOutputSrc: srcs = butler.get('src', dataId).getSources() setSourceSetBlobsNone(srcs) import lsst.afw.detection as afwDetect pmMatch = afwDetect.matchXy(sources, srcs, 1.0, True) for icSrc, src, d in pmMatch: icSrc.setPsfFlux(src.getPsfFlux()) # ref sources xc, yc = 0.5 * W, 0.5 * H radec = wcs.pixelToSky(xc, yc) ra = radec.getLongitude(afwCoord.DEGREES) dec = radec.getLatitude(afwCoord.DEGREES) radius = wcs.pixelScale().asDegrees() * math.hypot(xc, yc) * 1.1 pol = pexPolicy.Policy() pol.set('matchThreshold', 30) solver = measAstrom.createSolver(pol, log) idName = 'id' X = solver.getCatalogue(ra, dec, radius, primaryFilter, idName, anid) refsources = X.refsources setSourceSetBlobsNone(refsources) inds = X.inds referrs, stargal = None, None cols = solver.getTagAlongColumns(anid) colnames = [c.name for c in cols] col = 'starnotgal' if col in colnames: stargal1 = solver.getTagAlongBool(anid, col, inds) stargal = [] for i in range(len(stargal1)): stargal.append(stargal1[i]) fdict = maUtils.getDetectionFlags() # get the data for the secondary XX = solver.getCatalogue(ra, dec, radius, secondaryFilter, idName, anid) secondaryRefsources = XX.refsources setSourceSetBlobsNone(secondaryRefsources) polyData = secondaryLookup[primaryFilter][1] # Polynomial correction polyData.reverse() # Numpy wants decreasing powers polynomial = numpy.poly1d(polyData) # We already have the 'primary' magnitudes in the matches secondariesDict = dict() for s in secondaryRefsources: secondariesDict[s.getId()] = (s.getPsfFlux(), s.getPsfFluxErr()) del secondaryRefsources keepref = [] keepi = [] for i in xrange(len(refsources)): ra, dec = refsources[i].getRa(), refsources[i].getDec( ) # ra,dec in Rads x, y = wcs.skyToPixel( afwCoord.Coord( afwGeom.PointD(numpy.degrees(ra), numpy.degrees(dec)))) if x < 0 or y < 0 or x > W or y > H: continue refsources[i].setXAstrom(x) refsources[i].setYAstrom(y) if stargal[i]: refsources[i].setFlagForDetection( refsources[i].getFlagForDetection() | fdict["STAR"]) # color term primaryMag = -2.5 * numpy.log10(refsources[i].getPsfFlux()) secondaryMag = -2.5 * numpy.log10( secondariesDict[refsources[i].getId()][0]) diff = polynomial(primaryMag - secondaryMag) refsources[i].setPsfFlux(10.0**(-0.4 * (primaryMag + diff))) keepref.append(refsources[i]) keepi.append(i) refsources = keepref if referrs is not None: referrs = [referrs[i] for i in keepi] if stargal is not None: stargal = [stargal[i] for i in keepi] measAstrom.joinMatchList(matches, refsources, first=True, log=log) measAstrom.joinMatchList(matches, sources, first=False, log=log) return matches, calib, refsources
def plotsForField(inButler, keys, fixup, plots=None, prefix=''): if plots is None: plots = ['photom','matches','corr','distortion'] filters = inButler.queryMetadata('raw', 'filter', **keys) print 'Filters:', filters filterName = filters[0] psources = inButler.get('icSrc', **keys) # since the butler does lazy evaluation, we don't know if it fails until... try: print 'Got sources', psources except: print '"icSrc" not found. Trying "src" instead.' psources = inButler.get('src', **keys) print 'Got sources', psources pmatches = inButler.get('icMatch', **keys) #print 'Got matches', pmatches #matchmeta = pmatches.getSourceMatchMetadata() #matches = pmatches.getSourceMatches() #print 'Match metadata:', matchmeta sources = psources.getSources() calexp = inButler.get('calexp', **keys) #print 'Got calexp', calexp wcs = calexp.getWcs() #print 'Got wcs', wcs #print wcs.getFitsMetadata().toString() wcs = afwImage.cast_TanWcs(wcs) #print 'After cast:', wcs photocal = calexp.getCalib() zp = photocal.getMagnitude(1.) print 'Zeropoint is', zp # ref sources W,H = calexp.getWidth(), calexp.getHeight() log = Log.getDefaultLog() log.setThreshold(Log.DEBUG); kwargs = {} if fixup: # ugh, mask and offset req'd because source ids are assigned at write-time # and match list code made a deep copy before that. # (see svn+ssh://svn.lsstcorp.org/DMS/meas/astrom/tickets/1491-b r18027) kwargs['sourceIdMask'] = 0xffff kwargs['sourceIdOffset'] = -1 (matches,ref) = measAstrom.generateMatchesFromMatchList( pmatches, sources, wcs, W, H, returnRefs=True, log=log, **kwargs) print 'Got', len(ref), 'reference catalog sources' # pull 'stargal' and 'referrs' arrays out of the reference sources fdict = maUtils.getDetectionFlags() starflag = int(fdict["STAR"]) stargal = [bool((r.getFlagForDetection() & starflag) > 0) for r in ref] referrs = [float(r.getPsfFluxErr() / r.getPsfFlux() * 2.5 / -np.log(10)) for r in ref] nstars = sum([1 for s in stargal if s]) print 'Number of sources with STAR set:', nstars visit = keys['visit'] raft = keys['raft'] sensor = keys['sensor'] prefix += 'imsim-v%i-r%s-s%s' % (visit, raft.replace(',',''), sensor.replace(',','')) if 'photom' in plots: print 'photometry plots...' tt = 'LSST ImSim v%i r%s s%s' % (visit, raft.replace(',',''), sensor.replace(',','')) wcsPlots.plotPhotometry(sources, ref, matches, prefix, band=filterName, zp=zp, referrs=referrs, refstargal=stargal, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix, band=filterName, zp=zp, delta=True, referrs=referrs, refstargal=stargal, title=tt) # test w/ and w/o referrs and stargal. if False: wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'A', band=filterName, zp=zp, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'B', band=filterName, zp=zp, referrs=referrs, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'C', band=filterName, zp=zp, refstargal=stargal, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'A', band=filterName, zp=zp, delta=True, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'B', band=filterName, zp=zp, delta=True, referrs=referrs, title=tt) wcsPlots.plotPhotometry(sources, ref, matches, prefix + 'C', band=filterName, zp=zp, delta=True,refstargal=stargal, title=tt) if 'matches' in plots: print 'matches...' wcsPlots.plotMatches(sources, ref, matches, wcs, W, H, prefix) if 'corr' in plots: #print 'corr...' # requires astrometry.libkd (not available in 0.30) #wcsPlots.plotCorrespondences2(sources, ref, matches, wcs, W, H, prefix) #print 'corr...' #wcsPlots.plotCorrespondences(sources, ref, matches, wcs, W, H, prefix) pass if 'distortion' in plots: print 'distortion...' wcsPlots.plotDistortion(wcs, W, H, 400, prefix, 'SIP Distortion (exaggerated x 10)', exaggerate=10.) print 'distortion...' wcsPlots.plotDistortion(wcs, W, H, 400, prefix, 'SIP Distortion (exaggerated x 100)', exaggerate=100., suffix='-distort2.')