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