-
Notifications
You must be signed in to change notification settings - Fork 13
/
dgutils.py
227 lines (214 loc) · 8.07 KB
/
dgutils.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
import json
import numpy as np
from operator import itemgetter
from uncertainties import ufloat, umath
def combine(filelist, **kwargs):
datagramlist = []
for fn in filelist:
with open(fn, "r") as infile:
fndata = json.load(infile)
for datagram in fndata:
datagramlist.append(datagram)
return datagramlist
def reduce(datagramfile, **kwargs):
tasktype = kwargs.pop("parameters", {"type": "avg"})["type"]
assert tasktype in [
"avg",
"sum",
"diff",
], f'DGUTILS: reduce provided with an incorrent "type": {tasktype}.'
with open(datagramfile, "r") as infile:
origdg = json.load(infile)
if tasktype == "avg":
weights = [len(origdg) for i in range(len(origdg))]
elif tasktype == "sum":
weights = [1 for i in range(len(origdg))]
elif tasktype == "diff":
weights = [1] + [-1 for i in range(len(origdg) - 1)]
dgzero = np.array(origdg[0]["datastream"])
print(dgzero.shape)
flatresult = [0.0 for i in dgzero.flatten()]
for datagram in origdg:
dgi = np.array(datagram["datastream"])
assert (
dgi.shape == dgzero.shape
), f"DGUTILS: reduce provided with inconsistent datagram shapes."
weight = float(weights.pop(0))
for i in range(len(flatresult)):
flatresult[i] += dgi.flatten()[i] / weight
result = {}
result["datastream"] = np.array(flatresult).reshape(dgzero.shape).tolist()
result["metadata"] = {}
result["metadata"]["units"] = origdg[0]["metadata"]["units"]
result["metadata"]["version"] = kwargs.get("ver", {})
result["metadata"]["version"]["reduce"] = _VERSION
return result
def printdg(datagram, **kwargs):
print(len(datagram))
def pointdata(datagram, pars):
results = {
"params": {
"cavity": {
"r": pars["cavity"]["r"],
"h": pars["cavity"]["h"],
"Q": pars["cavity"]["Q"],
},
"sample": {
"r": pars["sample"]["r"],
"h": pars["sample"]["h"],
"rho": pars["sample"]["rho"],
"m": pars["sample"]["m"],
},
"mcpt": {
"A": pars["mcpt"]["A"],
"B": pars["mcpt"]["B"],
"C": pars["mcpt"]["C"],
"rp_nm": pars["mcpt"]["rp_nm"],
},
},
"metadata": {
"sample": {
"name": pars["sample"]["name"],
"id": pars["sample"]["id"],
"rep": pars["sample"]["rep"],
"date": pars["date"],
}
},
"data": [],
}
mcptdata = [i for i in datagram if i["input"]["datagram"] == "qftrace"]
gcdata = [i for i in datagram if i["input"]["datagram"] == "gctrace"]
expdata = [i for i in datagram if i["input"]["datagram"] == "meascsv"]
# MCPT parameters
Vc = (
np.pi
* ufloat(*results["params"]["cavity"]["r"]) ** 2
* ufloat(*results["params"]["cavity"]["h"])
)
Vs = (
np.pi
* ufloat(*results["params"]["sample"]["r"]) ** 2
* ufloat(*results["params"]["sample"]["h"])
)
delta = (
ufloat(*results["params"]["sample"]["m"])
/ ufloat(*results["params"]["sample"]["rho"])
) * (1 / Vs)
c_i = pars["cavity"]["i"]
s_i = pars["sample"]["i"]
A = pars["mcpt"]["A"]
B = pars["mcpt"]["B"]
C = pars["mcpt"]["C"]
timestamps = []
for section in mcptdata:
if pars["reference"] in section["input"]["export"]:
Q_c = ufloat(
np.average([p["Q0"][c_i] for p in section["results"][-10:-1]]),
np.std([p["Q0"][c_i] for p in section["results"][-10:-1]]),
)
for p in section["results"]:
timestamps.append(p["uts"])
t0 = min(timestamps)
tinf = max(timestamps) - t0
Qfac = ufloat(*results["params"]["cavity"]["Q"]["TM020"]) / Q_c
ffac = ufloat(1 / results["params"]["mcpt"]["rp_nm"], 0)
results["params"]["cavity"]["Qfac"] = [Qfac.n, Qfac.s, "-"]
results["params"]["cavity"]["ffac"] = [ffac.n, ffac.s, "-"]
# inlet flow composition
xin = []
for section in expdata:
xin = xin + [{"uts": p["uts"], "x": p["x"]} for p in section["results"]]
xin = sorted(xin, key=itemgetter("uts"))
# process one by one:
for section in datagram:
res = []
if section["input"]["datagram"] == "qftrace":
for p in section["results"]:
f0 = p["f0"][c_i] * ffac
Q0 = p["Q0"][c_i] * Qfac
fs = p["f0"][s_i] * ufloat(1, 0)
Qs = p["Q0"][s_i] * ufloat(1, 0)
r = {
"uts": p["uts"],
"f0": [f0.n, f0.s, "Hz"],
"fs": [fs.n, fs.s, "Hz"],
"Q0": [Q0.n, Q0.s, "-"],
"Qs": [Qs.n, Qs.s, "-"],
}
s = conductivity.Qf2σ(
Qs, Q0, fs, f0, Vs, Vc, A=A, B=B, C=C, delta=delta
)
for k, v in s.items():
r[k] = [v.n, v.s, "S/m" if k == "σ" else "-"]
res.append(r)
elif section["input"]["datagram"] == "meascsv":
for p in section["results"]:
r = {
"uts": p["uts"],
"T": [p["T"], 0.05, "°C"],
"v·": [p["flow"], 0.005, "ml/min"],
"xin": {},
}
for key in p["x"]:
if key not in r["xin"]:
r["xin"][key] = [100 * p["x"][key], 0, "%"]
tau = Vs / (ufloat(*r["v·"]) * (1e-6 / 60))
r["τ"] = [tau.n, tau.s, "s"]
ghsv = ufloat(*r["v·"]) * (1e-6 * 60) / Vs
r["GHSV"] = [ghsv.n, ghsv.s, "1/h"]
mvdot = (ufloat(*results["params"]["sample"]["m"]) * (1000)) / (
ufloat(*r["v·"]) / 60
)
r["m/v·"] = [mvdot.n, mvdot.s, "gs/ml"]
if "C3H8" in r["xin"]:
phi = (ufloat(*r["xin"]["C3H8"]) / ufloat(*r["xin"]["O2"])) / (
1 / 5
)
else:
phi = ufloat(0)
r["ϕ"] = [phi.n, phi.s, "-"]
res.append(r)
elif section["input"]["datagram"] == "gctrace":
for p in section["results"]:
r = {"uts": p["uts"], "xout": {}}
for key in [
"CO",
"methane",
"CO2",
"ethylene",
"ethane",
"propylene",
"propane",
"butane",
"acetic",
"acrylic",
"maleic",
]:
r["xout"][key] = [p["FID"].get(key, {}).get("x", 0), 0, "%"]
for key in ["O2", "N2"]:
r["xout"][key] = [p["TCD"].get(key, {}).get("x", 0), 0, "%"]
xC3H8 = 100 * np.interp(
p["uts"], [xp["uts"] for xp in xin], [xp["x"]["C3H8"] for xp in xin]
)
xO2 = 100 * np.interp(
p["uts"], [xp["uts"] for xp in xin], [xp["x"]["O2"] for xp in xin]
)
if xC3H8 > 1.0:
XS = conversion.p2XS(r["xout"], xC3H8, xO2, fuel="propane")
for k, v in XS.items():
if k.startswith("S"):
r[k] = {}
for kk, vv in v.items():
r[k][kk] = [vv.n, vv.s, "%"]
else:
r[k] = [v.n, v.s, "%"]
res.append(r)
metadata = section.get("metadata", {})
metadata["dgutils.pointdata"] = {
"version": _VERSION,
"date": dateutils.now(asstr=True),
}
results["data"].append(
{"input": section["input"], "metadata": metadata, "results": res}
)
return results