def researchCorrectorImageData(self, type, scopedata, cameradata, channel):
        if type == 'dark':
            imagetemp = leginondata.DarkImageData()
        elif type == 'bright':
            imagetemp = leginondata.BrightImageData()
        elif type == 'norm':
            imagetemp = leginondata.NormImageData()
        else:
            return None

        ## query only based on certain camera parameters, not all
        imagetemp['camera'] = leginondata.CameraEMData()
        for key in ('ccdcamera', 'dimension', 'binning', 'offset',
                    'gain index'):
            imagetemp['camera'][key] = cameradata[key]
        # query only based on certain scope parameters, not all
        imagetemp['scope'] = leginondata.ScopeEMData()
        for key in ('tem', 'high tension'):
            imagetemp['scope'][key] = scopedata[key]
        imagetemp['channel'] = channel
        try:
            ref = imagetemp.query(results=1)
        except Exception, e:
            self.logger.warning('Reference image query failed: %s' % e)
            ref = None
    def calculateTiles(self, tilesize):
        ## figure out the final shape of global space
        rowsize = int(numpy.ceil(float(self.shape[0]) / tilesize) * tilesize)
        colsize = int(numpy.ceil(float(self.shape[1]) / tilesize) * tilesize)

        ## make list of stage center for each tile
        print 'calculating tile positions...'
        centerpixel = rowsize / 2.0 - 0.5, colsize / 2.0 - 0.5
        firstpixel = tilesize / 2.0 - 0.5
        rowi = 0
        tiles = ordereddict.OrderedDict()
        for row in numpy.arange(firstpixel, rowsize, tilesize):
            coli = 0
            for col in numpy.arange(firstpixel, colsize, tilesize):
                pixel = {
                    'row': row - centerpixel[0],
                    'col': col - centerpixel[1]
                }
                newstage = self.trans.transform(pixel, self.stage, self.bin)
                tilescope = leginondata.ScopeEMData(initializer=self.scope)
                tilescope['stage position'] = newstage
                tilecamera = leginondata.CameraEMData(initializer=self.camera)
                tilecamera['dimension'] = {'x': tilesize, 'y': tilesize}
                args = tilescope, tilecamera, self.timestamp
                kwargs = {'rotation': self.rotation}
                tiles[(rowi, coli)] = {'args': args, 'kwargs': kwargs}
                coli += 1
            rowi += 1

        print 'Calculated %d tiles, %d rows, %d cols' % (len(tiles), rowi,
                                                         coli)
        return tiles
Example #3
0
	def upgradeLeginonDB(self):
		if not self.leginon_dbupgrade.tableExists('SessionData'):
			return
		
		while True:
			while True:
				sessionname = raw_input('Enter the name of the first session of which\n   frames are saved with Gatan K2 Summit under DM 2.31\n   (Press RETURN if none): ')
				if not sessionname:
					return
				results = leginondata.SessionData(name=sessionname).query()
				if not results:
					print '\033[31mSession not found, Try again.\033[0m'
					continue
				self.sessiondata = results[0]
				q = leginondata.CameraEMData(session=self.sessiondata)
				q['save frames'] = True
				results = q.query(results=1)
				if not results:
					print '\033[31mNo image with saved frame found in Session. Try again.\033[0m'
					continue
				camdata = results[0]
				if not camdata['ccdcamera']['name'] in VALID_CAMERA_NAMES:
					print '\033[31mThis is not a camera that needs changing. Try again.\033[0m'
					continue
				first_image = self. getFirstImageInSession()
				if not first_image:
					print '\033[31mThis session has no image saved.  Try agin.\033[0m'
					continue
				k2cameras = self.getRelatedFrameCameras(camdata)
				self.saveFrameOrientation(k2cameras,first_image)
				break
			answer = raw_input('Another K2 camera ? (Y/N)')
			if answer.upper() =='N':
				return
    def findBrightImageFromNorm(self, normdata):
        '''
		Find BrighetImageData based on imported NormImageData.
		This is needed for older data since BrightImageData was
		not linked to AcquisitionImages previously.
		'''
        if normdata['bright']:
            return normdata['bright']
        timestamp = normdata.timestamp
        normcam = normdata['camera']
        qcam = leginondata.CameraEMData(dimension=normcam['dimension'],
                                        offset=normcam['offset'],
                                        binning=normcam['binning'],
                                        ccdcamera=normcam['ccdcamera'])
        qcam['exposure type'] = 'normal'
        qcam['energy filtered'] = normcam['energy filtered']

        normscope = normdata['scope']
        qscope = leginondata.ScopeEMData(tem=normscope['tem'])
        qscope['high tension'] = normscope['high tension']
        q = leginondata.BrightImageData(camera=qcam,
                                        scope=qscope,
                                        channel=normdata['channel'])
        brightlist = q.query()
        for brightdata in brightlist:
            if brightdata.timestamp < timestamp:
                break
        return brightdata
	def alreadyAcquired(self, targetdata, presetname):
		'''
		determines if image already acquired using targetdata and presetname
		'''
		## if image exists with targetdata and presetdata, no acquire
		## we expect target to be exact, however, presetdata may have
		## changed so we only query on preset name

		# seems to have trouple with using original targetdata as
		# a query, so use a copy with only some of the fields
		presetquery = leginondata.PresetData(name=presetname)
		targetquery = leginondata.AcquisitionImageTargetData(initializer=targetdata)
		## don't care if drift correction was done on target after image was
		## acquired, so ignore version, delta row/col, parentimage, and fromtarget
		targetquery['version'] = None
		targetquery['delta row'] = None
		targetquery['delta column'] = None
		targetquery['image'] = None
		targetquery['fromtarget'] = None
		imagequery = leginondata.AcquisitionImageData(target=targetquery, preset=presetquery)
		## other things to fill in
		imagequery['scope'] = leginondata.ScopeEMData()
		imagequery['camera'] = leginondata.CameraEMData()
		imagequery['session'] = leginondata.SessionData()
		datalist = self.research(datainstance=imagequery)
		if datalist:
			## no need to acquire again, but need to republish
			self.reportStatus('output', 'Image was acquired previously, republishing')
			imagedata = datalist[0]
			self.publishDisplayWait(imagedata)
			return True
		else:
			return False
Example #6
0
def saveSessionDDinfoToDatabase(sessiondata):
    qcam = leginondata.CameraEMData(session=sessiondata)
    qcam['save frames'] = True
    acqimages = leginondata.AcquisitionImageData(camera=qcam).query()
    for imagedata in acqimages:
        infopath = os.path.join(sessiondata['image path'],
                                imagedata['filename'] + '.frames', 'info.path')
        saveImageDDinfoToDatabase(imagedata, infopath)
    def upgradeLeginonDB(self):
        if not self.leginon_dbupgrade.columnExists(
                'AcquisitionImageData',
                'REF|CorrectorPlanData|corrector plan'):
            self.leginon_dbupgrade.addColumn(
                'AcquisitionImageData', 'REF|CorrectorPlanData|corrector plan',
                self.leginon_dbupgrade.int)

        results = leginondata.InstrumentData(name='DE12').query(results=1)
        if not results:
            return
        decameradata = results[0]

        dim = {'x': 4096, 'y': 3072}
        camq = leginondata.CameraEMData(ccdcamera=decameradata, dimension=dim)
        plans = leginondata.CorrectorPlanData(camera=camq).query()
        pairs = {}
        ordered_keys = []
        for i, plan in enumerate(plans):
            if i == 0:
                ordered_keys.append(i)
                pairs[i] = (plan.timestamp.now(), plan.timestamp)
            else:
                ordered_keys.append(i)
                pairs[i] = (plans[i - 1].timestamp, plan.timestamp)
        camq = leginondata.CameraEMData(ccdcamera=decameradata, dimension=dim)
        camq['save frames'] = True
        imageq = leginondata.AcquisitionImageData(camera=camq)
        print 'Query all DE12 images.  This may take some time...'
        images = imageq.query()
        print 'Total of %d images' % (len(images), )
        for image in images:
            if image['corrector plan']:
                continue
            for key in ordered_keys:
                if image.timestamp > pairs[key][1] and image.timestamp < pairs[
                        key][0]:
                    print key, image.dbid, image['filename'], image.timestamp
                    status = self.leginon_dbupgrade.updateColumn(
                        'AcquisitionImageData',
                        'REF|CorrectorPlanData|corrector plan',
                        '%d' % plans[key].dbid, '`DEF_id`=%d' % image.dbid,
                        True)
                    if not status:
                        print break_from_failed_update
    def getPlan(self):
        ccdcamera = self.instrument.getCCDCameraData()

        newcam = leginondata.CameraEMData(ccdcamera=ccdcamera)
        if self.settings['camera settings'] is None:
            plan = None
        else:
            for i in ['dimension', 'offset', 'binning']:
                newcam[i] = dict(self.settings['camera settings'][i])
            plan = self.corclient.retrievePlan(newcam)
        return plan
Example #9
0
 def getImageCameraEMData(self):
     camdata = leginondata.CameraEMData(initializer=self.image['camera'])
     # local change. Need to remove before release
     if self.isOldNRAMMData() and self.image.dbid < 1989842:
         # image dimension is not consistent with the frames
         # Use the default camera dimension, binning, and offset of the frames
         # (rotated and flipped full size)
         defaultcamdata = self.getCorrectedImageData()['camera']
         for key in ('dimension', 'binning', 'offset'):
             camdata[key] = defaultcamdata[key]
     return camdata
def createGlobalOutput(imdata, angle=0.0, bin=1):
    sys.stderr.write('creating global image space\n')
    timestamp = imdata.timestamp
    scope = leginondata.ScopeEMData(initializer=imdata['scope'])
    camera = leginondata.CameraEMData(initializer=imdata['camera'])
    binning = {
        'x': camera['binning']['x'] * bin,
        'y': camera['binning']['y'] * bin
    }
    camera['binning'] = binning
    globaloutput = MontageImage(scope, camera, timestamp, rotation=angle)
    return globaloutput
    def queryCorrectionImage(self, scopedata, camdata, type, channel):
        # only query based on instrument and high tension
        scope = leginondata.ScopeEMData()
        scope['tem'] = scopedata['tem']
        scope['high tension'] = scopedata['high tension']

        # only query based on instrument, dimension, binning, offset
        camera = leginondata.CameraEMData()
        camera['ccdcamera'] = camdata['ccdcamera']
        camera['dimension'] = camdata['dimenion']
        camera['binning'] = camdata['binning']
        camera['offset'] = camdata['offset']

        ## first try requested channel, then try any channel
        corimg = None
        for channel in (channel, None):
            ## try cache
            try:
                key = self.getkey(scopedata, camdata, type, channel)
                return self.cache[key]
            except KeyError:
                pass
            self.node.logger.info('Loading %s...' % self.formatKey(key))
            qimage = leginondata.CorrectionImageData(scope=scope,
                                                     camera=camera,
                                                     type=type,
                                                     channel=channel)
            try:
                corimg = qimage.query(results=1)
                corimg = corimg[0]
                break
            except:
                pass

                self.node.logger.warning(
                    'requested correction channel %s not available, using channel: %s'
                    % (channel, corimg['channel']))
            else:
                self.node.logger.error('No correction image in database')
                corimg = None

        if corimg is not None:
            self.node.logger.info('Correction image loaded: %s' %
                                  (corimg['filename'], ))
            ## make it float to do float math later
            image = numpy.asarray(corimg['image'], numpy.float32)
            key = self.getkey(corimg['scope'], corimg['camera'],
                              corimg['type'], corimg['channel'])
            self.cache[key] = image
        else:
            image = None
        return image
 def __init__(self, scope, camera, timestamp, fileref=None, rotation=0.0):
     self.scope = leginondata.ScopeEMData(initializer=scope)
     self.camera = leginondata.CameraEMData(initializer=camera)
     self.shape = self.camera['dimension']['y'], self.camera['dimension'][
         'x']
     self.fileref = fileref
     self.timestamp = timestamp
     self.trans = caltransformer.getTransformer(scope['tem'],
                                                camera['ccdcamera'],
                                                scope['high tension'],
                                                scope['magnification'],
                                                timestamp, rotation)
     self.newStage(scope['stage position'])
        def reseachCorrectorPlan(self, cameradata):
                qcamera = leginondata.CameraEMData()
                # Fix Me: Ignore gain index for now because camera setting does not have it when theplan is saved.
                for key in ('ccdcamera','dimension','binning','offset'):
                        qcamera[key] = cameradata[key]
                qplan = leginondata.CorrectorPlanData()
                qplan['camera'] = qcamera
                plandatalist = qplan.query()

                if plandatalist:
                        return plandatalist[0]
                else:
                        return None
 def retrieveCorrectorPlanFromSettings(self):
     ccdcameraname = self.settings['instruments']['ccdcamera']
     camsettings = self.settings['camera settings']
     if ccdcameraname is None or camsettings is None:
         return None
     cameradata = leginondata.CameraEMData()
     try:
         cameradata['ccdcamera'] = self.instrument.getCCDCameraData(
             ccdcameraname)
     except:
         return None
     cameradata.update(camsettings)
     cdata = self.instrument.getData(leginondata.CameraEMData)
     cameradata['gain index'] = cdata['gain index']
     plan, plandata = self.retrieveCorrectorPlan(cameradata)
     return plan
    def storeCorrectorImageData(self, imagedata, type, channel):

        # check for bad shape
        imarray = imagedata['image']
        shape = imarray.shape
        cameradata = imagedata['camera']
        dim = cameradata['dimension']
        if dim['x'] != shape[1] or dim['y'] != shape[0]:
            raise RuntimeError('%s: bad array shape: %s' % (
                type,
                shape,
            ))

        if type == 'dark':
            refclass = leginondata.DarkImageData
        elif type == 'bright':
            refclass = leginondata.BrightImageData
        elif type == 'norm':
            refclass = leginondata.NormImageData
        refdata = refclass(initializer=imagedata)

        refdata['filename'] = self.makeCorrectorImageFilename(
            type, channel, imarray.shape)

        ## replace session of scope, camera, refdata with ref session
        refsession = self.getReferenceSession()
        scopedata = refdata['scope']
        cameradata = refdata['camera']
        newscope = leginondata.ScopeEMData(initializer=scopedata)
        newscope['session'] = refsession
        newcamera = leginondata.CameraEMData(initializer=cameradata)
        newcamera['session'] = refsession
        refdata['session'] = refsession
        refdata['scope'] = newscope
        refdata['camera'] = newcamera
        refdata['channel'] = channel

        self.logger.info('Saving new %s' % (type, ))
        refdata.insert(force=True)
        self.logger.info('Saved: %s' % (refdata['filename'], ))

        ## store in cache
        key = self.makeCorrectorKey(type, scopedata, cameradata, channel)
        ref_cache[key] = refdata

        return refdata
Example #16
0
def saveAllPreviousToDatabase():
    qcam = leginondata.CameraEMData()
    qcam['save frames'] = True
    acqimages = leginondata.AcquisitionImageData(camera=qcam).query()
    for imagedata in acqimages:
        # check for frames dir
        camdata = imagedata['camera']
        session = imagedata['session']
        impath = session['image path']
        fname = imagedata['filename']
        infoname = os.path.join(impath, fname + '.frames', 'info.txt')
        if os.path.exists(infoname):
            # check for existing ddinfo in db
            info = leginondata.DDinfoValueData(camera=camdata).query()
            if not info:
                print 'saving:', infoname
                saveImageDDinfoToDatabase(imagedata, infoname)
 def retrieveCorrectorImageFromSettings(self, reftype, channel):
     ccdcameraname = self.settings['instruments']['ccdcamera']
     camsettings = self.settings['camera settings']
     if ccdcameraname is None or camsettings is None:
         return None
     cameradata = leginondata.CameraEMData()
     try:
         cameradata['ccdcamera'] = self.instrument.getCCDCameraData(
             ccdcameraname)
     except:
         return None
     cameradata.update(camsettings)
     cdata = self.instrument.getData(leginondata.CameraEMData)
     cameradata['gain index'] = cdata['gain index']
     scopedata = self.instrument.getData(leginondata.ScopeEMData)
     imdata = self.retrieveCorrectorImageData(reftype, scopedata,
                                              cameradata, channel)
     return imdata
    def getAlternativeChannelNorm(self, refdata):
        '''
		Get norm image data of the other channel closest in time
		'''
        if refdata is None:
            return None
        reftype = 'norm'
        timestamp = refdata.timestamp
        refcam = refdata['camera']
        qcam = leginondata.CameraEMData(dimension=refcam['dimension'],
                                        offset=refcam['offset'],
                                        binning=refcam['binning'],
                                        ccdcamera=refcam['ccdcamera'])
        qcam['exposure time'] = refcam['exposure time']
        qcam['energy filtered'] = refcam['energy filtered']
        qcam['gain index'] = refcam['gain index']

        refscope = refdata['scope']
        qscope = leginondata.ScopeEMData(tem=refscope['tem'])
        qscope['high tension'] = refscope['high tension']
        altchannel = int(refdata['channel'] == 0)
        q = self.createRefQuery(reftype, qcam, qscope, altchannel)
        reflist = q.query()
        if len(reflist) == 0:
            # Not to query exposure time if none found
            qcam['exposure time'] = None
            q = self.createRefQuery(reftype, qcam, qscope, altchannel)
            reflist = q.query()
        if len(reflist) == 0:
            #no switching, no alternative channel found
            return refdata
        for newrefdata in reflist:
            if newrefdata.timestamp < timestamp:
                break
        before_ref = newrefdata
        reflist.reverse()
        for newrefdata in reflist:
            if newrefdata.timestamp > timestamp:
                break
        after_ref = newrefdata
        if after_ref.timestamp - timestamp > timestamp - before_ref.timestamp:
            return before_ref
        else:
            return after_ref
    def saveTableau(self, imagedata):
        init = imagedata
        tabim = self.tabimage
        filename = init['filename'] + '_tableau'
        cam = leginondata.CameraEMData(initializer=init['camera'])
        tab_bin = self.settings['tableau binning']
        new_bin = {
            'x': tab_bin * cam['binning']['x'],
            'y': tab_bin * cam['binning']['y']
        }
        cam['dimension'] = {'x': tabim.shape[1], 'y': tabim.shape[0]}
        cam['binning'] = new_bin

        tabimdata = leginondata.AcquisitionImageData(initializer=imagedata,
                                                     image=self.tabimage,
                                                     filename=filename,
                                                     camera=cam)
        tabimdata.insert(force=True)
        self.logger.info('Saved tableau.')
 def storeCorrectorPlan(self, plan):
         # import instrument here so that wx is not required unless Leginon is running
         import instrument
         camsettings = self.settings['camera settings']
         ccdname = self.settings['instruments']['ccdcamera']
         ccdcamera = self.instrument.getCCDCameraData(ccdname)
         cameradata = leginondata.CameraEMData()
         cameradata.update(self.settings['camera settings'])
         cameradata['ccdcamera'] = ccdcamera
         plandata = leginondata.CorrectorPlanData()
         plandata['session'] = self.session
         plandata['camera'] = cameradata
         plandata['bad_rows'] = plan['rows']
         plandata['bad_cols'] = plan['columns']
         plandata['bad_pixels'] = plan['pixels']
         plandata['despike'] = plan['despike']
         plandata['despike size'] = plan['despike size']
         plandata['despike threshold'] = plan['despike threshold']
         plandata.insert(force=True)
def makeAlignedImageData(old_imagedata,
                         new_camdata,
                         new_array,
                         alignlabel='a'):
    '''
		Prepare ImageData to be uploaded after alignment
		'''
    label_string = '-%s' % (alignlabel)
    camdata = leginondata.CameraEMData(
        initializer=new_camdata)  # new CameraEMData for the aligned image
    align_presetdata = leginondata.PresetData(
        initializer=old_imagedata['preset'])
    if old_imagedata['preset'] is None:
        old_name = 'ma'
        align_presetdata = leginondata.PresetData(
            name='ma-%s' % (label_string),
            magnification=old_imagedata['scope']['magnification'],
            defocus=old_imagedata['scope']['defocus'],
            tem=old_imagedata['scope']['tem'],
            ccdcamera=camdata['ccdcamera'],
            session=old_imagedata['session'],
        )
    else:
        old_name = align_presetdata['name']
        align_presetdata['name'] = old_name + label_string
    align_presetdata['dimension'] = camdata['dimension']
    align_presetdata['binning'] = camdata['binning']
    align_presetdata['offset'] = camdata['offset']
    align_presetdata['exposure time'] = camdata['exposure time']
    # make new imagedata with the align_preset amd aligned CameraEMData
    imagedata = leginondata.AcquisitionImageData(initializer=old_imagedata)
    imagedata['preset'] = align_presetdata
    imagefilename = imagedata['filename']
    bits = imagefilename.split(old_name)
    before_string = old_name.join(bits[:-1])
    newfilename = align_presetdata['name'].join((before_string, bits[-1]))
    imagedata['camera'] = camdata
    imagedata['camera']['align frames'] = True
    imagedata['image'] = new_array
    imagedata['filename'] = makeUniqueImageFilename(imagedata, old_name,
                                                    align_presetdata['name'])
    return imagedata
 def retrievePlan(self, camdata):
     qcam = leginondata.CameraEMData()
     qcam['ccdcamera'] = camdata['ccdcamera']
     qcam['dimension'] = camdata['dimension']
     qcam['offset'] = camdata['offset']
     qcam['binning'] = camdata['binning']
     qplan = leginondata.CorrectionPlanData(camera=qcam)
     plandatalist = qplan.query(results=1)
     if plandatalist:
         plandata = plandatalist[0]
         result = {}
         result['rows'] = list(plandata['bad_rows'])
         result['columns'] = list(plandata['bad_cols'])
         if plandata['bad_pixels'] is None:
             result['pixels'] = []
         else:
             result['pixels'] = list(plandata['bad_pixels'])
         return result
     else:
         return {'rows': [], 'columns': [], 'pixels': []}
    def getBrightImageFromNorm(self, normdata):
        '''
		Get bright image used to produce the norm image
		This is made to be back compatible to early leginondata that
		has no bright image association but would be the closest in time before
		the norm was calculated
		'''
        if normdata is None:
            return None
        # newer leginon data will have bright image associated with norm image
        if 'bright' in normdata.keys() and normdata['bright'] is not None:
            return normdata['bright']
        # bright image may have the same CameraEMData
        q = leginondata.BrightImageData(camera=normdata['camera'])
        brightresults = q.query(results=1)
        if brightresults:
            return brightresults[0]
        # otherwise need to look up timestamp
        timestamp = normdata.timestamp
        normcam = normdata['camera']
        qcam = leginondata.CameraEMData(dimension=normcam['dimension'],
                                        offset=normcam['offset'],
                                        binning=normcam['binning'],
                                        ccdcamera=normcam['ccdcamera'])
        qcam['exposure type'] = 'normal'
        qcam['energy filtered'] = normcam['energy filtered']
        qcam['gain index'] = normcam['gain index']

        normscope = normdata['scope']
        qscope = leginondata.ScopeEMData(tem=normscope['tem'])
        qscope['high tension'] = normscope['high tension']
        q = leginondata.BrightImageData(camera=qcam,
                                        scope=qscope,
                                        channel=normdata['channel'])
        brightlist = q.query()
        for brightdata in brightlist:
            if brightdata.timestamp < timestamp:
                break
        return brightdata
    gaindict['meanlst'] = meanlst
    gaindict['stdlst'] = stdlst
    gaindict['timelst'] = timelst
    gaindict['templst'] = templst

    return gaindict


if __name__ == '__main__':

    args = parseArguments()
    totalimages = args.n
    sinedon.setConfig('leginondata')

    darkq = leginondata.DarkImageData()
    camq = leginondata.CameraEMData()
    camq['binning'] = {'y': long(args.b), 'x': long(args.b)}
    camq['dimension'] = {'y': long(args.y), 'x': long(args.x)}
    darkq['camera'] = camq
    print "querying dark images"
    darkdata = darkq.query(results=totalimages)
    darkdata.reverse()
    darkd = getGainInfo(darkdata, totalimages)

    brightq = leginondata.BrightImageData()
    brightq['camera'] = camq
    print "querying bright images"
    brightdata = brightq.query(results=totalimages)
    brightdata.reverse()
    brightd = getGainInfo(brightdata, totalimages)
	def getImageCameraEMData(self):
		camdata = leginondata.CameraEMData(initializer=self.image['camera'])
		return camdata
        def calculateMosaicImage(self):
                '''
                calculates (but does not generate) an unscaled mosaic image
                '''
                if not self.tiles:
                        return
                param = self.calibrationclient.parameter()
                ## calculate parameter center of final mosaic image
                center = {'x': 0.0, 'y': 0.0}
                for tile in self.tiles:
                        tileparam = tile.imagedata['scope'][param]
                        center['x'] += tileparam['x']
                        center['y'] += tileparam['y']
                n = len(self.tiles)
                center['x'] /= n
                center['y'] /= n
                self.center = center

                ## Calculate pixel vector on final image to center of 
                ## each tile.
                ## To use calibrationclient's itransform method, we need
                ## a fake image from which to calculate a pixel vector
                ## Maybe could use an actual final image leginondata.
                someimage = self.tiles[0].imagedata
                self.fakescope = leginondata.ScopeEMData(initializer=someimage['scope'])
                self.fakescope[param] = dict(someimage['scope'][param])
                self.fakescope[param].update(center)
                ## assume the final fake image has same binning as first tile
                self.fakecamera = leginondata.CameraEMData(initializer=someimage['camera'])
                tile0 = self.tiles[0]
                mosaic0 = mosaic1 = None
                for tile in self.tiles:
                        tileparam = {}
                        ## calculate the parameter shift from center of 
                        ## mosaic image to center of tile
                        for axis in ('x','y'):
                                tileparam[axis] = tile.imagedata['scope'][param][axis]
                        ## calculate corresponding pixel shift (float)
                        center2center = self.positionByCalibration(tileparam)
                        ## for targeting, until it's fixed
                        tile.position = center2center

                        ## pixel shift mosaic center to tile center (int)
                        center2center = self.round(center2center)
                        tile.center_vect = center2center

                        ## pixel shift from center of mosaic to corners of tile
                        shape = tile.image.shape
                        corner_vect = center2center[0]-shape[0]/2, center2center[1]-shape[1]/2
                        corner1_vect = corner_vect[0]+shape[0], corner_vect[1]+shape[1]
                        tile.corner_vect = corner_vect
                        ## check if this is a min or max in the mosaic
                        if mosaic0 is None:
                                mosaic0 = [corner_vect[0], corner_vect[1]]
                                mosaic1 = [corner1_vect[0], corner1_vect[1]]
                        for axis in (0,1):
                                if corner_vect[axis] < mosaic0[axis]:
                                        mosaic0[axis] = corner_vect[axis]
                                if corner1_vect[axis] > mosaic1[axis]:
                                        mosaic1[axis] = corner1_vect[axis]
                ## mosaic shape at full scale
                self.mosaicshape = mosaic1[0]-mosaic0[0], mosaic1[1]-mosaic0[1]

                ## center of mosaic image
                mosaic_center = self.mosaicshape[0]/2, self.mosaicshape[1]/2

                ## position of corner and center
                for tile in self.tiles:
                        corner_pos = tile.corner_vect[0]-mosaic0[0], tile.corner_vect[1]-mosaic0[1]
                        center_pos = tile.center_vect[0]-mosaic0[0], tile.center_vect[1]-mosaic0[1]
                        tile.corner_pos = corner_pos
                        tile.center_pos = center_pos
mod['encore'] = 'asdf'

md['other'] = mod

scopedata = leginondata.ScopeEMData(('scopeasdf', ),
                                    initializer={
                                        'magnification': 1501,
                                        'beam tilt': {
                                            'x': 1.1,
                                            'y': 2.2
                                        }
                                    })
cameradata = leginondata.CameraEMData(('camasdf', ),
                                      initializer={
                                          'exposure time': 510,
                                          'binning': {
                                              'x': 1,
                                              'y': 1
                                          }
                                      })

## PresetData
pdata = leginondata.NewPresetData(('pdata', 1))
pdata['name'] = 'hole3'
pdata['magnification'] = 1900
pdata['spot size'] = 4
pdata['beam shift'] = {'x': 5.5, 'y': 9.3}
pdata['exposure time'] = 500
pdata['binning'] = {'x': 8, 'y': 8}
## PresetImageData: contains PresetData
mydata = leginondata.NewPresetImageData(('pidata', 1))
mydata['preset'] = pdata
				## beam tilt correction induced by image shift
				beamtiltclient = self.calclients['beam tilt']
				tem = self.instrument.getTEMData()
				cam = self.instrument.getCCDCameraData()
				ht = self.instrument.tem.HighTension
				mag = self.instrument.tem.Magnification
				imageshift = self.instrument.tem.getImageShift()
				self.beamtilt0 = self.instrument.tem.getBeamTilt()
				try:
					beamtilt = beamtiltclient.transformImageShiftToBeamTilt(imageshift, tem, cam, ht, self.beamtilt0, mag)
					self.instrument.tem.BeamTilt = beamtilt
					self.logger.info("beam tilt for image acquired (%.4f,%.4f)" % (self.instrument.tem.BeamTilt['x'],self.instrument.tem.BeamTilt['y']))
				except Exception, e:
					raise NoMoveCalibration(e)
			if self.settings['adjust time by tilt'] and abs(stagea) > 10 * 3.14159 / 180:
				camdata = leginondata.CameraEMData()
				camdata.friendly_update(presetdata)
				old_time = camdata['exposure time']
				new_time = old_time / math.cos(stagea)
				if new_time > 5000 or new_time <= 1:
					self.logger.warning('Ignore unreasonable exposure time at %d ms' % new_time)
					new_time = old_time
				camdata['exposure time'] = new_time
				self.logger.info('scale exposure time from %d to %d by cos(%d)' % (old_time,new_time,int(stagea*180/3.14159)))
				self.instrument.setData(camdata)
			self.onTarget = True
			self.setStatus('processing')
			return status

	def acquireCCD(self, presetdata, emtarget=None,channel=None):
		targetdata = emtarget['target']