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 numpy as np |
---|
20 | GSASIIpath.SetVersionNumber("$Revision: 3207 $") |
---|
21 | class 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 | |
---|
46 | def 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[1])*10. |
---|
76 | elif 'ANGLES' in line: |
---|
77 | twoth = float(fields[0]) |
---|
78 | elif 'CENTER' in line: |
---|
79 | cent = [float(fields[0]),float(fields[1])] |
---|
80 | elif 'NROWS' in line: |
---|
81 | sizexy[1] = int(fields[0]) |
---|
82 | elif 'NCOLS' in line: |
---|
83 | sizexy[0] = int(fields[0]) |
---|
84 | elif 'FORMAT' in line: |
---|
85 | frmt = int(fields[0]) |
---|
86 | elif 'HDRBLKS' in line: |
---|
87 | imageBeg = 512*int(fields[0]) |
---|
88 | elif 'NOVERFL' in line: |
---|
89 | Nunder = int(fields[0]) |
---|
90 | N2byte = 2*int(fields[1]) |
---|
91 | if N2byte%16: |
---|
92 | N2byte = (N2byte//16+1)*16 |
---|
93 | N4byte = 4*int(fields[2]) |
---|
94 | if N4byte%16: |
---|
95 | N4byte = (N4byte//16+1)*16 |
---|
96 | if frmt == 86: |
---|
97 | lines = ['FORMAT 86 Bruker files currently not readible by GSAS-II',] |
---|
98 | return lines,0,0,0 |
---|
99 | nxy = sizexy[0]*sizexy[1] |
---|
100 | cent = [cent[0]*pixSize[0]/1000.,cent[1]*pixSize[1]/1000.] |
---|
101 | cent[0] += dist*np.tan(np.pi*twoth/180.) |
---|
102 | File.seek(imageBeg) |
---|
103 | img = File.read(nxy) |
---|
104 | img2byte = File.read(N2byte) |
---|
105 | img4byte = File.read(N4byte) |
---|
106 | time0 = time.time() |
---|
107 | img = np.array(np.frombuffer(img,dtype='u1'),dtype=np.int32) |
---|
108 | img2byte = np.array(np.frombuffer(img2byte,dtype='u2'),dtype=np.int32) |
---|
109 | img4byte = np.array(np.frombuffer(img4byte,dtype='u4'),dtype=np.int32) |
---|
110 | ins2byte = np.argwhere(img==255) |
---|
111 | for j,i in enumerate(list(ins2byte)): |
---|
112 | img[i] = img2byte[j] |
---|
113 | ins4byte = np.argwhere(img==65535) |
---|
114 | for j,i in enumerate(list(ins4byte)): |
---|
115 | img[i] = img4byte[j] |
---|
116 | image = np.reshape(img,(sizexy[1],sizexy[0])) |
---|
117 | print ('import time: %.3f'%(time.time()-time0)) |
---|
118 | data = {'pixelSize':pixSize,'wavelength':wave,'distance':dist,'center':cent, |
---|
119 | 'size':sizexy,'target':target,'tilt':-twoth,'rotation':90.} |
---|
120 | data['pixLimit'] = 5 |
---|
121 | data['calibdmin'] = 1.0 |
---|
122 | data['cutoff'] = .5 |
---|
123 | Npix = sizexy[0]*sizexy[1] |
---|
124 | |
---|
125 | return lines,data,Npix,image |
---|
126 | |
---|