-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutputFile.py
128 lines (111 loc) · 3.79 KB
/
OutputFile.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
# rigolrdout - Read data and screenshots from Rigol oscilloscopes
# Copyright (C) 2012-2018 Johannes Bauer
#
# This file is part of rigolrdout.
#
# rigolrdout is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; this program is ONLY licensed under
# version 3 of the License, later versions are explicitly excluded.
#
# rigolrdout is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rigolrdout; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Johannes Bauer <[email protected]>
import os
import datetime
import json
from TMCDataTypes import TMCJSONEncoder
class OutputFile(object):
def __init__(self, include_serial = True):
self._creation = datetime.datetime.utcnow()
self._comment = None
self._connection = None
self._channel_info = None
self._acquisition_info = None
self._instrument = None
self._raw_data = { }
self._include_serial = include_serial
@property
def comment(self):
return self._comment
@comment.setter
def comment(self, value):
self._comment = value
@property
def connection(self):
return self._connection
@connection.setter
def connection(self, value):
self._connection = value
@property
def channel_info(self):
return self._channel_info
@channel_info.setter
def channel_info(self, value):
self._channel_info = value
@property
def acquisition_info(self):
return self._acquisition_info
@acquisition_info.setter
def acquisition_info(self, value):
self._acquisition_info = value
@property
def instrument(self):
return self._instrument
@instrument.setter
def instrument(self, value):
self._instrument = value
def add_raw_data(self, name, raw_data):
self._raw_data[name] = raw_data
def _metadata(self):
content = {
"created": self._creation.strftime("%Y-%m-%dT%H:%M:%SZ"),
"connection": self.connection,
}
if self.comment is not None:
content["comment"] = self.comment
if self.instrument is not None:
content["instrument"] = {
"vendor": self.instrument.vendor,
"device": self.instrument.device,
"fw_version": self.instrument.fw_version,
"instrument_parameters": {
"number_channels": self.instrument.instrument_parameters.number_channels,
},
}
if self._include_serial:
content["serial"] = self.instrument.serial
if self.channel_info is not None:
content["channel_info"] = self.channel_info
if self.acquisition_info is not None:
content["acquisition_info"] = self.acquisition_info
return content
def _write_json(self, filename):
content = self._metadata()
content["data"] = self._raw_data
with open(filename, "w") as f:
print(json.dumps(content, sort_keys = True, indent = 4, cls = TMCJSONEncoder), file = f)
def _write_files(self, filename):
content = self._metadata()
content["data"] = { }
for (name, raw_data) in self._raw_data.items():
raw_filename = filename + "_%s.%s" % (name, raw_data.file_format)
content["data"][name] = raw_data.to_repr(external_filename = os.path.basename(raw_filename))
with open(raw_filename, "wb") as f:
f.write(raw_data.data)
with open(filename + "_meta.json", "w") as f:
print(json.dumps(content, sort_keys = True, indent = 4, cls = TMCJSONEncoder), file = f)
def write(self, file_format, filename):
if file_format == "json":
return self._write_json(filename)
elif file_format == "files":
return self._write_files(filename)
else:
raise Exception("Unsupported file format: %s" % (file_format))