source: trunk/imports/G2img_CBF.py @ 4213

Last change on this file since 4213 was 4213, checked in by toby, 3 years ago

multiple small changes to allow docs build under 3.x

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Author Revision URL Id
File size: 3.6 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2019-12-20 05:04:03 +0000 (Fri, 20 Dec 2019) $
4# $Author: toby $
5# $Revision: 4213 $
6# $URL: trunk/imports/G2img_CBF.py $
7# $Id: G2img_CBF.py 4213 2019-12-20 05:04:03Z toby $
8########### SVN repository information ###################
9'''
10*Module G2img_CBF: .cbf cif image file*
11---------------------------------------
12
13'''
14
15from __future__ import division, print_function
16import time
17import GSASIIobj as G2obj
18import GSASIIpath
19import struct as st
20import numpy as np
21GSASIIpath.SetVersionNumber("$Revision: 4213 $")
22class CBF_ReaderClass(G2obj.ImportImage):
23    '''Routine to read a Read cif image data .cbf file.
24    This is used by Pilatus.
25    '''
26    def __init__(self):
27        super(self.__class__,self).__init__( # fancy way to self-reference
28            extensionlist=('.cbf',),
29            strictExtension=True,
30            formatName = 'CBF image',
31            longFormatName = 'CIF Binary Data Format image file (NB: Slow!)'
32            )
33
34    def ContentsValidator(self, filename):       
35        '''no test used at this time
36        '''
37        return True
38       
39    def Reader(self,filename, ParentFrame=None, **unused):
40        '''Read using Bob's routine :func:`GetCbfData`
41        '''
42        self.Comments,self.Data,self.Npix,self.Image = GetCbfData(self,filename)
43        if self.Npix == 0 or not self.Comments:
44            return False
45        self.LoadImage(ParentFrame,filename)
46        return True
47       
48def GetCbfData(self,filename):   
49    'Read cif binarydetector data cbf file'
50   
51    import unpack_cbf as cbf
52    if GSASIIpath.GetConfigValue('debug'):
53        print ('Read cif binary detector data cbf file: '+filename)
54    File = open(filename,'rb')
55    sizexy = [0,0]
56    pixSize = [172,172]     #Pixium4700?
57    cent = [0,0]
58    wave = 1.54187  #default <CuKa>
59    dist = 1000.
60    byteOrd = '<'
61    stream = File.read()
62    if 'bytes' in str(type(stream)):
63        stream = stream.decode('latin-1')
64    starter = '\x0c\x1a\x04\xd5'
65    imageBeg = stream.find(starter)+4
66    head = stream[:imageBeg]
67    term = '\r\n'       #CR-LF
68    if term in head:
69        pass
70    else:
71        term = '\n' #LF only
72    head = head.split(term)
73    for line in head:
74        fields = line.split()
75        if 'Wavelength' in line:
76            wave = float(fields[2])
77        elif 'Detector_distance' in line:
78            dist = float(fields[2])*1000.
79        elif 'Pixel_size' in line:
80            pixSize = [float(fields[2])*1.e6,float(fields[5])*1.e6]
81        elif 'Beam_xy' in line:
82            cent = [float(fields[2].strip('(').strip(',')),float(fields[3].strip(')'))]
83        elif 'X-Binary-Size:' in line:
84            compImageSize = int(fields[1])
85        elif 'BIG_ENDIAN' in line:
86            byteOrd = '>'
87        elif 'Fastest-Dimension' in line:
88            sizexy[0] = int(fields[1])
89        elif 'Second-Dimension' in line:
90            sizexy[1] = int(fields[1])
91        elif 'Number-of-Elements' in line:
92            Npix = int(fields[1])
93    nxy = sizexy[0]*sizexy[1]
94    cent = [cent[0]*pixSize[0]/1000.,cent[1]*pixSize[1]/1000.]
95    File.seek(0)
96    img = File.read()[imageBeg:imageBeg+compImageSize]
97    nimg = len(img)
98    image = np.zeros(nxy,dtype=np.int32)
99    time0 = time.time()
100    if 'bytes' in str(type(img)):
101        img = np.frombuffer(img,dtype=np.uint8)
102        image = cbf.unpack_cbf3(nimg,img,nxy,image)
103    else:
104        image = cbf.unpack_cbf(nimg,img,nxy,image)
105    image = np.reshape(image,(sizexy[1],sizexy[0]))
106    print ('import time: %.3f'%(time.time()-time0))
107    data = {'pixelSize':pixSize,'wavelength':wave,'distance':dist,'center':cent,'size':sizexy}
108    Npix = sizexy[0]*sizexy[1]
109   
110    return head,data,Npix,image
111       
Note: See TracBrowser for help on using the repository browser.