123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968 |
- function headerInfo = nhdr_nrrd_read(nrrdFileName, bReadData)
- [mainFpath,mainFileName,mainFext] = fileparts(nrrdFileName);
- headerInfo = struct();
- headerInfo.content = mainFileName;
- headerInfo.data = [];
- fidr = fopen(nrrdFileName, 'r');
- if (fidr == -1)
- error('ABORT: %s does not exist.\n', nrrdFileName);
- end
- try
- if ~(strcmpi(mainFext, '.nhdr') || strcmpi(mainFext, '.nrrd'))
- warning('%s looks like a %s file, not a nhdr or nrrd file.\n', nrrdFileName, mainFext );
- end
-
-
- cs = fgetl(fidr);
- assert(numel(cs) >= 8 && isequal(cs(1:4), 'NRRD'),...
- 'Bad signature. First line should be magic line of type "NRRD000X" with X an integer between 1 and 5.');
- nrrd_version = sscanf(cs(5:end), '%d');
- if nrrd_version > 5
- error('This reader only supports versions of the NRRD file format up to 5. Detected %d.', nrrd_version)
- end
-
- define_orientation = 0;
-
-
- while ~feof(fidr)
-
- cs = fgetl(fidr);
-
- if isempty(cs)
-
- break;
- end
-
- if foundKeyword( 'CONTENT:', cs )
-
- headerInfo.content = strtrim( cs( length('CONTENT:')+1:end ) );
-
- elseif foundKeyword('TYPE:', cs )
-
- headerInfo.type = strtrim( cs( length('TYPE:')+1:end ) );
-
- elseif foundKeyword('ENDIAN:', cs )
-
- headerInfo.endian = strtrim( cs( length('ENDIAN:')+1:end ) );
-
- elseif foundKeyword('ENCODING:', cs )
-
- headerInfo.encoding = strtrim( cs( length('ENCODING:')+1:end ) );
-
- elseif foundKeyword('DIMENSION:', cs )
-
- headerInfo.dimension = sscanf( cs( length('DIMENSION:')+1:end ), '%i' );
-
- elseif foundKeyword('SIZES:', cs )
-
- iSizes = sscanf( cs(length('SIZES:')+1:end), '%i' );
- headerInfo.sizes = iSizes(:)';
-
- elseif foundKeyword('KINDS:', cs )
-
- headerInfo.kinds = extractStringList( cs(length('KINDS:')+1:end) );
-
-
- elseif foundKeyword('SPACE:', cs )
-
-
- define_orientation = 1;
-
- if isfield(headerInfo, 'spacedimension')
- fprintf(['WARNING nhdr_nrrd_read %s:\n ''space'' field specifier will ' ...
- 'be checked for consistency but will be ignored afterwards ' ...
- 'because ''space dimension'' was specified before.\n'], fopen(fidr));
- end
-
- tmp_space = strtrim( cs( length('SPACE:')+1:end ) );
- tmp_spacedimension = nrrd_getSpaceDimensions(tmp_space);
- if tmp_spacedimension <= 0
- error('%s: unrecognized ''space'' descriptor ''%s''.', fopen(fidr), tmp_space)
- end
-
- if isfield(headerInfo, 'spacedimension')
-
- if internal_spacedimension ~= tmp_spacedimension
- error(['%s: ''space'' field specifier implies a spatial ' ...
- '(world) dimension equal to %d, which differs from ' ...
- 'the ''space dimension'' field specifier set to %d.'],...
- fopen(fidr), tmp_spacedimension, internal_spacedimension)
- end
-
- else
-
- headerInfo.space = tmp_space;
- internal_spacedimension = tmp_spacedimension;
- end
-
- elseif foundKeyword('SPACE DIMENSION:', cs)
-
-
- define_orientation = 1;
- if isfield(headerInfo, 'space')
- fprintf(['WARNING nhdr_nrrd_read %s:\n ''space dimension'' field specifier ' ...
- ' will be checked for consistency but will be ignored afterwards ' ...
- 'because ''space'' was specified before.\n'],...
- fopen(fidr));
- end
-
- tmp_spacedimension = sscanf( cs( length('SPACE DIMENSION:')+1:end), '%i' );
- if numel(tmp_spacedimension) ~= 1
- error(['%s: ''space dimension'' should be specified as one' ...
- ' integer number. Found %d element(s) instead.'],...
- fopen(fidr), numel(tmp_spacedimension))
- end
- if tmp_spacedimension <= 0
- error('%s: ''space dimension'' should be specified as a strictly positive integer (found %d).',...
- fopen(fidr), tmp_spacedimension)
- end
-
- if isfield(headerInfo, 'space')
- if tmp_spacedimension ~= internal_spacedimension
- error(['%s: ''space dimension'' field specifier set to %d, ' ...
- 'which differs from the space (world) dimension implied by the ' ...
- '''space'' field specifier which is equal to %d.'],...
- fopen(fidr), tmp_spacedimension, internal_spacedimension)
- end
-
-
- else
-
- headerInfo.spacedimension = tmp_spacedimension;
- internal_spacedimension = tmp_spacedimension;
- end
-
- elseif foundKeyword('SPACE DIRECTIONS:', cs )
-
-
-
-
-
-
- if ~define_orientation
- error('%s: field specifier ''space directions'' cannot be set before ''space'' or ''space dimension''.',fopen(fidr))
- end
- space_dir_tmp = strtrim(cs(length('SPACE DIRECTIONS:')+1:end));
- spacedir_vecs = strsplit(space_dir_tmp);
- SD_data = zeros(internal_spacedimension, internal_spacedimension);
-
-
-
- cnt_space_vectors = 0;
- for i = 1:length(spacedir_vecs)
- none_start_index = strfind(lower(spacedir_vecs{i}), 'none');
- if ~isempty(none_start_index)
-
- if ~strcmpi(spacedir_vecs{i}, 'none')
- fprintf(['WARNING nhdr_nrrd_read: detected %s instead of ' ...
- 'expected none for axis %d of the per-axis field specifications "space directions:".\n' ...
- ' There should be no quotation marks, parentheses or any other characters, just plain none.\n'],...
- spacedir_vecs{i}, i);
-
- spacedir_vecs{i} = 'none';
- end
- else
-
- cnt_space_vectors = cnt_space_vectors + 1;
- if cnt_space_vectors > internal_spacedimension
- error(['%s:\n ''space directions'' field specifier: ' ...
- 'number of space vectors detected exceeds space (world)' ...
- ' dimension, which is equal to %d.'],...
- fopen(fidr), internal_spacedimension)
- end
- btw_parentheses = spacedir_vecs{i}(1) == '(' ...
- && spacedir_vecs{i}(end) == ')';
- axis_vector = regexprep(spacedir_vecs{i}, '[()]', '');
- if ~btw_parentheses
- fprintf(['WARNING nhdr_nrrd_read: vector should be delimited ' ...
- 'by parentheses for axis %d of the per-axis field ' ...
- 'specifications "space directions:".\n' ...
- ' At least one missing parenthesis in ''%s''.\n'],...
- i, spacedir_vecs{i})
- end
-
- spacedir_vecs{i} = ['(', axis_vector, ')'];
-
- vector_entries = strsplit(axis_vector, ',');
- if length(vector_entries) ~= internal_spacedimension
- error(['%s:\n vector for data axis %d (space axis %d) of the ' ...
- 'per-axis field specifications "space directions:" should ' ...
- 'contain %d entries corresponding to the space (or world) dimension ' ...
- 'specified in the "space" or "space dimension" field specification.' ...
- ' Found %d here.'],...
- fopen(fidr), i, cnt_space_vectors, internal_spacedimension, ...
- length(vector_entries))
- end
- for j = 1:length(vector_entries)
- vector_entry = sscanf(vector_entries{j}, '%f');
- if isempty(vector_entry)
- error(['%s\n in field specification "space directions:", ' ...
- 'vector for data axis %d (space axis %d) too short. ' ...
- 'Detected %d entries instead of expected %d corresponding to ' ...
- 'space (world) dimension.'],...
- fopen(fidr), i, cnt_space_vectors, j-1, internal_spacedimension)
- end
- SD_data(j, cnt_space_vectors) = vector_entry;
- end
- end
- end
-
- headerInfo.spacedirections = spacedir_vecs;
-
- SD_data_chk = strrep(space_dir_tmp, 'none', '' );
- SD_data_chk = extractNumbersWithout(SD_data_chk, {'(',')',',', '"', ''''} );
- if numel(SD_data_chk) ~= (internal_spacedimension)^2
- error(['Expected ''space directions'' to specify a %d-by-%d matrix ' ...
- '(%d elements in total) in agreement with world space dimension.' ...
- ' Found %d element(s) instead.\n'],...
- internal_spacedimension, internal_spacedimension,...
- (internal_spacedimension)^2, numel(SD_data_chk));
- end
-
- if ~isequal(SD_data_chk(:), SD_data(:))
- error(['%s:\n ''space directions'' field specifier: couldn''t' ...
- ' read space vectors. Please refer to the NRRD format definition.'],...
- fopen(fidr))
- end
- headerInfo.spacedirections_matrix = SD_data;
-
-
-
- elseif foundKeyword('SPACE UNITS:', cs )
-
- if define_orientation ~= 1
- error('Field specification ''space units'' cannot be specified before ''space'' or ''space dimension''.')
- end
-
- space_units_tmp = strrep( cs(length('SPACE UNITS:')+1:end), 'none', '');
-
-
-
-
-
-
- headerInfo.spaceunits = extract_spaceunits_list( space_units_tmp );
-
- if length(headerInfo.spaceunits) ~= internal_spacedimension
- error(['Expected ''space units'' to contain %d elements enclosed in double quotes' ...
- ' to match the ''space'' or ''space dimension'' field but found the following ' ...
- '%d element(s):\n%s'],...
- internal_spacedimension, length(headerInfo.spaceunits), sprintf('%s\t',headerInfo.spaceunits{:}))
- end
-
- elseif foundKeyword('SPACE ORIGIN:', cs )
-
-
- assert(define_orientation==1, ...
- sprintf(['%s: field ''space origin'' cannot be specified' ...
- ' before ''space'' or ''space dimension''.'], ...
- fopen(fidr)));
-
- iSO = extractNumbersWithout( cs(length('SPACE ORIGIN:')+1:end), {'(',')',','} );
-
- assert(numel(iSO) == internal_spacedimension,...
- sprintf(['%s: expected ''space origin'' to specify a ' ...
- '%d-element vector to match the ''space'' or ' ...
- '''space dimension'' field but found %d ' ...
- 'element(s).'],...
- fopen(fidr),internal_spacedimension, numel(iSO)) );
-
- headerInfo.spaceorigin = iSO(:);
-
- elseif foundKeyword('MEASUREMENT FRAME:', cs )
-
-
- assert(define_orientation==1,...
- sprintf(['%s: field ''measurement frame'' cannot be ' ...
- 'specified before ''space'' or ''space dimension''.'],...
- fopen(fidr)));
-
- measframe_str = strrep( cs(length('MEASUREMENT FRAME:')+1:end), 'none', '');
-
- iMF = extractNumbersWithout( measframe_str, {'(',')',','} );
-
- assert(numel(iMF) == (internal_spacedimension)^2,...
- sprintf(['%s: expected ''measurement frame'' to specify a ' ...
- '%d-by-%d matrix (%d total elements) but found %d ' ...
- 'element(s).'],...
- fopen(fidr), internal_spacedimension, ...
- internal_spacedimension, (internal_spacedimension)^2,...
- numel(iMF)));
-
- headerInfo.measurementframe = reshape(iMF(:), [internal_spacedimension, internal_spacedimension]);
-
-
-
-
-
- elseif foundKeyword('THICKNESSES:', cs )
-
- sThicknesses = extractStringList( cs(length('THICKNESSES:')+1:end) );
- iThicknesses = [];
- lenThicknesses = length( sThicknesses );
- for iI=1:lenThicknesses
- iThicknesses = [iThicknesses, str2double(sThicknesses{iI}) ];
- end
- headerInfo.thicknesses = iThicknesses;
-
- elseif foundKeyword('CENTERINGS:', cs )
-
- headerInfo.centerings = extractStringList( cs(length('CENTERINGS:')+1:end ) );
-
- elseif foundKeyword('LINE SKIP:', cs) || foundKeyword('LINESKIP', cs)
-
- if foundKeyword('LINE SKIP:', cs)
- headerInfo.lineskip = sscanf( cs( length('LINE SKIP:')+1:end ), '%d' );
- else
- headerInfo.lineskip = sscanf( cs( length('LINESKIP:')+1:end ), '%d' );
- end
- assert(headerInfo.lineskip >= 0,...
- sprintf(['Field lineskip or line skip should be greater' ...
- ' than or equal to zero, detected %d.'], ...
- headerInfo.lineskip));
- elseif foundKeyword('BYTE SKIP:', cs) || foundKeyword('BYTESKIP:', cs)
-
- if foundKeyword('BYTE SKIP:', cs)
- headerInfo.byteskip = sscanf( cs( length('BYTE SKIP:')+1:end ), '%d' );
- else
- headerInfo.byteskip = sscanf( cs( length('BYTESKIP:')+1:end ), '%d' );
- end
- assert(headerInfo.byteskip >= -1, ...
- sprintf(['Field byteskip or byte skip can only take ' ...
- 'non-negative integer values or -1, detected %d.'], ...
- headerInfo.byteskip));
-
- elseif foundKeyword('MODALITY', cs )
-
- headerInfo.modality = strtrim( extractKeyValueString( cs(length('MODALITY')+1:end ) ) );
-
- elseif foundKeyword('DWMRI_B-VALUE', cs )
-
- headerInfo.bvalue = str2double( extractKeyValueString( cs(length('DWMRI_B-VALUE')+1:end ) ) );
-
- elseif foundKeyword('DWMRI_GRADIENT_', cs )
-
- [iGNr, dwiGradient] = extractGradient(cs(length('DWMRI_GRADIENT_')+1:end ));
- headerInfo.gradients(iGNr+1,:) = dwiGradient;
-
-
- elseif foundKeyword('DATA FILE:', cs ) || foundKeyword('DATAFILE:', cs)
-
-
- field_value = strtrim( cs(length('DATA FILE:')+1:end) );
- if foundKeyword('DATAFILE:', cs)
- field_value = strtrim( cs(length('DATAFILE:')+1:end) );
- end
-
- [filelist, LIST_mode, subdim] = extract_datafiles(field_value);
-
-
- headerInfo.datafiles = filelist;
-
-
-
- if LIST_mode
- datafile_cnt = 0;
- while ~feof(fidr)
- cs = fgetl(fidr);
- if isempty(cs)
- break;
- end
- datafile_cnt = datafile_cnt + 1;
- headerInfo.datafiles{datafile_cnt} = cs;
- end
- end
-
-
-
-
-
- else
-
-
-
- csTmp = strtrim( cs );
- if csTmp(1)~='#' && ~strcmp(cs(1:4),'NRRD')
- fprintf('WARNING nhdr_nrrd_read: Could not parse input line: ''%s'' \n', cs );
-
-
-
- end
- end
-
- end
-
-
-
-
-
-
- assert(isfield(headerInfo, 'sizes'), 'Missing required ''sizes'' field in header');
- assert(isfield(headerInfo, 'dimension'), 'Missing required ''dimension'' field in header');
- assert(isfield(headerInfo, 'encoding'), 'Missing required ''encoding'' field in header');
- assert(isfield(headerInfo, 'type'), 'Missing required ''type'' field in header');
-
-
-
- if isfield(headerInfo, 'byteskip') && headerInfo.byteskip == -1
- assert( strcmpi(headerInfo.encoding, 'raw'), ...
- sprintf('byte skip value of -1 is only valid with raw encoding. See definition of NRRD File Format for more information.\n'));
- end
-
- matlabdatatype = nrrd_getMatlabDataType(headerInfo.type);
-
-
- if ~(any(strcmpi(headerInfo.encoding,{'txt', 'text', 'ascii'})) || any(strcmpi(matlabdatatype, {'int8', 'uint8'})))
- assert(isfield(headerInfo, 'endian'), 'Missing required ''endian'' field in header');
- else
- if ~isfield(headerInfo,'endian')
- headerInfo.endian = 'little';
- end
- end
-
-
- if define_orientation
- assert(isfield(headerInfo, 'spacedirections'), ...
- ['Missing field ''space directions'', required if either' ...
- ' ''space'' or ''space dimension'' is set.']);
- if length(headerInfo.spacedirections) ~= headerInfo.dimension
- fprintf(['WARNING nhdr_nrrd_read %s:\n Unexpected format found for ''space directions'' specifier.\n',...
- ' Expected none entries and vectors delimited by parentheses such as (1.2,0.2,0.25), ', ...
- 'the number of entries being equal to the dimension field specification.\n',...
- ' See definition of NRRD File Format for more information.\n'], fopen(fidr));
- end
- end
-
-
- if isfield(headerInfo, 'lineskip') && headerInfo.lineskip > 0
- assert(isfield(headerInfo, 'byteskip') && headerInfo.byteskip == -1, ...
- sprintf(['lineskip option is currently not supported and can ' ...
- 'only be set to zero unless raw encoding is used and ' ...
- 'byte skip is set to -1 (which cancels the effect of ' ...
- 'lineskip altogether).']));
- end
-
-
- if isfield(headerInfo, 'byteskip') && ~strcmpi(headerInfo.encoding,'raw')
- assert( headerInfo.byteskip == 0, ...
- sprintf('byte skip option with non raw encoding is currently not supported and can only be set to zero.\n'));
- end
- if isfield(headerInfo, 'byteskip') && strcmpi(headerInfo.encoding, 'raw')
- assert( headerInfo.byteskip == -1, ...
- sprintf('non-negative byte skip values with raw encoding are currently not supported; byte skip can only be set to -1.\n'));
- end
-
- catch me
-
- fclose(fidr);
- rethrow(me);
- end
- if bReadData
- N_data_tot = prod(headerInfo.sizes);
- if isfield(headerInfo, 'datafiles')
-
-
-
-
- fclose(fidr);
-
-
-
- if ~isempty(subdim) && subdim~=headerInfo.dimension
- error(['(detached header): reading data from slices along axis other than the last' ...
- ' (i.e. slowest) one, is currently not supported.\n' ...
- 'Last argument [<subdim>] in ''data file'' or ''datafile'' field ' ...
- 'should be removed or set equal to %d, which is the detected' ...
- ' ''dimension'' field value, for now.'],...
- headerInfo.dimension);
- end
-
-
- N_data_files = length(headerInfo.datafiles);
- assert(mod(N_data_tot, N_data_files)==0, ...
- sprintf(['Number of detected data files (%d) does not divide total' ...
- ' number of values contained in data %d obtained from prod(sizes=[%s]).\n'],...
- N_data_files, N_data_tot, sprintf('%d ',headerInfo.sizes)));
-
- N_data_per_file = N_data_tot/N_data_files;
-
- headerInfo.data = zeros(headerInfo.sizes, matlabdatatype);
-
- for i = 1:N_data_files
-
-
- [~,fname_data,ext_data] = fileparts(headerInfo.datafiles{i});
-
- data_ind = (i-1)*N_data_per_file+1:i*N_data_per_file;
-
- if strcmpi(ext_data,'.nhdr')
-
- error(['datafile %di/%d: nhdr file should not be used as ' ...
- 'detached data file.'], ...
- i, length(headerInfo.datafiles));
-
- elseif strcmpi(ext_data, '.nrrd')
-
-
- bRead_Detached_Data = true;
- tmp_struct = nhdr_nrrd_read(fullfile(mainFpath, ...
- [fname_data, ext_data]), ...
- bRead_Detached_Data);
-
-
- assert(N_data_files==1 || headerInfo.dimension == tmp_struct.dimension +1, ...
- sprintf(['Detached header %s: the number of dimensions in detached ' ...
- 'nrrd data file %d/%d (%s) should be one fewer than that of' ...
- ' the header file.\nDetected %d instead of %d.\nDifferent ' ...
- 'datafile dimensions as specified by the nrrd standard are' ...
- ' not supported as of now.\n'],...
- fopen(fidr), i, N_data_files, headerInfo.datafiles{i},...
- tmp_struct.dimension, headerInfo.dimension-1 ));
-
- headerInfo.data(data_ind) = tmp_struct.data(:);
-
-
-
-
- tmp_struct = rmfield(tmp_struct, 'data');
- headerInfo.detached_header = tmp_struct;
- else
-
-
- fid_data = fopen( fullfile(mainFpath, [fname_data, ext_data]), 'r');
- if( fid_data < 1 )
- error(['While reading detached header file %s:\ndetached ' ...
- 'data file number %d/%d (%s) could not be opened.'], ...
- nrrdFileName, i, N_data_files, headerInfo.datafiles{i});
- end
- try
- tmp_data = readData(fid_data, N_data_per_file, headerInfo.encoding, matlabdatatype);
- fclose(fid_data);
- catch me_detached
- fclose(fid_data);
- rethrow(me_detached);
- end
-
- tmp_data = adjustEndian(tmp_data, headerInfo.endian);
-
- headerInfo.data(data_ind) = tmp_data(:);
- end
-
- end
-
- else
-
- try
- headerInfo.data = readData(fidr, N_data_tot, headerInfo.encoding, matlabdatatype);
- fclose(fidr);
- catch me_detached
- fclose(fidr);
- rethrow(me_detached);
- end
- headerInfo.data = adjustEndian(headerInfo.data, headerInfo.endian);
- headerInfo.data = reshape(headerInfo.data, headerInfo.sizes(:)');
- end
- else
- fclose(fidr);
- end
- end
- function [iGNr, dwiGradient] = extractGradient( st )
- iGNr = str2num( st(1:4) );
- assgnLoc = strfind( st, ':=' );
- if ( isempty(assgnLoc) )
- dwiGradient = [];
- return;
- else
-
- dwiGradient = sscanf( st(assgnLoc+2:end), '%f' );
-
- end
- end
- function kvs = extractKeyValueString( st )
- assgnLoc = strfind( st, ':=' );
- if ( isempty(assgnLoc) )
- kvs = [];
- return;
- else
-
- kvs = st(assgnLoc(1)+2:end);
-
- end
- end
- function sl = extractStringList( strList )
- sl = strsplit(strtrim(strList));
- end
- function iNrs = extractNumbersWithout( inputString, withoutTokens )
- auxStr = inputString;
- for iI=1:length( withoutTokens )
-
- auxStr = strrep( auxStr, withoutTokens{iI}, ' ' );
-
- end
- iNrs = sscanf( auxStr, '%f' );
- end
- function fk = foundKeyword( keyWord, cs )
- lenKeyword = length( keyWord );
- fk = (lenKeyword <= length(cs)) && strcmpi( cs(1:lenKeyword), keyWord);
- end
- function su_ca = extract_spaceunits_list( fieldValue )
- fv_trimmed = strtrim( fieldValue );
- su_ca = strsplit(fv_trimmed, '"');
- su_ca = su_ca(~ ( strcmp(su_ca, '') | strcmp(su_ca, ' ') ) );
- for i = 1:length(su_ca)
- su_ca{i} = strtrim( su_ca{i} );
- end
- end
- function [filelist, LIST_mode, subdim] = extract_datafiles( field_string)
- field_string = strtrim(field_string);
- filelist = {};
- LIST_mode = 0;
- subdim = [];
- if length(field_string)>= 4 && strcmpi(field_string(1:4), 'LIST')
-
- subdim = sscanf(field_string(5:end), '%d');
- LIST_mode = 1;
- return;
- else
-
- str_lst = strsplit(field_string);
- if length(str_lst) == 1
-
- filelist{1} = str_lst{1};
-
-
- elseif any(length(str_lst)==[0, 2, 3]) || length(str_lst)>5
-
- error(['error in ''data list'' (or ''data list'') field, in non' ...
- ' ''LIST'' mode:\nexpected ''<filename>'' or ''<format> ' ...
- '<min> <max> <step> [<subdim>]'' but found instead ''%s'' ' ...
- '(%d elements instead of 1, 4 or 5).'],...
- sprintf('%s ',str_lst{:}), length(str_lst));
- else
-
-
-
- str_format = str_lst{1};
- id_min = sscanf(str_lst{2}, '%d');
- id_max = sscanf(str_lst{3}, '%d');
- step = sscanf(str_lst{4}, '%d');
-
-
- assert(step~=0,...
- sprintf(['detached data files with names specified by ' ...
- 'sprintf()-like format: step should be strictly positive' ...
- ' or negative, not zero.']));
- if step < 0
- assert(id_min >= id_max, ...
- sprintf(['detached data files with names specified by ' ...
- 'sprintf()-like format: when step is <0, min ' ...
- 'should be larger than or equal to max, here we ' ...
- 'found min=%d < max=%d.'],id_min, id_max));
- else
- assert(id_min <= id_max,...
- sprintf(['detached data files with names specified by ' ...
- 'sprintf()-like format: : when step is >0, min ' ...
- 'should be smaller than or equal to max, here we ' ...
- 'found min=%d > max=%d.'],id_min, id_max));
- end
-
-
- fileIDs = id_min:step:id_max;
- filelist = cell(length(fileIDs), 1);
- for i = 1:length(fileIDs)
- expanded_fname = sprintf(str_format, fileIDs(i));
- filelist{i} = expanded_fname;
- end
-
- if length(str_lst) == 5
- subdim = sscanf(str_lst{5}, '%d');
- end
- end
- end
-
- end
- function data = readData(fidIn, Nelems, meta_encoding, matlabdatatype)
- switch (meta_encoding)
- case {'raw'}
-
-
- data_bytes = (Nelems)*sizeOf(matlabdatatype);
- fseek(fidIn,0,'eof');
- tot_bytes_file = ftell(fidIn);
- fseek(fidIn,tot_bytes_file-data_bytes,'bof');
-
-
- data = fread(fidIn, inf, [matlabdatatype '=>' matlabdatatype]);
-
- case {'gzip', 'gz'}
-
- tmp = fread(fidIn, Inf, 'uint8=>uint8');
-
- tmpBase = tempname(pwd);
- tmpFile = [tmpBase '.gz'];
- fidTmp = fopen(tmpFile, 'wb');
- assert(fidTmp > 3, 'Could not open temporary file for GZIP decompression')
-
- try
- fwrite(fidTmp, tmp, 'uint8');
- catch me
- fclose(fidTmp);
- delete(tmpFile);
- rethrow(me);
- end
- fclose(fidTmp);
-
- try
- gunzip(tmpFile);
- catch me
- delete(tmpFile);
- rethrow(me);
- end
- delete(tmpFile);
-
- fidTmp = fopen(tmpBase, 'rb');
-
- try
- data = readData(fidTmp, Nelems, 'raw', matlabdatatype);
- catch me
- fclose(fidTmp);
- delete(tmpBase);
- rethrow(me);
- end
- fclose(fidTmp);
- delete(tmpBase);
- case {'txt', 'text', 'ascii'}
-
- data = fscanf(fidIn, '%f');
- data = cast(data, matlabdatatype);
-
- otherwise
- assert(false, 'Unsupported encoding')
- end
- assert(Nelems == numel(data),...
- sprintf(['Error reading binary content of %s: detected %d elements' ...
- ' instead of %d announced in header.'],...
- fopen(fidIn), numel(data), Nelems));
- end
- function data = adjustEndian(data, meta_endian)
- [~,~,endian] = computer();
- needToSwap = (isequal(endian, 'B') && isequal(lower(meta_endian), 'little')) || ...
- (isequal(endian, 'L') && isequal(lower(meta_endian), 'big'));
- if (needToSwap)
- data = swapbytes(data);
- end
- end
- function numBytes = sizeOf(dataClass)
-
- eval(['var = ' dataClass '(0);']);
-
- W = whos('var');
- numBytes = W.bytes;
- end
|