def prepareTiltFile(sessionname, seriesname, tiltfilename, tiltseriesnumber, raw_path, frame_aligned_images, link=False, coarse=True):
	'''
	Creates tlt file from basic image information and copies raw images
	'''
	
	sessiondata = apDatabase.getSessionDataFromSessionName(sessionname)
	tiltseriesdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(tiltseriesnumber,sessiondata)
	tiltdata = apTomo.getImageList([tiltseriesdata])
	apDisplay.printMsg("getting imagelist")
	
	frame_tiltdata, non_frame_tiltdata = frameOrNonFrameTiltdata(tiltdata)
	tilts,ordered_imagelist,accumulated_dose_list,ordered_mrc_files,refimg = apTomo.orderImageList(frame_tiltdata, non_frame_tiltdata, frame_aligned=frame_aligned_images)
	if frame_aligned_images == "True":  #Azimuth is only present in the non-frame aligned images
		a,ordered_imagelist_for_azimuth,c,d,e = apTomo.orderImageList(frame_tiltdata, non_frame_tiltdata, frame_aligned="False")
	
	#tilts are tilt angles, ordered_imagelist are imagedata, ordered_mrc_files are paths to files, refimg is an int
	maxtilt = max([abs(tilts[0]),abs(tilts[-1])])
	apDisplay.printMsg("highest tilt angle is %f" % maxtilt)
	
	if coarse == "True":
		if frame_aligned_images == "True":  #Azimuth is only present in the non-frame aligned images
			azimuth = apTomo.getAverageAzimuthFromSeries(ordered_imagelist_for_azimuth)
		else:
			azimuth = apTomo.getAverageAzimuthFromSeries(ordered_imagelist)
		
		rawexists = apParam.createDirectory(raw_path)
		
		apDisplay.printMsg("Copying raw images, y-flipping, normalizing, and converting images to float32 for Protomo...") #Linking removed because raw images need to be y-flipped for Protomo:(.
		newfilenames, new_ordered_imagelist = apProTomo.getImageFiles(ordered_imagelist, raw_path, link=False, copy="True")
		
		###create tilt file
		#get image size from the first image
		imagesizex = tiltdata[0]['image'].shape[1]
		imagesizey = tiltdata[0]['image'].shape[0]
		
		#shift half tilt series relative to eachother
		#SS I'm arbitrarily making the bin parameter here 1 because it's not necessary to sample at this point
		shifts = apTomo.getGlobalShift(ordered_imagelist, 1, refimg)
		
		#OPTION: refinement might be more robust by doing one round of IMOD aligment to prealign images before doing protomo refine
		origins = convertShiftsToOrigin(shifts, imagesizex, imagesizey)
	
		writeTiltFile2(tiltfilename, seriesname, newfilenames, origins, tilts, azimuth, refimg)
	
	return tilts, accumulated_dose_list, new_ordered_imagelist, maxtilt
        def start(self):
        
                ### some of this should go in preloop functions
        
                ###do queries
                sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
                self.sessiondata = sessiondata
                tiltseriesdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['tiltseries'],sessiondata)
                tiltdata=apTomo.getImageList([tiltseriesdata])
                description = self.params['description']
                apDisplay.printMsg("getting imagelist")

                tilts,ordered_imagelist,ordered_mrc_files,refimg = apTomo.orderImageList(tiltdata)
                #tilts are tilt angles, ordered_imagelist are imagedata, ordered_mrc_files are paths to files, refimg is an int

                ###set up files
                seriesname='series'+str(self.params['tiltseries'])
                tiltfilename=seriesname+'.tlt'
                param_out=seriesname+'.param'
                maxtilt=max([abs(tilts[0]),abs(tilts[-1])])
                apDisplay.printMsg("highest tilt angle is %f" % maxtilt)
                self.params['cos_alpha']=math.cos(maxtilt*math.pi/180)
                self.params['raw_path']=os.path.join(self.params['rundir'],'raw')


                rawexists=apParam.createDirectory(self.params['raw_path'])

                apDisplay.printMsg("copying raw images")
                newfilenames=apProTomo.getImageFiles(ordered_imagelist,self.params['raw_path'], link=False)
        
                ###create tilt file

                #get image size from the first image
                imagesizex=tiltdata[0]['image'].shape[0]
                imagesizey=tiltdata[0]['image'].shape[1]

                #shift half tilt series relative to eachother
                #SS I'm arbitrarily making the bin parameter here 1 because it's not necessary to sample at this point
                shifts = apTomo.getGlobalShift(ordered_imagelist, 1, refimg)
                
                #OPTION: refinement might be more robust by doing one round of IMOD aligment to prealign images before doing protomo refine
                origins=apProTomo2Prep.convertShiftsToOrigin(shifts, imagesizex, imagesizey)

                #determine azimuth
                azimuth=apTomo.getAverageAzimuthFromSeries(ordered_imagelist)
                apProTomo2Prep.writeTileFile2(tiltfilename, seriesname, newfilenames, origins, tilts, azimuth, refimg)
		
=======
		imagesizex = tiltdata[0]['image'].shape[0]
		imagesizey = tiltdata[0]['image'].shape[1]

>>>>>>> .r18846
		#shift half tilt series relative to eachother
		#SS I'm arbitrarily making the bin parameter here 1 because it's not necessary to sample at this point
		shifts = apTomo.getGlobalShift(ordered_imagelist, 1, refimg)
		
		#OPTION: refinement might be more robust by doing one round of IMOD aligment to prealign images before doing protomo refine
		origins = apProTomo2Prep.convertShiftsToOrigin(shifts, imagesizex, imagesizey)

		#determine azimuth
<<<<<<< .mine
		azimuth = apTomo.getAverageAzimuthFromSeries(ordered_imagelist)
		apProTomo2Prep.writeTiltFile2(tiltfilename, seriesname, newfilenames, origins, tilts, azimuth, refimg)
=======
		azimuth = apTomo.getAverageAzimuthFromSeries(ordered_imagelist)
		if self.params['azimuth'] is not None:
			azimuth = self.params['azimuth']
		apProTomo2Prep.writeTileFile2(tiltfilename, seriesname, newfilenames, origins, tilts, azimuth, refimg)
>>>>>>> .r18846

#=====================
if __name__ == '__main__':
	protomo2prep = ProTomo2Prep()
	protomo2prep.start()
	protomo2prep.close()