function  galookit(varargin)
% this is the original piece of "gautoedit", which started out as just
% a "lookit" program
%
%JH march 2001

if (nargin == 0)
   %initialize

   fighandle = findobj('Tag','galookit_fig');
   if (~isemptys(fighandle)) %then there is one already - BAD - can only have one
      error('only one galookit can run per matlab session')
   end
   
   unittype = 'points';
   AA.max_inches = 3;
   
   %AA is the whole structure
   %AA.h will be the handles;
   
   asetup;

   if (~isfield(config, 'prefix'))
      config.prefix = 'a_';
   end
   
   config.ddrange = 1;  %override for this
   aflagit_setup;

   
   %adding this for compatibility
   if (~isfield(pfc.cfg, 'underway'))
      pfc.cfg.underway = 100;
   end
   if (~isfield(pfc.cfg,'agility'))
      pfc.cfg.agility = 3;        
   end
   if (~isfield(pfc.cfg,'degperminute'))
      pfc.cfg.degperminute = 20;  
   end
   if (~isfield(pfc.cfg,'glitchfactor'))
      pfc.cfg.glitchfactor = 3;   
   end
   if (~isfield(pfc.cfg,'topglitchbin'))
      pfc.cfg.topglitchbin = 1;   
   end
   
   [junk, flagstruct] = aflagit;
   pfc.flag = flagstruct.flag;
   %%%%%%%%%%%%%%%%%%%%%%%%%
   
   %store the config info in AA (put in userdata at the bottom of initialization
   AA.config = config;
   AA.pfc = pfc;
   
   
   AA.start_dd_str = sprintf('%5.2f',config.start_dd);
   AA.ddrange_str = sprintf('%5.2f',config.ddrange);
   
   
   fighandle  = figure(...
                       'Color',[0.8 0.8 0.8], ...
                       'Tag','galookit', ...
                       'units',unittype,...
                       'ToolBar','none', ...
                       'menubar','none', ...
                       'color',[0 .9 .95], ...
                       'tag', 'galookit_fig',...
                       'position',round(AA.max_inches*[0 13 800 500]/11));
   AA.h.alookitfig = fighandle;
   
   % pixels starting in the lower left corner:  [right, up, width, height]
   
   AA.figpos = get(AA.h.alookitfig,'position');
   AA.figwidth = AA.figpos(3);
   AA.figheight = AA.figpos(4);
   
   AA.box_start = .1*AA.figwidth;
   AA.box_width = .3*AA.figwidth;
   AA.box_height = .15*AA.figheight;
   
   AA.label_start = AA.box_start + AA.box_width + .05*AA.figwidth;
   AA.label_width = .5*AA.figwidth;
   
   
   AA.buttonstart = .15*AA.figwidth;
   AA.buttonwidth = .3*AA.figwidth;
   AA.buttonheight = .2*AA.figheight;
   
   
   %%% decimal day start
   AA.h.start_dd_text = ...
       uicontrol(...
          'Parent',AA.h.alookitfig, ...
          'units',unittype,...
          'BackgroundColor',[1 1 1], ...
          'ListboxTop',0, ...
          'Position',...
          [AA.box_start .8*AA.figheight, AA.box_width, AA.box_height],...
          'Style','edit', ...
          'string', AA.start_dd_str,...
          'Tag','start_dd_box');
   
   
   AA.start_dd_label = ...
       uicontrol(...
          'Parent',AA.h.alookitfig, ...
          'units',unittype,...
          'ListboxTop',0, ...
          'BackgroundColor',.9*[1 1 1], ...               
          'Position',...
          [AA.label_start .8*AA.figheight, AA.label_width, .8*AA.box_height],...
          'Style','text', ...
          'string', 'decimal day start',...
          'fontweight', 'bold',...
          'Tag','start_dd_label');
   
   
   %%% decimal day range
   AA.h.ddrange_text = ...
       uicontrol(...
          'Parent',AA.h.alookitfig, ...
          'units',unittype,...
          'BackgroundColor',[1 1 1], ...
          'ListboxTop',0, ...
          'Position',...
          [AA.box_start .65*AA.figheight, AA.box_width, AA.box_height],...
          'Style','edit', ...
          'string', AA.ddrange_str,...
          'Tag','ddrange_box');
   
   
   AA.h.ddrange_label = ...
       uicontrol(...
          'Parent',AA.h.alookitfig, ...
          'units',unittype,...
          'ListboxTop',0, ...
          'BackgroundColor',.9*[1 1 1], ...               
          'Position',...
          [AA.label_start .65*AA.figheight AA.label_width, .8*AA.box_height],...
          'Style','text', ...
          'string', 'decimal day range',...
          'fontweight', 'bold',...
          'Tag','ddrange_label');
   
   
   %%% lookit now
   AA.h.nowbutton = ...
       uicontrol(...
          'Parent',AA.h.alookitfig, ...
          'units',unittype,...
          'Callback','galookit(''now'')',...
          'ListboxTop',0, ...
          'Position',...
          [AA.buttonstart .2*AA.figheight AA.buttonwidth AA.buttonheight],...
          'string', 'LOOKIT', ...
          'fontweight', 'bold',...
          'fontsize', 12);
   

   %%% lookit now
   AA.h.nowbutton = ...
       uicontrol(...
          'Parent',AA.h.alookitfig, ...
          'units',unittype,...
          'Callback','galookit(''next'')',...
          'ListboxTop',0, ...
          'Position',...
          [(AA.figwidth - AA.buttonstart - AA.buttonwidth) ...
           .2*AA.figheight AA.buttonwidth AA.buttonheight],...
          'string', 'NEXT...', ...
          'fontweight', 'bold',...
          'fontsize', 12);
   
   
   %last thing: store config in userdata
   set(fighandle, 'userdata', AA);
elseif (nargin > 1)
      error('must run galookit(''now'') or galookit(''next'') ')
else
   
   %find the object
   fighandle = findobj('Tag','galookit_fig');
   
   %get the userdata
   AA = get(fighandle,'userdata');
   
   %put config and pfc in environment
   config = AA.config
   pfc = AA.pfc;

   %override start_dd with value in box
   start_dd_str = get(AA.h.start_dd_text,'string');
   config.start_dd = str2num(start_dd_str);

   %override ddrange with value in box
   ddrange_str = get(AA.h.ddrange_text,'string');
   config.ddrange = str2num(ddrange_str);

   if strcmp(varargin{1},'next')
      %override start_dd: do the next one instead of this one
      config.start_dd = config.start_dd + config.ddrange;
   else
      %don't override start_dd; exit if argment wasn't right
      if (~strcmp(varargin{1},'now'))
         error('must run galookit(''now'') or galookit(''next'') ')
      end
   end


   AA.config = config; %update AA.config
   set(fighandle, 'userdata', AA);  %update userdata

   
   %now replicate run_alookit ------------------------------
   
   if (~exist('flagit', 'var'))
      flagit = 1;
   end
   %set up some graphing parameters for apuv.m
   topo_clev = [-9000:500:0];  % Topography contour levels.
   mvec_scale = 100;           %100cm/s per inch on paper
   
   [data, config] = agetmat(config);

   if (flagit)
      flagged = aflagit(data, config, pfc);
   else
      flagged = 0*data.umeas;
   end
   config.flagged = flagged;   %set flagit to zero to show all data
   

   base_fig = gcf;
   figure(base_fig + 1);

   %adding for compatibility
   userawnav = 0;
   AA.uvmin = -60;
   AA.uvmax = 60;
   AA.badrect = [];
   AA.badpoly = [];
   AA.badprof = [];
   AA.mabs = [];
   AA.config.os_cutoff = AA.pfc.cfg.wire_onstation;

   
   
   
   alookit;
   figure(base_fig + 3);
   apuv(data, config, 'pfc', pfc);
   disp(' ')

end


