
function umebrowser(his)

%
% function umebrowser(his)
%
% browser for HIS data from a .ume file
%
% umebrowser.m is for browsing HIS specta loaded into MATLAB with umereader.m.
% Two figures are created: spectra in figure 1 and header information in 
% figure 2.  
% In figure 1, a gui in the lower right hand corner (created with editnum.m)
% is used to select a record number and that spectrum is displayed in the 
% plot panel.  The current record number and the measurement time are shown 
% in the title, which is color coded to match the spectrum.  Several blue 
% gui buttons are created down the right hand side of the figure using
% blowup.m.  These are for changing the axes limits of the plot.  As you go
% from one record to another, the previous axes limits are maintained.  Also
% in figure 1, two grey buttons in the lower left hand corner are created
% for "holding" the plots.  If "hold on" is pushed, spectra will not be
% erased as more records are plotted while if "hold off" is pushed, only the
% current record will be displayed.  A red button in the lower left hand
% corner is for quitting (both figures 1 and 2) umebrowser.m
%
% Figure 2 contains miscellaneous header information and is also updated
% whenever a new record is selected.  The holding information in figure 1
% is passed to figure 2.  Four plots on the left hand side of the figure 
% show 1) mean channel radiances, 2) black body temperature, 3) flags, and
% 4) DC levels and phase versus record number.  The currently selected 
% record are hlightlighted and color coded to match figure 1.  Two plots on
% the right hand side of the figure show the aircraft altitude versus time
% and aircraft latitude versus longitude along with a low resolution
% coastline map.  The axes limits of all 6 plots can be changed simply by 
% clicking in a plot and creating a rubber band box with the mouse button
% held down.  Click on the blue Default button in the upper right hand
% corner to return the currently selected plot to its original axes limits.
% Click on the red Quit button to quit (figure 2 only).
%
% calling sequence: "umebrowser(his)"
%
% input data is the structure "his" (which can be created with umereader.m)
%
% warnings/bugs: 1) can be very slow for large data sets
%                2) overwrites figures with handles 1 and 2 in the workspace
%                3) in the record selector gui, the record number index,
%                   not the actual record number, is shown.
%                4) a very annoying bug that I haven't figured out is 
%                   an error meesage that occurs whenever the x,y limits of
%                   a plot in figure 2 are not set to default when a new
%                   record is selected.  the error does not affect the results.
%                5) a problem with MATLAB is that sometimes y-axis labels
%                   are not displayed, or only partially displayed, on the screen.
%                6) ...
%
% author: Dave Tobin on 10/31/97.  email: dave.tobin@ssec.wisc.edu
%

warning off


%%%%%%%%%%%%%%%%%% set up spectra screen in figure 1 %%%%%%%%%%%%%%%%%%%%%%%%%%

% create a new figure and give it a name
figure(1);clf;
set(1,'Name',['U-Wisconsin HIS .ume file : ' his.filename '  screen 1'])
set(1,'Units','norm','Position',[0.28 0.38 0.70  0.55],'Color',[1 1 .9]/1.2);

% initialize with the first record
index = 1;
k=plot(his.freq,his.bt(index,:));
ind=find(his.freq >= his.wn1 & his.freq <= his.wn2);
ymaxval = max(max(his.bt(:,ind)));yminval = min(min(his.bt(:,ind)));
set(gca,'Position',[.13 .2 .775 .70]);grid on;
set(gca,'XLim',[his.wn1 his.wn2]);set(gca,'YLim',[yminval ymaxval]);
title(['record # ' num2str(his.record_number(index)) ' : ' datestr(dn(his.nss1970(index)),0) ' UTC']);
set(gca,'FontWeight','b');
co = get(gca,'ColorOrder');
l=length(co);coi=index-l*fix(index/l)+1;
set(k,'Color',co(coi,:));
xlabel('wavenumber [cm^{-1}]');ylabel('T_b [K]');
g=get(gca,'Title');set(g,'FontSize',12,'FontWeight','b','Color',co(coi,:));

% add blowup and hold buttons
blowup;holder

% create the editnum gui
p=[.75 .03 0.15 0.1];
cbk = 'index = get(gco,''Value'');umecallback';
h=editnum(1,1,length(his.record_number),1,'INT_SL_B','units','norm','pos',p,'callback',cbk);

% create quit button
cbkq1 = 'close(findobj(''Type'',''figure''));clear ans c co coi g h hflag hi i ind1 ind2 index l tag;';
cbkq2 = 'clear xextval yextval xminval xmaxval yminval ymaxval cbkq1 cbkq2';
q=uicontrol('Style','PushButton','Position',[0 0 .1 .05],'Units','norm','BackgroundColor',[.8 0 0],...
    'String','Quit','ForegroundColor',[.85 .85 .85],'Callback',[cbkq1 cbkq2]);


%%%%%%%%%%%%%%%%%% set up header screen in figure 2 %%%%%%%%%%%%%%%%%%%%%%%%%%

% create the figure and give it a name
figure(2);clf
set(2,'Name',['U-Wisconsin HIS .ume file : ' his.filename '  screen 2'])
set(2,'Units','norm','Position',[0.10 0.2 0.7  0.6],'Color',[1 1 .9]/1.2);

% display position/time data 
% plotting the time axis here uses the matlab function "datetick"
subplot(426)
plot(dn(his.nss1970),his.aircraft_alt/1e3,'r-');grid;datetick('x',15)
set(gca,'Position',[.64 .55 .3 .3],'FontWeight','b')
xlabel('time (UTC)');ylabel('altitude [ft/1e3]')
zoom on
set(gca,'Tag','6','Units','norm');

% display lat, lon data
subplot(428)
load coastlin 
plot(his.aircraft_lon,his.aircraft_lat,'r-',coastlines(1,:),coastlines(2,:),'k');grid
set(gca,'Position',[.59 .08 .4 .4],'FontWeight','b')
set(gca,'XLim',[min(his.aircraft_lon)-15 max(his.aircraft_lon)+15],...
    'YLim',[min(his.aircraft_lat)-15 max(his.aircraft_lat)+15]);
axis square
xlabel('longitude');ylabel('latitude')
zoom on
set(gca,'Tag','8','Units','norm');

% plot mean radiances versus time
% the channels selected here go along with whatever is defined as his.wnbr1
% and his.wnbr2 in umereader.m
subplot(421)
ind1 = find(his.freq >= his.wnbr1(1) & his.freq <= his.wnbr2(1));
ind2 = find(his.freq >= his.wnbr1(2) & his.freq <= his.wnbr2(2));
h = plot(his.record_number,mean(his.bt(:,ind1)'),'r-',...
         his.record_number,mean(his.bt(:,ind2)'),'b-');grid
set(gca,'Position',[.08 .75 .48 .2],'FontWeight','b','XTickLabel',[]);
ylabel('T_b [K]');
xlim=get(gca,'XLim');ylim = get(gca,'YLim');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.1,...
      [num2str(his.wnbr1(1)) '-' num2str(his.wnbr2(1)) ' cm^{-1}']);
set(h,'Color','r','Units','norm','FontWeight','b');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.2,...
      [num2str(his.wnbr1(2)) '-' num2str(his.wnbr2(2)) ' cm^{-1}']);
set(h,'Color','b','Units','norm','FontWeight','b');
zoom on
set(gca,'Tag','1','Units','norm');

% plot black body temperatures
subplot(423)
h = plot(his.record_number,his.hbb_temp,'r-',...
         his.record_number,his.cbb_temp,'b-');grid
set(gca,'Position',[.08 .53 .48 .2],'FontWeight','b','XTickLabel',[]);
ylabel('T_{BB} [C]');
ylim = get(gca,'YLim');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.1,'HBB');
set(h,'Color','r','Units','norm','FontWeight','b');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.2,'CBB');
set(h,'Color','b','Units','norm','FontWeight','b');
zoom on
set(gca,'Tag','3','Units','norm');

% plot misc flags
subplot(425)
h = plot(his.record_number,his.bad_flag,'r--',...
         his.record_number,his.noise_flag,'g:',...
	 his.record_number,his.qc_flag,'b-.');grid
set(h(2),'Color',[0 .7 0])
set(gca,'Position',[.08 .31 .48 .2],'FontWeight','b','XTickLabel',[]);
ylabel('flags [ ]');
ylim = get(gca,'YLim');ylim(1)=ylim(1)-0.2;ylim(2)=ylim(2)*1.1;
set(gca,'YLim',ylim);
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.1,'bad');set(h,'Color','r','Units','norm','FontWeight','b');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.2,'noise');set(h,'Color',[0 .7 0],'Units','norm','FontWeight','b');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.3,'quality');set(h,'Color','b','Units','norm','FontWeight','b');
zoom on
set(gca,'Tag','5','Units','norm');

% plot misc others: phase STD and DC levels
subplot(427)
h = plot(his.record_number,his.std_phase,'r',...
         his.record_number,his.dc_ch1,'g',...
         his.record_number,his.dc_ch2,'b',...
         his.record_number,his.dc_ch3,'k');grid
set(gca,'Position',[.08 .09 .48 .2],'FontWeight','b');
set(h(2),'Color',[0 .7 0])
ylim = get(gca,'YLim');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.1,'std phase');set(h,'Color','r','Units','norm','FontWeight','b');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.2,'DC ch1');set(h,'Color',[0 .7 0],'Units','norm','FontWeight','b');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.3,'DC ch2');set(h,'Color','b','Units','norm','FontWeight','b');
h=text(xlim(1)+diff(xlim)*0.1,ylim(1)+diff(ylim)*0.4,'DC ch3');set(h,'Color','k','Units','norm','FontWeight','b');
xlabel('record number')
zoom on;
set(gca,'Tag','7','Units','norm');

% create button for default axes limits and quit button
def = uicontrol('Style','Pushbutton','String','Default',...
                'Units','norm','Position',[.92 .91 .08 .04],'BackgroundColor',[.2 .6 .7], ...
                'Callback','axis auto','ForegroundColor',[.85 .85 .85],'Units','norm');
q=uicontrol('Style','PushButton','Position',[.92 .95 .08 .04],'Units','norm','BackgroundColor',[.8 0 0],...
    'String','Quit','ForegroundColor',[.85 .85 .85],'Callback','close(2)');

	    
% create two text gui buttons for record number and date/time
thand(1) = uicontrol('Style','Text','String',['record # ' num2str(his.record_number(1))],...
                  'Units','norm','Position',[.7 .92 .2 .03],'BackgroundColor',[1 1 .9]/1.2, ...
                  'FontWeight','b','FontSize',12,'ForegroundColor',co(coi,:),'Tag','t1','Units','norm');
thand(2) = uicontrol('Style','Text','String',[datestr(dn(his.nss1970(1)),0)],...
                  'Units','norm','Position',[.7 .87 .2 .03],'BackgroundColor',[1 1 .9]/1.2, ...
                  'FontWeight','b','FontSize',12,'ForegroundColor',co(coi,:),'Tag','t2','Units','norm');

% initialize with index 1
index = 1;umecallback
