
% input parameter:
% ----------------------------------------------
% fname:  filename of e.g. ~/data/Fedje/20000950505_LYN.SPEC
% ix:     x position in grid
% iy:     y position in grid

% output parameter:
% ----------------------------------------------
% Sc    : power spectrum of double precision values

function [Sc]=rd_wera_spec(fname,ix,iy)

global  H_bin
global  h_mt;    
global  h_N;     
global  h_dT;    
global  h_nx;    
global  h_ny;    
global  h_na;   
global  h_frq;   

fid=fopen(fname,'r','l');


% read header and decode some usefull parameters
% ---------------------------------------------------
[H_bin,count]   = fread(fid,512,'char');

header=char(H_bin)';

mt_pos  =findstr('MT:',         header);
N_pos   =findstr('SAMPLES',     header);
dT_pos  =findstr('RATE:',       header);
nr_pos  =findstr('NRRANGES:',   header);
frq_pos =findstr('FREQUENZ',    header);
h_nx_pos=findstr('NX=',         header);
h_ny_pos=findstr('NY=',         header);

h_mt    =str2num(char(  H_bin(mt_pos  +3 : mt_pos   +3 + 6))');
h_N     =str2num(char(  H_bin(N_pos   -5 : N_pos    -5 + 4))'); % 2048
h_dT    =str2double(char(H_bin(dT_pos  +5 : dT_pos   +5 + 7))');
h_nr    =str2num(       header(151  :153));
h_nx    = str2num(char(  H_bin(h_nx_pos  +3 : h_nx_pos   +3 + 3))');
h_ny    = str2num(char(  H_bin(h_ny_pos  +3 : h_ny_pos   +3 + 3))');

h_frq   =str2num(char(  H_bin(frq_pos  +8 : frq_pos   +8 + 6))');

h_N=512; % Spectra in spec-files are organized with 512 spectral lines

% read data into temporary array S
% ---------------------------------------------------
[S,count]       = fread(fid,[h_N, h_nx * h_ny],'float32');

status = fclose(fid);

% Linear adress as function of ia and ir        
LAdr = (ix-1) * h_ny + (iy-1) + 1;
Sc = S(:,LAdr);    
clear('S');

% end



