function partmatrix=readMatrix(filename,firstPix,numPix,firstEcho,numEchoes)
% ========================================================================
% Extended Chirp Scaling Algorithm
% Maurice Rueegg
% April 2005, May 2009
% ========================================================================
% Program to extract a part of a SUN raster file
% ========================================================================
% Copyright (c) Maurice Rueegg (2005,2009)
% Permission to use these scripts for SAR processing and related research
% is granted. Please acknowledge the author.
% The author disclaims any responsibility for or liability related to this
% Software. Any questions or claims related to the Software can be
% directed to info@znuerb.com 
% ========================================================================

partmatrix=complex(zeros(numEchoes,numPix));

% open the raster file and determine the file size from the header
fid=fopen(filename,'r','ieee-be');
headerInts=fread(fid,8,'uint32');
ras_magic=headerInts(1);
ras_width=headerInts(2);
ras_height=headerInts(3);
ras_depth=headerInts(4);
ras_length=headerInts(5);
ras_type=headerInts(6);
ras_maptype=headerInts(7);
ras_maplength=headerInts(8);

for k=1:firstEcho-1
    [curEcho,read_count]=fread(fid,ras_width*2,'float32');
end    

read_count=0;

for k=1:numEchoes
    [curEcho,read_count]=fread(fid,[1,ras_width*2],'float32');
    if read_count<ras_width*2
        error(['read only ',num2str(read_count),' complex values instead of ',...
                num2str(ras_width)])
    end
    im_range=2*firstPix:2:2*(firstPix-1+numPix);
    re_range=im_range-1;
    partmatrix(k,:)=(curEcho(re_range)+curEcho(im_range).*j);
end
fclose(fid);
return

% = end ==================================================================
