function [idchan, freq, coef] = rd_nte_be(fname);

% function [idchan, freq, coef] = rd_nte(fname);
%
% Reads in binary FORTRAN non-LTE data file created by program "wrt_nte.m".
% same as /home/hannon/Fit_deltaR_nonLTE/Src/rd_nte.m and expects one of
% Scott Hannon's big-endian files
%
% Input:
%    fname = {string} name of binary FORTRAN data file to read
%
% Output:
%    idchan   = [nchan x 1] channel ID
%    freq     = [nchan x 1] center frequency
%    coef     = [nchan x 6] non-LTE coeffcients
%

% Created: 15 March 2005, Scott Hannon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

expected_coeffs = 6;     %% this is for SARTAv106
expected_coeffs = 7;     %% this is for SARTAv108
% Expected value of ifm = 4*(1 + 1 + 6) = 32   for v106
% Expected value of ifm = 4*(1 + 1 + 7) = 36   for v108
% This is 4 bytes each for (idchan, freq, & 6 or 7 coefs)
ifm_exp=32;
ifm_exp=36;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The code below should not require modifications
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

d = dir(fname);
junk = d.bytes/(ifm_exp + 8);
nchan = round(junk);
if (abs(junk - nchan) > 0.0001)
   error('Unexpected file size')
else
  fprintf(1,'expecting %4i channels \n',nchan)
end

% Open input file
fid=fopen(fname,'r','ieee-be');

% Dimension output arrays
idchan=zeros(nchan,1);
freq=zeros(nchan,1);
coef=zeros(nchan,expected_coeffs);

% Loop over the channels
for ic=1:nchan

   % Read FORTRAN start-of-record marker
   [ifm,count]=fread(fid,1,'integer*4');
   if (count == 0)
      ic
      disp('The FORTRAN data file contains fewer channels than expected')
   end
   if (ifm ~= ifm_exp)
      ifm
      ifm_exp
      error('FORTRAN start-of-record marker is unexpected size')
   end

   % Read data for this channel
   idchan(ic)=fread(fid,1,'integer*4');
   freq(ic)=fread(fid,1,'real*4');
   coef(ic,:)=fread(fid,[1,expected_coeffs],'real*4');

   % Read FORTRAN end-of-record marker
   ifm=fread(fid,1,'integer*4');
   if (ifm ~= ifm_exp)
      ifm
      ifm_exp
      error('FORTRAN end-of-record marker is unexpected size')
   end

end % end of loop over bands

fclose(fid);

%%% end of function %%%

disp(' ')
disp(' ----------->>>>>>>>>>>>>>>>> ')
disp('now edit wrt_nte_le.m so as to set fnameOUT, and then run it!!!!')
disp(' ----------->>>>>>>>>>>>>>>>> ')
disp(' ')
