1- import numpy as np
21import csv , time
2+ import numpy as np
3+ from scipy .interpolate import griddata
34
45
56def getStringRep (data ,nAmpl ,nAngl ):
@@ -52,30 +53,31 @@ class Footprint:
5253 #name,amplitudeCount,angleCount,<tunex;tuney>,<...;...>,...,angleCount,...
5354 #TODO make robust
5455 def __init__ (self ,stringRep ,dSigma = 1.0 ):
55- self ._dSigma = dSigma ;
56- self ._tunes = []; #tunes[nampl][nangl]['H'/'V']
57- self ._nampl = 0 ;
58- self ._maxnangl = 0 ;
59- self ._name = 'noName' ;
60- items = stringRep .strip ().split (',' );
61- self ._name = items [0 ];
62- self ._nampl = int (items [1 ]);
63- current = 2 ;
56+ self ._dSigma = dSigma
57+ self ._tunes = [] #tunes[nampl][nangl]['H'/'V']
58+ self ._nampl = 0
59+ self ._maxnangl = 0
60+ self ._name = 'noName'
61+ items = stringRep .strip ().split (',' )
62+ # print items
63+ self ._name = items [0 ]
64+ self ._nampl = int (items [1 ])
65+ current = 2
6466 for i in np .arange (self ._nampl ):
65- self ._tunes .append ([]);
66- nangl = int (items [current ]);
67+ self ._tunes .append ([])
68+ nangl = int (items [current ])
6769 if nangl > self ._maxnangl :
68- self ._maxnangl = nangl ;
69- current = current + 1 ;
70+ self ._maxnangl = nangl
71+ current = current + 1
7072 for j in np .arange (nangl ):
71- self ._tunes [i ].append ([]);
72- tunesString = items [current ].lstrip ('<' ).rstrip ('>' ).split (';' );
73- tunes = {};
73+ self ._tunes [i ].append ([])
74+ tunesString = items [current ].lstrip ('<' ).rstrip ('>' ).split (';' )
75+ tunes = {}
7476 #print tunesString
75- tunes ['H' ] = float (tunesString [0 ]);
76- tunes ['V' ] = float (tunesString [1 ]);
77- self ._tunes [i ][j ] = tunes ;
78- current = current + 1 ;
77+ tunes ['H' ] = float (tunesString [0 ])
78+ tunes ['V' ] = float (tunesString [1 ])
79+ self ._tunes [i ][j ] = tunes
80+ current = current + 1
7981
8082 def getName (self ):
8183 return self ._name ;
@@ -207,7 +209,7 @@ def drawPlottable(self):
207209 # self.draw(fig,lines)
208210 return lines ;
209211
210- def getIntermediateTunes (self ,ampl ,angl ):
212+ def getIntermediateTunes (self , ampl , angl ):
211213 if angl > np .pi / 2.0 :
212214 print 'ERROR angle ' ,angl ,' is larger than pi/2' ;
213215 ampl0 = int (ampl / self ._dSigma );
@@ -231,7 +233,7 @@ def getIntermediateTunes(self,ampl,angl):
231233 tuneY = (self .getVTune (ampl0 ,angl0 )* (1.0 - d1 )* (1.0 - d2 ) + self .getVTune (ampl0 + 1 ,angl0 )* d1 * (1.0 - d2 ) + self .getVTune (ampl0 ,angl0 + 1 )* d2 * (1.0 - d1 ) + self .getVTune (ampl0 + 1 ,angl0 + 1 )* d1 * d2 );
232234 return [tuneX ,tuneY ];
233235
234- def getTunesForAmpl (self , qx , qy ):
236+ def _getTunesForAmpl (self , qx , qy ):
235237
236238 ampl = np .sqrt (qx ** 2 + qy ** 2 )
237239
@@ -243,6 +245,112 @@ def getTunesForAmpl(self, qx, qy):
243245
244246 return self .getIntermediateTunes (ampl , angl )
245247
248+ def getTunesForAmpl (self , sx , sy ):
249+
250+ # Base as from MAD-X
251+ n_amp = self .getNbAmpl ()- 1
252+ n_ang = self .getNbAngl ()
253+ d_sigma = self ._dSigma
254+
255+ a = np .arange (0. , n_amp * d_sigma , d_sigma ) + d_sigma
256+ p = np .arange (0. , np .pi / 2 + np .pi / 2 / (n_ang - 1 ), np .pi / 2 / (n_ang - 1 )) #- np.pi/2/(n_ang-1)
257+ x = np .zeros (n_amp * n_ang )
258+ y = np .zeros (n_amp * n_ang )
259+
260+ i = 0
261+ small = 0.05
262+ big = np .sqrt (1. - small ** 2 )
263+ for k , n in enumerate (a ):
264+ for l , m in enumerate (p ):
265+ if l == 50 :
266+ x [i ] = n * small
267+ y [i ] = n * big
268+ elif l == 0 :
269+ x [i ] = n * big
270+ y [i ] = n * small
271+ else :
272+ x [i ] = n * np .cos (m )
273+ y [i ] = n * np .sin (m )
274+ # x[i] = n*np.cos(m)
275+ # y[i] = n*np.sin(m)
276+ i += 1
277+
278+ '''
279+ while (n <= nsigmax)
280+ {
281+ angle = 1.8*m*pi/180;
282+ if (m == 0) {xs=n*big; ys=n*small;}
283+ elseif (m == 50) {xs=n*small; ys=n*big;}
284+ else
285+ {
286+ xs=n*cos(angle);
287+ ys=n*sin(angle);
288+ }
289+ value,xs,ys;
290+ start,fx=xs,fy=ys;
291+ m=m+1;
292+ if (m == 51) { m=0; n=n+0.1;}
293+ };
294+ '''
295+
296+ '''
297+ a = np.arange(0., n_amp*d_sigma, d_sigma) + d_sigma
298+ p = np.arange(0., np.pi/2, np.pi/2/n_ang) - np.pi/2/n_ang
299+ aa, pp = np.meshgrid(a, p)
300+ aa, pp = aa.T, pp.T
301+ x = aa * np.cos(pp)
302+ y = aa * np.sin(pp)
303+
304+ small = a * 0.05
305+ big = a * np.sqrt(1-small**2)
306+ x[:,0] = big
307+ x[:,-1] = small
308+ y[:,0] = small
309+ y[:,-1] = big
310+
311+ x = x.flatten()
312+ y = y.flatten()
313+ '''
314+
315+ qx = np .array ([g ['H' ] for f in self ._tunes [1 :] for g in f ])
316+ qy = np .array ([g ['V' ] for f in self ._tunes [1 :] for g in f ])
317+
318+ # Make regular sampling over 6**2/2/3 = 6 sigma
319+ xi = sx ** 2 / 2 / 3
320+ yi = sy ** 2 / 2 / 3
321+ xi = sx
322+ yi = sy
323+
324+ points = np .array ([x , y ]).T
325+ pi = np .array ([xi , yi ]).T
326+
327+ qxi = griddata (points , qx , pi )
328+ qyi = griddata (points , qy , pi )
329+ qxi = griddata (points , qx , pi , fill_value = 0. )
330+ qyi = griddata (points , qy , pi , fill_value = 0. )
331+
332+ '''
333+ import matplotlib.pyplot as plt
334+
335+ plt.close(1)
336+ fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(20, 10))
337+
338+ ax1.scatter(x, y, c=qx, marker='x')
339+ ax1.scatter(xi, yi, c=qxi.T, s=40, lw=0)
340+
341+ # ax2.scatter(x, y, c=qx, marker='x')
342+ # ax2.scatter(xi, yi, c=qxi, s=40, lw=0)
343+
344+ # ax3.scatter(u, v, c=qx, lw=0)
345+ # ax3.set_xlim(-1e-3, 4e-3)
346+ # ax3.set_ylim(-1e-3, 4e-3)
347+
348+ plt.show()
349+ '''
350+ # print qxi
351+
352+ return qxi .T , qyi .T
353+
246354 #
247355 #WARNING use with extreme care, this meant for a very specific type of footprint
248356 #
0 commit comments