

% rnccallback.m

% update plot
if cflag == 1
  xv = get(xaxis,'value');
  yv = get(yaxis,'value');
  st = [];
  for i = 1:length(yv)
    x=names{xv};y=names{yv(i)};
    eval(['lx=length(aeri.' x ');']);
    eval(['ly=length(aeri.' y ');']);
    eval(['l1=length(find(isnan(aeri.' x ')==1));'])
    eval(['l2=length(find(isnan(aeri.' y ')==1));'])
    if lx == ly
      st = [st 'aeri.' x ',aeri.' y ','];
    end
    if l1 >= 1
      set(msg,'String',[num2str(l1) ' NaN''s in ' x])
    elseif l2 >= 1
      set(msg,'String',[num2str(l2) ' NaN''s in ' y])
    elseif lx ~= ly
      set(msg,'String',[x ' and ' y ' do not have the same number of elements'])
    else
      set(msg,'String','no messages')
    end
  end
  if ~isempty(st)
    eval(['h=plot(' st(1:length(st)-1) ');']);
    set(h,'Marker','.','MarkerSize',10);
    set(gca,'Fontweight','b')
    xlabel(x);ylabel(y)
    grid on
    set(get(gca,'Ylabel'),'Clipping','off')
    % add color coded curve labels
    ax = axis;
    for i = 1:length(yv)
      t=text(ax(1)+(ax(2)-ax(1))*0.05,ax(3)+(ax(4)-ax(3))*0.1*i,names{yv(i)});
      set(t,'Color',get(h(i),'Color'),'FontWeight','b','Units','norm');
    end
  end


% update x-axis editlist from x-axis text edit box
elseif cflag == 2
  st = get(xaxis2,'String');
  for i = 1:nvars
    z=names{i};
    if length(z) == length(st) & lower(z(1:length(st)))==lower(st)
      xv = i;set(xaxis,'Value',xv);set(xaxis2,'String',names{xv})
    end
  end  
  
% update y-axis editlist from y-axis text edit box
elseif cflag == 3
  st = get(yaxis2,'String');
  for i = 1:nvars
    z=names{i};
    if length(z) == length(st) & lower(z(1:length(st)))==lower(st)
      yv = i;set(yaxis,'Value',yv);set(yaxis2,'String',names{yv})
    end
  end  

% update x and y axis text edit boxes from axes editlists
elseif cflag == 4
  xv = get(xaxis,'value');
  yv = get(yaxis,'value');
  set(xaxis2,'String',names{xv});
  set(yaxis2,'String',names{yv(length(yv))});

% output section
elseif cflag == 5
  % get desired type of output
  otype = get(oub,'Value');
  % print postscript file
  if otype == 1
    figure('units','norm','pos',[0.05 .5 .25 .4],'Name','rncbrowser PS output screen')
    h1=uicontrol('Style','edit','pos',[.1 .75 .8 .1],'FontSize',14,...
	'units','norm','String','file.ps','back',[.85 .9 .85],'fore',[.2 .2 .2]);
    uicontrol('Style','Text','String','filename:','FontWeight','b',...
	'FontSize',10,'pos',[.1 .85 .8 .05],'units','norm','FontUnits','norm',...
	'back',get(gcf,'Color'),'fore',[.2 .2 .2]);
    h2 = uicontrol('style','popupmenu','units','norm','pos',[.25 .60 .5 .12],...
	'String','color|black+white','Value',1,'FontWeight','b','back',[.45 .65 .65]);
    h3 = uicontrol('style','popupmenu','units','norm','pos',[.25 .45 .5 .12],...
	'String','landscape|portrait|tall','Value',1,'FontWeight','b','back',[.45 .65 .65]);
    h4 = uicontrol('style','popupmenu','units','norm','pos',[.25 .30 .5 .12],...
	'String','.ps|.eps','Value',1,'FontWeight','b','back',[.45 .65 .65]);
    h5 = uicontrol('style','popupmenu','units','norm','pos',[.25 .15 .5 .12],...
	'String','standard|Level 2','Value',1,'FontWeight','b','back',[.45 .65 .65]);
    h6 = uicontrol('style','pushbutton','units','norm','pos',[.20 0.02 .25 .10],...
	'String','print','FontWeight','b','back',[0 .8 0],'Callback','cflag=8;rnccallback');
    h7 = uicontrol('style','pushbutton','units','norm','pos',[.55 0.02 .25 .10],...
	'String','cancel','FontWeight','b','back',[.8 0 0],'Callback',...
	'close;clear h1 h2 h3 h4 h5 h6 h7 pstring1 pstring0 filename');
  % print jpg file
  elseif otype == 2
    figure('units','norm','pos',[0.05 .5 .25 .4],'Name','rncbrowser JPEG output screen')
    uicontrol('Style','Text','String','filename:','FontWeight','b',...
	'FontSize',10,'pos',[.1 .85 .8 .05],'units','norm','FontUnits','norm',...
	'back',get(gcf,'Color'),'fore',[.2 .2 .2]);
    h1=uicontrol('Style','edit','pos',[.1 .75 .8 .1],'FontSize',14,...
	'units','norm','String','file.jpg','back',[.85 .9 .85],'fore',[.2 .2 .2]);
    uicontrol('Style','Text','String','quality level:','FontWeight','b',...
	'FontSize',10,'pos',[.2 .62 .6 .05],'units','norm','FontUnits','norm',...
	'back',get(gcf,'Color'),'fore',[.2 .2 .2]);    
    h2 = editnum(gcf,1,100,.1, 'INT_SL_B', 'units','norm',...
	'pos',[.2 .50 .6 .12], 'back',[.85 .9 .85]);
    set(h2,'Value',75,'String','75')
    h3 = uicontrol('style','popupmenu','units','norm','pos',[.25 .30 .5 .12],...
	'String','landscape|portrait|tall','Value',1,'FontWeight','b','back',[.45 .65 .65]);
    h4 = uicontrol('style','pushbutton','units','norm','pos',[.20 0.02 .25 .10],...
	'String','print','FontWeight','b','back',[0 .8 0],'Callback','cflag=9;rnccallback');
    h5 = uicontrol('style','pushbutton','units','norm','pos',[.55 0.02 .25 .10],...
	'String','cancel','FontWeight','b','back',[.8 0 0],'Callback',...
	'close;clear h1 h2 h3 h4 h5 pstring1 pstring0 filename');  

  elseif otype == 3
    % save x variable
    xv = get(xaxis,'value');x=names{xv};
    disp(['    saving x-axis variable: aeri.' x ' to ' x '.asc']);
    eval([x ' = aeri.' x ';save ' x '.asc ' x ' -ascii -double;clear ' x])
    % save y variables
    yv = get(yaxis,'value');
    for i = 1:length(yv)
      y = names{yv(i)};
      disp(['    saving y-axis variable: aeri.' y ' to ' y '.asc']);
      eval([y ' = aeri.' y ';save ' y '.asc ' y ' -ascii -double;clear ' y])
    end

  elseif otype == 4
    % save x variable
    xv = get(xaxis,'value');x=names{xv};
    disp(['    saving x-axis variable: aeri.' x ' to ' x '.mat']);
    eval([x ' = aeri.' x ';save ' x '.mat ' x ' ;clear ' x])
    % save y variables
    yv = get(yaxis,'value');
    for i = 1:length(yv)
      y = names{yv(i)};
      disp(['    saving y-axis variable: aeri.' y ' to ' y '.mat']);
      eval([y ' = aeri.' y ';save ' y '.mat ' y ' ;clear ' y])
    end

  elseif otype == 5
    %print out x variable
    xv = get(xaxis,'value');x=names{xv};
    disp(['x-axis variable: aeri.' x ' : ']);eval(['aeri.' x])
    disp(' ');disp('Hit any key to continue');pause;disp(' ')
    % print out y variables
    yv = get(yaxis,'value');
    for i = 1:length(yv)
      y=names{yv(i)};
      disp(['y-axis variable: aeri.' y ' : ']);eval(['aeri.' y])
      if i ~= length(yv);
	disp(' ');disp('Hit any key to continue');pause
      end
    end
  end  % on otype

% change the number of subplots
elseif cflag == 7
  % quit blowup and delte all axes with tag == screen1 
  blowup(4)
  delete(findobj('Type','axes','Parent',gcf));
  % get number of desired subplots from gui
  nplots = get(npb,'Value');
  % set up plots. only does 1-4 currently
  if nplots == 1
    set(gca,'Position',[.13 .33 .775 .57],'units','norm')
  elseif nplots == 2
    subplot(211);set(gca,'Position',[.13 .67 .775 .28],'units','norm')
    subplot(212);set(gca,'Position',[.13 .33 .775 .28],'units','norm')
  elseif nplots == 3
    subplot(311);set(gca,'Position',[.13 .77 .775 .18],'units','norm')
    subplot(312);set(gca,'Position',[.13 .55 .775 .18],'units','norm')
    subplot(313);set(gca,'Position',[.13 .33 .775 .18],'units','norm')
  elseif nplots == 4
    subplot(411);set(gca,'Position',[.13 .84 .775 .13],'units','norm')
    subplot(412);set(gca,'Position',[.13 .67 .775 .13],'units','norm')
    subplot(413);set(gca,'Position',[.13 .50 .775 .13],'units','norm')
    subplot(414);set(gca,'Position',[.13 .33 .775 .13],'units','norm')
  end
  % restart blowup
  blowup

% pull up PS print box
elseif cflag == 8
  filename = get(h1,'String');
  if get(h4,'Value') == 1 
    pstring1 = '-dps';
  else
    pstring1 = '-deps';
  end
  if get(h2,'Value') == 1
    pstring1 = [pstring1 'c'];
  end
  if get(h5,'Value') == 2
    pstring1 = [pstring1 '2'];
  end
  if get(h3,'Value') == 1
    pstring0 = ' orient landscape; ';
  elseif get(h3,'Value') == 2
    pstring0 = 'orient portrait; ';
  else
    pstring0 = 'orient tall; ';
  end
  close
  eval([pstring0 'print ' pstring1 ' ' filename])
  clear h1 h2 h3 h4 h5 h6 h7 pstring1 pstring0
  
% pull up JPEG print box
elseif cflag == 9
  filename = get(h1,'String');
  pstring1 = '-djpeg';
  pstring1 = [pstring1 get(h2,'String')];
  if get(h3,'Value') == 1
    pstring0 = ' orient landscape; ';
  elseif get(h3,'Value') == 2
    pstring0 = 'orient portrait; ';
  else
    pstring0 = 'orient tall; ';
  end
  close
  eval([pstring0 'print ' pstring1 ' ' filename])
  clear h1 h2 h3 h4 h5 pstring1 pstring0 filename

% update screen 2
elseif cflag == 10
  index = get(sc2,'Value');
  coi=index-length(co)*fix(index/length(co))+1;
  ax = axis;
  plot(aeri.wnum1,aeri.mean_rad(index,:),'Color',co(coi,:))
  set(gca,'Position',[.13 .20 .775 .70],'units','norm',...
      'FontWeight','b')
  axis(ax)
  grid on;xlabel('wavenumber');ylabel('Radiance')
  title([datestr(dn(aeri.time_offset(index) + aeri.base_time),0)])
  set(get(gca,'Title'),'Color',co(coi,:),'FontWeight','b','FontSize',14)
  
% quit screen 2
elseif cflag == 98
  clf
  close
  clear sc2 index co coi q ind cflag 

% quit screen 1
elseif cflag == 99
  clf
  close
  clear S ans ax cflag h i l lx ly msg names nvars pb plb prb sab st ...
      stype t v x xaxis xaxis2 xsave xv y yaxis yaxis2 yl ysave yv z ...
      qb frb npb nplots l1 l2 otype oub
end



