forked from HeLiuCMU/HEXOMAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntBin.py
191 lines (168 loc) · 7.23 KB
/
IntBin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import numpy as np
import struct
"""
Note:
1. Can only integrate to one degree
2. This function just put all data together, so there may be some pixel show multiple times in the data chunk. e.g. same (x,y) value appears multiple times.
3. Due to the same reason, the Peak IDs will be messed up (Peak ID from several images are put together to one image)
4. Although issues 2 and 3 exist, I9 still works fine with integerated binary files. Look at "ImageData.cpp".
"""
output='/home/hedm/work/allison_apr18/WE43Reduced_int_z19/WE43_z19_'
ImagePar={'nDetectors':2,
'sBinFilePrefix':'/home/hedm/work/allison_apr18/WE43Reduced_1/WE43_z19_',
'nReductionNSUM':4,
'nBinFileIndexStart':0,
'fOmegaStart':-90,
'fOmegaStop':90,
}
def IntegrateBinFiles(oPar,outputprefix):
"""
Note: only can integrate to one degree
"""
nDegrees=oPar['fOmegaStop']-oPar['fOmegaStart']
for k in range(oPar['nDetectors']):
indx=oPar['nBinFileIndexStart']
remap_indx=indx
for i in range(nDegrees):
integ=[[],[],[],[]]
for j in range(oPar['nReductionNSUM']):
print('Reading:',oPar['sBinFilePrefix']+"{0:06d}".format(indx)+'.bin'+str(k))
try:
bi=ReadI9BinaryFiles(oPar['sBinFilePrefix']+"{0:06d}".format(indx)+'.bin'+str(k))
except:
print('Reading failed')
for ii in range(4):
integ[ii].extend(bi[ii])
indx=indx+1
print('Writing:',outputprefix+'{0:06d}'.format(remap_indx)+'.bin'+str(k))
filename=outputprefix+'{0:06d}'.format(remap_indx)+'.bin'+str(k)
try:
WritePeakBinaryFile(integ,filename)
except:
print('Writing failed')
remap_indx=remap_indx+1
def ReadI9BinaryFiles(filename):
"""
%% [float(32) version header]
%% [SBlockHeader]
%% [SubHeader for first coordinate]
%% [ n uint(16) pixel first coordinate ]
%% [SubHeader for second coordinate]
%% [ n uint(16) pixel second coordinate ]
%% [SubHeader for intensity ]
%% [ n float(32) pixel intensity ]
%% [SubHeader for peakID ]
%% [ n uint(32) for peakID ]
"""
fid=open(filename,'rb')
headerMain={}
fid.read(4)
ReadUFFHeader(fid,headerMain)
# print headerMain['NameSize']
ChildXLoc=struct.unpack('I',fid.read(4))[0]
ChildYLoc=struct.unpack('I',fid.read(4))[0]
ChildIntLoc=struct.unpack('I',fid.read(4))[0]
CildPeakLoc=struct.unpack('I',fid.read(4))[0]
NumPeaks=struct.unpack('I',fid.read(4))[0]
Header1={}
ReadUFFHeader(fid,Header1)
# print Header1['NameSize']
nElements=(Header1['DataSize']-Header1['NameSize'])/2
#print(nElements)
#print(fid.read(2*nElements))
nElements = int(nElements)
x=struct.unpack('{0:d}H'.format(nElements),fid.read(2*nElements))
Header1={}
ReadUFFHeader(fid,Header1)
nCheck=(Header1['DataSize']-Header1['NameSize'])/2
# print Header1['NameSize']
if nCheck!=nElements:
raise Exception('Number of elements mismatch')
y=struct.unpack('{0:d}H'.format(nElements),fid.read(2*nElements))
Header1={}
ReadUFFHeader(fid,Header1)
nCheck=(Header1['DataSize']-Header1['NameSize'])/4
# print Header1['NameSize']
if nCheck!=nElements:
raise Exception('Number of elements mismatch')
intensity=struct.unpack('{0:d}f'.format(nElements),fid.read(4*nElements))
Header1={}
ReadUFFHeader(fid,Header1)
nCheck=(Header1['DataSize']-Header1['NameSize'])/2
# print Header1['NameSize']
if nCheck!=nElements:
raise Exception('Number of elements mismatch')
PeakID=struct.unpack('{0:d}H'.format(nElements),fid.read(2*nElements))
fid.close()
return np.array(x),np.array(y),np.array(intensity),np.array(PeakID)
def ReadUFFHeader(fid,header):
tmp=fid.read(4)
uBlockHeader=struct.unpack('I',tmp)[0]
if uBlockHeader != int('FEEDBEEF',16):
raise Exception('file is corrupted')
header['BlockType']=struct.unpack('H',fid.read(2))[0]
header['DataFormat']=struct.unpack('H',fid.read(2))[0]
header['NumChildren']=struct.unpack('H',fid.read(2))[0]
header['NameSize']=struct.unpack('H',fid.read(2))[0]
header['DataSize']=struct.unpack('I',fid.read(4))[0]
header['ChunkNumber']=struct.unpack('H',fid.read(2))[0]
header['TotalChunks']=struct.unpack('H',fid.read(2))[0]
header['BlockName']=struct.unpack('{0:d}s'.format(header['NameSize']),fid.read(header['NameSize']))[0]
def WritePeakBinaryFile(snp,sFilename):
headerMain={'BlockType':1,'DataFormat':1,'NumChildren':4,'NameSize':9,'BlockName':'PeakFile','DataSize':0,'ChunkNumber':0,'TotalChunks':0}
headerX={'BlockType':1,'DataFormat':1,'NumChildren':0,'NameSize':12,'BlockName':'PixelCoord0','DataSize':0,'ChunkNumber':0,'TotalChunks':0}
headerY={'BlockType':1,'DataFormat':1,'NumChildren':0,'NameSize':12,'BlockName':'PixelCoord1','DataSize':0,'ChunkNumber':0,'TotalChunks':0}
headerInt={'BlockType':1,'DataFormat':1,'NumChildren':0,'NameSize':10,'BlockName':'Intensity','DataSize':0,'ChunkNumber':0,'TotalChunks':0}
headerPeakID={'BlockType':1,'DataFormat':1,'NumChildren':0,'NameSize':7,'BlockName':'PeakID','DataSize':0,'ChunkNumber':0,'TotalChunks':0}
n=len(snp[0])
headerPeakID['DataSize']=n*2+7
headerInt['DataSize']=n*4+10
headerX['DataSize']=n*2+12
headerY['DataSize']=n*2+12
ChildrenPtrSize=4*4
HeaderSize=20
ChildXLoc=4
ChildYLoc=ChildXLoc+headerX['DataSize']+HeaderSize
ChildIntLoc=ChildYLoc+headerY['DataSize']+HeaderSize
ChildPeakLoc=ChildIntLoc+headerInt['DataSize']+HeaderSize
headerMain['DataSize']=ChildPeakLoc+headerPeakID['DataSize']+HeaderSize+ChildrenPtrSize+4
f=open(sFilename,'wb')
f.write(struct.pack('f',1))
WriteUFFHeader(f,headerMain)
f.write(struct.pack('I',ChildXLoc))
f.write(struct.pack('I',ChildYLoc))
f.write(struct.pack('I',ChildIntLoc))
f.write(struct.pack('I',ChildPeakLoc))
if n>0:
nPeak=len(np.unique(snp[3]))
else:
nPeak=0
f.write(struct.pack('I',nPeak))
WriteUFFHeader(f,headerX)
if n>0:
f.write(struct.pack('{0:d}H'.format(n),*snp[0]))
WriteUFFHeader(f,headerY)
if n>0:
f.write(struct.pack('{0:d}H'.format(n),*snp[1]))
WriteUFFHeader(f,headerInt)
if n>0:
f.write(struct.pack('{0:d}f'.format(n),*snp[2]))
WriteUFFHeader(f,headerPeakID)
if n>0:
f.write(struct.pack('{0:d}H'.format(n),*snp[3]))
f.close()
def WriteUFFHeader(fid,header):
uBlockHeader = int('FEEDBEEF',16)
fid.write(struct.pack('I',uBlockHeader))
fid.write(struct.pack('H',header['BlockType']))
fid.write(struct.pack('H',header['DataFormat']))
fid.write(struct.pack('H',header['NumChildren']))
fid.write(struct.pack('H',header['NameSize']))
fid.write(struct.pack('I',header['DataSize']))
fid.write(struct.pack('H',header['ChunkNumber']))
fid.write(struct.pack('H',header['TotalChunks']))
fid.write(struct.pack('{0:d}s'.format(header['NameSize']),str.encode(header['BlockName'])))
def main():
IntegrateBinFiles(ImagePar,output)
return
if __name__=='__main__': main()