-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructNPYheader.m
88 lines (65 loc) · 2.78 KB
/
constructNPYheader.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
function header = constructNPYheader(dataType, shape, varargin)
if ~isempty(varargin)
fortranOrder = varargin{1}; % must be true/false
littleEndian = varargin{2}; % must be true/false
else
fortranOrder = true;
littleEndian = true;
end
dtypesMatlab = {'uint8','uint16','uint32','uint64','int8','int16','int32','int64','single','double', 'logical'};
dtypesNPY = {'u1', 'u2', 'u4', 'u8', 'i1', 'i2', 'i4', 'i8', 'f4', 'f8', 'b1'};
magicString = uint8([147 78 85 77 80 89]); %x93NUMPY
majorVersion = uint8(1);
minorVersion = uint8(0);
% build the dict specifying data type, array order, endianness, and
% shape
dictString = '{''descr'': ''';
if littleEndian
dictString = [dictString '<'];
else
dictString = [dictString '>'];
end
dictString = [dictString dtypesNPY{strcmp(dtypesMatlab,dataType)} ''', '];
dictString = [dictString '''fortran_order'': '];
if fortranOrder
dictString = [dictString 'True, '];
else
dictString = [dictString 'False, '];
end
dictString = [dictString '''shape'': ('];
% if length(shape)==1 && shape==1
%
% else
% for s = 1:length(shape)
% if s==length(shape) && shape(s)==1
%
% else
% dictString = [dictString num2str(shape(s))];
% if length(shape)>1 && s+1==length(shape) && shape(s+1)==1
% dictString = [dictString ','];
% elseif length(shape)>1 && s<length(shape)
% dictString = [dictString ', '];
% end
% end
% end
% if length(shape)==1
% dictString = [dictString ','];
% end
% end
for s = 1:length(shape)
dictString = [dictString num2str(shape(s))];
if s<length(shape)
dictString = [dictString ', '];
end
end
dictString = [dictString '), '];
dictString = [dictString '}'];
totalHeaderLength = length(dictString)+10; % 10 is length of magicString, version, and headerLength
headerLengthPadded = ceil(double(totalHeaderLength+1)/16)*16; % the whole thing should be a multiple of 16
% I add 1 to the length in order to allow for the newline character
% format specification is that headerlen is little endian. I believe it comes out so using this command...
headerLength = typecast(int16(headerLengthPadded-10), 'uint8');
zeroPad = zeros(1,headerLengthPadded-totalHeaderLength, 'uint8')+uint8(32); % +32 so they are spaces
zeroPad(end) = uint8(10); % newline character
header = uint8([magicString majorVersion minorVersion headerLength dictString zeroPad]);
end