source: trunk/imports/G2img_CBF.py @ 3207

Last change on this file since 3207 was 3207, checked in by toby, 4 years ago

New command-line tutorial

  • 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: 2017-12-27 02:18:10 +0000 (Wed, 27 Dec 2017) $
4# $Author: toby $
5# $Revision: 3207 $
6# $URL: trunk/imports/G2img_CBF.py $
7# $Id: G2img_CBF.py 3207 2017-12-27 02:18:10Z 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
21import unpack_cbf as cbf
22GSASIIpath.SetVersionNumber("$Revision: 3207 $")
23class CBF_ReaderClass(G2obj.ImportImage):
24    '''Routine to read a Read cif image data .cbf file.
25    This is used by Pilatus.
26    '''
27    def __init__(self):
28        super(self.__class__,self).__init__( # fancy way to self-reference
29            extensionlist=('.cbf',),
30            strictExtension=True,
31            formatName = 'CBF image',
32            longFormatName = 'CIF Binary Data Format image file (NB: Slow!)'
33            )
34
35    def ContentsValidator(self, filename):       
36        '''no test used at this time
37        '''
38        return True
39       
40    def Reader(self,filename, ParentFrame=None, **unused):
41        '''Read using Bob's routine :func:`GetCbfData`
42        '''
43        self.Comments,self.Data,self.Npix,self.Image = GetCbfData(self,filename)
44        if self.Npix == 0 or not self.Comments:
45            return False
46        self.LoadImage(ParentFrame,filename)
47        return True
48       
49def GetCbfData(self,filename):   
50    'Read cif binarydetector data cbf file'
51   
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.