This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
YASPmetaclassNEW.py
230 lines (168 loc) · 6.09 KB
/
YASPmetaclassNEW.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
#!/usr/bin/env python
import sys, gzip
try:
from numpy import *
except ImportError:
try:
from Numeric import *
from LinearAlgebra import *
except ImportError: sys.exit()
from string import split
from string import replace
def replac(nm): #-- added aug12_08 calaga
nm=replace(nm,"-","_")
nm=replace(nm,".","_")
return nm
#########################
class YASPtwiss:
#########################
"Read orbit data from YASP output in the same way as metaclass"
def __init__(self, filename=''):
self.HNAME=[]
self.HCO=[]
self.HCOerr=[]
self.Hindx={}
self.VNAME=[]
self.VCO=[]
self.VCOerr=[]
self.Vindx={}
self.HcorrNAME=[]
self.Hcorr=[]
self.VcorrNAME=[]
self.Vcorr=[]
if filename=='':
return
if '.gz' in filename:
f=gzip.open(filename, 'rb')
else:
f=open(filename, 'r')
#fspl=filename.split('/')[-1].split('.')
#self.exciter=fspl[1]+fspl[2]
#self.plane=fspl[3]
#if "minus" in fspl[4]:
# self.str=-1.0
#else:
# self.str=1.0
corr=0
count=0
for line in f:
cl=line.split()
if ("@ " in line) :
label=replac(cl[1])
try:
exec "self."+label+"= '"+str((cl[3]))+"'"
except:
print "Problem parsing:", line
#print "Ignored"
if "# CORRECTOR" in line:
corr=1
#if ("* " in line) :
# alllabels=split(line)
# print "alllabels",len(alllabels)
# for j in range(1,len(alllabels)):
# exec "self."+alllabels[j]+"= []"
if corr==0 and cl[1]=='H' and cl[8]=="OK":
self.HCO.append(float(cl[3])*1e-3)
self.HCOerr.append(0.0)
#self.Hindx[replace(cl[0], '"', '')]=len(self.HNAME)-1
#self.Hindx[replace(cl[0], '"', '').upper()]=len(self.HNAME)-1
self.HNAME.append(cl[0])
self.Hindx[self.HNAME[-1]]=len(self.HNAME)-1
self.Hindx[self.HNAME[-1].upper()]=len(self.HNAME)-1
if corr==0 and cl[1]=='V' and cl[8]=="OK":
self.VCO.append(float(cl[3])*1e-3)
self.VCOerr.append(0.0)
self.VNAME.append(cl[0])
self.Vindx[self.VNAME[-1]]=len(self.VNAME)-1
self.Vindx[self.VNAME[-1].upper()]=len(self.VNAME)-1
if corr==1 and cl[1]=='H':
self.Hcorr.append(cl[4])
self.HcorrNAME.append(cl[0])
self.Hindx[self.HcorrNAME[-1]]=len(self.HcorrNAME)-1
self.Hindx[self.HcorrNAME[-1].upper()]=len(self.HcorrNAME)-1
if corr==1 and cl[1]=='V':
self.Vcorr.append(cl[4])
self.VcorrNAME.append(cl[0])
self.Vindx[self.VcorrNAME[-1]]=len(self.VcorrNAME)-1
self.Vindx[self.VcorrNAME[-1].upper()]=len(self.VcorrNAME)-1
self.HCO=array(self.HCO)
self.VCO=array(self.VCO)
def minus(self, b):
c=YASPtwiss()
for bpm in self.VNAME:
aindx=self.Vindx[bpm]
try:
bindx=b.Vindx[bpm]
c.VNAME.append(bpm)
c.Vindx[bpm]=len(c.VNAME)-1
c.VCO.append( self.VCO[aindx] - b.VCO[bindx] )
c.VCOerr.append( self.VCOerr[aindx] + b.VCOerr[bindx])
except:
print "Doing YASP V minus:", bpm, "is not in all files"
for bpm in self.HNAME:
aindx=self.Hindx[bpm]
try:
bindx=b.Hindx[bpm]
c.HNAME.append(bpm)
c.Hindx[bpm]=len(c.HNAME)-1
c.HCO.append( self.HCO[aindx] - b.HCO[bindx] )
c.HCOerr.append( self.HCOerr[aindx] + b.HCOerr[bindx])
except:
print "Doing YASP H minus:", bpm, "is not in all files"
return c
def YASPwrite(self,ofile):
f=open(ofile+'x',"w")
for i in range(len(self.HNAME)):
print >>f , self.HNAME[i], self.HCO[i], self.HCOerr[i]
f.close()
f=open(ofile+'y',"w")
for i in range(len(self.VNAME)):
print >>f , self.VNAME[i], self.VCO[i], self.VCOerr[i]
f.close()
def YASPaverage(list):
"Average YASP classes"
if len(list)==0:
print "Nothing to average"
return
elif len(list)==1:
return list[0]
else:
a=YASPtwiss()
for bpm in list[0].VNAME:
average=0
rms=0
count=0
for files in list:
flag=1
try:
co=files.VCO[files.Vindx[bpm]]
average=average+co
rms=rms+co**2
count=count+1
except:
print "Doing V average:", bpm, "not found in all files"
flag=0
if flag==1 and count > 0:
a.VNAME.append(bpm)
a.Vindx[bpm]=len(a.VNAME)-1
a.VCO.append(average/count)
a.VCOerr.append( sqrt(rms/count - average**2/count**2 + 1e-18) )
count=0
rms=0
average=0
flag=1
for files in list:
try:
co=files.HCO[files.Hindx[bpm]]
average=average + co
rms=rms + co**2
count=count+1
except:
print "Doing H average:", bpm, "not found in all files"
flag=0
if flag==1 and count > 0:
a.HNAME.append(bpm)
a.Hindx[bpm]=len(a.HNAME)-1
a.HCO.append(average/count)
a.HCOerr.append( sqrt(rms/count - average**2/count**2 + 1e-18) )
return a