function [AxisChoices] = dodsaxisdlg(mode, names, units, axvals, ...
    Title, mapchoices, colors, fontsize)

% DODSAXISDLG Axis dialog box for DODS Matlab GUI.

global axnames axunits ax mapnames dodsax_fontsize dodsaxbtn

if strcmp(mode,'resetaxes')
  val1 = get(dodsaxbtn(2),'value');
  val2 = get(dodsaxbtn(3),'value')-1;
  if val2 == 0;
    mapnames{val1} = '';
  else
    mapnames{val1} = axnames{val2};
  end
  % refresh the text

  str2 = sprintf('%s\n', ...
      [ 'Longitude: ', mapnames{1}], ...
      [ 'Latitude: ', mapnames{2}], ...
      [ 'Depth/Height: ', mapnames{3}], ...
      [ 'Time: ', mapnames{4}]);
  set(dodsaxbtn(1), 'string', cellstr(str2));
  drawnow

elseif strcmp(mode, 'start')
  axnames = names;
  axunits = units;
  ax = axvals;
  mapnames = mapchoices;
  
  if isempty(colors)
    colors = browse('getcolors');
  else
    if all(colors == 0) | ~all(size(colors) == [3 3])
      colors = browse('getcolors');
      colors = colors([6 1 9],:);
    end
  end
  
  if fontsize == 0
    fontsize = browse('getfontsize');
  end
  dodsax_fontsize = fontsize;
  
  % create the text
  str2 = '';
  for i = 1:length(axnames)
    str2 = [str2, sprintf('Axis: %s     Range: %g %g\nLength: %i   Units: %s\n\n', ...
	  axnames{i}, mminmax(ax{i}), length(ax{i}), axunits{i})];
  end
  str2=str2(1:length(str2)-1);
  
  str = sprintf('%s\n', ...
      'The axis maps for these data are:', ...
      str2, ...
      'Please double-check these automated interpretations', ...
      'and CORRECT THEM if neccessary:');
  
  str2 = sprintf('%s\n', ...
      [ 'Longitude: ', mapnames{1}], ...
      [ 'Latitude: ', mapnames{2}], ...
      [ 'Depth/Height: ', mapnames{3}], ...
      [ 'Time: ', mapnames{4}]);
  
  TextString1=cellstr(str);
  TextString2=cellstr(str2);
  
  %%%%%%%%%%%%%%%%%%%%%%%
  %%% Create OpenFig %%%
  %%%%%%%%%%%%%%%%%%%%%%%
  FigPos=get(0,'DefaultFigurePosition');
  FigWidth=75;FigHeight=45;
  FigPos(3:4)=[FigWidth FigHeight];
  OpenFig=dialog('Visible'         ,'off'           , ...
      'Name'            ,Title                      , ...
      'Pointer'         ,'arrow'                    , ...
      'Units'           ,'points'                   , ...
      'Position'        ,FigPos                     , ...
      'Color'           ,colors(2,:)                , ...
      'UserData'        ,0                          , ...
      'IntegerHandle'   ,'off'                      , ...
      'WindowStyle'     ,'normal'                   , ...
      'HandleVisibility','callback'                 , ...
      'Tag'             ,Title                        ...
      );
  %%%%%%%%%%%%%%%%%%%%%
  %%% Set Positions %%%
  %%%%%%%%%%%%%%%%%%%%%
  DefOffset=3;
  DefBtnWidth=40;
  
  NumButtons=2;
  BtnString{1} = 'Ok';
  BtnString{2} = 'Cancel';
  BtnHeight=20;
  BtnYOffset=DefOffset;
  BtnFontSize=browse('getfontsize');
  
  BtnWidth=DefBtnWidth;
  
  ExtControl=uicontrol(OpenFig   , ...
      'Style'    ,'pushbutton'     , ...
      'String'   ,' '              , ...
      'foregroundcolor',colors(1,:), ...
      'backgroundcolor',colors(2,:), ...
      'String'   ,' '              , ...
      'FontUnits','points'         , ...
      'FontSize' ,BtnFontSize   ...
      );
  
  for lp=1:NumButtons,
    set(ExtControl,'String',BtnString{lp});
    BtnExtent=get(ExtControl,'Extent');
    BtnWidth=max(BtnWidth,BtnExtent(3)+8);
  end % lp
  delete(ExtControl);
  
  MsgTxtXOffset=DefOffset;
  
  FigWidth=max(FigWidth,MsgTxtXOffset+NumButtons*(BtnWidth+2*DefOffset));
  FigPos(3)=FigWidth;
  set(OpenFig,'Position',FigPos);

  BtnXOffset = [DefOffset; 2*DefOffset+BtnWidth];
  
  MsgTxtYOffset=DefOffset+BtnYOffset+BtnHeight;
  MsgTxtWidth=FigWidth-DefOffset-MsgTxtXOffset;
  MsgTxtHeight=FigHeight-DefOffset-MsgTxtYOffset;
  CBString = 'uiresume(gcf)';
  for lp=1:NumButtons,
    ButtonTag=['Btn' num2str(lp)];
    BtnHandle(lp)=uicontrol(OpenFig, ...
	'Style','pushbutton', ...
	'Units','points', ...
	'Position', ...
        [BtnXOffset(lp) BtnYOffset BtnWidth BtnHeight], ...
	'CallBack', CBString, ...
	'String', BtnString{lp}, ...
	'foregroundcolor',colors(1,:), ...
	'backgroundcolor',colors(2,:), ...
	'HorizontalAlignment','center', ...
	'FontUnits','points', ...
	'FontSize',BtnFontSize , ...
	'Tag',ButtonTag);
    
  end
  
  MsgHandle=uicontrol(OpenFig            , ...
      'Style'              ,'text'         , ...
      'Units'              ,'points'       , ...
      'Position'           ,[MsgTxtXOffset      ...
	MsgTxtYOffset      ...
	0.95*MsgTxtWidth   ...
	MsgTxtHeight       ...
	]              , ...
      'String'             ,{' '}          , ...
      'foregroundcolor'    ,colors(1,:)    , ...
      'backgroundcolor'    ,colors(2,:)    , ...
      'Tag'                ,'TextString'     , ...
      'HorizontalAlignment','left'         , ...    
      'FontUnits'          ,'points'       , ...
      'FontWeight'         ,'bold'         , ...
      'FontSize'           ,BtnFontSize    );
  
  
  junk = TextString2{1};
  k = find(abs(junk) ~= 10);
  junk(k) = char(32);
  
  TextString = [TextString1{1}, junk];
  TextString = cellstr(TextString);
  [WrapString,NewMsgTxtPos]=textwrap(MsgHandle,TextString,75);
  
  NumLines=size(WrapString,1);
  
  % The +2 is to add some slop for the border of the control.
  MsgTxtWidth=max(MsgTxtWidth,NewMsgTxtPos(3)+2);
  MsgTxtHeight=NewMsgTxtPos(4)+2;
  
  MsgTxtXOffset=DefOffset;
  FigWidth=max(NumButtons*(BtnWidth+DefOffset)+DefOffset, ...
      MsgTxtXOffset+MsgTxtWidth+DefOffset);
% *** here  !
  MsgTxtYOffset=BtnYOffset+BtnHeight+DefOffset;
  FigHeight=MsgTxtYOffset+MsgTxtHeight+DefOffset;    
  BtnXOffset=[(FigWidth/3-BtnWidth/2); ...
    (2*FigWidth/3-BtnWidth/2)];
  
  ScreenUnits=get(0,'Units');
  set(0,'Units','points');
  ScreenSize=get(0,'ScreenSize');
  set(0,'Units',ScreenUnits);
  
  FigPos(1)=(ScreenSize(3)-FigWidth)/2;
  FigPos(2)=(ScreenSize(4)-FigHeight)/2;
  FigPos(3:4)=[FigWidth FigHeight];
  
  set(OpenFig ,'Position',[FigPos(1) FigPos(2)-100 FigPos(3) ...
	FigPos(4)+100]);
  
  BtnPos=get(BtnHandle,{'Position'});BtnPos=cat(1,BtnPos{:});
  BtnPos(:,1)=BtnXOffset;
  BtnPos=num2cell(BtnPos,2);  
  set(BtnHandle,{'Position'},BtnPos);  
  
  delete(MsgHandle);
  AxesHandle=axes('Parent',OpenFig, 'Position', ...
      [0 0.3 1 0.7],'Visible','off');
  t=text('Parent', AxesHandle, ...
      'Units', 'points',  ...
      'FontUnits', 'points',  ...
      'FontSize', BtnFontSize,  ...
      'HorizontalAlignment', 'left',  ...
      'VerticalAlignment', 'bottom',  ...
      'HandleVisibility', 'callback',  ...
      'Position', [MsgTxtXOffset MsgTxtYOffset 0], ...
      'String', TextString,  ...
      'Color', colors(1,:), ...
      'Interpreter', 'none', ...
      'Tag', 'TextString');
  % correct badly displaying stuff here: (dbyrne 02/01/04)
  set(AxesHandle,'units','points');
  q=get(AxesHandle,'pos'); 
  set(AxesHandle,'units','norm')
  r=get(t,'extent');
  p=q(4)-r(4);
  set(t,'pos',[MsgTxtXOffset p 0])
  dodsaxbtn(1) = text('Parent', AxesHandle, ...
      'Units', 'points',  ...
      'FontUnits', 'points',  ...
      'FontSize', BtnFontSize,  ...
      'HorizontalAlignment', 'left',  ...
      'VerticalAlignment', 'bottom',  ...
      'HandleVisibility', 'callback',  ...
      'Position', [MsgTxtXOffset MsgTxtYOffset 0], ...
      'String', TextString2,  ...
      'Color', colors(3,:), ...
      'fontweight','bold', ...
      'Interpreter', 'none', ...
      'Tag', 'TextString');
  set(dodsaxbtn(1),'vert','top','pos',[MsgTxtXOffset p 0])
  clear p q r t
  frame1pos = [0 0.1 1 0.15];
  uicontrol('parent',OpenFig,'style','frame', 'units', ...
      'norm','pos', frame1pos, ...
      'backgroundColor', colors(2,:))
  maps = [{'Longitude'};{'Latitude'};{'Depth'};{'Time'}];
  
  pop1pos = [frame1pos(1)+0.02 frame1pos(2)+frame1pos(4)/2-0.035 ...
	frame1pos(3)/2-0.06 0.08];
  pop2pos = [pop1pos(1)+pop1pos(3)+0.02 pop1pos(2:4)];
  
  dodsaxbtn(2) = uicontrol('parent',OpenFig,'style','popup', ...
      'units','norm', ...
      'pos', pop1pos, ...
      'string', maps, ...
      'fontsize', BtnFontSize, ...
      'foreground', colors(1,:), ...
      'background', colors(2,:));
  dodsaxbtn(3) = uicontrol('parent',OpenFig,'style','popup', ...
      'units','norm', ...
      'pos', pop2pos, ...
      'string', [{'none'}; axnames], ...
      'fontsize', BtnFontSize, ...
      'foreground', colors(1,:), ...
      'background', colors(2,:), ...
      'callback','dodsaxisdlg(''resetaxes'')');
	  
  set(findobj(OpenFig),'HandleVisibility','callback');
  set(OpenFig ,'WindowStyle','modal','Visible','on');
  drawnow;
  
  uiwait(OpenFig);
  
  TempHide=get(0,'ShowHiddenHandles');
  set(0,'ShowHiddenHandles','on');
  
  if any(get(0,'Children')==OpenFig),
    choice = get(get(OpenFig,'currentobject'),'string');
    if strcmp(choice,'Ok');
      AxisChoices = mapnames;
    else
      AxisChoices = [0];
    end
    delete(OpenFig);
  end
	  
  set(0,'ShowHiddenHandles',TempHide);
end