#NEWLINE# def _read_coil_def_file( fname ) : #NEWLINE# #INDENT# 'Helper to read a coil def file' #NEWLINE# big_val = 0.5 #NEWLINE# coils = list ( ) #NEWLINE# with open ( fname , 'r' ) as fid : #NEWLINE# #INDENT# lines = fid . readlines ( ) #NEWLINE# #UNINDENT# lines = lines [ : : ( - 1 ) ] #NEWLINE# while ( len ( lines ) > 0 ) : #NEWLINE# #INDENT# line = lines . pop ( ) #NEWLINE# if ( line [ 0 ] != '#' ) : #NEWLINE# #INDENT# vals = np . fromstring ( coil , sep = ' ' ) #NEWLINE# assert ( len ( vals ) in ( 6 , 7 ) ) #NEWLINE# start = line . find ( '"' ) #NEWLINE# end = ( len ( line . strip ( ) ) - 1 ) #NEWLINE# assert ( line . strip ( ) [ end ] == '"' ) #NEWLINE# desc = line [ start : end ] #NEWLINE# npts = int ( vals [ 3 ] ) #NEWLINE# coil = dict ( coil_type = vals [ 1 ] , coil_class = vals [ 0 ] , desc = desc , accuracy = vals [ 2 ] , size = vals [ 4 ] , base = vals [ 5 ] ) #NEWLINE# rmag = list ( ) #NEWLINE# cosmag = list ( ) #NEWLINE# w = list ( ) #NEWLINE# for p in range ( npts ) : #NEWLINE# #INDENT# line = lines . pop ( ) #NEWLINE# while ( line [ 0 ] == '#' ) : #NEWLINE# #INDENT# line = lines . pop ( ) #NEWLINE# #UNINDENT# vals = np . fromstring ( line , sep = ' ' ) #NEWLINE# assert ( len ( vals ) == 7 ) #NEWLINE# w . append ( vals [ 0 ] ) #NEWLINE# rmag . append ( vals [ [ 1 , 2 , 3 ] ] ) #NEWLINE# cosmag . append ( vals [ [ 4 , 5 , 6 ] ] ) #NEWLINE# #UNINDENT# w = np . array ( w ) #NEWLINE# rmag = np . array ( rmag ) #NEWLINE# cosmag = np . array ( cosmag ) #NEWLINE# size = np . sqrt ( np . sum ( ( cosmag ** 2 ) , axis = 1 ) ) #NEWLINE# if np . any ( ( np . sqrt ( np . sum ( ( rmag ** 2 ) , axis = 1 ) ) > big_val ) ) : #NEWLINE# #INDENT# raise RuntimeError ( 'Unreasonable integration point' ) #NEWLINE# #UNINDENT# if np . any ( ( size <= 0 ) ) : #NEWLINE# #INDENT# raise RuntimeError ( 'Unreasonable normal' ) #NEWLINE# #UNINDENT# cosmag /= size [ : , np . newaxis ] #NEWLINE# coil . update ( dict ( w = w , cosmag = cosmag , rmag = rmag ) ) #NEWLINE# coils . append ( coil ) #NEWLINE# #UNINDENT# #UNINDENT# logger . info ( '%d coil definitions read' , len ( coils ) ) #NEWLINE# return coils