罗宁的博客

Python脚本转换为php脚本

php脚本

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
<?php
/**
* 解析心电图
*
* 功能1:
* 功能2:
*
* @author luoning<lniftt@163.com>
*/
class JudgeEcg
{
public static function parse($path)
{
$result = ["start time(ms):"];
if (!is_file($path)) {
throw new Exception($path . " is not exists");
}
$medInfo = file_get_contents($path);
$medLength = strlen($medInfo);
$res = unpack("a3a1/aa2/a12a3/a8a4/aa5/a" . ($medLength - 25) . "last", $medInfo);
if ($res["a1"] != 'MED') {
throw new Exception($path . " is not a med file");
}
$ver = hexdec(bin2hex($res['a2']));
$res['a4'] = hexdec(bin2hex($res['a4']));
$result[] = $res['a4'];
$res['a5'] = hexdec(bin2hex($res['a5']));
if (!in_array($res['a5'], [2, 3])) {
throw new Exception("more than 2 kinds num, exit.");
}
$buff2 = $medLength - 25 - 6 * $res['a5'];
$zengya = unpack("a{$res['a5']}b1/a{$res['a5']}b2/a{$res['a5']}b3/a{$res['a5']}b4/a{$res['a5']}b5/a{$res['a5']}b6/a{$buff2}last", $res['last']);
$zengya['b1'] = bin2hex($zengya['b1']);
$zengya['b2'] = hexdec(bin2hex($zengya['b2']));
$zengya['b3'] = bin2hex($zengya['b3']);
$zengya['b4'] = bin2hex($zengya['b4']);
$zengya['b5'] = bin2hex($zengya['b5']);
$zengya['b6'] = bin2hex($zengya['b6']);
if (substr($zengya['b1'], 0, 2) != "00") {
throw new Exception("所处理数据并非单导联,暂不支持");
}
$result[] = "ecg type:";
$result[] = "1";
if (intval(hexdec(substr($zengya['b6'], 0, 2))) == 1) {
$result[] = "采样增益类型为:";
$result[] = "200";
} else {
$result[] = "未知增益类型";
}
if (intval(hexdec(substr($zengya['b5'], 0, 2))) == 3) {
$result[] = "满程电压为:";
$result[] = "3";
} else {
$result[] = "未知满程电压";
}
$vate = self::getHz(intval(hexdec(substr($zengya['b3'], 0, 2))));
if ($vate) {
$result[] = '频率为:';
$result[] = $vate;
} else {
$result[] = "未知采样频率.";
}
$result[] = "基准精度为:";
$result[] = pow(2, intval(hexdec(substr($zengya['b4'], 0, 2))));
$headSpace = self::judge($ver);
$buff3 = strlen($zengya['last']) - 8 - 1 - $headSpace;
$cInfo = unpack("a8c1/a1c2/a{$headSpace}c3/a{$buff3}last", $zengya['last']);
$cInfo['c1'] = bin2hex($cInfo['c1']);
$cInfo['c2'] = bin2hex($cInfo['c2']);
$cInfo['c3'] = bin2hex($cInfo['c3']);
$length = hexdec($cInfo['c3']);
//echo "预留长度为:" . $length . PHP_EOL;
$buff4 = strlen($cInfo['last']) - $length - 2 - 14400;
//format4 = "%ds2s14400s%ds"% (length, BUF4)
//c4, d2, d3, last = struct.unpack(format4, last)
//echo "a{$length}c4/a2d2/a14400d3/a{$buff4}last" . PHP_EOL;
$bInfo = unpack("a{$length}c4/a2d2/a14400d3/a{$buff4}last", $cInfo['last']);
$result[] = "data";
//print c4
//print "索引有效个数 %d "% d2
//''' 剩余last为数据区部分,绘图专用'''
$bInfo['last'] = bin2hex($bInfo['last']);
$temp = explode("ffff00", $bInfo['last']);
//echo $bInfo['last'] . PHP_EOL;
$string = "";
$stringEcg = "";
$emp = explode("ffff17", $temp[0]);
if (count($emp) > 1) {
$tmpEmp = array_values(array_slice($emp, 1));
$tmpLength = strlen($tmpEmp[0]) - 4;
$format9 = "a4c1/a{$tmpLength}c2";
if (count($tmpEmp) == 1) {
$stringEcg .= unpack($format9, $tmpEmp[0])['c2'] . "\n";
} elseif (count($tmpEmp) == 2) {
$stringEcg .= unpack($format9, $tmpEmp[0])['c2'] . "\n";
$stringEcg .= unpack($format9, $tmpEmp[1])['c2'] . "\n";
}
}
$tmpTemp = array_values(array_slice($temp, 1));
foreach ($tmpTemp as $k => $value) {
if (strpos($value, 'ffff17') !== false) {
$tmpValue = array_values(array_slice(explode("ffff17", $value), 1));
$tmpLength = strlen($tmpValue[0]) - 4;
$tmpFormat = "a4c1/a{$tmpLength}c2";
foreach ($tmpValue as $aInfo) {
$stringEcg .= unpack($tmpFormat, $aInfo)['c2'] . "\n";
}
} else {
$tmpLength = strlen($value) - 4;
$tmpFormat = "a4c1/a{$tmpLength}c2";
$string .= unpack($tmpFormat, $value)['c2'];
}
}
$result[] = $string;
$result[] = "string------------------";
return array_merge($result, self::doneEcg($stringEcg));
}
public static function doneEcg($string)
{
$strecg = '';
$strrr = '';
$string = array_values(array_slice(explode("\n", $string), 1));
foreach ($string as $k => $v) {
if (!$v) {
continue;
}
$tmpLength = strlen($v) - 26;
$tmpFormat = "a2c1/a8c2/a2c3/a2c4/a2c5/a2c6/a2c7/a2c8/a{$tmpLength}/c9";
$tmpData = unpack($tmpFormat, $v);
# r波位置 r波类型 心率 rr间期
$strecg .= $tmpData['c2'] . ',' . $tmpData['c4'] . ";";
$strrr .= intval(hexdec($tmpData['c8'])) . ",";
}
return ["ecganalyst_data", $strecg, "ecg_rr", $strrr];
}
public static function judge($ver)
{
return $ver == 0 ? 1 : 2;
}
public static function getHz($key)
{
$result = [100, 128, 200, 256, 500, 512];
return isset($result[$key]) ? $result[$key] : false;
}
}

python脚本

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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# encoding: utf-8
# 访问 http://tool.lu/pyc/ 查看更多信息
from __future__ import division
import os
import shutil
import time
import struct
import binascii
DIR_MED = '/tmp/med/'
def switch_hz(args):
result = {
0: 100,
1: 128,
2: 200,
3: 256,
4: 500,
5: 512 }
return result.get(args)
def walk_file(filepath, BUF, format1, users):
try:
fp = open(filepath, 'rb')
filedata = fp.read(BUF)
(a1, a2, a3, a4, a5, last) = struct.unpack(format1, filedata)
if a1 == 'MED':
pass
1
return 2
ver = int(binascii.b2a_hex(a2), 16)
a4 = int(binascii.b2a_hex(a4), 16)
print 'start time(ms):'
print a4
a5 = binascii.b2a_hex(a5)
n = int(a5, 16)
if n == 2:
pass
1
print 'more than 2 kinds num, exit.'
BUF2 = BUF - 25 - n * 6
format2 = '%ds%ds%ds%ds%ds%ds%ds' % (n, n, n, n, n, n, BUF2)
(b1, b2, b3, b4, b5, b6, last) = struct.unpack(format2, last)
b1 = binascii.b2a_hex(b1)
b3 = binascii.b2a_hex(b3)
b4 = binascii.b2a_hex(b4)
b5 = binascii.b2a_hex(b5)
b6 = binascii.b2a_hex(b6)
if b1[:2] == '00':
print 'ecg type:'
print '1'
else:
print '\xe6\x89\x80\xe5\xa4\x84\xe7\x90\x86\xe6\x95\xb0\xe6\x8d\xae\xe5\xb9\xb6\xe9\x9d\x9e\xe5\x8d\x95\xe5\xaf\xbc\xe8\x81\x94,\xe6\x9a\x82\xe4\xb8\x8d\xe6\x94\xaf\xe6\x8c\x81.'
return 9
if b1[:2] == '00'(b6[:2], 16) == 1:
print '\xe9\x87\x87\xe6\xa0\xb7\xe5\xa2\x9e\xe7\x9b\x8a\xe7\xb1\xbb\xe5\x9e\x8b\xe4\xb8\xba:'
print '200'
else:
print '\xe6\x9c\xaa\xe7\x9f\xa5\xe5\xa2\x9e\xe7\x9b\x8a\xe7\xb1\xbb\xe5\x9e\x8b.'
if int(b5[:2], 16) == 3:
print '\xe6\xbb\xa1\xe7\xa8\x8b\xe7\x94\xb5\xe5\x8e\x8b\xe4\xb8\xba\xef\xbc\x9a'
print '3'
else:
print '\xe6\x9c\xaa\xe7\x9f\xa5\xe6\xbb\xa1\xe7\xa8\x8b\xe7\x94\xb5\xe5\x8e\x8b'
try:
hertz = switch_hz(int(b3[:2], 16))
print '\xe9\xa2\x91\xe7\x8e\x87\xe4\xb8\xba:'
print hertz
except:
print '\xe6\x9c\xaa\xe7\x9f\xa5\xe9\x87\x87\xe6\xa0\xb7\xe9\xa2\x91\xe7\x8e\x87.'
return 9
print '\xe5\x9f\xba\xe7\xba\xbf\xe7\xb2\xbe\xe5\xba\xa6\xe4\xb8\xba:'
print pow(2, int(b4[:2], 16))
headspace = judge(ver)
BUF3 = len(last) - 8 - 1 - headspace
format3 = '8s1s%ds%ds' % (headspace, BUF3)
(c1, c2, c3, last) = struct.unpack(format3, last)
c3 = binascii.b2a_hex(c3)
length = int(c3, 16)
BUF4 = len(last) - length - 2 - 14400
format4 = '%ds2s14400s%ds' % (length, BUF4)
(c4, d2, d3, last) = struct.unpack(format4, last)
last = binascii.b2a_hex(last)
temp = last.split('ffff00')
string = ''
stringecg = ''
try:
emp = temp[0].split('ffff17')[1:]
format9 = '4s%ds' % (len(emp[0]) - 4)
if len(emp) == 1:
stringecg += struct.unpack(format9, emp[0])[1] + '\n'
elif len(emp) == 2:
stringecg += struct.unpack(format9, emp[0])[1] + '\n'
stringecg += struct.unpack(format9, emp[1])[1] + '\n'
except:
pass
for nn in temp[1:]:
if 'ffff17' in nn:
mum = nn.split('ffff17')
mumber = mum[0]
format5 = '4s%ds' % (len(mumber) - 4)
string += struct.unpack(format5, mumber)[1]
try:
if len(mum) == 2:
mumecg = mum[1]
format7 = '4s%ds' % (len(mumecg) - 4)
stringecg += struct.unpack(format7, mumecg)[1] + '\n'
elif len(mum) == 3:
mumecg1 = mum[1]
mumecg2 = mum[2]
format7 = '4s%ds' % (len(mumecg1) - 4)
stringecg += struct.unpack(format7, mumecg1)[1] + '\n'
stringecg += struct.unpack(format7, mumecg2)[1] + '\n'
elif len(mum) == 4:
mumecg1 = mum[1]
mumecg2 = mum[2]
mumecg3 = mum[3]
format7 = '4s%ds' % (len(mumecg1) - 4)
stringecg += struct.unpack(format7, mumecg1)[1] + '\n'
stringecg += struct.unpack(format7, mumecg2)[1] + '\n'
stringecg += struct.unpack(format7, mumecg3)[1] + '\n'
elif len(mum) == 5:
mumecg1 = mum[1]
mumecg2 = mum[2]
mumecg3 = mum[3]
mumecg4 = mum[4]
format7 = '4s%ds' % (len(mumecg1) - 4)
stringecg += struct.unpack(format7, mumecg1)[1] + '\n'
stringecg += struct.unpack(format7, mumecg2)[1] + '\n'
stringecg += struct.unpack(format7, mumecg3)[1] + '\n'
stringecg += struct.unpack(format7, mumecg4)[1] + '\n'
elif len(mum) == 6:
mumecg1 = mum[1]
mumecg2 = mum[2]
mumecg3 = mum[3]
mumecg4 = mum[4]
mumecg4 = mum[5]
format7 = '4s%ds' % (len(mumecg1) - 4)
stringecg += struct.unpack(format7, mumecg1)[1] + '\n'
stringecg += struct.unpack(format7, mumecg2)[1] + '\n'
stringecg += struct.unpack(format7, mumecg3)[1] + '\n'
stringecg += struct.unpack(format7, mumecg4)[1] + '\n'
stringecg += struct.unpack(format7, mumecg5)[1] + '\n'
else:
mumecg1 = mum[1]
mumecg2 = mum[2]
mumecg3 = mum[3]
mumecg4 = mum[4]
mumecg4 = mum[5]
mumecg4 = mum[6]
format7 = '4s%ds' % (len(mumecg1) - 4)
stringecg += struct.unpack(format7, mumecg1)[1] + '\n'
stringecg += struct.unpack(format7, mumecg2)[1] + '\n'
stringecg += struct.unpack(format7, mumecg3)[1] + '\n'
stringecg += struct.unpack(format7, mumecg4)[1] + '\n'
stringecg += struct.unpack(format7, mumecg5)[1] + '\n'
stringecg += struct.unpack(format7, mumecg6)[1] + '\n'
except Exception,e:
continue
format6 = '4s%ds' % (len(nn) - 4)
string += struct.unpack(format6, nn)[1]
print 'data'
print string
donecg(stringecg)
try:
del_med(filepath)
except:
print 'fail to del '
return filepath
except:
return 9
def donecg(string):
strecg = ''
strrr = ''
i = 0
for line in string.split('\n')[1:]:
if not line:
break
continue
format8 = '2s8s2s2s2s4s2s4s%ds' % (len(line) - 26)
ecganalyst = struct.unpack(format8, line)
strecg += ecganalyst[1] + ',' + ecganalyst[3] + ';'
strrr += '%d,' % int(ecganalyst[7], 16)
print 'ecganalyst_data'
print strecg
print 'ecg_rr'
print strrr
def dealwithecg(string):
filename = '/tmp/med/ecg.txt'
pro_file(filename, string)
def del_med(filepath):
try:
if os.path.exists(filepath):
try:
pathall = '/'.join(filepath.split('/')[:-1])
os.remove(filepath)
os.rmdir(pathall)
except Exception,e:
pass
else:
return 0
except:
print 'fail to del ' + filepath
return 9
def pro_name(a4, users):
try:
filename = '%d.txt' % a4
filepath = DIR_MED + users + '/' + filename
usermed = DIR_MED + users + '/'
try:
os.mkdir(DIR_MED)
except:
print 'dir exist'
if os.path.exists(usermed):
if os.path.exists(filepath):
os.remove(filepath)
return filepath
return filepath
os.path.exists(usermed)
os.mkdir(usermed)
return filepath
except:
return 9
def pro_file(filename, string):
try:
fp = file(filename, 'wb')
fp.write(string)
fp.close()
print 'finish'
print '\xe8\xa7\xa3\xe6\x9e\x90\xe6\x96\x87\xe4\xbb\xb6\xe7\x9a\x84\xe8\xb7\xaf\xe5\xbe\x84\xe4\xb8\xba\xef\xbc\x9a'
print filename
except:
return 9
def judge(ver):
if ver == 0:
return 1
return 2
def getsize(filepath, users):
try:
if os.path.exists(filepath):
pass
1
print filepath + ' is not exists!'
return 1
num = os.path.getsize(filepath)
BUF = num
format1 = '3s1s12s8s1s%ds' % (BUF - 25)
writepath = walk_file(filepath, BUF, format1, users)
return writepath
except:
print 'getsize fail.return 9.'
return 9