source: trunk/imports/G2img_SFRM.py @ 3330

Last change on this file since 3330 was 3330, checked in by vondreele, 5 years ago

modify Bruker image import to keep "TARGET" and use it in SaveIntegration? to put Ka1/Ka2 wavelengths into PWDR Instrument Parameters

File size: 4.4 KB
Line 
1# -*- coding: utf-8 -*-
2########### SVN repository information ###################
3# $Date: 2017-12-26 20:18:10 -0600 (Tue, 26 Dec 2017) $
4# $Author: toby $
5# $Revision: 3207 $
6# $URL: https://subversion.xray.aps.anl.gov/pyGSAS/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_SFRM: .sfrm image file*
11---------------------------------------
12
13'''
14
15from __future__ import division, print_function
16import time
17import GSASIIobj as G2obj
18import GSASIIpath
19import numpy as np
20GSASIIpath.SetVersionNumber("$Revision: 3207 $")
21class SFRM_ReaderClass(G2obj.ImportImage):
22    '''Routine to read a Read Bruker Advance image data .sfrm file.
23    '''
24    def __init__(self):
25        super(self.__class__,self).__init__( # fancy way to self-reference
26            extensionlist=('.sfrm',),
27            strictExtension=True,
28            formatName = 'SFRM image',
29            longFormatName = 'Bruker SFRM Binary Data Format image file'
30            )
31
32    def ContentsValidator(self, filename):       
33        '''no test used at this time
34        '''
35        return True
36       
37    def Reader(self,filename, ParentFrame=None, **unused):
38        '''Read using Bob's routine :func:`GetSFRMData`
39        '''
40        self.Comments,self.Data,self.Npix,self.Image = GetSFRMData(self,filename)
41        if self.Npix == 0 or not len(self.Comments):
42            return False
43        self.LoadImage(ParentFrame,filename)
44        return True
45       
46def GetSFRMData(self,filename):   
47    'Read cbf compressed binarydetector data sfrm file'
48   
49    if GSASIIpath.GetConfigValue('debug'):
50        print ('Read cbf compressed binary detector data sfrm file: '+filename)
51    File = open(filename,'rb')
52    sizexy = [0,0]
53    pixSize = [135.3,135.3]     #Pixium4700?
54    cent = [0,0]
55    wave = 1.54187  #default <CuKa>
56    dist = 250.
57    stream = File.read()
58    if 'bytes' in str(type(stream)):
59        stream = stream.decode('latin-1')
60    starter = 'IMG: '
61    meanwaves = {'Cu':1.54051,'Ti':2.74841,'Cr':2.28962,'Fe':1.93597,
62        'Co':1.78892,'Mo':0.70926,'Ag':0.559363}
63    imageBeg = stream.find(starter)+4
64    head = np.array(list(stream[:imageBeg].split('CFR:')[0]))
65    head = head.reshape(-1,80)
66    lines = []
67    for line in head:
68        line = ''.join(line)
69        lines.append(line)
70        fields = line.split(':')[1].split()
71        if 'TARGET' in line:
72            wave = meanwaves[fields[0]]
73            target = fields[0].capitalize()
74        elif 'DISTANC' in line:
75            dist = float(fields[0])*10.
76        elif 'CENTER' in line:
77            cent = [float(fields[0]),float(fields[1])]
78        elif 'NROWS' in line:
79            sizexy[1] = int(fields[0])
80        elif 'NCOLS' in line:
81            sizexy[0] = int(fields[0])
82        elif 'FORMAT' in line:
83            frmt = int(fields[0])
84        elif 'HDRBLKS' in line:
85            imageBeg = 512*int(fields[0])
86        elif 'NOVERFL' in line:
87            Nunder = int(fields[0])
88            N2byte = 2*int(fields[1])
89            if N2byte%16:
90                N2byte = (N2byte//16+1)*16
91            N4byte = 4*int(fields[2])
92            if N4byte%16:
93                N4byte = (N4byte//16+1)*16
94    if frmt == 86:
95        lines = ['FORMAT 86 Bruker files currently not readible by GSAS-II',]
96        return lines,0,0,0
97    nxy = sizexy[0]*sizexy[1]
98    cent = [cent[0]*pixSize[0]/1000.,cent[1]*pixSize[1]/1000.]
99    File.seek(imageBeg)
100    img = File.read(nxy)
101    img2byte = File.read(N2byte)
102    img4byte = File.read(N4byte)
103    time0 = time.time()
104    img = np.array(np.frombuffer(img,dtype='u1'),dtype=np.int32)
105    img2byte = np.array(np.frombuffer(img2byte,dtype='u2'),dtype=np.int32)
106    img4byte = np.array(np.frombuffer(img4byte,dtype='u4'),dtype=np.int32)
107    ins2byte = np.argwhere(img==255)
108    for j,i in enumerate(list(ins2byte)):
109        img[i] = img2byte[j]
110    ins4byte = np.argwhere(img==65535)
111    for j,i in enumerate(list(ins4byte)):
112        img[i] = img4byte[j]
113    image = np.reshape(img,(sizexy[1],sizexy[0]))
114    print ('import time: %.3f'%(time.time()-time0))
115    data = {'pixelSize':pixSize,'wavelength':wave,'distance':dist,'center':cent,'size':sizexy,'target':target}
116    Npix = sizexy[0]*sizexy[1]
117   
118    return lines,data,Npix,image
119       
Note: See TracBrowser for help on using the repository browser.