def _check_to_int(self, x, prop): """check that an incoming value can be converted to an integer and returne the int version of it, or raise an error""" if(not fields_funks._is_number(x)): raise(EMFError("""Conductor property '%s' must be numeric. It cannot be set to: %s""" % (prop, repr(x)))) elif(int(x) != float(x)): raise(EMFError("""Conductor property '%s' must be an integer. It cannot be set to: %s""" % (prop, repr(x)))) return(int(x))
def _write_FLD_entries(ofile, *entries): """format entries and writes them into a .FLD file targeted by ofile""" for entry in entries: if (fields_funks._is_number(entry)): if (_is_int(entry)): w = '{:< d} \n'.format(int(entry)) else: w = '{:< .2f} \n'.format(float(entry)) if ('.' in w): idx = w.index('.') if ('0' in w[:idx]): idx = w.index('0') if (not fields_funks._is_number(w[idx - 1])): w = w[:idx] + w[idx + 1:] else: w = str(entry) + '\n' ofile.write(w)
def read_DAT(file_path): """Read a DAT file, which can have some funky extra characters if the numbers are too large (percent signs) args: file_path - string, path to DAT file returns: pandas DataFrame with 'Bx','By','Bprod','Bmax','Ex','Ey','Eprod', and 'Emax' columns, and the distance ('x') in the index""" #check that the target file is a DAT fields_funks._check_extension( file_path, 'DAT', """Input file must have a '.DAT' extension.""") #load data und_message = 'Electric Field cannot be computed for underground circuit' und_only = False with open(file_path, 'r') as ifile: #read through the header line = ifile.readline() while (not ('----' in line)): line = ifile.readline() if (und_message in line): und_only = True #get the data data = [[] for i in range(9)] line = ifile.readline() while (line): if (line): #check strange % signs printed with large numbers if (line[0][0] == '%'): line += ifile.readline() l = [i.replace('%', '') for i in line.split()] if (l and all([fields_funks._is_number(i) for i in l])): for i in range(5): data[i].append(float(l[i])) if (not (und_only)): for i in range(5, 9): data[i].append(float(l[i])) else: for i in range(5, 9): data[i].append(0.) line = ifile.readline() return (pd.DataFrame(data=dict( zip(['Bx', 'By', 'Bprod', 'Bmax', 'Ex', 'Ey', 'Eprod', 'Emax'], data[1:])), index=data[0]))
def _check_to_float(self, value, prop): """check that an incoming value can be converted to a float and return the float version of it, or raise an error""" if(not fields_funks._is_number(value)): raise(EMFError("""Conductor property '%s' must be numeric. It cannot be set to: %s""" % (prop, repr(value)))) return(float(value))