|
| 1 | +function h5save(filename, varname, A, sizeA, dtype) |
| 2 | + |
| 3 | +narginchk(3, 5) |
| 4 | + |
| 5 | +if nargin < 4 || isempty(sizeA) |
| 6 | + if isvector(A) |
| 7 | + sizeA = length(A); |
| 8 | + else |
| 9 | + sizeA = size(A); |
| 10 | + end |
| 11 | +end |
| 12 | + |
| 13 | +if nargin >= 5 && ~isempty(dtype) |
| 14 | + A = coerce_ds(A, dtype); |
| 15 | +end |
| 16 | +if ischar(A) |
| 17 | + A = string(A); |
| 18 | + sizeA = size(A); |
| 19 | +end |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +if h5exists(filename, varname) |
| 24 | + exist_file(filename, varname, A, sizeA) |
| 25 | +else |
| 26 | + new_file(filename, varname, A, sizeA) |
| 27 | +end % if |
| 28 | + |
| 29 | +end % function |
| 30 | + |
| 31 | + |
| 32 | +function exist_file(filename, varname, A, sizeA) |
| 33 | + |
| 34 | +diskshape = h5size(filename, varname); |
| 35 | +if length(diskshape) >= 2 |
| 36 | + % start is always a row vector, regardless of shape of array |
| 37 | + start = ones(1,ndims(A)); |
| 38 | +elseif ~isempty(diskshape) |
| 39 | + start = 1; |
| 40 | +end |
| 41 | + |
| 42 | +if isscalar(A) |
| 43 | + h5write(filename, varname, A) |
| 44 | +elseif all(diskshape == sizeA) |
| 45 | + h5write(filename, varname, A, start, sizeA) |
| 46 | +elseif all(diskshape == fliplr(sizeA)) |
| 47 | + h5write(filename, varname, A.', start, fliplr(sizeA)) |
| 48 | +else |
| 49 | + error('h5save:value_error', ['shape of ',varname,': ', int2str(sizeA), ' does not match existing HDF5 shape ', int2str(diskshape)]) |
| 50 | +end |
| 51 | + |
| 52 | +end % function |
| 53 | + |
| 54 | + |
| 55 | +function new_file(filename, varname, A, sizeA) |
| 56 | + |
| 57 | +if ~ismatrix(A) |
| 58 | + % enable Gzip compression--remember Matlab's dim order is flipped from |
| 59 | + % C / Python |
| 60 | + switch length(sizeA) |
| 61 | + case 4, chunksize = [sizeA(1), sizeA(2), 1, sizeA(4)]; |
| 62 | + case 3, chunksize = [sizeA(1), sizeA(2), 1]; |
| 63 | + otherwise, error('h5save:fixme', '%s is bigger than 4 dims', varname) |
| 64 | + end |
| 65 | + h5create(filename, varname, sizeA, 'DataType', class(A), ... |
| 66 | + 'Deflate', 1, 'Fletcher32', true, 'Shuffle', true, 'ChunkSize', chunksize) |
| 67 | +else |
| 68 | + h5create(filename, varname, sizeA, 'DataType', class(A)) |
| 69 | +end % if |
| 70 | + |
| 71 | +h5write(filename, varname, A) |
| 72 | + |
| 73 | +end % function |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +% Copyright 2020 Michael Hirsch, Ph.D. |
| 78 | + |
| 79 | +% Licensed under the Apache License, Version 2.0 (the "License"); |
| 80 | +% you may not use this file except in compliance with the License. |
| 81 | +% You may obtain a copy of the License at |
| 82 | + |
| 83 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 84 | + |
| 85 | +% Unless required by applicable law or agreed to in writing, software |
| 86 | +% distributed under the License is distributed on an "AS IS" BASIS, |
| 87 | +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 88 | +% See the License for the specific language governing permissions and |
| 89 | +% limitations under the License. |
0 commit comments