function  gautoedit(varargin)

if (isempty(varargin))
   %start things up  ----------
   initialize;
elseif strcmp(varargin{1},'plot_next')
   plot_next;
elseif strcmp(varargin{1},'plot_now')
   plot_now;
elseif strcmp(varargin{1},'update_userdata')
   update_userdata;
else
   error('not a good argument')
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%                      functions                                    %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function initialize
   
   %initialize window with boxes

   fighandle = findobj('Tag','galookit_fig');
   if (~isempty(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;
   config.ddrange = 1;  %override for this
   aflagit_setup;

   %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);

% end of function   
   
%%%%%%%%%%%%%%%%%%%%% end first_init %%%%%%%%%%%%%%%%%%%%

function update_userdata
   % update userdata to reflect values in boxes

   %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 things in old userdata with stuff in boxes
   %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);
   
   AA.config = config; %update AA.config
   set(fighandle, 'userdata', AA);  %update userdata

%end of function   
   
%%%%%%%%%%%%%%%%%%%%  end of update %%%%%%%%%%%%%%%%%%%%

function plot_now

   %now replicate run_alookit ------------------------------

   gautoedit('update_userdata');
   
   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);
   alookit;
   figure(base_fig + 3);
   apuv(data, config, topo_clev, mvec_scale, pfc);
   disp(' ')

%end of function

%%%%%%%%%%%%%%%%%%%%% end plot_now  %%%%%%%%%%%%%%%%%%%%

function plot_next

   %update userdata with the stuff in the boxes
   gautoedit('update_userdata');
   
   %now reset config.start_dd
   fighandle = findobj('Tag','galookit_fig');
   AA = get(fighandle,'userdata');
   config.start_dd = config.start_dd + config.ddrange;
   % udate structure
   AA.config = config; 
   %update box
   AA.start_dd_str = sprintf('%5.2f',config.start_dd);
   set(AA.h.start_dd_text, 'string', AA.start_dd_str);
   %update userdata
   set(fighandle, 'userdata', AA);  %update userdata
   
   gautoedit('plot_now');

% end of function   
   
%%%%%%%%%%%%%%%%%%%% end plot_next %%%%%%%%%%%%%%%%%%%%
