def processImage(self, imgdata):
        stackedname = os.path.join(self.params['rundir'],
                                   imgdata['filename'] + "power.jpg")

        if os.path.isfile(stackedname) and apFile.fileSize(stackedname) > 100:
            return

        ### make the power spectra
        powerspectra = imagefun.power(imgdata['image'],
                                      mask_radius=1.0,
                                      thresh=4)
        binpowerspectra = imagefun.bin2(powerspectra, self.params['bin'])
        del powerspectra
        if self.params['hp'] is True:
            binpowerspectra = apImage.fermiHighPassFilter(binpowerspectra,
                                                          apix=4.0,
                                                          radius=2000.0)
        binpowerspectra = apImage.normStdevMed(binpowerspectra, size=5)
        binpowerspectra = apImage.pixelLimitFilter(binpowerspectra, pixlimit=4)
        binpowerspectra = apImage.normRange(binpowerspectra)

        ### filter the image
        imagedata = imagefun.bin2(imgdata['image'], self.params['bin'])
        del imgdata['image']
        imagedata = apImage.normStdevMed(imagedata, size=5)
        imagedata = apImage.pixelLimitFilter(imagedata, pixlimit=2)
        imagedata = apImage.normRange(imagedata)

        ### write to file
        stacked = numpy.hstack([imagedata, binpowerspectra])
        del imagedata, binpowerspectra
        apImage.arrayToJpeg(stacked, stackedname)
	def processImage(self, imgdata):
		stackedname = os.path.join(self.params['rundir'], imgdata['filename']+"power.jpg")

		if os.path.isfile(stackedname) and apFile.fileSize(stackedname) > 100:
			return

		### make the power spectra
		powerspectra = imagefun.power(imgdata['image'], mask_radius=1.0, thresh=4)
		binpowerspectra = imagefun.bin2(powerspectra, self.params['bin'])
		del powerspectra
		if self.params['hp'] is True:
			binpowerspectra = apImage.fermiHighPassFilter(binpowerspectra, apix=4.0, radius=2000.0)
		binpowerspectra = apImage.normStdevMed(binpowerspectra, size=5) 
		binpowerspectra = apImage.pixelLimitFilter(binpowerspectra, pixlimit=4)
		binpowerspectra = apImage.normRange(binpowerspectra)

		### filter the image
		imagedata = imagefun.bin2(imgdata['image'], self.params['bin'])
		del imgdata['image']
		imagedata = apImage.normStdevMed(imagedata, size=5) 
		imagedata = apImage.pixelLimitFilter(imagedata, pixlimit=2)
		imagedata = apImage.normRange(imagedata)

		### write to file
		stacked = numpy.hstack([imagedata, binpowerspectra])
		del imagedata, binpowerspectra
		apImage.arrayToJpeg(stacked, stackedname)
def getTiltedRotateShift(img1, img2, tiltdiff, angle=0, bin=1, msg=True):
        """
        takes two images tilted 
        with respect to one another 
        and tries to find overlap
        
        img1 (as numpy array)
        img2 (as numpy array)
        tiltdiff (in degrees)
                negative, img1 is more compressed (tilted)
                positive, img2 is more compressed (tilted)
        """

        ### untilt images by stretching and compressing
        # choose angle s/t compressFactor = 1/stretchFactor
        # this only works if one image is untilted (RCT) of both images are opposite tilt (OTR)
        #halftilt = abs(tiltdiff)/2.0
        halftiltrad = math.acos(math.sqrt(math.cos(abs(tiltdiff)/180.0*math.pi)))
        # go from zero tilt to half tilt
        compressFactor = math.cos(halftiltrad)
        # go from max tilt to half tilt
        stretchFactor = math.cos(halftiltrad) / math.cos(abs(tiltdiff)/180.0*math.pi)
        if tiltdiff > 0:
                if msg is True:
                        apDisplay.printMsg("compress image 1")
                untilt1 = transformImage(img1, compressFactor, angle)
                untilt2 = transformImage(img2, stretchFactor, angle)
                xfactor = compressFactor
        else:
                if msg is True:
                        apDisplay.printMsg("stretch image 1")
                untilt1 = transformImage(img1, stretchFactor, angle)
                untilt2 = transformImage(img2, compressFactor, angle)
                xfactor = stretchFactor

        ### filtering was done earlier
        filt1 = untilt1
        filt2 = untilt2

        if filt1.shape != filt2.shape:
                newshape = ( max(filt1.shape[0],filt2.shape[0]), max(filt1.shape[1],filt2.shape[1]) )
                apDisplay.printMsg("Resizing images to: "+str(newshape))
                filt1 = apImage.frame_constant(filt1, newshape, filt1.mean())
                filt2 = apImage.frame_constant(filt2, newshape, filt2.mean())

        ### cross-correlate
        cc = correlator.cross_correlate(filt1, filt2, pad=True)
        rad = min(cc.shape)/20.0
        cc = apImage.highPassFilter(cc, radius=rad)
        cc = apImage.normRange(cc)
        cc = blackEdges(cc)
        cc = apImage.normRange(cc)
        cc = blackEdges(cc)
        cc = apImage.normRange(cc)
        cc = apImage.lowPassFilter(cc, radius=10.0)

        #find peak
        peakdict = peakfinder.findSubpixelPeak(cc, lpf=0)
        #import pprint
        #pprint.pprint(peak)
        pixpeak = peakdict['subpixel peak']
        if msg is True:
                apDisplay.printMsg("Pixel peak: "+str(pixpeak))
                apImage.arrayToJpegPlusPeak(cc, "guess-cross-ang"+str(abs(angle))+".jpg", pixpeak)

        rawpeak = numpy.array([pixpeak[1], pixpeak[0]]) #swap coord
        shift = numpy.asarray(correlator.wrap_coord(rawpeak, cc.shape))*bin

        if msg is True:
                apDisplay.printMsg("Found xy-shift btw two images"
                        +";\n\t SNR= "+str(round(peakdict['snr'],2))
                        +";\n\t halftilt= "+str(round(halftiltrad*180/math.pi, 3))
                        +";\n\t compressFactor= "+str(round(compressFactor, 3))
                        +";\n\t stretchFactor= "+str(round(stretchFactor, 3))
                        +";\n\t xFactor= "+str(round(xfactor, 3))
                        +";\n\t rawpeak= "+str(numpy.around(rawpeak*bin, 1))
                        +";\n\t shift= "+str(numpy.around(shift, 1))
                )

        return shift, xfactor, peakdict['snr']
def getTiltedRotateShift(img1, img2, tiltdiff, angle=0, bin=1, msg=True):
	"""
	takes two images tilted 
	with respect to one another 
	and tries to find overlap
	
	img1 (as numpy array)
	img2 (as numpy array)
	tiltdiff (in degrees)
		negative, img1 is more compressed (tilted)
		positive, img2 is more compressed (tilted)
	"""

	### untilt images by stretching and compressing
	# choose angle s/t compressFactor = 1/stretchFactor
	# this only works if one image is untilted (RCT) of both images are opposite tilt (OTR)
	#halftilt = abs(tiltdiff)/2.0
	halftiltrad = math.acos(math.sqrt(math.cos(abs(tiltdiff)/180.0*math.pi)))
	# go from zero tilt to half tilt
	compressFactor = math.cos(halftiltrad)
	# go from max tilt to half tilt
	stretchFactor = math.cos(halftiltrad) / math.cos(abs(tiltdiff)/180.0*math.pi)
	if tiltdiff > 0:
		if msg is True:
			apDisplay.printMsg("compress image 1")
		untilt1 = transformImage(img1, compressFactor, angle)
		untilt2 = transformImage(img2, stretchFactor, angle)
		xfactor = compressFactor
	else:
		if msg is True:
			apDisplay.printMsg("stretch image 1")
		untilt1 = transformImage(img1, stretchFactor, angle)
		untilt2 = transformImage(img2, compressFactor, angle)
		xfactor = stretchFactor

	### filtering was done earlier
	filt1 = untilt1
	filt2 = untilt2

	if filt1.shape != filt2.shape:
		newshape = ( max(filt1.shape[0],filt2.shape[0]), max(filt1.shape[1],filt2.shape[1]) )
		apDisplay.printMsg("Resizing images to: "+str(newshape))
		filt1 = apImage.frame_constant(filt1, newshape, filt1.mean())
		filt2 = apImage.frame_constant(filt2, newshape, filt2.mean())

	### cross-correlate
	cc = correlator.cross_correlate(filt1, filt2, pad=True)
	rad = min(cc.shape)/20.0
	cc = apImage.highPassFilter(cc, radius=rad)
	cc = apImage.normRange(cc)
	cc = blackEdges(cc)
	cc = apImage.normRange(cc)
	cc = blackEdges(cc)
	cc = apImage.normRange(cc)
	cc = apImage.lowPassFilter(cc, radius=10.0)

	#find peak
	peakdict = peakfinder.findSubpixelPeak(cc, lpf=0)
	#import pprint
	#pprint.pprint(peak)
	pixpeak = peakdict['subpixel peak']
	if msg is True:
		apDisplay.printMsg("Pixel peak: "+str(pixpeak))
		apImage.arrayToJpegPlusPeak(cc, "guess-cross-ang"+str(abs(angle))+".jpg", pixpeak)

	rawpeak = numpy.array([pixpeak[1], pixpeak[0]]) #swap coord
	shift = numpy.asarray(correlator.wrap_coord(rawpeak, cc.shape))*bin

	if msg is True:
		apDisplay.printMsg("Found xy-shift btw two images"
			+";\n\t SNR= "+str(round(peakdict['snr'],2))
			+";\n\t halftilt= "+str(round(halftiltrad*180/math.pi, 3))
			+";\n\t compressFactor= "+str(round(compressFactor, 3))
			+";\n\t stretchFactor= "+str(round(stretchFactor, 3))
			+";\n\t xFactor= "+str(round(xfactor, 3))
			+";\n\t rawpeak= "+str(numpy.around(rawpeak*bin, 1))
			+";\n\t shift= "+str(numpy.around(shift, 1))
		)

	return shift, xfactor, peakdict['snr']