writing column names is optional
This commit is contained in:
@ -12,23 +12,29 @@
|
|||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import sys,os
|
import sys,os,argparse
|
||||||
path = os.environ["LAMMPS_PYTHON_TOOLS"]
|
path = os.environ["LAMMPS_PYTHON_TOOLS"]
|
||||||
sys.path.append(path)
|
sys.path.append(path)
|
||||||
from log import log
|
from log import log
|
||||||
|
|
||||||
if len(sys.argv) < 3:
|
# set up arg parser
|
||||||
raise Exception("Syntax: log2txt.py log.lammps data.txt X Y ...")
|
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]
|
args = parser.parse_args()
|
||||||
datafile = sys.argv[2]
|
logfile = args.lammpslog
|
||||||
columns = sys.argv[3:]
|
datafile = args.outname
|
||||||
|
columns = args.cols
|
||||||
|
writenames = args.n
|
||||||
|
|
||||||
lg = log(logfile)
|
lg = log(logfile)
|
||||||
if columns == []:
|
if columns == []:
|
||||||
lg.write(datafile)
|
lg.write(datafile, writenames)
|
||||||
else:
|
else:
|
||||||
str = "lg.write(datafile,"
|
str = f"lg.write(datafile, {writenames},"
|
||||||
for word in columns: str += '"' + word + '",'
|
for word in columns: str += '"' + word + '",'
|
||||||
str = str[:-1] + ')'
|
str = str[:-1] + ')'
|
||||||
eval(str)
|
eval(str)
|
||||||
|
|||||||
@ -153,7 +153,7 @@ class log:
|
|||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
def write(self,filename,*keys):
|
def write(self,filename,writenames,*keys):
|
||||||
if len(keys):
|
if len(keys):
|
||||||
colmap = []
|
colmap = []
|
||||||
for key in keys:
|
for key in keys:
|
||||||
@ -175,6 +175,7 @@ class log:
|
|||||||
f = open(filename,"w")
|
f = open(filename,"w")
|
||||||
|
|
||||||
# write col names from dict in the right order
|
# write col names from dict in the right order
|
||||||
|
if writenames:
|
||||||
colnames = [k for j in colmap for k,v in self.ptr.items() if v == j]
|
colnames = [k for j in colmap for k,v in self.ptr.items() if v == j]
|
||||||
for j in range(len(colnames)):
|
for j in range(len(colnames)):
|
||||||
print(colnames[j], file=f, end=" ")
|
print(colnames[j], file=f, end=" ")
|
||||||
|
|||||||
Reference in New Issue
Block a user