forked from mandalgrouptamu/SemiClassical-NAMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavg.py
56 lines (48 loc) · 1.54 KB
/
avg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
import sys
import numpy as np
from glob import glob
print("-"*50)
def getInput(input,key):
txt = [i for i in input if i.find(key)!=-1][0].split("=")[1].split("#", 1)[0].replace("\n","")
return txt.replace(" ","")
try:
input = open(sys.argv[1], 'r').readlines()
print(f"Reading {sys.argv[1]} for average")
except:
print("Reading input.txt for average")
input = open('input.txt', 'r').readlines()
try:
filename = sys.argv[2]
filenames = [filename]
except:
print ("Averaging all .txt files")
filenames = [i.replace("RUN/run-0/","") for i in glob("RUN/run-0/*.txt")]
print (filenames)
try:
fold = int(sys.argv[3])
except:
print (f"Detected {len(glob('RUN/run-*'))} folders")
fold = len(glob("RUN/run-*"))
if fold == 0:
print("No outputs found!")
for filename in filenames:
try:
outName = filename
dirs = [f"RUN/run-{i}" for i in range(fold)]
dat = np.loadtxt(dirs[0]+"/"+filename)
for i in range(1, fold):
dat += np.loadtxt(dirs[i]+"/"+filename)
method = getInput(input,"Method")
if method.find('sqc')!= -1:
norm = np.sum(dat[:,1:], axis=1)
for t in range(len(dat[:,0])):
norm = np.sum(dat[t,1:])
dat[t,1:] = dat[t,1:]/norm
dat[t,0] = dat[t,0]/fold
np.savetxt(outName, dat)
else:
np.savetxt(outName, dat/fold)
except:
pass
print ("Gathered all data, feel free to remove the folder 'RUN'")