From help-request at octave dot org Sun Jan 9 06:23:43 2005 Subject: Re: MATLAB script for parsing ENVI headers? From: Miroslaw Kwasniak To: "help at octave dot org" Date: Sun, 9 Jan 2005 13:25:46 +0100 --bp/iNruPH9dso1Pn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Jan 08, 2005 at 03:45:32PM -0800, Jonathan Greenberg wrote: > I was wondering if anyone knew of a MATLAB/Octave script which can parse an > ENVI header file, retrieving all of the info necessary to import the file > into MATLAB (number type, endian, samples, lines, bands, etc...) If you wrote about ENVI by rsinc it's a simple structured text file. I attached a prototype of function. Example of run: octave:1> OCTAVE_VERSION OCTAVE_VERSION = 2.1.63 octave:2> hdr=read_envi_hdr('x.hdr'); fieldnames (hdr) { [1,1] = band_names [2,1] = bands [3,1] = byte_order [4,1] = data_type [5,1] = data_type_txt [6,1] = description [7,1] = file_type [8,1] = fwhm [9,1] = header_offset [10,1] = interleave [11,1] = lines [12,1] = samples [13,1] = sensor_type [14,1] = spectra_names [15,1] = wavelength [16,1] = z_plot_range } Mirek --bp/iNruPH9dso1Pn Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="read_envi_hdr.m" ## 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 ## at deftypefn {Function File} {@var{hdr} =} read_envi_hdr (@var{file_name}) ## Reads envi hdr from file at var{file_name} to a structure. ## at 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 --bp/iNruPH9dso1Pn-- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------