def doFindStars( imName = None, maskName = None, satMaskName = None, invertMask = False, **kargs ): """Find stars and centroid and shape-fit them. Inputs: - all the arguments for loadFiles plus values shown by showDef """ global isSat, sd im, mask, satMask = loadFiles(imName, maskName, satMaskName, invertMask) # check keyword arguments for paramName in kargs: if paramName not in FindParamNames: raise RuntimeError("Invalid argument: %s" % (paramName,)) # fill in defaults globalDict = globals() for paramName in FindParamNames: if paramName not in kargs: kargs[paramName] = globalDict[paramName] # split off ccd info ccdInfoDict = {} for paramName in CCDInfoNames: ccdInfoDict[paramName] = kargs.pop(paramName) ccdInfo = PyGuide.CCDInfo(**ccdInfoDict) # find stars and centroid print("Calling PyGuide.findStars") ctrDataList, imStats = PyGuide.findStars( data = im, mask = mask, satMask = satMask, ccdInfo = ccdInfo, **kargs) print("%s stars found:" % (len(ctrDataList),)) printStarHeader() for ctrData in ctrDataList: # measure star shape try: shapeData = PyGuide.starShape( data = im, mask = mask, xyCtr = ctrData.xyCtr, rad = ctrData.rad, ) except RuntimeError as e: print("starShape failed: %s" % (e,)) shapeData = PyGuide.StarShapeData() printStarData(ctrData, shapeData)
def doFindStars(imName=None, maskName=None, satMaskName=None, invertMask=False, **kargs): """Find stars and centroid and shape-fit them. Inputs: - all the arguments for loadFiles plus values shown by showDef """ global isSat, sd im, mask, satMask = loadFiles(imName, maskName, satMaskName, invertMask) # check keyword arguments for paramName in kargs: if paramName not in FindParamNames: raise RuntimeError("Invalid argument: %s" % (paramName, )) # fill in defaults globalDict = globals() for paramName in FindParamNames: if paramName not in kargs: kargs[paramName] = globalDict[paramName] # split off ccd info ccdInfoDict = {} for paramName in CCDInfoNames: ccdInfoDict[paramName] = kargs.pop(paramName) ccdInfo = PyGuide.CCDInfo(**ccdInfoDict) # find stars and centroid print("Calling PyGuide.findStars") ctrDataList, imStats = PyGuide.findStars(data=im, mask=mask, satMask=satMask, ccdInfo=ccdInfo, **kargs) print("%s stars found:" % (len(ctrDataList), )) printStarHeader() for ctrData in ctrDataList: # measure star shape try: shapeData = PyGuide.starShape( data=im, mask=mask, xyCtr=ctrData.xyCtr, rad=ctrData.rad, ) except RuntimeError as e: print("starShape failed: %s" % (e, )) shapeData = PyGuide.StarShapeData() printStarData(ctrData, shapeData)
def doStarShape( imName=None, maskName=None, xyCtr=None, rad=rad, invertMask=False, verbosity=verbosity, ): """Shape-fit a star Inputs: - Most of the arguments for loadFiles plus rad, xyCtr and verbosity. """ global im, imFits, mask, maskFits im, mask, satMask = loadFiles(imName, maskName, None, invertMask) if xyCtr is None: print("xyCtr is required") return if rad is None: print("rad argument is required because the default is presently None") return shapeData = PyGuide.starShape( data=im, mask=mask, xyCtr=xyCtr, rad=rad, verbosity=verbosity, ) print(" xctr yctr ampl bkgnd fwhm rad chiSq") try: print("%7.2f %7.2f %13.1f %9.1f %7.1f %7d %7.1f" % ( xyCtr[0], xyCtr[1], shapeData.ampl, shapeData.bkgnd, shapeData.fwhm, rad, shapeData.chiSq, )) except (ValueError, TypeError) as e: print("(printing free-form due to format error: %s)" % (e, )) print(xyCtr[0], xyCtr[1], shapeData.ampl, shapeData.bkgnd, shapeData.fwhm, rad, shapeData.chiSq) if not shapeData.isOK: print("starShape failed:", shapeData.msgStr)
def doStarShape( imName = None, maskName = None, xyCtr = None, rad = rad, invertMask = False, verbosity = verbosity, ): """Shape-fit a star Inputs: - Most of the arguments for loadFiles plus rad, xyCtr and verbosity. """ global im, imFits, mask, maskFits im, mask, satMask = loadFiles(imName, maskName, None, invertMask) if xyCtr is None: print("xyCtr is required") return if rad is None: print("rad argument is required because the default is presently None") return shapeData = PyGuide.starShape( data = im, mask = mask, xyCtr = xyCtr, rad = rad, verbosity = verbosity, ) print(" xctr yctr ampl bkgnd fwhm rad chiSq") try: print("%7.2f %7.2f %13.1f %9.1f %7.1f %7d %7.1f" % ( xyCtr[0], xyCtr[1], shapeData.ampl, shapeData.bkgnd, shapeData.fwhm, rad, shapeData.chiSq, )) except (ValueError, TypeError) as e: print("(printing free-form due to format error: %s)" % (e,)) print(xyCtr[0], xyCtr[1], shapeData.ampl, shapeData.bkgnd, shapeData.fwhm, rad, shapeData.chiSq) if not shapeData.isOK: print("starShape failed:", shapeData.msgStr)
elif rtype[i] == 'circle': xcen.append(float(larr[1])) ycen.append(float(larr[2])) rad.append(float(larr[3])) rad2.append(float(larr[3] * numpy.sqrt(2))) else: print "Error: region type is not recognized (or possibly you are specifying an unrecognized coordinate system)" if float(rad2[i]) > float(rad[i]): outerrad = rad2[i] else: outerrad = float(rad[i])*numpy.sqrt(2) # subimage = image[:,ycen[i]-rad[i]:ycen[i]+rad[i],xcen[i]-rad[i]:xcen[i]+rad[i]] subimage = image[:,ycen[i]-outerrad:ycen[i]+outerrad,xcen[i]-outerrad:xcen[i]+outerrad] # center = PyGuide.centroid(subimage[0],None,None,[outerrad,outerrad],rad[i],ccdinf) center = PyGuide.centroid(subimage[0],None,None,[rad[i],rad[i]],rad[i],ccdinf,thresh=1,verbosity=0,smooth=0) innerradius = PyGuide.starShape(subimage[0],None,center.xyCtr,rad[i]).fwhm outerradius = innerradius * numpy.sqrt(2) if center.isOK: (newxcen,newycen)=center.xyCtr # print "%s %s" % (str(center.xyCtr),str(center.xyErr)) else: print "Error: %s. Using the point you selected instead of the star center." % str(center.msgStr) (newxcen,newycen)=[outerrad,outerrad] inner = core(subimage,newxcen,newycen,innerradius) equalarea = core(subimage,newxcen,newycen,outerradius) - inner vspec = inner - equalarea for j in xrange(1,vspec.shape[0]): print "%f %f" % (j,vspec[j]) break i=i+1
def doCentroid( imName = None, maskName = None, satMaskName = None, xyGuess = None, invertMask = False, **kargs ): """Centroid and shape-fit a star Inputs: - all the arguments for loadFiles plus most values shown by showDef """ global im, imFits, mask, maskFits, satMask, satMaskFits, isSat, sd im, mask, satMask = loadFiles(imName, maskName, satMaskName, invertMask) if xyGuess is None: print("xyGuess is required") return # check keyword arguments for paramName in kargs: if paramName not in CentroidParamNames: raise RuntimeError("Invalid argument: %s" % (paramName,)) # fill in defaults globalDict = globals() for paramName in CentroidParamNames: if paramName not in kargs: kargs[paramName] = globalDict[paramName] # split off ccd info ccdInfoDict = {} for paramName in CCDInfoNames: ccdInfoDict[paramName] = kargs.pop(paramName) ccdInfo = PyGuide.CCDInfo(**ccdInfoDict) if kargs["rad"] is None: print("rad argument is required because the default is presently None") return verbosity = kargs["verbosity"] # centroid ctrData = PyGuide.centroid( data = im, mask = mask, satMask = satMask, xyGuess = xyGuess, ccdInfo = ccdInfo, **kargs) if not ctrData.isOK: print("centroid failed:", ctrData.msgStr) return shapeData = PyGuide.starShape( data = im, mask = mask, xyCtr = ctrData.xyCtr, rad = ctrData.rad, verbosity = verbosity, ) # print results printStarHeader() printStarData(ctrData, shapeData) if not shapeData.isOK: print("starShape failed:", shapeData.msgStr)
def pyguide_checking(imgArray): """ Uses PyGuide to find stars, get counts, and determine if new exposure time is necessary. Input: - imgArray numpy array from the CCD Output: - True if exposure was good, False if bad - True if exposure time should be decreased, False if increased """ # search image for stars centroidData, imageStats = PyGuide.findStars(imgArray, mask=None, satMask=None, ccdInfo=CCDInfo) # keep track of targets goodTargets = [] lowTargets = 0 highTargets = 0 print( "these are the %i stars pyguide found in descending order of brightness:" % len(centroidData)) for centroid in centroidData: # for each star, measure its shape shapeData = PyGuide.starShape( np.asarray( imgArray, dtype="float32"), # had to explicitly cast for some reason mask=None, xyCtr=centroid.xyCtr, rad=centroid.rad) if not shapeData.isOK: print("starShape failed: %s" % (shapeData.msgStr, )) else: print("xyCenter=[%.2f, %.2f] CCD Pixel Counts=%.1f, FWHM=%.1f, BKGND=%.1f, chiSq=%.2f" %\ (centroid.xyCtr[0], centroid.xyCtr[1], shapeData.ampl,shapeData.fwhm, shapeData.bkgnd, shapeData.chiSq)) #if shapeData.ampl < 0.2*MAX_COUNTS: # lowTargets+=1 #elif shapeData.ampl > 0.9*MAX_COUNTS: # highTargets+=1 #else: # goodTargets.append([centroid,shapeData]) goodTargets.append([centroid, shapeData]) print() print( str(len(goodTargets)) + " targets are in the linear (20-90%) range --- " + str(lowTargets) + " low targets --- " + str(highTargets) + " high targets") ### highlight detections ### size of green circle scales with total counts ### bigger circles for brigher stars if DISPLAY_TARGETS: plt.clf() plt.imshow(imgArray, cmap="gray", vmin=200, vmax=MAX_COUNTS) # vmin/vmax help with contrast plt.ion() plt.show() for centroid in centroidData: xyCtr = centroid.xyCtr + np.array( [-0.5, -0.5] ) # offset by half a pixel to match imshow with 0,0 at pixel center rather than edge counts = centroid.counts plt.scatter(xyCtr[0], xyCtr[1], s=counts / MAX_COUNTS, marker="o", edgecolors="lime", facecolors="none") plt.gca().invert_yaxis() plt.draw() plt.pause(0.1) # Successful exposure, return True. The False is thrown away #if len(goodTargets) >= 1: # goodTargets = [goodTargets[0]] return goodTargets
# use for image /Test\ Images/ecam/2004-04-27/gimg0170.fits #mask = data < 0 #mask[64:101, 78:92] = 1 print "searching for stars" ctrDataList, imStats = PyGuide.findStars( data=data, mask=mask, satMask=None, ccdInfo=CCDInfo, verbosity=0, doDS9=False, )[0:2] for ctrData in ctrDataList: xyCtr = ctrData.xyCtr rad = ctrData.rad print "star xyCtr=%.2f, %.2f, radius=%s" % (xyCtr[0], xyCtr[1], rad) shapeData = PyGuide.starShape( data, mask=mask, xyCtr=xyCtr, rad=rad, ) if not shapeData.isOK: print "starShape failed: %s" % (shapeData.msgStr, ) else: print "star ampl=%.1f, fwhm=%.1f, bkgnd=%.1f, chiSq=%.2f" %\ (shapeData.ampl,shapeData.fwhm, shapeData.bkgnd, shapeData.chiSq) print
print "%.1f %.1f %.1f %.2f %.2f %.2f NaN NaN NaN NaN NaN NaN NaN NaN NaN %r" % ( fwhm, ampl, bkgnd, xyCtr[0], xyCtr[1], maskWidth, ctrData.msgStr, ) nBadCtr += 1 continue else: ctrData = PyGuide.CentroidData( isOK = True, xyCtr = xyCtr, ) shapeData = PyGuide.starShape( data = data, mask = mask, xyCtr = ctrData.xyCtr, rad = fwhm * 3, ) if not shapeData.isOK: print "%.1f %.1f %.1f %.2f %.2f %.2f %.2f %.2f NaN NaN NaN NaN NaN NaN NaN %r" % ( fwhm, ampl, bkgnd, xyCtr[0], xyCtr[1], maskWidth, ctrData.xyCtr[0], ctrData.xyCtr[1], shapeData.msgStr, ) nBad += 1 continue fwhmErr = pctErr(shapeData.fwhm, fwhm) amplErr = pctErr(shapeData.ampl, ampl) bkgndErr = pctErr(shapeData.bkgnd, bkgnd) print "%.1f %.1f %.1f %.2f %.2f %.2f %.2f %.2f %.1f %.1f %.1f %.2f %.1f %.1f %.1f %r" % (
# equalarea = core(image,xcen[i],ycen[i],float(rad[i])*numpy.sqrt(2)) - inner # vspec = inner - equalarea # DEBUG print "Region type: " + rtype + " x,y: " + str(xcen) + "," + str(ycen) + " radii: " + str(rad) # DEBUG print "subimage shape %s with input params rad: %s ycen: %s xcen: %s " % (subimage.shape,rad[i],ycen[i],xcen[i]) if float(rad2[i]) > float(rad[i]): outerrad = rad2[i] else: outerrad = float(rad[i])*numpy.sqrt(2) # subimage = image[:,ycen[i]-rad[i]:ycen[i]+rad[i],xcen[i]-rad[i]:xcen[i]+rad[i]] subimage = image[:,ycen[i]-outerrad:ycen[i]+outerrad,xcen[i]-outerrad:xcen[i]+outerrad] # center = PyGuide.centroid(subimage[0],None,None,[outerrad,outerrad],rad[i],ccdinf) center = PyGuide.centroid(subimage[0],None,None,[rad[i],rad[i]],rad[i],ccdinf,thresh=1,verbosity=0,smooth=0) if center.isOK: (newxcen,newycen)=center.xyCtr starshape = PyGuide.starShape(subimage[0],None,center.xyCtr,rad[i]) if starshape.isOK: innerradius = starshape.fwhm else: print starshape print "Problem with inner radius calculation for the source at %s" % str(center.xyCtr) print "Real coordinates: %s " % coords.Position(imWCS.pix2wcs(center.xyCtr[0]+xcen[i]-outerrad,center.xyCtr[1]+ycen[i]-outerrad)).hmsdms() continue outerradius = innerradius * numpy.sqrt(2) error = None # print "%s %s" % (str(center.xyCtr),str(center.xyErr)) else: print "Error: %s. Using the point you selected instead of the star center." % str(center.msgStr) error = str(center.msgStr) (newxcen,newycen)=[outerrad,outerrad] innerradius = rad[i]
# use for image /Test\ Images/ecam/2004-04-27/gimg0170.fits #mask = data < 0 #mask[64:101, 78:92] = 1 print "searching for stars" ctrDataList, imStats = PyGuide.findStars( data = data, mask = mask, satMask = None, ccdInfo = CCDInfo, verbosity = 0, doDS9 = False, )[0:2] for ctrData in ctrDataList: xyCtr = ctrData.xyCtr rad = ctrData.rad print "star xyCtr=%.2f, %.2f, radius=%s" % (xyCtr[0], xyCtr[1], rad) shapeData = PyGuide.starShape( data, mask = mask, xyCtr = xyCtr, rad = rad, ) if not shapeData.isOK: print "starShape failed: %s" % (shapeData.msgStr,) else: print "star ampl=%.1f, fwhm=%.1f, bkgnd=%.1f, chiSq=%.2f" %\ (shapeData.ampl,shapeData.fwhm, shapeData.bkgnd, shapeData.chiSq) print
def doCentroid(imName=None, maskName=None, satMaskName=None, xyGuess=None, invertMask=False, **kargs): """Centroid and shape-fit a star Inputs: - all the arguments for loadFiles plus most values shown by showDef """ global im, imFits, mask, maskFits, satMask, satMaskFits, isSat, sd im, mask, satMask = loadFiles(imName, maskName, satMaskName, invertMask) if xyGuess is None: print("xyGuess is required") return # check keyword arguments for paramName in kargs: if paramName not in CentroidParamNames: raise RuntimeError("Invalid argument: %s" % (paramName, )) # fill in defaults globalDict = globals() for paramName in CentroidParamNames: if paramName not in kargs: kargs[paramName] = globalDict[paramName] # split off ccd info ccdInfoDict = {} for paramName in CCDInfoNames: ccdInfoDict[paramName] = kargs.pop(paramName) ccdInfo = PyGuide.CCDInfo(**ccdInfoDict) if kargs["rad"] is None: print("rad argument is required because the default is presently None") return verbosity = kargs["verbosity"] # centroid ctrData = PyGuide.centroid(data=im, mask=mask, satMask=satMask, xyGuess=xyGuess, ccdInfo=ccdInfo, **kargs) if not ctrData.isOK: print("centroid failed:", ctrData.msgStr) return shapeData = PyGuide.starShape( data=im, mask=mask, xyCtr=ctrData.xyCtr, rad=ctrData.rad, verbosity=verbosity, ) # print results printStarHeader() printStarData(ctrData, shapeData) if not shapeData.isOK: print("starShape failed:", shapeData.msgStr)