function plotmaster(fctn, varargin)
% Initial switchyard file (for gplotadcp, gplotfiles, & gplotconfig)

global XYDATA
global UVDATA
global VECFIG
global CONTFIG
global PANELFIG
global ALLFIG
evalin('base','global UVDATA')
evalin('base','global XYDATA')
evalin('base','global VECFIG')
evalin('base','global CONTFIG')
evalin('base','global PANELFIG')
evalin('base','global ALLFIG')

switch(fctn)
	case 'radioreset'           % from gplot  - primary radio buttons
       strarg = char(varargin(1));
       radioreset(strarg);
       
	case 'okcallback'           % #1 callback >> from gplot - btnOK
       ds = findobj(gcbf,'tag','Radiodata');
       getdata = get(ds,'value');  % if getdata = 0, use saved config (callconfig)
       if getdata,
          calldatafiles 
       else
          callconfig
       end
       
	case 'okcallback2'         % #2 callback >> from gplotfiles - btnOK

       tp = findobj(gcbf,'tag','txtboxinpath');
       fullfilepath = get(tp,'string');
       tpout = findobj(gcbf,'tag','txtboxoutpath');
       outpath = get(tpout,'string');
   
       if isempty(fullfilepath) | ~exist(fullfilepath,'file')
          msg = sprintf('\nInput data path/file incomplete \nor incorrect.');
          noticeOK(msg)
          return
       end
       if  isempty(outpath) | ~exist(outpath,'dir')
          noticeOK('Output path incomplete or incorrect.');
          return
       end         
       if  ~isempty(findstr('~',outpath))  
          noticeOK('Home path must be typed out.');
          return
       end

       if ALLFIG.plottype == '1' | ALLFIG.plottype == '2'
          if ~(strcmp(fullfilepath(end-5:end), 'xy.mat'))
               msg = sprintf('\nNote: an *xy.mat file was not selected?  \nError = Start over?');
               noticeOK(msg)
               uiwait
               return
          end
       else
          if ~(strcmp(fullfilepath(end-3:end), '.blk'))
               msg = sprintf('\nNote: a *.blk file was not selected?  \nError = Start over?');
               noticeOK(msg)
               uiwait
               return
          end
       end
       
       ALLFIG.outpath =  outpath;           %%%%
       ALLFIG.fullfilepath = fullfilepath;     %%%%
       
       switch (ALLFIG.plottype)
       case '1'
          VECFIG.outpath = outpath;
          VECFIG.savepath = outpath;
          VECFIG.loadpath = outpath;
          VECFIG.dbpath = [];
          VECFIG.xydatapath = fullfilepath;
          vectormaster('setup'); % to xy.mat
       case '2'
          CONTFIG.outpath = outpath;
          CONTFIG.savepath = outpath;
          CONTFIG.loadpath = outpath;
          CONTFIG.dbpath = [];
          CONTFIG.xydatapath = fullfilepath;
          contourmaster('setup'); % to xy.mat
       case '3'
          paneloutpath = outpath;
          PANELFIG.outpath = paneloutpath; 
          [ALLFIG.default_path, uifile] = fileparts(fullfilepath);
          ALLFIG.fileroot = uifile(1:end-2);  % chops 'dir' in dir.blk
          panelmaster('setup', ALLFIG.default_path); % to a????dir.blk 
       end
       
   case 'okcallback3'          %  #3 callback >>   from gplotconfig - btnOK
      global ALLFIG
      cp = findobj(gcbf,'tag','txtboxconfig');
      fullpath = get(cp,'string'); 
      ALLFIG.fullfilepath = fullpath;
      if exist(fullpath, 'file')
         if strcmp(ALLFIG.plottype, '1'), 
            [VECFIG.loadpath, VECFIG.loadfn] = fileparts(fullpath);
            vectormaster('configsetup',fullpath); 
         end;
         if strcmp(ALLFIG.plottype, '2'), 
            [CONTFIG.loadpath, CONTFIG.loadfn] = fileparts(fullpath);
            contourmaster('configsetup',fullpath); 
         end;  
         if strcmp(ALLFIG.plottype, '3'),   
            panelmaster('configsetup',fullpath); end; 
      else
         noticeOK('File not found, please try again');
      end
    
   case 'getcfgfile'       % from [...] on gplotconfig
      getfile(1)
      
   case 'getmatfile'       % from [...] on gplotfiles (first line)
      getfile(2)
      
   case 'getoutpath'       % from [...] on gplotfiles (second line)
      curdr = pwd;
      gcd
      outdr =  pwd;
      cd(curdr)
      cph = findobj(gcf,'tag','txtboxoutpath');
      set(cph,'string', outdr); 
      
 case 'help'                % from gplot >> btnHelp
      type gplot_info.txt
      noticeOK('Look in the Matlab console window')
       
end  
%%%%%%%%%%%%
   
function radioreset(n)

global ALLFIG
if str2num(n) < 5, ALLFIG.plottype = n; end;

vv = findobj(gcbf,'tag','Radiovector');
cc = findobj(gcbf,'tag','Radiocontour');
pg = findobj(gcbf,'tag','Radiopanel');

ds = findobj(gcbf,'tag','Radiodata');
cf = findobj(gcbf,'tag','Radioconfig');

switch(n)
   case '1'
      set(cc,'value',0);
      set(pg,'value',0);
      ALLFIG.pathname = 'vector';
      ALLFIG.defconfig = 'vec_cfg.mat';
   case '2'
      set(vv,'value',0);
      set(pg,'value',0);
      ALLFIG.pathname = 'contour';
      ALLFIG.defconfig = 'cont_cfg.mat';
   case '3'
      set(cc,'value',0);
      set(vv,'value',0);
      ALLFIG.pathname = 'adcpdb';
      ALLFIG.defconfig = 'pan_cfg.mat';
   % case 4 used to be web
   case '5'
      set(cf,'value',0);
   case '6'
      set(ds,'value',0);
   end
   
%%%%%%%%%%%%
   
function calldatafiles()

global VECFIG
global CONTFIG
global PANELFIG
global ALLFIG

close(gcf)
pause(0.2) % in order to assure resizing of next form
gplotfiles

tp = findobj(gcf,'tag','txtboxinpath');
tpout = findobj(gcf,'tag','txtboxoutpath');
curdir = pwd;
ptext = fullfile(curdir, ALLFIG.pathname, '*_xy.mat');

incruisedir = 0;
if exist(fullfile(curdir,'vector'),'dir') & exist(fullfile(curdir,'contour'),'dir') & exist(fullfile(curdir,'adcpdb'),'dir')
   incruisedir = 1;
end
if str2num(ALLFIG.plottype) == 3 & incruisedir == 0
   close(gcf)
   msg = sprintf('\nPanel plots must be run from \n(datasets) cruiseID directory.');
   msg = sprintf('%s\n\n>>cd  & start over', msg);
   noticeOK(msg)
   return
end

fromwebplotflag = 0;
if isfield(VECFIG, 'webplot') 
   if VECFIG.webplot == 1
      fromwebplotflag = 1;
      VECFIG.webplot = 0;
   end 
end
if isfield(CONTFIG, 'webflag') 
   if CONTFIG.webflag == 1
      fromwebplotflag = 1;
      CONTFIG.webflag = 0;
   end 
end

if incruisedir == 1
   set(tp,'string',ptext);
   set(tpout,'string', fileparts(ptext));
else
   set(tp,'string', fullfile(curdir, '*xy.mat'));
   set(tpout,'string',curdir);
end

if fromwebplotflag
   if strcmp(ALLFIG.plottype, '1')
      set(tp,'string', fullfile(ALLFIG.outpath, 'v_sect_xy.mat')); 
   else
      set(tp,'string', fullfile(ALLFIG.outpath, 'c_sect_xy.mat'));
   end
end
if isfield(ALLFIG, 'outpath')
   set(tpout,'string', ALLFIG.outpath);
else
   set(tpout,'string', pwd);
end 

if str2num(ALLFIG.plottype) == 3  % revise for panels
   lbl = findobj(gcf, 'tag', 'StaticText3');
   set(lbl, 'string', 'Enter path/file to adcp db (use *dir.blk)');
   set(tpout,'string', curdir);
   if exist(fullfile(curdir,'edit'),'dir')
      set(tpout,'string', fullfile(curdir,'edit'));
   end
   if incruisedir == 1  
      rootnm = ['a', curdir(end-3:end), 'dir.blk'];     
      defnm = fullfile(curdir, 'adcpdb', rootnm);
      if exist(defnm, 'file'), 
         set(tp, 'string', defnm); 
      else
         set(tp, 'string', fullfile(curdir, 'adcpdb', '*dir.blk'));
      end
   else
      if exist( '/home/moli4/datasets/','dir') 
         set(tp,'string','/home/moli4/datasets/*dir.blk');
      else
         set(tp, 'string', fullfile(pwd, 'adcpdb', '*dir.blk'));
      end
   end
end

%%%%%%%%%%%%
   
function callconfig()

global ALLFIG

close(gcf)
pause(0.2)
gplotconfig

curdir = pwd;
if ~isfield(ALLFIG, 'outpath'), ALLFIG.outpath = pwd; end;

if str2num(ALLFIG.plottype) == 1
   guess = fullfile(curdir, 'vector', '*cfg.mat');
   g = findstr(guess, 'vector');
   if length(g) > 1 | ~isempty(findstr(guess, 'contour')) | ~isempty(findstr(guess, 'edit'))
      guess = fullfile(curdir, '*cfg.mat'); 
   end;
elseif str2num(ALLFIG.plottype) == 2
   guess = fullfile(curdir, 'contour', '*cfg.mat');
   g = findstr(guess, 'contour');
   if length(g) > 1 | ~isempty(findstr(guess, 'vector')) | ~isempty(findstr(guess, 'edit'))
      guess = fullfile(curdir, '*cfg.mat'); 
   end;
else  % str2num(ALLFIG.plottype) == 3
   guess = fullfile(curdir, 'edit', '*cfg.mat');
end 
tp = findobj(gcf,'tag','txtboxconfig');
set(tp, 'string', guess)
set(tp,'HorizontalAlignment','right')

incruisedir = 0;
if exist(fullfile(curdir,'vector'),'dir') & exist(fullfile(curdir,'contour'),'dir') & exist(fullfile(curdir,'adcpdb'),'dir')
   incruisedir = 1;
end
if (str2num(ALLFIG.plottype) == 3 & incruisedir == 0)
   close(gcf)
   msg = sprintf('\nPanel plots must be run from \n(datasets) cruiseID directory.');
   msg = sprintf('%s\n\n>>cd  & start over', msg);
   noticeOK(msg)
   return
end

%%%%%%%%%%%%%

function getfile(index)
   global ALLFIG
   if index == 1 % [...] from config
      cph = findobj(gcf,'tag','txtboxconfig');
      cpstr = get(cph,'string');
      [uifile, uipath] = uigetfile(sprintf('%s', cpstr), 'Select a file of the form *cfg.mat', 200, 100);
   end
   if index == 2 % [...] from data, top line
      cph = findobj(gcf,'tag','txtboxinpath');
      cpstr = get(cph,'string');      
      if str2num(ALLFIG.plottype) == 3 
         [uifile, uipath] = uigetfile(sprintf('%s', cpstr), 'Select a file of the form *dir.blk', 200, 100);
      else
         [uifile, uipath] = uigetfile(sprintf('%s', cpstr), 'Select a file of the form *_xy.mat', 200, 100);
      end
   end
   
   if uipath ~= 0   % not cancelled 
      fullname = strcat(uipath, uifile);
      set(cph,'string', fullname);
   else
      set(cph, 'enable', 'on');  % keep avail. if cancelled & reset
      set(cph, 'string', cpstr);
   end
   
%%%%%%%%%%% EOF
