-
Notifications
You must be signed in to change notification settings - Fork 1
/
datToNPY.m
42 lines (34 loc) · 1.46 KB
/
datToNPY.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
function datToNPY(inFilename, outFilename, dataType, shape, varargin)
% function datToNPY(inFilename, outFilename, shape, dataType, [fortranOrder, littleEndian])
%
% make a NPY file from a flat binary file, given that you know the shape,
% dataType, ordering, and endianness of the flat binary file.
%
% The point here is you don't want to read in all the data from the
% existing binary file - instead you can just create the appropriate header
% and then concatenate it with the data.
%
% ** completely untested
if ~isempty(varargin)
fortranOrder = varargin{1}; % must be true/false
littleEndian = varargin{2}; % must be true/false
else
fortranOrder = true;
littleEndian = true;
end
header = constructNPYheader(dataType, shape, fortranOrder, littleEndian);
% ** TODO: need to put the header into a temp file instead, in case the
% outFilename is the same as the inFilename (and then delete the temp file
% later)
fid = fopen(tempFilename, 'w');
fwrite(fid, header, 'uint8');
fclose(fid)
str = computer;
switch str
case {'PCWIN', 'PCWIN64'}
[~,~] = system(sprintf('copy /b %s+%s %s', tempFilename, inFilename, outFilename));
case {'GLNXA64', 'MACI64'}
[~,~] = system(sprintf('cat %s %s > %s', tempFilename, inFilename, outFilename));
otherwise
fprintf(1, 'I don''t know how to concatenate files for your OS, but you can finish making the NPY youself by concatenating %s with %s.\n', tempFilename, inFilename);
end