From e1badd963fb1e5e2140392807207a60406aeba06 Mon Sep 17 00:00:00 2001 From: arcan1s Date: Fri, 13 Jun 2014 16:26:24 +0400 Subject: [PATCH] add combine-qti --- combine-qti/combine-qti.py | 104 +++++++++++++++++++++++++++++++++++++ get_dipole/get-dipole.py | 12 ++--- 2 files changed, 110 insertions(+), 6 deletions(-) create mode 100755 combine-qti/combine-qti.py diff --git a/combine-qti/combine-qti.py b/combine-qti/combine-qti.py new file mode 100755 index 0000000..8765cfd --- /dev/null +++ b/combine-qti/combine-qti.py @@ -0,0 +1,104 @@ +#!/usr/bin/python2 +# combine-qti +# Copyright (C) 2014 Evgeniy Alekseev +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +import argparse, os + + +def readData(file): + """function to read data""" + windows = 0 + data = [] + # preread + with open (file, "r") as input: + for line in input: + if (line.split('\t')[0] == ""): + windows = int(line.split('\t')[1].strip()) + with open (file, "r") as input: + for window in range(windows): + for line in input: + if (line.strip() == ""): + data.append("
\n") + break + elif (line.strip() == ""): + data.append("\n") + break + for line in input: + if (line.strip() == '
'): + data.append("\n") + break + elif (line.strip() == ''): + data.append("\n") + break + data.append(line) + return windows, data + + +def printHeader(file, windowsNum=1): + """function to print header""" + iter = 0 + suffix = "" + while (os.path.exists(file + suffix)): + suffix = ".%i" % (iter) + iter = iter + 1 + if (suffix != ""): + os.rename(file, file + suffix) + with open(file, "w") as output: + output.write("QtiPlot 0.9.8 project file\n") + output.write("\tPython\n") + output.write("\t%i\n" % (windowsNum)) + + +def printData(file, data): + """function to print data""" + with open(file, "a") as output: + for line in data: + output.write(line) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description = 'Combine several *.qti files into one') + parser.add_argument('-d', '--directory', dest = 'dir', + help = 'Directory with input files or use --input', action = 'store', default = False) + parser.add_argument('-i', '--input', dest = 'input', + help = 'Input files, comma separated', action = 'store', default = False) + parser.add_argument('-o', '--output', dest = 'output', + help = 'Output file', action = 'store', default = False) + args = parser.parse_args() + + input = [] + if (args.dir): + if (os.path.isdir(args.dir)): + input = [file for file in os.listdir(args.dir) if (os.path.splitext(file)[1] == ".qti")] + else: + if (args.input): + for file in args.input.split(','): + if (os.path.isfile(file)): input.append(file) + output = args.output + if (not input) or (not output): + print ("Files are not set") + exit() + + totalWindows = 0 + totalData = [] + for file in input: + windows, data = readData(file) + totalWindows = totalWindows + windows + totalData.append(data) + printHeader(output, totalWindows) + for data in totalData: + printData(output, data) diff --git a/get_dipole/get-dipole.py b/get_dipole/get-dipole.py index 552a744..d93eca2 100755 --- a/get_dipole/get-dipole.py +++ b/get_dipole/get-dipole.py @@ -5,8 +5,8 @@ import argparse, math def get_aver_dipole(dipole): """function to get average dipole moment""" - aver_dipole = math.sqrt((dipole[0] * dipole[0] + - dipole[1] * dipole[1] + + aver_dipole = math.sqrt((dipole[0] * dipole[0] + + dipole[1] * dipole[1] + dipole[2] * dipole[2])) aver_dipole = round(aver_dipole, 4) return aver_dipole @@ -56,15 +56,15 @@ if __name__ == "__main__": parser.add_argument('-v', '--vector', dest = 'vector', help = 'Return vector', action = 'store_true', default = False) args = parser.parse_args() - + if ((not args.input) or (not args.coord)): print "Input files are not set" - exit - + exit() + charges = get_charges(args.input) coords = get_coordinates(args.coord) if (args.vector): print get_dipole(charges, coords) else: print get_aver_dipole(get_dipole(charges, coords)) - +