forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeCalFiles.py
155 lines (132 loc) · 6.27 KB
/
MergeCalFiles.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
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
#pylint: disable=no-init
from mantid.api import *
from mantid.kernel import *
class MergeCalFiles(PythonAlgorithm):
def category(self):
return "DataHandling\\Text;Diffraction\\DataHandling\\CalFiles"
def seeAlso(self):
return [ "ReadGroupsFromFile","CreateDummyCalFile","CreateCalFileByNames",
"AlignDetectors","DiffractionFocussing","LoadCalFile","SaveCalFile" ]
def name(self):
return "MergeCalFiles"
def summary(self):
return "Combines the data from two Cal Files."
def PyInit(self):
self.declareProperty(FileProperty("UpdateFile","", FileAction.Load, ['cal']),
doc="The cal file containing the updates to merge into another file.")
self.declareProperty(FileProperty("MasterFile","", FileAction.Load, ['cal']),
doc="The master file to be altered, the file must be sorted by UDET")
self.declareProperty(FileProperty("OutputFile","", FileAction.Save, ['cal']),
doc="The file to contain the results")
self.declareProperty("MergeOffsets", False, doc="If True, the offsets from file1 will be merged "
+ "to the master file. Default: False")
self.declareProperty("MergeSelections", False, doc="If True, the selections from file1 will be merged "
+ "to the master file. Default: False")
self.declareProperty("MergeGroups", False, doc="If True, the Groups from file1 will be merged to "
+ "the master file. Default: False")
#pylint: disable=too-many-branches
def PyExec(self):
#extract settings
mergeOffsets = self.getProperty("MergeOffsets").value
mergeSelections = self.getProperty("MergeSelections").value
mergeGroups = self.getProperty("MergeGroups").value
updateFileName = self.getPropertyValue("UpdateFile")
masterFileName = self.getPropertyValue("MasterFile")
outputFileName = self.getPropertyValue("OutputFile")
if masterFileName == outputFileName :
raise RuntimeError('The output file must be different to the master file.')
self.DisplayMessage(mergeOffsets,mergeSelections,mergeGroups,updateFileName,masterFileName)
updateFile = open(updateFileName,"r")
updateDict=dict()
lastNumber = 0
linesUpdated = 0
linesUntouched = 0
linesAdded = 0
for line in updateFile:
if not self.IsComment(line):
#process line
try:
(number,UDET,offset,select,group) = self.ProcessLine(line)
except ValueError:
pass
#remeber all of the values
updateDict[UDET] = (offset,select,group)
updateFile.close()
self.log().information(str(len(updateDict)) + " updates found in " + updateFileName)
masterFile = open(masterFileName,"r")
outputFile = open(outputFileName,"w")
for line in masterFile:
if self.IsComment(line):
#copy the comment over
outputFile.write(line)
else:
#process line
try:
(number,UDET,masterOffset,masterSelect,masterGroup) = self.ProcessLine(line)
lastNumber = number
#If line to be updated
if UDET in updateDict:
(offset,select,group)=updateDict.pop(UDET)
linesUpdated += 1
if mergeOffsets:
masterOffset = offset
if mergeSelections:
masterSelect = select
if mergeGroups:
masterGroup = group
else:
linesUntouched += 1
outputFile.write(self.FormatLine(number,UDET,masterOffset,masterSelect,masterGroup))
except ValueError:
#invalid line - ignore it
# linesInvalid += 1
pass
#add any lines at the end
for UDET in updateDict.keys():
(offset,select,group)=updateDict[UDET]
lastNumber += 1
outputFile.write(self.FormatLine(lastNumber,UDET,offset,select,group))
linesAdded += 1
self.log().information("{0} lines Updated, {1} lines added, {2} lines untouched".format(linesUpdated,linesAdded,linesUntouched))
#close the files
masterFile.close()
outputFile.close()
#pylint: disable=too-many-arguments
def DisplayMessage(self,mergeOffsets,mergeSelections,mergeGroups,fileName1,fileName2):
#Log the settings string
outputString = "Merging "
if mergeOffsets:
outputString+= "offsets, "
if mergeSelections:
outputString+= "selections, "
if mergeGroups:
outputString+= "groups, "
#strip the final comma
outputString = outputString [0:len(outputString)-2]
outputString += " from file " + fileName1 + " into " + fileName2
self.log().information(outputString)
def IsComment(self,line):
return line.startswith("#")
def ProcessLine(self,line):
try:
elements = line.split()
number =int(elements[0])
UDET =int(elements[1])
offset =float(elements[2])
select =int(elements[3])
group =int(elements[4])
except:
raise ValueError("invalid line: " + line)
return (number,UDET,offset,select,group)
#pylint: disable=too-many-arguments
def FormatLine(self,number,UDET,offset,select,group):
line = "{0:9d}{1:16d}{2:16.7f}{3:9d}{4:9d}\n".format(number,UDET,offset,select,group)
return line
#############################################################################################
AlgorithmFactory.subscribe(MergeCalFiles())