classname=os.path.join(classnamepath,'combined.spi')
			apEMAN.combineSpiParticleList(spilist,classname)
			clhcbasename='combined'
					      
			# convert spider lst to EMAN lst
			convertlst = apEMAN.convertSpiderToEMAN(classname,clslist[cls])
			# change location of start file reference
			f=open(convertlst,'r')
			fnew = open(convertlst+'.tmp','w')
			for l in f:
				fnew.write(re.sub('\tstart','\t../../start',l))
			f.close()
			fnew.close()
			os.rename(convertlst+'.tmp',convertlst)
			
			apEMAN.makeClassAverages(convertlst,'goodavgs.hed',e,params['mask'])

		### otherwise just use the best class average
		else:
			bestclass=cclist.index(max(cclist))
			print "Using average %d for class %d" % (bestclass, cls)

			#get N imgs
			clhcbasename = 'clhc_cls'+string.zfill(bestclass+1,4)
			classname=os.path.join(classnamepath, clhcbasename+'.spi')
		
			nptcls=apEMAN.getNPtcls(classname,spider=True)
			averages[bestclass].setNImg(nptcls)
			averages[bestclass].setRAlign(e)
			averages[bestclass].writeImage('goodavgs.hed',-1)
			
            classname = os.path.join(classnamepath, 'combined.spi')
            apEMAN.combineSpiParticleList(spilist, classname)
            clhcbasename = 'combined'

            # convert spider lst to EMAN lst
            convertlst = apEMAN.convertSpiderToEMAN(classname, clslist[cls])
            # change location of start file reference
            f = open(convertlst, 'r')
            fnew = open(convertlst + '.tmp', 'w')
            for l in f:
                fnew.write(re.sub('\tstart', '\t../../start', l))
            f.close()
            fnew.close()
            os.rename(convertlst + '.tmp', convertlst)

            apEMAN.makeClassAverages(convertlst, 'goodavgs.hed', e,
                                     params['mask'])

        ### otherwise just use the best class average
        else:
            bestclass = cclist.index(max(cclist))
            print "Using average %d for class %d" % (bestclass, cls)

            #get N imgs
            clhcbasename = 'clhc_cls' + string.zfill(bestclass + 1, 4)
            classname = os.path.join(classnamepath, clhcbasename + '.spi')

            nptcls = apEMAN.getNPtcls(classname, spider=True)
            averages[bestclass].setNImg(nptcls)
            averages[bestclass].setRAlign(e)
            averages[bestclass].writeImage('goodavgs.hed', -1)
					newline = re.sub('\tstart','\t../../start',lines[line])
					if line%2:
						nodd+=1
						odd.write(newline)
					else:
						neven+=1
						even.write(newline)
				odd.close()
				even.close()

				# create even and odd class stacks
				evenstack = 'newavgs.even.hed'
				oddstack = 'newavgs.odd.hed'

				if neven>0:
					apEMAN.makeClassAverages(evenlst,evenstack,e,params['mask'])
				if nodd>0:
		       			apEMAN.makeClassAverages(oddlst,oddstack,e,params['mask'])
			

	pad=int(params['boxsize']*1.25)
	if pad%2:
		pad=pad+1

	# create 3d model:
	make3dcommand='make3d newavgs.hed out=threed.%d.asym.mrc mask=%d sym=c1 pad=%d mode=2 hard=%d' % (params['iter'], params['mask'], pad, params['hard'])
	apEMAN.writeEMANTime('../refine.log', make3dcommand)
	print make3dcommand
	proc = subprocess.Popen(make3dcommand, shell=True)
	proc.wait()