-
Notifications
You must be signed in to change notification settings - Fork 1
/
avgSamplePlanes.py
executable file
·177 lines (166 loc) · 6.69 KB
/
avgSamplePlanes.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
#
import numpy as np
import sys, os
import argparse
import gzip
def strdecode(v):
if sys.version_info[0] < 3:
return v.decode('utf-8')
else:
try:
return str(v, encoding='utf-8')
except:
return v
# See https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console
def progress(count, total, suffix=''):
"""
print out a progressbar
"""
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s %s\r' % (bar, percents, '%', suffix))
sys.stdout.flush()
def avgPlanes(filelist, coordfile='', getheaders=False, skiprows=2, progressbar=True):
"""
Average sample planes
"""
Nfiles=len(filelist)
for ip, planefile in enumerate(filelist):
if progressbar: progress(ip+1, Nfiles, suffix='[%i/%i]'%(ip+1,Nfiles))
if ip==0:
alldat = np.loadtxt(planefile, skiprows=skiprows)
else:
alldat += np.loadtxt(planefile, skiprows=skiprows)
if progressbar: print("")
avgdat=alldat/Nfiles
# Add the coordinates if necessary
if len(coordfile)>0:
cdat=np.loadtxt(coordfile, skiprows=skiprows)
returndat = np.hstack((cdat, avgdat))
else:
returndat = avgdat
# Get the headers if necessary
headers=[]
if getheaders:
fname, fext = os.path.splitext(filelist[0])
# Load the headers from the coordfile
if len(coordfile)>0:
if ((fext == '.gz') or (fext == '.GZ')):
with gzip.open(coordfile) as fp:
junk = fp.readline().strip().split()[1]
headers.extend(fp.readline().strip().split()[1:])
else:
with open(coordfile) as fp:
junk = fp.readline().strip().split()[1]
headers.extend(fp.readline().replace('#','').strip().split()[:])
# Else just use the one file headers
if ((fext == '.gz') or (fext == '.GZ')):
with gzip.open(filelist[0]) as fp:
timestring = fp.readline().strip().split()[1]
headers.extend(fp.readline().strip().split()[1:])
else:
with open(filelist[0]) as fp:
timestring = fp.readline().strip().split()[1]
headers.extend(fp.readline().replace('#','').strip().split()[:])
# return everything
return returndat, headers
def avgPlanesUU(filelist, avgdat, coordfile='',
getheaders=False, skiprows=2, progressbar=True):
"""
Average sample planes
"""
Nfiles=len(filelist)
for ip, planefile in enumerate(filelist):
if progressbar: progress(ip+1, Nfiles, suffix='[%i/%i]'%(ip+1,Nfiles))
u = np.loadtxt(planefile, skiprows=skiprows)
Ncols = u.shape[1]
up = u - avgdat[:,-Ncols:]
if ip==0:
alldat = up*up
else:
alldat += up*up
if progressbar: print("")
avgdat=alldat/Nfiles
# Add the coordinates if necessary
if len(coordfile)>0:
cdat=np.loadtxt(coordfile, skiprows=skiprows)
returndat = np.hstack((cdat, avgdat))
else:
returndat = avgdat
# Get the headers if necessary
headers=[]
if getheaders:
fname, fext = os.path.splitext(filelist[0])
# Load the headers from the coordfile
if len(coordfile)>0:
if ((fext == '.gz') or (fext == '.GZ')):
with gzip.open(coordfile) as fp:
junk = fp.readline().strip().split()[1]
headers.extend(fp.readline().strip().split()[1:])
else:
with open(coordfile) as fp:
junk = fp.readline().strip().split()[1]
headers.extend(fp.readline().strip().split()[1:])
# Else just use the one file headers
if ((fext == '.gz') or (fext == '.GZ')):
with gzip.open(filelist[0]) as fp:
timestring = fp.readline().strip().split()[1]
headers.extend(fp.readline().strip().split()[1:])
else:
with open(filelist[0]) as fp:
timestring = fp.readline().strip().split()[1]
headers.extend(fp.readline().replace('#','').strip().split()[:])
# with open(filename) as fp:
# timestring = fp.readline().strip().split()[1]
# headers.extend(fp.readline().strip().split()[1:])
# return everything
return returndat, headers
def saveavg(avgdat, headers, savefile, headerinfo=''):
strheaders = [strdecode(s) for s in headers]
# construct the headers
colheaders=' '.join(strheaders)
topheader='#Time: 0.0 AVERAGED PLANES '+headerinfo
np.savetxt(savefile, avgdat, header=topheader+'\n# '+colheaders, comments='')
return
def main():
# Handle arguments
parser = argparse.ArgumentParser(description='Average sample planes')
parser.add_argument('PLANEFILE', nargs='*', help="Plot these sample plane(s)")
parser.add_argument('--noprogressbar', action='store_true',
help="Do not display progress bar")
parser.set_defaults(noprogressbar=False)
parser.add_argument('--coordfile', default='', help="Specify coordinate file")
parser.add_argument('--savefile', default='', help="Save averaged plane to this file")
parser.add_argument('--headerinfo', default='', help="Add extra string to header")
parser.add_argument('--skiprows', help="Number of comment rows to skip in each datafile",
type=int, default=2)
parser.add_argument('--filetemplate', default='', help="Template for filenames")
parser.add_argument('--iters', help="Iterations to be used with --filetemplate argument",
type=int, nargs='+')
# Get the default and user arguments
args=parser.parse_args()
filelist = args.PLANEFILE
progressbar = (not args.noprogressbar)
coordfile = args.coordfile
savefile = args.savefile
skiprows = args.skiprows
headerinfo = args.headerinfo
filetemplate= args.filetemplate
iters = args.iters
# Construct the filelist
if (len(filetemplate)>0 and len(iters)>0):
for i in iters: filelist.append(filetemplate%i)
#print(filelist)
#print(iters)
# Average the data
avgdat, headers=avgPlanes(filelist, coordfile=coordfile, skiprows=skiprows, getheaders=True, progressbar=progressbar)
if len(savefile)>0:
print('Saving '+savefile)
saveavg(avgdat, headers, savefile, headerinfo=headerinfo)
return
if __name__ == "__main__":
# Call the main function
main()