From e6fd431a149c3f7451a546657c9433e113b200ae Mon Sep 17 00:00:00 2001
From: Ryoichi Miyamoto <ryoichi.miyamoto@esss.se>
Date: Tue, 3 Jan 2017 16:48:21 +0100
Subject: [PATCH] "dic_cls" in LATTICE class updated according with the update
 of lib_tw_elem.py

---
 ess/lib_tw.py | 76 +++++++++++++++++++++++++--------------------------
 1 file changed, 37 insertions(+), 39 deletions(-)

diff --git a/ess/lib_tw.py b/ess/lib_tw.py
index b5b9486..4dac25c 100644
--- a/ess/lib_tw.py
+++ b/ess/lib_tw.py
@@ -7,9 +7,6 @@
         correction is established with new classes.
 '''
 
-from __future__ import print_function
-from __future__ import absolute_import
-
 #---- Lib
 
 from numpy        import *
@@ -21,9 +18,9 @@ from itertools import chain
 from subprocess import check_output
 from os         import system
 from os.path    import isdir,isfile
-import sys
+from sys        import exit
 
-from .lib_tw_elem import *
+from lib_tw_elem import *
 
 
 
@@ -52,30 +49,32 @@ class LATTICE:
         # Elem/comm class dict
         dic_cls={'DRIFT'               : DRIFT               ,
                  'QUAD'                : QUAD                ,
-                 'THIN_STEERING'       : THIN_STEERING       ,
+                 'THIN_STEERING'       : THIN_STEERING       ,                 
                  'GAP'                 : GAP                 ,
                  'DTL_CEL'             : DTL_CEL             ,
                  #
                  'BEND'                : BEND                ,
-                 'EDGE'                : EDGE                ,
+                 'EDGE'                : EDGE                ,                 
                  'APERTURE'            : APERTURE            ,
                  'DIAG_POSITION'       : DIAG_POSITION       ,
                  #
                  'STEERER'             : STEERER             ,
                  'CHOPPER'             : CHOPPER             ,
                  #
+                 'ADJUST'              : ADJUST              ,                 
                  'FREQ'                : FREQ                ,
                  'MARKER'              : MARKER              ,
-                 'ADJUST'              : ADJUST              ,
                  #
                  'ERROR_BEAM_STAT'     : ERROR_BEAM_STAT     ,
                  'ERROR_BEAM_DYN'      : ERROR_BEAM_DYN      ,
                  'ERROR_QUAD_NCPL_STAT': ERROR_QUAD_NCPL_STAT,
-                 'ERROR_QUAD_CPL_STAT' : ERROR_QUAD_CPL_STAT ,
+                 'ERROR_QUAD_CPL_STAT' : ERROR_QUAD_CPL_STAT , 
                  'ERROR_CAV_NCPL_STAT' : ERROR_CAV_NCPL_STAT ,
                  'ERROR_CAV_NCPL_DYN'  : ERROR_CAV_NCPL_DYN  ,
                  'ERROR_CAV_CPL_STAT'  : ERROR_CAV_CPL_STAT  ,
-                 'ERROR_CAV_CPL_DYN'   : ERROR_CAV_CPL_DYN   }
+                 'ERROR_CAV_CPL_DYN'   : ERROR_CAV_CPL_DYN   ,
+                 'ERROR_STAT_FILE'     : ERROR_STAT_FILE     ,
+                 'ERROR_DYN_FILE'      : ERROR_DYN_FILE      }
 
         # Field map dict
         dic_fmap={}
@@ -128,7 +127,7 @@ class LATTICE:
         found=False
         correctors=[]
         for element in self.lst:
-            if found:
+            if found: 
                 #WARNING I worry that there could be an inactive comment/element between the ADJUST and actual corrector
                 logging.debug("Found element {} for corrector family {}".format(element.typ,i))
                 correctors.append(element)
@@ -176,7 +175,7 @@ class LATTICE:
                 self.lst[i].s       =self.lst[i-1].s
                 self.lst[i].freq    =self.lst[i-1].freq
             self.lst[i].update_idx()
-
+        
         # Assign apt (using apt of the previous elem for diag elem)
         for i in range(len(self.lst)):
             try:
@@ -369,14 +368,14 @@ class PROJECT:
 class PARTRAN:
     '''
         Note:
-
+        
         - The list not complete. Add parameters as needed.
-
+        
         History:
-
+        
         - 2016.02.17: Changed how to identify the line of indices.
         - 2016.02.17: Added a logic to avoid #/0 for LEBT.
-
+                
     '''
     def __init__(self,file_name):
 
@@ -389,7 +388,7 @@ class PARTRAN:
         with open(file_name) as file:
             for lin in file.readlines():
                 lin=lin.split()
-                if '##' in lin[0]:
+                if '##' in lin[0]:                    
                     idx_s    =lin.index("z(m)"   ); idx_gamma=lin.index("gama-1" )
                     idx_x    =lin.index("x0"     ); idx_y    =lin.index("y0"     );                              idx_phs =lin.index("p0"   )
                     idx_sigx =lin.index("SizeX"  ); idx_sigy =lin.index("SizeY"  ); idx_sigz=lin.index("SizeZ"); idx_sigp=lin.index("SizeP")
@@ -420,9 +419,9 @@ class PARTRAN:
         for i in range(len(self.s)):
             if self.epsx[i]==0.0: self.epsx[i]=inf
             if self.epsy[i]==0.0: self.epsy[i]=inf
-            if self.epsz[i]==0.0: self.epsz[i]=inf
+            if self.epsz[i]==0.0: self.epsz[i]=inf                                
             if self.epsp[i]==0.0: self.epsp[i]=inf
-
+        
         # Additional instances
         self.gamma= data[idx_gamma]+1.0
         self.beta = sqrt(1.0-1.0/self.gamma**2)
@@ -439,10 +438,10 @@ class PARTRAN:
         # Set inf emitt back to 0
         for i in range(len(self.s)):
             if self.epsx[i]==inf: self.epsx[i]=0.0
-            if self.epsy[i]==inf: self.epsy[i]=0.0
+            if self.epsy[i]==inf: self.epsy[i]=0.0                            
             if self.epsz[i]==inf: self.epsz[i]=0.0
             if self.epsp[i]==inf: self.epsp[i]=0.0
-
+        
         # Convert to list (not necessary?)
         self.s    =self.s.tolist()    ; self.gamma=self.gamma.tolist(); self.beta=self.beta.tolist()
         self.x    =self.x.tolist()    ; self.y    =self.y.tolist()    ; self.z   =self.z.tolist()   ; self.phs =self.phs.tolist()
@@ -514,11 +513,11 @@ class DENSITY:
           Nelem       : loss_num_(..), loss_pow_(..)
 
           Nelem x Nstep: den
-
+          
         - History:
-
+ 
           * 2015.11.12: Baseline ver
-          * 2016.03.29: Adapted to ver 9 (apt includes shifts)
+          * 2016.03.29: Adapted to ver 9 (apt includes shifts) 
     '''
     def __init__(self,file_name):
 
@@ -551,7 +550,7 @@ class DENSITY:
                     Ibeam.append(fromfile(file,dtype=float32,count=1)[0])
                     s.append(    fromfile(file,dtype=float32,count=1)[0])
                     if ver>=9: apt.append(fromfile(file,dtype=float32,count=4))
-                    else     : apt.append(fromfile(file,dtype=float32,count=2))
+                    else     : apt.append(fromfile(file,dtype=float32,count=2))   
                     Nstep=fromfile(file,dtype=uint32,count=1)[0]
                     cent_ave.append(fromfile(file,dtype=float32,count=7))
                     cent_rms.append(fromfile(file,dtype=float32,count=7))
@@ -870,7 +869,7 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
                 L[i]=L_dt[dL.index(dL_min)]; Ndt+=1
         # Check
         if Ndt!=len(L_dt):
-            print('drift-tube # unmatched, in file: {}, matched: {}'.format(len(L_dt),Ndt))
+            print 'drift-tube # unmatched, in file: '+str(len(L_dt))+', matched: '+str(Ndt)
     except:
         pass
 
@@ -878,7 +877,7 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
     loss_den=loss[::]
     for i in range(len(loss_den)):
         if loss_den[i]!=0.0 and L[i]==0.0:
-            print('Caution: inf loss density at elem # {} !!!!'.format(i))
+            print 'Caution: inf loss density at elem # ',i,'!!!!'
             for k in range(i)[::-1]:
                 if L[k]!=0.0: loss_den[k]+=loss_den[i]; loss_den[i]=0.0; break
 
@@ -888,7 +887,7 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
 
     return loss_den
 
-    sys.exit()
+    exit()
 
     # Define L
     l=[s[i+1]-s[i] for i in range(len(s)-1)]; l.insert(0,0.0)
@@ -907,7 +906,7 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
                 l[i]=l_dt[dl.index(dl_min)]; Ndt+=1
         # Check
         if Ndt!=len(l_cell):
-            print('drift-tube # unmatched, in file: {}, matched: {}'.format(len(l_cell),Ndt))
+            print 'drift-tube # unmatched, in file: '+str(len(l_cell))+', matched: '+str(Ndt)
     except:
         pass
 
@@ -915,13 +914,12 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
     for i in range(len(l)):
         if l[i]==0.0 and loss[i]!=0.0:
             if i==1:
-                print('The 1st elem has 0 length and yet has a loss, exiting...')
-                sys.exit()
+                print 'The 1st elem has 0 length and yet has a loss, exiting...'; exit()
             else:
                 k=1
                 while l[i-k]==0.0: k+=1
                 loss[i-k]+=loss[i]; loss[i]=0.0
-                print('Caution: Inf loss density at elem # {} !!!!'.format(i))
+                print 'Caution: Inf loss density at elem #',i,'!!!!'
 
     # Finally, convert to loss density
     loss_den=[]
@@ -956,13 +954,13 @@ def loss_elem2den(s,loss,file_name_dt='',dlt_dt=5e-6):
 ##     # Check the replacement.
 ##     if Ndt!=len(l_cell):
 ##         print 'drift-tube # unmatched, in file: '+str(len(l_cell))+', from matching: '+str(Ndt)
-##         print 'review the threshold, exiting...'; sys.exit()
+##         print 'review the threshold, exiting...'; exit()
 
 ##     # Treat inf loss den.
 ##     for i in range(len(l)):
 ##         if l[i]==0.0 and loss[i]!=0.0:
 ##             if i==1:
-##                 print 'The 1st elem has 0 length and yet has a loss, exiting...'; sys.exit()
+##                 print 'The 1st elem has 0 length and yet has a loss, exiting...'; exit()
 ##             else:
 ##                 k=1
 ##                 while l[i-k]==0.0: k+=1
@@ -1365,23 +1363,23 @@ def _update_field_map_dict(dictionary,folder_path):
 ##         if path_cal[-1]!='/': path_cal+='/'
 
 ##         # Check the calc dir exists.
-##         if isdir(path_cal)==False: print 'Calc dir does not exist !!!! Exiting ...'; sys.exit()
+##         if isdir(path_cal)==False: print 'Calc dir does not exist !!!! Exiting ...'; exit()
 
 ##         # Check "tracelx64" is in the calc dir.
 ##         if isfile(path_cal+'tracelx64')==False:
 ##             try   : system('cp /opt/cea/tracelx64 '+path_cal)
-##             except: print 'tracelx64 not in calc dir !!!! Exiting ...'; sys.exit()
+##             except: print 'tracelx64 not in calc dir !!!! Exiting ...'; exit()
 
 ##         # Check "tracewin.key" is in the calc dir.
 ##         if isfile(path_cal+'tracewin.key')==False:
 ##             try   : system('cp /opt/cea/tracewin.key '+path_cal)
-##             except: print 'tracewin.key not in calc dir !!!! Exiting ...'; sys.exit()
+##             except: print 'tracewin.key not in calc dir !!!! Exiting ...'; exit()
 
 ##         # Check the project/lattice file is in the calc dir.
 ##         if '/' not in file_name_proj: file_name_proj=path_cal+file_name_proj
 ##         if '/' not in file_name_lat : file_name_lat =path_cal+file_name_lat
-##         if isfile(file_name_proj)==False: print 'project file not in calc dir !!!! Exiting ...'; sys.exit()
-##         if isfile(file_name_lat) ==False: print 'lattice file not in calc dir !!!! Exiting ...'; sys.exit()
+##         if isfile(file_name_proj)==False: print 'project file not in calc dir !!!! Exiting ...'; exit()
+##         if isfile(file_name_lat) ==False: print 'lattice file not in calc dir !!!! Exiting ...'; exit()
 
 ##         # Instances (Add more options as needed.)
 ##         self.path_cal      =path_cal
-- 
GitLab