daffv15_write_metadata.m 3.55 KB
Newer Older
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
function [ ] = daffv15_write_metadata( fid, metadata )
%DAFF_WRITE_METADATA Write a DAFF metadata block into DAFF binary file
%
% fid       File handle
% metadata  N-lenght structure with field names 'name', 'value' and
%           'datatype' ('BOOL' == 0, 'INT' == 1, 'FLOAT' == 2, 'STRING' == 3)
%
% Note: also accepts old format (struct with key/value pairs that will be
% converted automatically using daffv15_medata_addKey() )
%
% <ITA-Toolbox>
% This file is part of the application openDAFF for the ITA-Toolbox. All rights reserved.
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>
%
% This file is part of OpenDAFF, http://www.opendaff.org
%
    
    % Only structures are allowed
    if ~isstruct( metadata )
        error( 'Metadata must be a structure' );
    end
    
    nkeys = length( metadata );
    
    % Check old metadata format and convert (for convenience)
    if nkeys == 1
        metadata_new_format = [];
29 30 31 32
        metadata_fieldnames = fieldnames( metadata );
        for i=1:length(metadata_fieldnames)
            name = metadata_fieldnames{i};
            eval([' value = metadata.' name ]);
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
            type = '';
            
            % Logical scalar (DAFF_BOOL)
            if islogical(value) && isscalar(value)
                type = 'BOOL';
            end

            % Integer scalar (DAFF_INT)
            if isinteger(value) && isfinite(value) && isreal(value) && isscalar(value)
                type = 'INT';
            end

            % Floating point scalar (DAFF_FLOAT)
            if isfloat(value) && isfinite(value) && isreal(value) && isscalar(value)
                type = 'FLOAT';
            end        

            % String (DAFF_STRING)
            if ischar( value )
                type = 'STRING';
            end
            
            if isempty( type )
                error( 'Unsupported datatype %s for key ''%s'' in metadata', class( value ), name );
            end
            
59
            metadata_new_format = daffv15_metadata_addKey( metadata_new_format, name, type, value );
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
        end
        
        metadata = metadata_new_format;
    end        
        
    % Number of keys
    fwrite( fid, nkeys, 'int32' );
        
    % Write each metadata struct entry
    for i=1:nkeys
        
        name = metadata(i).name;
        value = metadata(i).value;
        datatype = metadata(i).datatype;
        
        % Bool
        if datatype == 0
            fwrite(fid, 0, 'int32');
            fwrite(fid, name, 'char');
            fwrite(fid, 0, 'char');
            if (value == true)
                fwrite(fid, 1, 'int32');
            else
                fwrite(fid, 0, 'int32');
            end
        end
        
        % Integer scalar (DAFF_INT)
        if datatype == 1
            fwrite(fid, 1, 'int32');
            fwrite(fid, name, 'char');
            fwrite(fid, 0, 'char');
            fwrite(fid, value, 'int32');  
        end
        
        % Floating point scalar (DAFF_FLOAT)
        if datatype == 2
            fwrite(fid, 2, 'int32');
            fwrite(fid, name, 'char');
            fwrite(fid, 0, 'char');
            fwrite(fid, value, 'double');
        end
        
        % String (DAFF_STRING)
        if datatype == 3
            fwrite(fid, 3, 'int32');
            fwrite(fid, name, 'char');
            fwrite(fid, 0, 'char');
            fwrite(fid, value, 'char');
            fwrite(fid, 0, 'char');
        end
      
        if datatype > 3
            error( 'Unsupported datatype %s for key ''%s'' in metadata', class( value ), name );
        end
    end
end