
function his = umereader(file);

%
% function his = umereader(file);
%
% HIS .ume file reader (i.e. 100 word header files)
%
% calling sequence is i.e. "his = readall;"
%
% optional input is "file", the input .ume file name.
% 
% output is the structure "his" containing wavenumbers, the header,
% radiances, and brightness temperatures.  specific header fields are
% also defined.
%
% warnings/bugs: 1) for use w/ browser.m, 1900 is added to the year.
%                2) does almost no error checking
%
% author: Dave Tobin on 10/31/97.  email: dave.tobin@ssec.wisc.edu
%

warning off

% if filename is not specified, get from user
if nargin == 0 
  [fname,pname] = uigetfile('*.ume');
  file = [pname fname];
end
his.filename = file;

% Here are the guts of the program.  
% open the file, read the data, and close the file
fid = fopen(file,'r','l');
data = fread(fid,[2150, inf],'float32');
fclose(fid);

% get number of spectra and take transpose of data
nspec=size(data);nspec=nspec(2);
data = data';

% grab the 100 word header
header = data(1:nspec,1:100);
his.header = header;

npts = header(1,31);

% define some specific header data
if 1
  his.record_number = header(:,1);
  his.channel_number = header(:,2);
  his.mirror_position = header(:,3);
  his.no_pts_in_ifgm = header(:,8);
  his.seconds = header(:,11);
  his.hbb_temp = header(:,12);
  his.cbb_temp = header(:,13);
  his.dc_ch1 = header(:,14);
  his.dc_ch2 = header(:,15);
  his.dc_ch3 = header(:,16);
  his.year = header(:,17) + 1900;
  his.month = header(:,18);
  his.day = header(:,19);
  his.bad_flag = header(:,20);
  his.noise_flag = header(:,21);
  his.std_phase = header(:,22);
  his.begin_time = header(:,23);   % HHMMSS
  his.current_time = header(:,24); % HHMMSS
  his.qc_flag = header(:,25);      % 2 = spectrum o.k.
  his.no_pts_in_fft = header(:,30);
  his.no_pts_in_spectrum = header(:,31);
  his.aircraft_lat = header(:,51);
  his.aircraft_lon = header(:,52);
  his.aircraft_alt = header(:,53);
  his.aircraft_heading = header(:,54);
end

% grab the radiances
his.rad = data(1:nspec,101:101+npts-1);

% Create frequency scale.
low = header(1,34);
high = header(1,35);
his.freq = linspace(low,high,npts);

% compute brightness temperatures
if 1
  his.bt = his.rad*0;
  for i = 1:nspec;his.bt(i,:) = radtot(his.freq,his.rad(i,:)/1E3);end
end

% the rest of the parameters are for use w/ umebrowser.m
if 1
  % define min, max wavenumbers for regions of good data
  if his.channel_number(1) == 3
      his.wn1 = 600; his.wn2 = 1075;
    elseif his.channel_number(1) == 2
      his.wn1 = 1080;his.wn2=1800;
    elseif his.channel_number(1) == 1
      his.wn1 = 2050;his.wn2=2650;
  end

  % pull hour, minute, second from current_time (HHMMSS)
  his.hour = fix(his.current_time/1e4);
  his.minute = fix((his.current_time - 1e4*his.hour)/1e2);
  his.second =  fix(his.current_time - his.hour*1e4 - his.minute*1e2);

  % compute # seconds since 1970 (dont' ask why)
  his.nss1970 = nosecsince1970(his.year,his.month,his.day,his.hour,his.minute,his.second);

  % define wavenumber regions for radiance vs time
  if his.channel_number(1) == 3
    his.wnbr1(1) = 898;his.wnbr2(1) = 904;
    his.wnbr1(2) = 700;his.wnbr2(2) = 710;
  elseif his.channel_number(1) == 2
    his.wnbr1(1) = 1124;his.wnbr2(1) = 1132;
    his.wnbr1(2) = 1582;his.wnbr2(2) = 1588;
  elseif his.channel_number(1) == 1
    his.wnbr1(1) = 2384;his.wnbr2(1) = 2392;
    his.wnbr1(2) = 2460;his.wnbr2(2) = 2470;
end

end

