-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMatlabFileArrayWriter.cs
More file actions
282 lines (236 loc) · 10.4 KB
/
MatlabFileArrayWriter.cs
File metadata and controls
282 lines (236 loc) · 10.4 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace MatlabFileIO
{
internal interface IMatlabFileWriterLocker
{
bool HasFinished();
}
public class MatLabFileArrayWriter : IMatlabFileWriterLocker
{
private BinaryWriter writeStream;
bool hasFinished;
//vars required to writeback size and dimensions at matrix finalisation
private long totalLengthStartPosition;
private long dataLengthStartPosition;
private long dimensionsStartPosition;
int[] dimensions = new int[2] { 0, 0 };
int numberOfDimensions { get { return dimensions.Length; } }
private long dataLength = 0;
private long headerLength = 0;
private int totalPaddingAdded = 0;
public MatLabFileArrayWriter(Type t, string varName, BinaryWriter writeStream)
{
if(t == typeof(Int64) || t == typeof(UInt64))
throw new Exception("64 bit integer arrays are not supported by matlab file format");
this.writeStream = writeStream;
this.hasFinished = false;
long beginPosition = writeStream.BaseStream.Position;
//array header
WriteArrayTag();
//flags
WriteFlags(t);
//dimensions
WriteDimensionsPlaceholder();
// array name
WriteName(varName);
//keep track of how many bytes were already consumed for this header
headerLength = writeStream.BaseStream.Position - beginPosition;
//data header
WriteDataHeader(t);
//reset dataLength, as this might have been affected by padding
dataLength = 0;
totalPaddingAdded = 0;
}
private void WriteDataHeader(Type t)
{
//type of contents
writeStream.Write(MatfileHelper.MatlabArrayTypeNumber(t));
//store position, so we can later overwrite this placeholder
dataLengthStartPosition = writeStream.BaseStream.Position;
//add placeholder for size
for (int i = 0; i < 4; i++)
writeStream.Write((byte)0xcc);
}
public void AddRow(object dataToAppend)
{
Array data = dataToAppend as Array;
if(data == null)
{
data = Array.CreateInstance(dataToAppend.GetType(), 1);
data.SetValue(dataToAppend, 0);
}
//store this dimension size, and check if it is the same as any previous data stored
if (dimensions[1] == 0) //first data
dimensions[1] = data.Length;
else //not first data
if (dimensions[1] != data.Length) //different size!
throw new Exception("Data to be appended has a different size than previously appended data!");
//dump data in stream
int size = 0;
if (data.GetType().Equals(typeof(byte[])))
{
byte[] castedDataByte = data as byte[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataByte[i]);
size = sizeof(byte);
}
else if (data.GetType().Equals(typeof(double[]))) {
double[] castedDataDouble = data as double[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataDouble[i]);
size = sizeof(double);
}
else if (data.GetType().Equals(typeof(float[])))
{
float[] castedDataSingle = data as float[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataSingle[i]);
size = sizeof(float);
}
else if (data.GetType().Equals(typeof(Int16[])))
{
Int16[] castedDataI16 = data as Int16[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataI16[i]);
size = sizeof(Int16);
}
else if (data.GetType().Equals(typeof(UInt16[])))
{
UInt16[] castedDataUI16 = data as UInt16[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataUI16[i]);
size = sizeof(UInt16);
}
else if (data.GetType().Equals(typeof(Int32[])))
{
Int32[] castedDataI32 = data as Int32[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataI32[i]);
size = sizeof(Int32);
}
else if (data.GetType().Equals(typeof(UInt32[])))
{
UInt32[] castedDataUI32 = data as UInt32[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataUI32[i]);
size = sizeof(UInt32);
}
else if (data.GetType().Equals(typeof(Int64[])))
{
Int64[] castedDataI64 = data as Int64[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataI64[i]);
size = sizeof(Int64);
}
else if (data.GetType().Equals(typeof(UInt64[])))
{
UInt64[] castedDataUI64 = data as UInt64[];
for (int i = 0; i < data.Length; i++)
writeStream.Write(castedDataUI64[i]);
size = sizeof(UInt64);
}
else if (data.GetType().Equals(typeof(sbyte[]))) {
sbyte[] castedDataChar = data as sbyte[];
for (int i = 0; i < data.Length; i++)
{
writeStream.Write(castedDataChar[i]);
}
size = sizeof(sbyte);
}
else if (data.GetType().Equals(typeof(char[]))) //Char is internally sbyte
{
char[] castedDataChar = data as char[];
for (int i = 0; i < data.Length; i++)
{
writeStream.Write(castedDataChar[i]);
writeStream.Write((byte)0);
}
size = sizeof(char);
}
else
throw new NotImplementedException("Writing arrays of " + data.GetType().ToString() + " to .mat file not implemented");
dataLength += data.Length * size;
//needed for array dimensions
dimensions[0]++;
}
public void FinishArray(Type t)
{
totalPaddingAdded += writeStream.AdvanceTo8ByteBoundary();
//now need to overwrite the dimensions with the correct value
writeStream.Seek((int)dimensionsStartPosition, SeekOrigin.Begin);
//silly matlab format dimension definition wasn't made for realtime streaming... without the following, strings would need to be transposed in matlab to be readable
if (t.Equals(typeof(char)))
{
writeStream.Write(dimensions[0]);
writeStream.Write(dimensions[1]);
}
else
{
writeStream.Write(dimensions[1]);
writeStream.Write(dimensions[0]);
}
//and the full size of the array
writeStream.Seek((int)totalLengthStartPosition, SeekOrigin.Begin);
writeStream.Write((int)(headerLength+dataLength+totalPaddingAdded));
//and the size of the data only
writeStream.Seek((int)dataLengthStartPosition, SeekOrigin.Begin);
writeStream.Write((int)dataLength);
//set pointer back to end of stream
writeStream.Seek(0, SeekOrigin.End);
//indicate this array has finished, so the writeStream can be used by others
this.hasFinished = true;
}
private void WriteArrayTag()
{
//miMatrix type (always 14)
writeStream.Write((int)14);
//store position, so we can later overwrite this placeholder
totalLengthStartPosition = writeStream.BaseStream.Position;
//placeholder for total array size
for (int i = 0; i < 4; i++)
writeStream.Write((byte)0xff);
}
private void WriteFlags(Type arrayElementDataType)
{
//write 4 values for flag block
//Array flags use uint32 data type
writeStream.Write(MatfileHelper.MatlabDataTypeNumber(typeof(UInt32)));
//flag block length (always 8)
writeStream.Write((int)8);
//array class
writeStream.Write(MatfileHelper.MatlabArrayTypeNumber(arrayElementDataType));
//padding (always 0)
writeStream.Write((int)0);
}
private void WriteDimensionsPlaceholder()
{
//data type contained in dimensions subelement: Int32
writeStream.Write(MatfileHelper.MatlabDataTypeNumber(typeof(Int32)));
//always 8 bytes long
writeStream.Write((int)8);
//store position, so we can later overwrite these 2 placeholders
dimensionsStartPosition = writeStream.BaseStream.Position;
//placeholder for first dimension
for (int i = 0; i < dimensions.Length * MatfileHelper.MatlabBytesPerType(typeof(Int32)); i++)
writeStream.Write((byte)0xee);
totalPaddingAdded += writeStream.AdvanceTo8ByteBoundary();
}
private void WriteName(string name)
{
//write 4 values for name block
//data type contained in name block (always 1)
writeStream.Write(MatfileHelper.MatlabDataTypeNumber(typeof(SByte)));
//size (without padding!)
int nameLength = name.Length;
writeStream.Write((int)nameLength);
//write name itself
for (int i = 0; i < nameLength; i++)
writeStream.Write((byte)name[i]);
totalPaddingAdded += writeStream.AdvanceTo8ByteBoundary();
}
public bool HasFinished() { return this.hasFinished; }
}
}