writing column names is optional

This commit is contained in:
ali
2022-03-10 15:23:21 -06:00
parent 96fbbc2b1b
commit 7343f6e5b8
2 changed files with 20 additions and 13 deletions

View File

@ -12,23 +12,29 @@
from __future__ import print_function
import sys,os
import sys,os,argparse
path = os.environ["LAMMPS_PYTHON_TOOLS"]
sys.path.append(path)
from log import log
if len(sys.argv) < 3:
raise Exception("Syntax: log2txt.py log.lammps data.txt X Y ...")
# set up arg parser
parser = argparse.ArgumentParser()
parser.add_argument('lammpslog', help='name of the lammps log file')
parser.add_argument('outname', help='name of the file to be written')
parser.add_argument('cols', nargs='*', help='any number of column names, optional')
parser.add_argument('-n', action='store_true', help='save column names as the header of the file')
logfile = sys.argv[1]
datafile = sys.argv[2]
columns = sys.argv[3:]
args = parser.parse_args()
logfile = args.lammpslog
datafile = args.outname
columns = args.cols
writenames = args.n
lg = log(logfile)
if columns == []:
lg.write(datafile)
lg.write(datafile, writenames)
else:
str = "lg.write(datafile,"
str = f"lg.write(datafile, {writenames},"
for word in columns: str += '"' + word + '",'
str = str[:-1] + ')'
eval(str)

View File

@ -73,7 +73,7 @@ class log:
self.data = []
# flist = list of all log file names
words = arglist[0].split()
self.flist = []
for word in words: self.flist += glob.glob(word)
@ -153,7 +153,7 @@ class log:
# --------------------------------------------------------------------
def write(self,filename,*keys):
def write(self,filename,writenames,*keys):
if len(keys):
colmap = []
for key in keys:
@ -175,10 +175,11 @@ class log:
f = open(filename,"w")
# write col names from dict in the right order
colnames = [k for j in colmap for k,v in self.ptr.items() if v == j]
for j in range(len(colnames)):
if writenames:
colnames = [k for j in colmap for k,v in self.ptr.items() if v == j]
for j in range(len(colnames)):
print(colnames[j], file=f, end=" ")
print("\n", file=f, end="")
print("\n", file=f, end="")
# write data
for i in range(self.nlen):