示例#1
0
 def __sexFindFWHM(self, tries, threshold, deblendmin):
     focpos = []
     fwhm = []
     fwhm_min = None
     fwhm_MinimumX = None
     keys = tries.keys()
     keys.sort()
     sextr = sextractor.Sextractor(threshold=threshold,
                                   deblendmin=deblendmin)
     for k in keys:
         try:
             sextr.runSExtractor(tries[k])
             fwhm, fwhms, nstars = sextr.calculate_FWHM(
                 min_stars, self.filterGalaxies)
         except Exception, ex:
             self.log('W', 'offset {0}: {1}'.format(k, ex))
             continue
         self.log(
             'I',
             'offset {0} fwhm {1} with {2} stars'.format(k, fwhm, nstars))
         focpos.append(k)
         fwhm.append(fwhm)
         if (fwhm_min is None or fwhm < fwhm_min):
             fwhm_MinimumX = k
             fwhm_min = fwhm
示例#2
0
 def findBestFWHM(self,
                  tries,
                  defaultFit=H3,
                  min_stars=95,
                  ds9display=False,
                  threshold=2.7,
                  deblendmin=0.03):
     # X is FWHM, Y is offset value
     self.focpos = []
     self.fwhm = []
     fwhm_min = None
     self.fwhm_MinimumX = None
     keys = tries.keys()
     keys.sort()
     sextr = sextractor.Sextractor(threshold=threshold,
                                   deblendmin=deblendmin)
     for k in keys:
         try:
             sextr.runSExtractor(tries[k])
             fwhm, fwhms, nstars = sextr.calculate_FWHM(
                 min_stars, self.filterGalaxies)
         except Exception, ex:
             self.log('W', 'offset {0}: {1}'.format(k, ex))
             continue
         self.log(
             'I',
             'offset {0} fwhm {1} with {2} stars'.format(k, fwhm, nstars))
         focpos.append(k)
         fwhm.append(fwhm)
         if (fwhm_min is None or fwhm < fwhm_min):
             fwhm_MinimumX = k
             fwhm_min = fwhm
示例#3
0
def processImage(fn,
                 d,
                 obs_id=None,
                 threshold=2.7,
                 pr=False,
                 ds9cat=None,
                 bysegments=False,
                 stars=[]):
    """Process image, print its FWHM. Works with multi extension images.
	"""
    ff = pyfits.fitsopen(fn)

    rts2 = rts2comm.Rts2Comm()

    if d:
        d.set('file mosaicimage iraf ' + fn)

    sexcols = [
        'X_IMAGE', 'Y_IMAGE', 'MAG_BEST', 'FLAGS', 'CLASS_STAR', 'FWHM_IMAGE',
        'A_IMAGE', 'B_IMAGE', 'EXT_NUMBER', 'FLUX_BEST', 'BACKGROUND'
    ]

    c = sextractor.Sextractor(
        sexcols,
        threshold=threshold,
        sexconfig='/home/observer/findfwhm/Sextractor/focus.sex',
        starnnw='/home/observer/findfwhm/Sextractor/default.nnw')
    c.runSExtractor(fn)
    c.sortObjects(2)

    for st in stars:
        # append distance - none and star number - to star list
        st.append(None)
        st.append(None)

    # dump Sextractor to DS9 catalogue
    if ds9cat:
        cat = open(ds9cat, 'w')
        cat.write('\t'.join(sexcols) + '\n')
        for x in c.objects:
            cat.write('\t'.join(map(lambda y: str(y), x)) + '\n')
        cat.close()

    seg_fwhms = map(lambda x: FWHM(), range(0, len(ff) + 1))
    for x in c.objects:
        if pr:
            print '\t'.join(map(lambda y: str(y), x))
        segnum = int(x[8])
        for st in stars:
            if st[0] == segnum:
                dist = math.sqrt((x[0] - st[1])**2 + (x[1] - st[2])**2)
                if st[4] is None or st[4] > dist:
                    st[4] = dist
                    st[5] = x

        if x[3] == 0 and x[4] != 0:
            if d:
                d.set(
                    'regions',
                    'tile {0}\nimage; circle {1} {2} 10 # color=green'.format(
                        segnum, x[0], x[1]))

            seg_fwhms[0].addFWHM(x[5], x[6], x[7])
            seg_fwhms[segnum].addFWHM(x[5], x[6], x[7])
        elif d:
            d.set(
                'regions',
                '# tile {0}\nphysical; point {1} {2} # point = x 5 color=red'.
                format(segnum, x[0], x[1]))

    # average results
    map(FWHM.average, seg_fwhms)
    # default suffix
    defsuffix = '_KCAM'
    try:
        defsuffix = '_' + ff[0].header['CCD_NAME']
    except KeyError, er:
        pass
示例#4
0
	def runOnImage(self,fn,partial_len=None,interactive=False,sequences_num=15,mag_limit_num=7):
		"""
		Run algorithm on image. Extract sources with sextractor, and
		pass them through sequence finding algorithm, and fit focusing position.
		"""

		c = sextractor.Sextractor(['NUMBER','X_IMAGE','Y_IMAGE','MAG_BEST','FLAGS','CLASS_STAR','FWHM_IMAGE','A_IMAGE','B_IMAGE'],sexpath='/usr/bin/sextractor',sexconfig='/usr/share/sextractor/default.sex',starnnw='/usr/share/sextractor/default.nnw')
		c.runSExtractor(fn)


		self.objects = c.objects
		# sort by flux/brightness
		self.objects.sort(cmp=lambda x,y:cmp(x[3],y[3]))

		print 'from {0} extracted {1} sources'.format(fn,len(c.objects))

		d = None

		if interactive:
	  		d = ds9()
			# display in ds9
			d.set('file {0}'.format(fn))

			for x in self.objects:
				d.set('regions','image; point {0} {1} # point=x 5 color=red'.format(x[1],x[2]))

		sequences = []
		usednum = []

		for x in self.objects:
			# do not examine already used objects..
			if x[0] in usednum:
				continue

		  	# find object in a row..
			b = self.findRowObjects(x,partial_len)
			if b is None:
				continue
			sequences.append(b)
			if d:
				d.set('regions select none')
				d.set('regions','image; circle {0} {1} 20 # color=yellow tag = sel'.format(x[1],x[2]))
			for obj in b:
				usednum.append(obj[0])
			if d:
				print 'best mag: ',x[3]
				d.set('regions select group sel')
				d.set('regions delete select')
				for obj in b:
					if obj[0] is None:
						d.set('regions','image; point {0} {1} # point=boxcircle 15 color = red'.format(obj[1],obj[2]))
					else:
						d.set('regions','image; circle {0} {1} 10 # color = green'.format(obj[1],obj[2]))
			if len(sequences) > sequences_num:
				break
		# if enough sequences were found, process them and try to fit results
		if len(sequences) > sequences_num:
			# get median of FWHM from each sequence
			fwhm=[]
			for x in range(0,len(self.shifts) + 1):
				fa = []
				for o in sequences:
					if o[x][0] is not None:
						fa.append(o[x][6])
				# if length of magnitude estimates is greater then limit..
				if len(fa) >= mag_limit_num:
					m = numpy.median(fa)
					fwhm.append(m)
				else:
					if interactive:
						print 'removing focuser position, because not enough matches were found',self.focpos[x]
					self.focpos.remove(self.focpos[x])
			# fit it
			foc = focusing.Focusing()

			res,ftype = foc.doFitOnArrays(fwhm,self.focpos,focusing.H2)
			if interactive:
				print res,ftype
				foc.plotFit(res,ftype)