def start(self):
         parttree = apFrealign.parseFrealignParamFile(self.params['paramfile'])
         phases = []
         for partdict in parttree:
                 phases.append(partdict['phase_residual'])
         del parttree
         phases.sort()
         phases = numpy.array(phases, dtype=numpy.float32)
         if self.params['gracex'] is None:
                 apDisplay.printMsg("xFile:   %s"%(self.params['paramfile']))
                 apDisplay.printMsg("xMean:   %.3f +/- %.3f (%d total)"%(phases.mean(), phases.std(), phases.shape[0]))
                 apDisplay.printMsg("xRange:  %.2f (min) <> %.2f (0.1) <> %.2f (0.25) <> %.2f (med) <> %.2f (0.75) <> %.2f (0.9) <> %.2f (max)"
                         %(phases.min(), phases[int(phases.shape[0]*0.1)], phases[int(phases.shape[0]*0.25)], 
                         numpy.median(phases), phases[int(phases.shape[0]*0.75)], phases[int(phases.shape[0]*0.9)], phases.max()))
         else:
                 f = open("boxplot.dat", "a")
                 #boxplot line: (X, median, upper/lower limit, upper/lower whisker)
                 f.write("%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n"
                         %(self.params['gracex'], numpy.median(phases), 
                         phases[int(phases.shape[0]*0.75)], phases[int(phases.shape[0]*0.25)],
                         phases[int(phases.shape[0]*0.9)], phases[int(phases.shape[0]*0.1)],
                 ))
                 f.close()
                 f = open("means.dat", "a")
                 f.write("%d\t%.8f\n"%(self.params['gracex'], phases.mean()))
                 f.close()
 def createTiltedCtfLines(self,start_particle,end_particle):
         part_tree = apFrealign.parseFrealignParamFile(self.frealign_paramfile)
         print len(part_tree)
         print start_particle
         p = part_tree[start_particle-1]
         total_particles = end_particle - start_particle + 1
         # TO DO: the original script requires a count of particles at a given
         # ctf value followed by 1 if there are more lines
         lastcard = '%d,%d,%d,%.1f,%.1f,%.1f,0' % (total_particles,p['mag'],p['film'],p['defoc1'],p['defoc2'],p['astang'])
         return [lastcard]
	def createParticleDataFile(self, iteration, package_database_object):
		''' puts all relevant particle information into a single text file that can be read by the uploader '''
		
		os.chdir(self.projmatchpath)
		
		### read particle info
		paramfile = os.path.join(self.projmatchpath, "params.%03d.par"%(iteration))
		parttree = apFrealign.parseFrealignParamFile(paramfile)

		### write data in appion format to input file for uploading to the database
		partdatafilepath = os.path.join(self.resultspath, "particle_data_%s_it%.3d_vol001.txt" % (self.params['timestamp'], iteration))
		particledataf = open(partdatafilepath, "w")
		particledataf.write("### column info: ")
		particledataf.write("(1) particle number ")
		particledataf.write("(2) phi ")
		particledataf.write("(3) theta ")
		particledataf.write("(4) omega ")
		particledataf.write("(5) shiftx ")
		particledataf.write("(6) shifty ")
		particledataf.write("(7) mirror ")
		particledataf.write("(8) 3D reference # ")
		particledataf.write("(9) 2D class # ")
		particledataf.write("(10) quality factor ")
		particledataf.write("(11) kept particle ")
		particledataf.write("(12) postRefine kept particle \n")				

		### add each particle to the file
		count = 0
		for partdict in parttree:
			count += 1
			if count % 500 == 0:
				apDisplay.printMsg("Wrote %d particles to Particle Data File: %s"%(count,partdatafilepath))
			phi, theta, omega = apEulerCalc.convertFrealignEulersTo3DEM(float(partdict['phi']), float(partdict['theta']), float(partdict['psi']))
			particledataf.write("%.6d\t" % int(partdict['partnum'])) ### NOTE: IT IS IMPORTANT TO START WITH 1, OTHERWISE STACKMAPPING IS WRONG!!!
			particledataf.write("%.6f\t" % phi)
			particledataf.write("%.6f\t" % theta)
			particledataf.write("%.6f\t" % omega)
			particledataf.write("%.6f\t" % float(-partdict['shiftx']))
			particledataf.write("%.6f\t" % float(-partdict['shifty']))
			particledataf.write("%.6d\t" % 0)
			particledataf.write("%.6d\t" % 1)
			particledataf.write("%.6d\t" % 0)
			particledataf.write("%.6d\t" % 1)
			if partdict['phase_residual'] > package_database_object['thresh']:
				particledataf.write("%.6f\t" % 1)
			else:
				particledataf.write("%.6f\t" % 1) #TODO: this should be false???
			particledataf.write("%.6f\n" % 1)
			
		### close the new file
		particledataf.close()
		
		os.chdir(self.params['rundir'])
				
		return
	def needIter0Recon(self):
		'''
		Function to determine if a reconstruction should be made before the first refimement.
		New Trial using euler angles from other refinement run should do an reconstruction first
		'''
		if self.params['startiter'] > 1:
			return False
		### need iteration 0 recon if params.000.par came from a reconiter
		# Not using database in case of limited access.
		initparamfile = 'params.000.par'
		parttree = apFrealign.parseFrealignParamFile(initparamfile)
		for p in parttree:
			if not (p['psi']==p['phi']==p['theta']==p['shiftx']==p['shifty']):
				return True
		return False
	def uploadIteration(self, iternum):
		if self.runq is None:
			### setup refinement run
			self.runq = self.getRunData()

		### read iteration info
		iterparams = self.readParamsFromCombineFile(iternum)

		### read particle info
		paramfile = os.path.join(self.params['rundir'], "params.iter%03d.par"%(iternum))
		parttree = apFrealign.parseFrealignParamFile(paramfile)

		### get volume info
		volumeMrcFile = self.processFrealignVolume(iternum, symname=iterparams['symmdata']['eman_name'])

		### get volume info
		iterdata = self.setIterData(iterparams, volumeMrcFile)

		### insert FSC data
		self.insertFSCData(iternum, iterdata)

		### insert particle data
		self.insertRefineParticleData(iterdata, parttree)