## Copyright (C) 2005 Miroslaw Kwasniak (Miros\law Kwa\sniak) ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## -*- texinfo -*- ## Reading of ENVI hdr file version 0.0002 ## @deftypefn {Function File} address@hidden =} read_envi_hdr (@var{file_name}) ## Reads envi hdr from file @var{file_name} to a structure. ## @end deftypefn function hdr = read_envi_hdr( fname ) data_type = { 'bit8', 'int16', 'int32', 'float32', 'float64', ... 'uint16', 'uint32', 'int64', 'uint64'}; num_set = ' 0123456789,.'; if nargin ~= 1 ; error( 'read_envi_hdr( fname )' ); endif fh = fopen( fname, 'rt' ); if fh<0; error( [" Can't open " fname ] ); endif l = fgetl (fh); if ~ strcmp( l, 'ENVI' ); fclose( fh ); error('Bad file'); endif while true l = fgetl (fh); if ~isstr( l ); break; end i = index( l, '=' ); if i == 0 ; fclose( fh ); error('Corrupted file'); endif name = strrep( deblank(l(1:i-1)) , ' ', '_' ); l = l(i+1:end); i = index( l, '{' ); if ~i value = l; else l(i) = ''; value = ''; while true i = index( l, '}' ); if i; l(i) = '' ; endif value = [ value, l ]; if i; break end l = fgetl (fh); if ~isstr( l ); fclose( fh ); error('Corrupted file'); endif endwhile endif if isempty( setdiff( value, num_set ) ) v = str2num( value ); if ~isempty( v ); value = v; endif endif hdr.(name) = value; endwhile fclose( fh ); if struct_contains ( hdr , 'data_type' ) hdr.data_type_txt = data_type { hdr.data_type }; endif endfunction