forked from mandalgrouptamu/SemiClassical-NAMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavg.py
51 lines (43 loc) · 1.26 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
#!/usr/bin/env python3
import sys
import numpy as np
from glob import glob
print("-"*50)
try:
fold = sys.argv[1]
except:
fold = "output"
try:
filename = sys.argv[2]
filenames = [filename]
except:
print ("Averaging all .txt files")
fnames = glob(f"{fold}/*.txt")
# get name from filenames: {name}-{number}.txt
# the name itself could include a dash
names = ["-".join(f.split("/")[-1].split("-")[:-1]) for f in fnames]
filenames = list(set(names))
print(f"Averaging files that have the name:")
for f in filenames:
print(f"{f}-*.txt")
for filename in filenames:
try:
outName = filename + ".txt"
fnames = glob(f"{fold}/{filename}-*.txt")
dat = 0
for i in fnames:
dat += np.loadtxt(i)
N = len(fnames)
if i.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]/N
np.savetxt(outName, dat)
else:
np.savetxt(outName, dat/N)
print ("Averaging done!")
except Exception as e:
print(f"Could not average {filename}-*.txt :")
print(e)