def start(self):
        ### get stack data
        notstackdata = apStack.getOnlyStackData(self.params['notstackid'])
        tiltstackdata = apStack.getOnlyStackData(self.params['tiltstackid'])

        ### get good particle numbers
        includeParticle, tiltParticlesData = self.getGoodAlignParticles()
        self.numpart = len(includeParticle)

        ### make doc file of Euler angles
        eulerfile = self.makeEulerDoc(tiltParticlesData)

        ### write kept particles to file
        self.params['keepfile'] = os.path.join(
            self.params['rundir'], "keepfile" + self.timestamp + ".lst")
        apDisplay.printMsg("writing to keepfile " + self.params['keepfile'])
        kf = open(self.params['keepfile'], "w")
        for partnum in includeParticle:
            kf.write(str(partnum) + "\n")
        kf.close()

        ### make new stack of tilted particle from that run
        tiltstackfile = os.path.join(tiltstackdata['path']['path'],
                                     tiltstackdata['name'])
        rctstackfile = os.path.join(self.params['rundir'],
                                    "rctstack" + self.timestamp + ".hed")
        apFile.removeStack(rctstackfile, warn=False)
        apStack.makeNewStack(tiltstackfile, rctstackfile,
                             self.params['keepfile'])
        spiderstack = self.convertStackToSpider(rctstackfile)
        #self.mirrorParticles(tiltParticlesData, spiderstack)

        ### iterations over volume creation

        ### back project particles into filter volume
        volfile = os.path.join(self.params['rundir'],
                               "volume%s-%03d.spi" % (self.timestamp, 0))
        backproject.backprojectCG(spiderstack,
                                  eulerfile,
                                  volfile,
                                  numpart=self.numpart,
                                  pixrad=self.params['radius'])
        alignstack = spiderstack

        ### center/convert the volume file
        mrcvolfile = self.processVolume(volfile, 0)

        for i in range(self.params['numiters']):
            looptime = time.time()
            iternum = i + 1
            apDisplay.printMsg("running backprojection iteration " +
                               str(iternum))
            ### xy-shift particles to volume projections
            alignstack = backproject.rctParticleShift(
                volfile,
                alignstack,
                eulerfile,
                iternum,
                numpart=self.numpart,
                pixrad=self.params['radius'],
                timestamp=self.timestamp)
            apFile.removeFile(volfile)

            ### back project particles into better volume
            volfile = os.path.join(
                self.params['rundir'],
                "volume%s-%03d.spi" % (self.timestamp, iternum))
            backproject.backproject3F(alignstack,
                                      eulerfile,
                                      volfile,
                                      numpart=self.numpart)

            ### center/convert the volume file
            mrcvolfile = self.processVolume(volfile, iternum)

            apDisplay.printColor(
                "finished volume refinement loop in " +
                apDisplay.timeString(time.time() - looptime), "cyan")

        ### optimize Euler angles
        #NOT IMPLEMENTED YET

        ### perform eotest
        if self.params['eotest'] is True:
            self.runEoTest(alignstack, eulerfile)
        self.runRmeasure()

        ### insert volumes into DB
        self.insertRctRun(mrcvolfile)
    def runEoTest(self, alignstack, eulerfile):
        evenvolfile = os.path.join(self.params['rundir'],
                                   "evenvolume%s.spi" % (self.timestamp))
        oddvolfile = os.path.join(self.params['rundir'],
                                  "oddvolume%s.spi" % (self.timestamp))
        eveneulerfile = os.path.join(self.params['rundir'],
                                     "eveneulers%s.spi" % (self.timestamp))
        oddeulerfile = os.path.join(self.params['rundir'],
                                    "oddeulers%s.spi" % (self.timestamp))
        evenpartlist = os.path.join(self.params['rundir'],
                                    "evenparts%s.lst" % (self.timestamp))
        oddpartlist = os.path.join(self.params['rundir'],
                                   "oddparts%s.lst" % (self.timestamp))

        ### Create New Doc Files
        of = open(oddeulerfile, "w")
        ef = open(eveneulerfile, "w")
        op = open(oddpartlist, "w")
        ep = open(evenpartlist, "w")
        inf = open(eulerfile, "r")
        evenpart = 0
        oddpart = 0
        for line in inf:
            spidict = operations.spiderInLine(line)
            if spidict:
                partnum = spidict['row']
                if partnum % 2 == 0:
                    ep.write("%d\n" % (partnum - 1))
                    evenpart += 1
                    outline = operations.spiderOutLine(evenpart,
                                                       spidict['floatlist'])
                    ef.write(outline)
                elif partnum % 2 == 1:
                    op.write("%d\n" % (partnum - 1))
                    oddpart += 1
                    outline = operations.spiderOutLine(oddpart,
                                                       spidict['floatlist'])
                    of.write(outline)
        inf.close()
        of.close()
        ef.close()
        op.close()
        ep.close()

        ### Create stacks
        evenstack = os.path.join(self.params['rundir'],
                                 "evenstack%s.spi" % (self.timestamp))
        emancmd = "proc2d %s %s list=%s spiderswap" % (alignstack, evenstack,
                                                       evenpartlist)
        apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)
        oddstack = os.path.join(self.params['rundir'],
                                "oddstack%s.spi" % (self.timestamp))
        emancmd = "proc2d %s %s list=%s spiderswap" % (alignstack, oddstack,
                                                       oddpartlist)
        apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)

        ### Create Volumes
        backproject.backproject3F(evenstack, eveneulerfile, evenvolfile,
                                  evenpart)
        backproject.backproject3F(oddstack, oddeulerfile, oddvolfile, oddpart)
        if not os.path.isfile(evenvolfile) or not os.path.isfile(oddvolfile):
            apDisplay.printError("Even-Odd volume creation failed")

        ### Calculate FSC
        apix = apStack.getStackPixelSizeFromStackId(
            self.params['tiltstackid']) * self.params['tiltbin']
        emancmd = "proc3d %s %s" % (evenvolfile, evenvolfile + ".mrc")
        apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)
        emancmd = "proc3d %s %s" % (oddvolfile, oddvolfile + ".mrc")
        apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)
        fscfile = os.path.join(self.params['rundir'],
                               "fscdata%s.fsc" % (self.timestamp))
        emancmd = "proc3d %s %s fsc=%s" % (evenvolfile + ".mrc",
                                           oddvolfile + ".mrc", fscfile)
        apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)

        if not os.path.isfile(fscfile):
            apDisplay.printError("Even-Odd fsc calculation failed")
        boxsize = self.getBoxSize()
        self.fscresolution = apRecon.getResolutionFromFSCFile(fscfile,
                                                              boxsize,
                                                              apix,
                                                              msg=True)
        apDisplay.printColor(
            ("Final FSC resolution: %.5f" % (self.fscresolution)), "cyan")

        for fname in (evenvolfile, oddvolfile, evenstack, oddstack,
                      eveneulerfile, oddeulerfile, evenpartlist, oddpartlist):
            apFile.removeFile(fname)
	def runEoTest(self, alignstack, eulerfile):
		evenvolfile = os.path.join(self.params['rundir'], "evenvolume%s.spi"%(self.timestamp))
		oddvolfile = os.path.join(self.params['rundir'], "oddvolume%s.spi"%(self.timestamp))
		eveneulerfile = os.path.join(self.params['rundir'], "eveneulers%s.spi"%(self.timestamp))
		oddeulerfile = os.path.join(self.params['rundir'], "oddeulers%s.spi"%(self.timestamp))
		evenpartlist = os.path.join(self.params['rundir'], "evenparts%s.lst"%(self.timestamp))
		oddpartlist = os.path.join(self.params['rundir'], "oddparts%s.lst"%(self.timestamp))

		### Create New Doc Files
		of = open(oddeulerfile, "w")
		ef = open(eveneulerfile, "w")
		op = open(oddpartlist, "w")
		ep = open(evenpartlist, "w")
		inf = open(eulerfile, "r")
		evenpart = 0
		oddpart = 0
		for line in inf:
			spidict = operations.spiderInLine(line)
			if spidict:
				partnum = spidict['row']
				if partnum % 2 == 0:
					ep.write("%d\n"%(partnum-1))
					evenpart += 1
					outline = operations.spiderOutLine(evenpart, spidict['floatlist'])
					ef.write(outline)
				elif partnum % 2 == 1:
					op.write("%d\n"%(partnum-1))
					oddpart += 1
					outline = operations.spiderOutLine(oddpart, spidict['floatlist'])
					of.write(outline)
		inf.close()
		of.close()
		ef.close()
		op.close()
		ep.close()

		### Create stacks
		evenstack = os.path.join(self.params['rundir'], "evenstack%s.spi"%(self.timestamp))
		emancmd = "proc2d %s %s list=%s spiderswap"%(alignstack,evenstack,evenpartlist)
		apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)
		oddstack = os.path.join(self.params['rundir'], "oddstack%s.spi"%(self.timestamp))
		emancmd = "proc2d %s %s list=%s spiderswap"%(alignstack,oddstack,oddpartlist)
		apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)

		### Create Volumes
		backproject.backproject3F(evenstack, eveneulerfile, evenvolfile, evenpart)
		backproject.backproject3F(oddstack, oddeulerfile, oddvolfile, oddpart)
		if not os.path.isfile(evenvolfile) or  not os.path.isfile(oddvolfile):
			apDisplay.printError("Even-Odd volume creation failed")

		### Calculate FSC
		apix = apStack.getStackPixelSizeFromStackId(self.params['tiltstackid'])*self.params['tiltbin']
		emancmd = "proc3d %s %s"%(evenvolfile, evenvolfile+".mrc")
		apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)
		emancmd = "proc3d %s %s"%(oddvolfile, oddvolfile+".mrc")
		apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)
		fscfile = os.path.join(self.params['rundir'], "fscdata%s.fsc"%(self.timestamp))
		emancmd = "proc3d %s %s fsc=%s"%(evenvolfile+".mrc", oddvolfile+".mrc", fscfile)
		apEMAN.executeEmanCmd(emancmd, verbose=True, showcmd=True)

		if not os.path.isfile(fscfile):
			apDisplay.printError("Even-Odd fsc calculation failed")
		boxsize = self.getBoxSize()
		self.fscresolution = apRecon.getResolutionFromFSCFile(fscfile, boxsize, apix, msg=True)
		apDisplay.printColor( ("Final FSC resolution: %.5f" % (self.fscresolution)), "cyan")

		for fname in (evenvolfile, oddvolfile, evenstack, oddstack, eveneulerfile, oddeulerfile, evenpartlist, oddpartlist):
			apFile.removeFile(fname)
	def start(self):
		### get stack data
		notstackdata = apStack.getOnlyStackData(self.params['notstackid'])
		tiltstackdata = apStack.getOnlyStackData(self.params['tiltstackid'])

		### get good particle numbers
		includeParticle, tiltParticlesData = self.getGoodAlignParticles()
		self.numpart = len(includeParticle)

		### make doc file of Euler angles
		eulerfile = self.makeEulerDoc(tiltParticlesData)

		### write kept particles to file
		self.params['keepfile'] = os.path.join(self.params['rundir'], "keepfile"+self.timestamp+".lst")
		apDisplay.printMsg("writing to keepfile "+self.params['keepfile'])
		kf = open(self.params['keepfile'], "w")
		for partnum in includeParticle:
			kf.write(str(partnum)+"\n")
		kf.close()

		### make new stack of tilted particle from that run
		tiltstackfile = os.path.join(tiltstackdata['path']['path'], tiltstackdata['name'])
		rctstackfile = os.path.join(self.params['rundir'], "rctstack"+self.timestamp+".hed")
		apFile.removeStack(rctstackfile, warn=False)
		apStack.makeNewStack(tiltstackfile, rctstackfile, self.params['keepfile'])
		spiderstack = self.convertStackToSpider(rctstackfile)
		#self.mirrorParticles(tiltParticlesData, spiderstack)

		### iterations over volume creation

		### back project particles into filter volume
		volfile = os.path.join(self.params['rundir'], "volume%s-%03d.spi"%(self.timestamp, 0))
		backproject.backprojectCG(spiderstack, eulerfile, volfile,
			numpart=self.numpart, pixrad=self.params['radius'])
		alignstack = spiderstack

		### center/convert the volume file
		mrcvolfile = self.processVolume(volfile, 0)

		for i in range(self.params['numiters']):
			looptime = time.time()
			iternum = i+1
			apDisplay.printMsg("running backprojection iteration "+str(iternum))
			### xy-shift particles to volume projections
			alignstack = backproject.rctParticleShift(volfile, alignstack, eulerfile, iternum,
				numpart=self.numpart, pixrad=self.params['radius'], timestamp=self.timestamp)
			apFile.removeFile(volfile)

			### back project particles into better volume
			volfile = os.path.join(self.params['rundir'], "volume%s-%03d.spi"%(self.timestamp, iternum))
			backproject.backproject3F(alignstack, eulerfile, volfile,
				numpart=self.numpart)

			### center/convert the volume file
			mrcvolfile = self.processVolume(volfile, iternum)

			apDisplay.printColor("finished volume refinement loop in "
				+apDisplay.timeString(time.time()-looptime), "cyan")

		### optimize Euler angles
		#NOT IMPLEMENTED YET

		### perform eotest
		if self.params['eotest'] is True:
			self.runEoTest(alignstack, eulerfile)
		self.runRmeasure()

		### insert volumes into DB
		self.insertRctRun(mrcvolfile)
	def start(self):
		### get stack data
		notstackdata = apStack.getOnlyStackData(self.params['notstackid'])
		tiltstackdata = apStack.getOnlyStackData(self.params['tiltstackid'])

		for cnum in self.classlist:

			print "\n"
			apDisplay.printMsg("###########################")
			apDisplay.printMsg("Processing stack of class "+str(cnum)+"")
			apDisplay.printMsg("###########################")
			print "\n"

			### get good particle numbers
			includeParticle, tiltParticlesData = self.getGoodAlignParticles(cnum)
			self.numpart = len(includeParticle)

			### write kept particles to file
			apParam.createDirectory(os.path.join(self.params['rundir'], str(cnum)))
			self.params['keepfile'] = os.path.join(self.params['rundir'], str(cnum), "keepfile"+self.timestamp+".lst")
			apDisplay.printMsg("writing to keepfile "+self.params['keepfile'])
			kf = open(self.params['keepfile'], "w")
			for partnum in includeParticle:
				kf.write(str(partnum)+"\n")
			kf.close()

			### make new stack of tilted particle from that run
			tiltstackfile = os.path.join(tiltstackdata['path']['path'], tiltstackdata['name'])
			otrstackfile = os.path.join(self.params['rundir'], str(cnum), "otrstack"+self.timestamp+".hed")
			apFile.removeStack(otrstackfile)
			apStack.makeNewStack(tiltstackfile, otrstackfile, self.params['keepfile'])
			spiderstack = self.convertStackToSpider(otrstackfile, cnum)

			### make doc file of Euler angles
			eulerfile = self.makeEulerDoc(tiltParticlesData, cnum)

			### iterations over volume creation
			looptime = time.time()

			### back project particles into volume
			volfile = os.path.join(self.params['rundir'], str(cnum), "volume%s-%03d.spi"%(self.timestamp, 0))
			self.initialBPRP(cnum, volfile, spiderstack, eulerfile, len(includeParticle), self.params['radius'])
			### RCT backproject method
			#backproject.backprojectCG(spiderstack, eulerfile, volfile, numpart=len(includeParticle), pixrad=self.params['radius'])

			### filter the volume (low-pass Butterworth)
			apix = apStack.getStackPixelSizeFromStackId(self.params['tiltstackid'])
			backproject.butterworthLP(volfile, apix)

			### need work... filtered volume overwrites on the existing volume
			backproject.normalizeVol(volfile)

			alignstack = spiderstack

			### center/convert the volume file
			mrcvolfile = self.processVolume(volfile, cnum, 0)

			for i in range(self.params['numiters']):
				iternum = i+1
				apDisplay.printMsg("running backprojection iteration "+str(iternum))
				### xy-shift particles to volume projections
				alignstack = backproject.otrParticleShift(volfile, alignstack, eulerfile, iternum,
					numpart=len(includeParticle), pixrad=self.params['radius'], timestamp=self.timestamp, classnum=cnum)
				apDisplay.printColor("finished volume refinement in "
					+apDisplay.timeString(time.time()-looptime), "cyan")

				### back project particles into better volume
				volfile = os.path.join(self.params['rundir'], str(cnum), "volume%s-%03d.spi"%(self.timestamp, iternum))
				backproject.backproject3F(alignstack, eulerfile, volfile, numpart=len(includeParticle))

				### filter the volume (low-pass Butterworth)
				backproject.butterworthLP(volfile, apix)

				### need work... filtered volume has a different name
				backproject.normalizeVol(volfile)

				### center/convert the volume file
				mrcvolfile = self.processVolume(volfile, cnum, iternum)

			###############################
			#										#
			# Andres's refinement steps	#
			#										#
			###############################
			print "\n"
			apDisplay.printMsg("##################################")
			apDisplay.printMsg("Starting Andres' refinement steps")
			apDisplay.printMsg("##################################")
			print "\n"


			for j in range(self.params['refineiters']):
				iternum = j+1
				appiondata.ApPathData.direct_query(1)
				apDisplay.printMsg("Starting projection-matching refinement/XMIPP iteration "+str(iternum))

				boxsize = self.getBoxSize()
				### projection-matching refinement/XMIPP
				apshout, apshstack, apsheuler = self.projMatchRefine(cnum, volfile, alignstack, eulerfile, boxsize, len(includeParticle), self.params['radius'], iternum)

				apDisplay.printMsg("Calculating weighted cross-correlation coefficients")

				### calculation of weighted cross-correlation coefficients
				apshout_weighted = self.cccAPSH(apshout, cnum, iternum)

				apDisplay.printMsg("Creating select files based on weighted cross-correlation coefficients")

				### create select files based on calculated weighted-cross-correlation
				corrSelect = self.makecccAPSHselectFile(apshout_weighted, cnum, iternum, factor=0.1)

				### create volume file names
				apshVolfile = os.path.join(self.params['rundir'], str(cnum), "apshVolume-%03d.spi"%(iternum))

				### run BPRP on selected particles
				self.APSHbackProject(apshstack, apsheuler, apshVolfile, cnum, corrSelect)

				### center volume
				filename = os.path.splitext(apshVolfile)[0]
				apshVolFileCentered = filename+"_centered.spi"
				backproject.centerVolume(apshVolfile, apshVolFileCentered)

				### calculate FSC

				### generate odd and even select files for FSC calculation
				corrSelectOdd, corrSelectEven = self.splitOddEven(cnum, corrSelect, iternum)
				fscout = self.runEoTest(corrSelectOdd, corrSelectEven, cnum, apshstack, apsheuler, iternum)
				self.runRmeasure(apshVolFileCentered)

				### filter volume
				backproject.butterworthFscLP(apshVolFileCentered, fscout)

				### reset file names for next round
				volfile = apshVolFileCentered
				eulerfile = apsheuler
				mrcvolfile = self.processVolume(volfile, cnum, iternum)

				print "\n"
				apDisplay.printMsg("###########################")
				apDisplay.printMsg("Done with iteration "+str(j+1)+"")
				apDisplay.printMsg("###########################")
				print "\n"

		#if len(self.classlist) > 1:
			#get a list of all unique combinations of volumes
		#	pairlist = self.computeClassVolPair()

		### insert volumes into DB
		self.insertOtrRun(mrcvolfile)