-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsweep_averager.py
256 lines (223 loc) · 7.6 KB
/
sweep_averager.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import sys, pandas, numpy, os, datetime, variablenames
from statistics import mode
"""
# Authoring, or whatever.
Tristan Anderson
tja1015@wildcats.unh.edu
Do you want to spend the rest of your life
selling sugared water or do you want a chance
to change the world?
Proceed Formally.
"""
# Program Explanation:
"""
Takes the .ta1 files of the first child directories,
and creates an average of them, writing it in a parent directory.
"""
def ta1parser(path,columns=[], delimeter='\t'):
#import datetime
#from statistics import mode
header = []
h2 = []
tf_file = []
with open(path, 'r') as f:
for index, line in enumerate(f):
##########################################
"""
This if-else block extracts data from
the data files that are given to the
gui. It reads and interprets the header
of the file, and will attempt to use
the data for later on.
"""
######### HEADER EXTRACTION #############
if index == 0:
dateish = line.split('\t')[1]
#2019-12-19 23:59:47
TE_DATE = datetime.datetime.strptime(dateish, "%Y-%m-%d %H:%M:%S")
#Time\t2019-12-19 22:01:07\tVapor Pressure Temperature (K)\t1.219363
vp = line.split('\t')[3] # Vapor pressure
try:
float(vp)
vptf = True
except:
vptf = False
if index == 1:
#Magnet Current (A)\t48.666000\tCCCS.T3 (K)\t2.860396
I = line.split('\t')[1]
# Get the primary thermistor
t3 = line.split('\t')[3]
try:
I = float(I)
itf = True
except ValueError:
itf = False
try:
float(t3)
t3tf = True
except ValueError:
t3tf = False
if t3tf and vptf:
T = round((float(vp)+float(t3))/2,4)
elif t3tf and not vptf:
T = round((float(t3)),4)
elif vptf and not t3tf:
T = round((float(vp)),4)
elif not t3tf and not vptf:
T = ""
if index == 2:
# Central Freq (MHz)\t212.990000\tFreq Span (MHz)\t0.400000
try:
cfq = float(line.split('\t')[1])
fqs = float(line.split('\t')[3])
except ValueError:
cfq = line.split('\t')[1]
fqs = line.split('\t')[3]
##########################################
header.append(len(line.split(delimeter)))
h2.append(line.split(delimeter))
data_width = mode(header)
for element in header:
if element == data_width:
tf_file.append(False)
else:
tf_file.append(True)
lines_to_skip = 0
while any(tf_file[lines_to_skip:]): # While any values are true, iterate through it, deleting the first element of the list.
lines_to_skip += 1
ta1f =[]
for index, element in enumerate(h2[lines_to_skip:]):
ta1f.append([])
for i in range(len(element)):
ta1f[index].append(float(element[i]))
return {"data":ta1f, "lines_to_skip":lines_to_skip, "Temperature":T, "Mag Current":I, "Central Freq (MHz)":cfq, "Freq Span (MHz)":fqs, "Time":dateish}
def ta1filewriter(f, y, x, T, I,cfq,fqspan, date="None", nmr_status="Baseline"):
f.write(variablenames.dmsa_time_colname+"\t"+date+'\t')
f.write(variablenames.dmsa_secondary_thermometer_colname+"\t"+str(T)+'\n')
f.write(variablenames.dmsa_magnet_psu_amperage_colname+"\t"+str(I)+'\t')
f.write(variablenames.dmsa_primary_thermometer_colname+"\t"+str(T)+'\n')
f.write(variablenames.dmsa_sweep_centroid_colname+"\t"+str(cfq)+'\t')
f.write(variablenames.dmsa_sweep_width_colname+"\t"+str(fqspan)+'\n')
f.write(variablenames.dmsa_system_status_colname+"\t"+str(nmr_status)+'\n')
f.write("#\tMHz\t"+variablenames.dmsa_terminal_colname+"\n")
for index, val in enumerate(y):
f.write(str(x[index])+"\t"+ str(val)+"\n")
def kc1(filesdir, dn='', dump='.', additive="TE"):
"""
Takes the .ta1 files within filesdir, and creates an average of them
"""
files_to_combine = []
# Given a file directory, look at the files within said directory
for file in os.listdir(filesdir):
if file.endswith('.ta1'): # If they're of the .ta1 file type, I've probably created them
files_to_combine.append(file)
if len(files_to_combine) == 0: # If theres no files in the directory, skip it
print("No files in", filesdir, "skipping...")
return False
whoops = {} # Take the kernel from the .ta1 file parser, and save it in a dictonary.
for path in files_to_combine:
#{"data":ta1f, "lines_to_skip":lines_to_skip, "Temperature":T, "Mag Current":I, "Central Freq (MHz)":cfq, "Freq Span (MHz)":fqs}
whoops[path.split('.')[0]] = ta1parser(filesdir+path)
# HERE is where the headers for the .ta1 columns can be changed. You can modify this without having to propogate these changes through
# the entire NMR suite.
header = ["MHz", "NMR Data"]
x = header[0]
y = header[1]
dfdict = {}
xl = []
yl = []
dates = []
avgt = 0
ac=0
icurent = 0
ic =0
cfq = 0
cc = 0
freqspan = 0
fc = 0
for key in whoops:
# Make a dataframe for the nmr sweeps
dfdict[key] = pandas.DataFrame(whoops[key]["data"], columns=header)
# The Try/Except tree here is a form of averaging across all of these files.
try:
# Add the temperature to itself
avgt += whoops[key]["Temperature"]
# Count that we've added something
ac += 1
except:
pass
try:
# Passthrough current.
# DANGER.
icurent = whoops[key]["Mag Current"]
except:
pass
try:
cfq += whoops[key]["Central Freq (MHz)"]
# Count that we've added something
cc += 1
except:
pass
try:
freqspan += whoops[key]["Freq Span (MHz)"]
# Count that we've added something
fc += 1
except:
pass
try:
dates.append(whoops[key]["Time"])
except:
pass
xl.append(dfdict[key][x].values)
#print(xl)
yl.append(dfdict[key][y].values)
try:
avgt, icurrent, cfq, freqspan = avgt/ac, icurent, cfq/cc, freqspan/fc
except ZeroDivisionError:
print("ZDE for: ", dn)
da = []
for dateish in dates:
# Gets the date
da.append(datetime.datetime.strptime(dateish, "%Y-%m-%d %H:%M:%S"))
# Casting errors can sometimes occur
# In casting means of 2D arrays into a 1D aray.
# This occurs when
# Files of the wrong structure (or of mis-matched sweep length)
# are implemented into a
# Series of datasets which are being averaged.
try:
xavg = numpy.mean(xl, axis=0)
yavg = numpy.mean(yl, axis=0)
except ValueError as e:
print("***ERROR:", e)
print("Will not write" ,dn+additive+"_average.ta1", "to file. Skipping.")
print("Ensure no files have mis-matched number of sweep datapoints.")
print("Checking file sizes may be a good way to start looking mis-matched #'s.")
return 0
os.chdir(dump)
with open(dn+additive+"_average.ta1", 'w') as f:
ta1filewriter(f, yavg, xavg, avgt, icurent, cfq, freqspan,date=(min(da)+(max(da)-min(da))/2).strftime("%Y-%m-%d %H:%M:%S"))
def avg_nested_dirs(filesdir, returndir='.'):
additive=''.join(filesdir.split('/')[-3]) # Is a string of the element between /^^/ <- there. Useful in making the user type less.
for (dirpath, dirnames, filenames) in os.walk(filesdir):
dirnames = dirnames
break
for dirname in dirnames:
kc1(filesdir+dirname+'/', dn=dirname+'_', dump=filesdir, additive=additive)
print("Avering nested directories complete.")
os.chdir(returndir)
def avg_single_dir(filesdir, returndir='.'):
additive=''.join(filesdir.split('/')[-3])
kc1(filesdir, dump=filesdir, additive=additive)
print("Avering single directory complete.")
os.chdir(returndir)
if __name__ == "__main__":
# Needs some work
bldirs = "sep_2020/data_record_9-12-2020/TE/912_514p/"
additive=''.join(bldirs.split('/')[-3])
kc1("sep_2020/data_record_9-14-2020/TE/914_641_650p_for_karl/", dump="sep_2020/data_record_9-14-2020/TE/914_641_650p_for_karl/", additive='TE_')
for (dirpath, dirnames, filenames) in os.walk(bldirs):
dirnames = dirnames
break
for dirname in dirnames:
kc1(bldirs+dirname+'/', dn=dirname+'_', dump=bldirs, additive=additive)