-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunisensCsv2Bin.m
144 lines (118 loc) · 5 KB
/
unisensCsv2Bin.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
function unisensCsv2Bin(path, new_path)
%UNISENSCSV2BIN convert unisens dataset with csv entries to dataset with bin entries
% Converts all unisens signal entries from csv format (*.csv) to bin format (*.bin).
% Event entries and values entries are not affected
%
% path: the path of the dataset to be converted, i.e. the unisens folder, that contains a unsiens.xml file
% new_path: the path of the unisensDataset with entries in bin format
%
% Copyright 2017 movisens GmbH
addUnisensJar();
if nargin==0 || nargin>2
error('unisensTools:missingArugments','Wrong number of Arguments.\nUsage:\nunisensCsv2Bin(''path_to_unisens_bin_dataset\'') \nunisensBin2Csv(''path_to_unisens_bin_dataset\'', ''path_to_new_unisens_csv_dataset\'')');
end
if nargin ==1
new_path = [path '_bin'];
end
%open unisens dataset
j_unisensFactory = org.unisens.UnisensFactoryBuilder.createFactory();
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 unisensCsv2Bin().']);
%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);
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)
%copy entry information
newId = strrep(char(j_entry.getId()),'csv','bin');
j_entry_new=j_unisens_new.createSignalEntry(newId, j_entry.getChannelNames, org.unisens.DataType.DOUBLE , j_entry.getSampleRate());
j_entry_new.setFileFormat(j_entry_new.createBinFileFormat());
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
data = j_entry.readScaled(position, count);
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