def loadLatticeFormat_dat(filename, lattice): try: f = open(filename, 'r') latticeString = f.read() except: print 'Bad datafile! From loadLatticeFormat_dat...' return 0 # Useful parameters from lattice beamdata = lattice.getBeamdata() m_0 = beamdata[2] beta = beamdata[0] gamma = gammaFromBeta(beta) q = beamdata[3] newLattice = Lattice("Loaded Lattice", beamdata, lattice.getTwiss(), lattice.getMultipart()) try: # Parsing for line in iter(latticeString.splitlines()): words = line.split() typeOfElem = words[0] if typeOfElem == "DRIFT": # Useful params L = float(words[1])/1000 # /1000 is for converting mm to m # Useless params R = float(words[2]) Ry = float(words[3]) # Params not stated that I need to construct the element name = "d" # Create the element newLattice.createDrift(name, L) elif typeOfElem == "QUAD": # Useful params L = float(words[1])/1000 G = float(words[2]) # They say G I say K, what is the difference? Ans: See TraceWin documentation page 102 my K is their -k # Indirect params Brho = m_0*constants.c*beta*gamma/q k = np.sqrt(float(abs(G/Brho))) # For some reason abs(...) is a sympy float and np.sqrt just can't handle that if q*G > 0: K = -k # focus in horiz (x) else: K = k # defocus in horiz (x) # Useless params R = float(words[2]) # Params not stated that I need construct the element name = "q" # Create the element newLattice.createQuadrupole(name, K, L) elif typeOfElem == "MULTIPOLE": if int(words[1]) == 3: # sextupole L = float(words[2])/1000 k = float(words[4]) order = 2 # for the lie transform name = "s" newLattice.createSextupolerel(name, k, L, order) elif typeOfElem == "ROTATION": nu_x = float(words[1]) nu_y = float(words[2]) name = "r" newLattice.createRotation(name, nu_x, nu_y) elif typeOfElem == "END": continue #if typeOfElem == "": # Useful params # Useless params # Params not stated that I need construct the element # Create the element return newLattice except: print "Parsing failed" + str(sys.exc_info()[-1].tb_lineno) return 0