-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunisensBin2Csv.m
161 lines (132 loc) · 5.69 KB
/
unisensBin2Csv.m
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
function unisensBin2Csv(path, keepSensorScaling, new_path)
%UNISENSBIN2CSV convert unisens dataset with bin entries to dataset with csv entries
% Converts all unisens signal entries in a unisens dataset from binary format (*.bin) to csv format (*.csv)
%
% path: the path of the dataset to be converted, i.e. the unisens folder, that contains a unsiens.xml file
% keepScaling: if set to true the original scaling of signal entries is kept. Set keepScaling to false if csv file will be used directly without unisens library or tools, e.g in Excel.
% new_path: the path of the unisensDataset with entries in csv format
%
% Copyright 2017 movisens GmbH
addUnisensJar();
if nargin==0 || nargin>3
error('unisensTools:missingArugments','Wrong number of Arguments.\nUsage:\nunisensBin2Csv(''path_to_unisens_bin_dataset\'') \nunisensBin2Csv(''path_to_unisens_bin_dataset\'', false, ''path_to_new_unisens_csv_dataset\'')');
end
if nargin ==1
keepSensorScaling=true;
new_path = [path '_csv'];
end
if nargin ==2
new_path = [path '_csv'];
end
%open unisens dataset
j_unisensFactory = javaMethod ('createFactory', 'org.unisens.UnisensFactoryBuilder');
j_unisens = j_unisensFactory.createUnisens(path);
%create new unisens dataset
j_unisens_new = j_unisensFactory.createUnisens(new_path);
%set comment
j_unisens_new.setComment([char(j_unisens.getComment()) ' Converted by unisensBin2Csv().']);
%copy custom attibutes
j_custom_attributes = j_unisens.getCustomAttributes();
j_key_iterator = j_custom_attributes.keySet().iterator();
while( j_key_iterator. hasNext() )
j_key = j_key_iterator.next();
j_unisens_new.addCustomAttribute(j_key,j_custom_attributes.get(j_key));
end
%copy context information
j_context = j_unisens.getContext();
if ~isempty(j_context)
j_unisens_new.createContext(j_context.getSchemaUrl());
copyfile([path filesep 'context.xml'],new_path);
end
%set measurement id
measurement_id = j_unisens.getMeasurementId();
if ~isempty(measurement_id)
j_unisens_new.setMeasurementId(j_unisens.getMeasurementId());
end
%set duration in [s]
j_unisens_new.setDuration(j_unisens.getDuration());
%set timestamp in [s]
j_timestamp_start = j_unisens.getTimestampStart();
if ~isempty(j_timestamp_start)
j_unisens_new.setTimestampStart(j_timestamp_start);
end
%loop over all timed entries (signal, values and event entries)
j_entries = j_unisens.getEntries();
nEntries = j_entries.size();
for i = 0:nEntries-1
j_entry = j_entries.get(i);
entry_class_name= j_entry.getClass.toString;
if (strcmp(entry_class_name, 'class org.unisens.ri.SignalEntryImpl')) || ...
(strcmp(entry_class_name, 'class org.unisens.ri.ValuesEntryImpl')) || ...
(strcmp(entry_class_name, 'class org.unisens.ri.EventEntryImpl'))
%here we go
if (strcmp(entry_class_name, 'class org.unisens.ri.SignalEntryImpl'))
%signalEntry
signal_entry_convert(j_entry, j_unisens_new, keepSensorScaling);
elseif (strcmp(entry_class_name, 'class org.unisens.ri.ValuesEntryImpl'))
%valuesEntry
values_entry_copy(j_entry, j_unisens_new);
elseif (strcmp(entry_class_name, 'class org.unisens.ri.EventEntryImpl'))
%eventEntry
event_entry_copy(j_entry, j_unisens_new);
end
elseif (strcmp(entry_class_name, 'class org.unisens.ri.CustomEntryImpl'))
%customEntry
disp('Crop not possible for custom Entries');
end
end
%copy groups
j_groups = j_unisens.getGroups();
nGroups = j_groups.size();
for i = 0:nGroups-1
j_group = j_groups.get(i);
j_group_cropped = j_unisens_new.createGroup(j_group.getId());
j_group_entries = j_group.getEntries();
nEntries = j_group.size();
for j = 0:nEntries
j_group_cropped.addEntry(j_unisens_new.getEntry(j_group_entries.get(j).getId()));
end
end
%unisens speichern
j_unisens_new.save();
j_unisens_new.closeAll();
j_unisens.closeAll();
end
function signal_entry_convert(j_entry, j_unisens_new, keepSensorScaling)
%copy entry information
newId = strrep(char(j_entry.getId()),'bin','csv');
if keepSensorScaling==true
j_entry_new=j_unisens_new.createSignalEntry(newId, j_entry.getChannelNames, j_entry.getDataType() , j_entry.getSampleRate());
j_entry_new.setLsbValue(j_entry.getLsbValue());
j_entry_new.setBaseline(j_entry.getBaseline());
else
j_entry_new=j_unisens_new.createSignalEntry(newId, j_entry.getChannelNames, javaMethod('valueOf','org.unisens.DataType','DOUBLE') , j_entry.getSampleRate());
end
j_entry_new.setFileFormat(j_entry_new.createCsvFileFormat());
j_entry_new.setComment(j_entry.getComment());
j_entry_new.setContentClass(j_entry.getContentClass());
j_entry_new.setUnit(j_entry.getUnit());
%copy data piecewise
position = 0;
total = j_entry.getCount();
while (position < total)
if (total - position > 1000000)
count = 1000000;
else
count = total - position;
end
if keepSensorScaling==true
data = j_entry.read(position, count);
else
data = j_entry.readScaled(position, count);
end
j_entry_new.append(data);
position = position + count;
end
end
function values_entry_copy(j_entry, j_unisens_new)
j_entry_new=j_unisens_new.addEntry(j_entry.clone(),true);
end
function event_entry_copy(j_entry, j_unisens_new)
j_entry_new=j_unisens_new.addEntry(j_entry.clone(),true);
end