-
Notifications
You must be signed in to change notification settings - Fork 0
/
BestFitFinderWithAmbiguities.py
278 lines (234 loc) · 9.7 KB
/
BestFitFinderWithAmbiguities.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
import subprocess
import shutil
#variables
j =1
k =2
kc = 0
kcpatch = 0
frequencies =[7063.9800, 7822.1260, 7105.532, 7908.580, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, \
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
uncertainties = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, \
0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
j_low = 5
j_high = 15
k_low = 0
k_high = 7
numberOfLines = 4 #Could be len(frequencies) if array were populated
# dimension of array = (j_high - j_low) * (k_high - k_low) * 2 - (j_high - j_low) - for the instances when k = 0
numberOfFiles = (j_high - j_low) * (k_high - k_low) * 2 - (j_high - j_low)
maximumNumberOfFiles = 10 * numberOfFiles
jQuantumIncrements= [0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
kQuantumIncrements = [0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0]
jArray=[0 for i in range(maximumNumberOfFiles)]
kArray=[0 for i in range(maximumNumberOfFiles)]
kcPatchArray=[0 for i in range(maximumNumberOfFiles)]
rejectedArray =[ 0 for i in range(maximumNumberOfFiles)]
rmsValuesArray=[0.1 for i in range(maximumNumberOfFiles)]
rms2Array=[0.1 for i in range(maximumNumberOfFiles)]
iterationNumberArray=[0 for i in range(maximumNumberOfFiles)]
firstPlaceIndex = 0
secondPlaceIndex = 1
thirdPlaceIndex = 2
def openfile():
global frequencies
global j_low
global j_high
global k_low
global k_high
global jQuantumIncrements
global kQuantumIncrements
global uncertainties
global numberOfLines
global numberOfFiles
filename = askopenfilename(parent=root)
f = open(filename)
#f.read()
#f.seek(0,2)
#fsize = f.tell()
#f.seek(max(fsize-1024,0), 0)
lines = f.readlines()
endIsAt = len(lines)
for index in range (0,endIsAt):
if('number of frequencies') in lines[index]:
placesToSearch = lines[index].split()
endOfPlacesToSearch = len(placesToSearch)
numberOfLines = int(placesToSearch[endOfPlacesToSearch - 1])
for secondIndex in range (0, numberOfLines):
inputData = lines[index+1+secondIndex].split()
frequencies[secondIndex] = float(inputData[0])
jQuantumIncrements[secondIndex] = int(inputData[1])
kQuantumIncrements[secondIndex] = int(inputData[2])
index+= 1+secondIndex
if('uncertainty') in lines[index]:
placesToSearch = lines[index].split()
endOfPlacesToSearch = len(placesToSearch)
for uindex in range (0, numberOfLines):
uncertainties[uindex] = float(placesToSearch[endOfPlacesToSearch - 1])
if('j_low') in lines[index]:
placesToSearch = lines[index].split()
endOfPlacesToSearch = len(placesToSearch)
j_low = int(placesToSearch[endOfPlacesToSearch - 1])
if('j_high') in lines[index]:
placesToSearch = lines[index].split()
endOfPlacesToSearch = len(placesToSearch)
j_high = int(placesToSearch[endOfPlacesToSearch - 1])
if('k_high') in lines[index]:
placesToSearch = lines[index].split()
endOfPlacesToSearch = len(placesToSearch)
k_high = int(placesToSearch[endOfPlacesToSearch - 1])
if('k_low') in lines[index]:
placesToSearch = lines[index].split()
endOfPlacesToSearch = len(placesToSearch)
k_low = int(placesToSearch[endOfPlacesToSearch - 1])
newNumberOfFiles = (j_high - j_low) * (k_high - k_low) * 2 - (j_high - j_low)
if(newNumberOfFiles>maximumNumberOfFiles):
print('Too Many files required !! RE-dimension the arrays')
numberOfFiles = newNumberOfFiles
f.close()
processFile()
def openFitFile(fileIndex):
print('fileIndex='+str(fileIndex))
f1=open('test.fit')
lines = f1.readlines()
endIsAt = len(lines)
for index in range (0,endIsAt):
#line = f1.readline()
if( 'rejected') in lines[index]:
print('rejected*******************************************************************************')
rejectedArray[fileIndex] =1
for index in range (endIsAt-4,endIsAt):
#line = f1.readline()
if( 'RMS ERROR=') in lines[index]:
print(lines[index])
placesToSearch = lines[index].split()
endOfPlacesToSearch = len(placesToSearch)
rmsValuesArray[fileIndex] = float(placesToSearch[endOfPlacesToSearch - 1])
rms2Array[fileIndex] = float(placesToSearch[endOfPlacesToSearch - 2])
iterationNumberArray[fileIndex] = int(placesToSearch[endOfPlacesToSearch - 7])
print(str(rmsValuesArray[fileIndex]))
print(str(rms2Array[fileIndex]))
print(str(iterationNumberArray[fileIndex]))
f1.close()
def writeLinFile():
filename = 'test' + str(j)+'_'+str(k) + '_' +str(kcpatch)+ '.lin'
#filename = 'test.lin'
outf = open(filename, 'w')
for index in range (0,numberOfLines):
lineToWrite = repr(j+1 + jQuantumIncrements[index]).rjust(3)+ str(k + kQuantumIncrements[index]).rjust(3)+ \
str(kcpatch + j+1 + jQuantumIncrements[index] - (k + kQuantumIncrements[index]) ).rjust(3)+ \
str(j + jQuantumIncrements[index]).rjust(3) +str(k + kQuantumIncrements[index]).rjust(3) +\
str(kcpatch + j + jQuantumIncrements[index] -(k + kQuantumIncrements[index] )).rjust(3)+ \
repr(frequencies[index]).rjust(33) + repr(uncertainties[index]).rjust(11) + '\n'
# can also use spaces and literals, e.g. ' '+ str(kcpatch + j+1 + jQuantumIncrements[index] - (k + kQuantumIncrements[index]) ).rjust(3)+ ' '+ \
outf.write(lineToWrite)
outf.close()
def sort():
if (rmsValuesArray[0] > rmsValuesArray[1]):
if(rmsValuesArray[2] > rmsValuesArray[0]):
firstPlaceIndex = 2
secondPlaceIndex = 0
thirdPlaceIndex = 1
elif (rmsValuesArray[2] > rmsValuesArray[1]):
firstPlaceIndex = 0
secondPlaceIndex = 2
thirdPlaceIndex = 1
else:
firstPlaceIndex = 0
secondPlaceIndex = 1
thirdPlaceIndex = 2
else:
if(rmsValuesArray[2] > rmsValuesArray[1]):
firstPlaceIndex = 2
secondPlaceIndex = 1
thirdPlaceIndex = 0
elif (rmsValuesArray[2] > rmsValuesArray[0]):
firstPlaceIndex = 1
secondPlaceIndex = 2
thirdPlaceIndex = 0
else:
firstPlaceIndex = 1
secondPlaceIndex = 0
thirdPlaceIndex = 2
print('First = ' +str(firstPlaceIndex))
print('Second= ' +str(secondPlaceIndex))
print('Third = ' +str(thirdPlaceIndex))
print(str(numberOfFiles))
for index in range (3,numberOfFiles):
if(rmsValuesArray[index] <=0):
#do nothing in the case of = 0
print('ZERO')
elif(rms2Array[index]/rmsValuesArray[index] > 1.25):
print('Non matching rms and rms2')
elif (rms2Array[index]/rmsValuesArray[index] < .75):
print('Non matching rms and rms2')
elif (rejectedArray[index] == 1):
print('Rejected by Fitter')
elif (rmsValuesArray[index] < rmsValuesArray[firstPlaceIndex]):
thirdPlaceIndex = secondPlaceIndex
secondPlaceIndex = firstPlaceIndex
firstPlaceIndex = index
elif(rmsValuesArray[index] < rmsValuesArray[secondPlaceIndex]):
thirdPlaceIndex = secondPlaceIndex
secondPlaceIndex = index
elif(rmsValuesArray[index] < rmsValuesArray[thirdPlaceIndex]):
thirdPlaceIndex = index
#print('First = ' +str(firstPlaceIndex))
#print('Second= ' +str(secondPlaceIndex))
#print('Third = ' +str(thirdPlaceIndex))
#print('index'+str(index))
print('First (index)= ' +str(firstPlaceIndex))
printParams(firstPlaceIndex)
print('Second= ' +str(secondPlaceIndex))
printParams(secondPlaceIndex)
print('Third = ' +str(thirdPlaceIndex))
printParams(thirdPlaceIndex)
def printParams(localIndex):
print("Local Index ="+str(localIndex) )
print('J=' +str(jArray[localIndex]))
print('K=' +str(kArray[localIndex]))
print('Kcpatch=' +str(kcPatchArray[localIndex]))
print('RMS = ' +str(rmsValuesArray[localIndex]))
print('rms2 = ' +str(rms2Array[localIndex]))
print('iterations =' +str(iterationNumberArray[localIndex]))
def processFile():
global j
global k
global jArray
global kArray
global kcPatchArray
global kcpatch
files_written = 0
for j in range (j_low, j_high):
for k in range (k_low, k_high):
if(k !=0):
loopLimit=2
else:
loopLimit = 1
for kcpatch in range (0,loopLimit):
writeLinFile()
jArray[files_written] = j
kArray[files_written] = k
kcPatchArray[files_written] = kcpatch
shutil.copy('test.parameters', 'test.par') # copy the parameters file to test.par
subprocess.call(["spfit", 'test']) # run spfit
print('index='+str(j)+','+str(k)+','+str(kcpatch))
openFitFile(files_written) # open the test.fit file and return the rms value
subprocess.call(["rm", 'test.par']) # delete the text.xyz files
subprocess.call(["rm", 'test.bak']) # run spfit
subprocess.call(["rm", 'test.var']) # run spfit
files_written +=1
print (str(files_written) + ' Files Written')
sort()
root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=openfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
# processFile()
root.mainloop()