-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmakeEerGain_raw2mrc.py
137 lines (114 loc) · 3.01 KB
/
makeEerGain_raw2mrc.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
#!/software/EMAN2/bin/python2.7
# Copyright (c) 2019 by FEI Company
from __future__ import print_function
from __future__ import division
from past.utils import old_div
import struct
from EMAN2 import *
from numpy import *
import sys,os
import getopt
import xml.etree.ElementTree as ET
def printHelp():
progname = "makeEerGain.py"
usage = progname + """ Convert eer gain in raw format to mrc
Options:
-i, --raw \t<input raw file>
-d, --defect \t<input SensorDefects.xml>
-o, --gain \t<output gain file (with mrc extenstion)>
-h, --help
Examples:
makeEerGain.py -i gain_post_ec_eer.raw -d SensorDefects.xml -o gain4k.mrc
"""
print(usage)
exit(0)
def getFilenames(argv):
rawFile = ''
xmlFile = ''
mrcFile = ''
try:
opts, args = getopt.getopt(argv,"hi:d:o:",["help","raw=","defect=","gain="])
except getopt.GetoptError:
printHelp()
for opt, arg in opts:
if opt in ('-h','--help'):
printHelp()
sys.exit()
elif opt in ("-i", "--raw"):
rawFile = arg
elif opt in ("-o", "--gain"):
mrcFile = arg
elif opt in ('-d', '--defect'):
xmlFile = arg
else:
print(opt+'unrecognized option')
#check inputs
if rawFile=='':
print("input raw file name is missing")
printHelp()
elif xmlFile=='':
print("input defect file is missing")
printHelp()
elif mrcFile=='':
print("output mrc file name is missing")
printHelp()
return rawFile, xmlFile, mrcFile
def main():
if len(sys.argv)<2:
printHelp()
rawFile, xmlFile, mrcFile = getFilenames(sys.argv[1:])
print("Reading defects from "+xmlFile)
defectPoint=[]
defectRow=[]
defectCol=[]
#defectArea=[]
tree = ET.parse(xmlFile)
root = tree.getroot()
for elem in root.iter():
if elem.tag=='point':
defectPoint.append(map(int,elem.text.split(',')))
elif elem.tag=='col':
defectCol.append(map(int,elem.text.split('-')))
elif elem.tag=='row':
defectRow.append(map(int,elem.text.split('-')))
gainList=[]
print("Reading "+rawFile)
with open(rawFile,'rb') as a:
a.seek(49)
for i in range(0,4096*4096):
s = struct.unpack('i32',a.read(4))
gainList.append(s[0])
gain64=array(gainList).reshape((4096,4096)).astype('float64')
print("Masking pixels in "+xmlFile)
for col in defectCol:
for j in range(col[0]-1,col[1]+2):
#print('col: %d'%j)
gain64[:,j]=0
for row in defectRow:
for i in range(row[0]-1,row[1]+2):
#print('row: %d'%i)
gain64[i,:]=0
for point in defectPoint:
gain64[point[1],point[0]]=0
#print("Masking edge")
gain64[0,:]=0
gain64[:,0]=0
gain64[4095,:]=0
gain64[:,4095]=0
if True:
ind = where(gain64>0)
mm=gain64[ind].mean()
ss=gain64[ind].std()
dd=16
print("Electrons in the gain: %f"%mm)
ind2=where(gain64>mm+ss*dd)
#ind3=where(gain64<mm-ss*dd)
#print("Extra defects: hot %d cold %d"%(len(ind2[0]),len(ind3[0])))
#gain64[ind3]=0
print("Masking %d hot pixels beyond %d sigma"%(len(ind2[0]),dd))
gain64[ind2]=0
print("Saving "+mrcFile)
e = EMNumPy.numpy2em(gain64.astype('float32'))
e.write_image(mrcFile)
if __name__ == "__main__":
main()