function panelmasternew(fctn, varargin)
%%% panel switchyard function
global XYDATA
global PANELFIG

switch(fctn)
   case 'setup'                    % from plotmaster - orig. call
      close(gcf);
      figpanelsetup;
   case 'configsetup'        % from plotmaster via gplotconfig
      close(gcf);
      configsetup(varargin);
   case 'mixedtypes'           % from gplotpanel - type popup
      vval = get(gcbo,'value');
      if vval == 16 
         mixedpanelssetup;     % cancel poss. so flag not set yet
      else
         PANELFIG.mixed = 0; % reset possible
         vbox{1} = '1';
         datefix(vbox); % redo the dates on the basis of current start dd
      end;
   case 'mixedok'
      readfigmixed;
   case 'datefix'                  % from gplotpanel - day edits
      datefix(varargin);
   case 'update'                 % from gplotpanel - update
      ddbeg = findobj(gcf,'tag','tboxbegdduse');
      begdd = get(ddbeg,'string');
      calbeg = findobj(gcf,'tag','tboxbegcaluse');
      begcal = get(calbeg,'string');
      ddend = findobj(gcf,'tag','tboxenddduse');
      enddd = get(ddend,'string');
      if ~isempty(begdd)
         datefix('1')
      elseif ~isempty(begcal)
         datefix('2')
      elseif ~isempty(enddd)
         datefix('3')
      else
         datefix('4')
      end
      if PANELFIG.lock == 0 % update print file name box
         fnh = findobj(gcf,'tag','Popuppaneltype');
         ptval = get(fnh,'value');
         PANELFIG.popupvalue = ptval;
         if ptval ~= 16
            ptstr = get(fnh,'string');
            ptype = ptstr{ptval};
            fn = getname(ptype); % local function
            PANELFIG.order = [];
            for n = 1:str2num(PANELFIG.panelsppg)
               PANELFIG.order{n} = fn;
            end
            fileprefix = PANELFIG.order{1};
         else
            fileprefix = 'mix';
         end
         PANELFIG.printfilefn = sprintf('%s%03d.ps', fileprefix, round(str2num(PANELFIG.begdd)));
         pfn = findobj(gcf,'tag','txtboxprintfn');
         set(pfn,'string',PANELFIG.printfilefn);
      end
   case 'xytsection'             % from gplotpanel - btnXYTzoom
      xytsection;
   case 'readfigpanel'	       % from gplotpanel - btnOK (update & preview)
      panelmaster('update')
      readfigpanel;      
   case 'calendar'               % from gplotpanel - chkcalendar
      fixtitle;
   case 'decimal'                % from gplotpanel- chkdecimal
      fixtitle;
   case 'savenow'               % from gplotpanel - btnSave
      cnffs = findobj(gcf,'tag','txtboxsavefn');
      fname = get(cnffs,'string');
      PANELFIG.savefn = fname;
      fname = strcat(PANELFIG.outpath, fname);
      save6(fname, 'PANELFIG'); 
      notice(' Finished ');
   case 'loadnow'                % from gplotpanel - btnLoad
      loadnow(0); 
   case 'printfile'
      ckp = findobj(gcf,'tag','chkboxprint');
      if get(ckp,'value')  % may be diabling instead of activating
         pfn = findobj(gcf,'tag','txtboxprintfn');
         PANELFIG.printfilefn = get(pfn,'string');
         if isempty(PANELFIG.printfilefn)
            noticeOK('File name undefined')
            return
         end
      end
   case 'lock'
      lkp = findobj(gcf,'tag','chklock');
      if get(lkp,'value')  % may be diabling instead of activating
         PANELFIG.lock = 1;
      else
         PANELFIG.lock = 0;
      end
   case 'cancel'
      close
      gplot
   end

%%%%%%%%%%%

function figpanelsetup

global PANELFIG
global XYDATA
global ALLFIG
  
% Load data
[PANELFIG.dbpathonly, PANELFIG.dbname] = fileparts(PANELFIG.dbpath);
if strcmp(PANELFIG.dbname(end-2:end), 'dir'), PANELFIG.dbname = PANELFIG.dbname(1:end-3); end
% PANELFIG.dbname = [PANELFIG.dbpath, ALLFIG.fileroot(1:end-1)];
% e.g. PANELFIG.dbpath = /home/moli4/datasets/cruiseID/adcpdb/
msg = sprintf('\n Do you want to retrieve prelim. data from an *xy.mat file? ("Yes")');
msg = sprintf('%s\n or, "No", I want to enter manually', msg);
PANELFIG.xyselect = select(msg, 'Yes', 'No');    

marks = findstr(PANELFIG.dbpathonly, '/');
default_path = PANELFIG.dbpathonly(1:marks(end)) % e.g. to cruiseID

if PANELFIG.xyselect  % = use *.mat file
   XYDATA.xyt = [];
   default_path = [default_path, '*xy.mat'];
   while isempty(XYDATA.xyt) 
      [uifile, uipath] = uigetfile(sprintf('%s', default_path),...
         'Please select a file of the form *_xy.mat'); 
      if (uifile~=0)
         troubles = 0;
         PANELFIG.xydatapath = fullfile(uipath, uifile);
         eval('XYDATA = load(PANELFIG.xydatapath);', 'troubles = 1')
         if troubles
            noticeOK('Trouble loading XYDATA')
            return
         end
%          [fpath,fname,fext,fver] = fileparts(uifile) ;
%          rootname = uifile(1:end-3);
%          eval(strcat('XYDATA = load(',char(39),uipath, rootname,'_xy.mat',char(39),');'));
      else 
         yn = select(' Do you wish to Cancel process? ', 'Yes','No');
         if yn == 1
            close
            gplot
            return
         end
      end  
   end % while: select files
   PANELFIG.default_path = default_path;
   XYDATA.default_path = default_path;
%    PANELFIG.rootname = rootname;
   skipdatacrunch = 0;
else % = enter minimal xy data manually (cruise root dir identified)
   status_ok = 1;
   while status_ok
      status_ok = 0; % hopefully out after 1 lap
      prompt = {'First, Enter db name'; 'Second, Enter year_base';'Third, Enter beg & end decimal days'};
      title = 'Fill in each element';
      if ~strcmp(default_path(end), '/'), default_path = [default_path,'/']; end;
%       dbsl = findstr(default_path,'/');
%       if isempty(str2num(default_path(end))) % not in cruise dir
%          dbend = default_path(dbsl(end-2)+3:dbsl(end-1)-1);
%          db = strcat(default_path, '../adcpdb/a', dbend);
%       else  % in cruise dir
%          dbend = default_path(dbsl(end-1)+3:dbsl(end)-1);
%          db = strcat(default_path, '/adcpdb/a', dbend);
%       end
      db = pwd;

      def = {db; '[2001]'; '[1 365]'};
      acell = getdatadlg(prompt, title,1,def);
      if isempty(acell), return; end;
      flds = {'dbname'; 'year_base'; 'dday'};
      XYDATA = cell2struct(acell, flds, 1); % convert then fix
      XYDATA.year_base = str2num(XYDATA.year_base);
      XYDATA.dday = str2num(XYDATA.dday);
      dbsl = findstr(XYDATA.dbname,'/');
      PANELFIG.dbpath = XYDATA.dbname(1:dbsl(end));
      if ~exist([XYDATA.dbname,'dir.blk'], 'file')
         noticeOK('Database not found, please start over. ');
         status_ok = 1;
      end
%       rootname = XYDATA.dbname(dbsl(end)+1:end);
%       PANELFIG.rootname = rootname;
      skipdatacrunch = 1;
   end
end

PANELFIG.default_path = default_path;
XYDATA.default_path = default_path;
if isfield(XYDATA,'year_base')
   PANELFIG.year_base = XYDATA.year_base;
else
   PANELFIG.year_base = '???';
end
%PANELFIG.dbname = XYDATA.dbname;

gplotpanels;  % display main panel dialog window

% check or reset a few of the items
df = findobj(gcf,'tag','textdata');
if skipdatacrunch
   set(df,'string','  manual');
else
   set(df,'string',uifile);
end
dbh = findobj(gcf,'tag','txtboxdb');
set(dbh,'string',[PANELFIG.dbname, 'dir.blk']);
set(dbh,'HorizontalAlignment','left');
dpp = findobj(gcf,'tag','txtboxdaysperp');
set(dpp,'string','1');
ppp = findobj(gcbf,'tag','txtboxpppg');
set(ppp,'string','4');
yb = findobj(gcf,'tag','textyear');
set(yb,'string',num2str(PANELFIG.year_base));
cnffs = findobj(gcf,'tag','txtboxsavefn');
set(cnffs,'string','p_cfg.mat');
dstr = findobj(gcf,'tag','dynotextoutpath');
if length(PANELFIG.outpath)>36
   set(dstr,'string',PANELFIG.outpath(end-34:end));
else
   set(dstr,'string',PANELFIG.outpath);
end
opl = findobj(gcf,'tag','txtboxloadpath');
set(opl,'string',PANELFIG.outpath);
cnffl = findobj(gcf,'tag','txtboxloadfn');
set(cnffl,'string','p_cfg.mat');
tbt = findobj(gcf,'tag','txtboxtitle');
pp = PANELFIG.default_path;
marks = findstr(pp,'/');
PANELFIG.cruiseID = upper(pp(marks(end-2)+1:marks(end-1)-1));
if length(PANELFIG.cruiseID) == 6 & ~isletter(PANELFIG.cruiseID(end)) 
   PANELFIG.title = PANELFIG.cruiseID;
else
   pp = what;
   marks = findstr(pp.path,'/');
   PANELFIG.cruiseID = upper(pp.path(marks(end)+1:end));
   if length(PANELFIG.cruiseID) == 6 & ~isletter(PANELFIG.cruiseID(end))
      PANELFIG.title = PANELFIG.cruiseID;
   else
      PANELFIG.title = 'panel';
   end
end;
set(tbt,'string',PANELFIG.title);

% preliminary crunching on loaded data
if ~skipdatacrunch
   % preparedata  --- not avail., no uv
   XYDATA.x = unwrap_lon(XYDATA.xyt(1,:)');    %Nx1 vectors
   XYDATA.y = XYDATA.xyt(2,:)';                    %Nx1 vectors
   XYDATA.dday = XYDATA.xyt(3,:)';               %Nx1 vectors
   
   badi=find(isnan(XYDATA.x) | isnan(XYDATA.y) | isnan(XYDATA.dday));
   XYDATA.x(badi)=[];
   XYDATA.y(badi)=[];
   XYDATA.dday(badi)=[];
   
   % have full data set ready in case "sections" not selected or reverted, however
   % various sub-programs are likely to balk if "sections" not used (data set too large)
   XYDATA.X = XYDATA.x;
   XYDATA.Y = XYDATA.y;
else
   msg = sprintf('\nNote: Graphic selection of \ndata (xytzoom) is Not \navailable.');
   notice(msg, 2); % Note: if 'noticeOk(msg)' used, "gcf", is lost ! 
   bxy = findobj(gcf, 'tag', 'btnXYTzoom');
   set(bxy, 'enable', 'off');
end

PANELFIG.ii = [];     
XYDATA.DDAY = XYDATA.dday;
%determine_ranges;
dbeg = findobj(gcf,'tag','tboxbegddposs');
PANELFIG.begddposs = sprintf('%.2f',min(XYDATA.DDAY));
set(dbeg,'string',PANELFIG.begddposs);
cal = to_date(PANELFIG.year_base, min(XYDATA.DDAY),'s');
PANELFIG.begcalposs = cal(1:10);
calbeg = findobj(gcf,'tag','tboxbegcalposs');
set(calbeg,'string',PANELFIG.begcalposs);

ddend = findobj(gcf,'tag','tboxendddposs');
PANELFIG.endddposs = sprintf('%.2f',max(XYDATA.DDAY));
set(ddend,'string',PANELFIG.endddposs);
cal = to_date(PANELFIG.year_base, max(XYDATA.DDAY),'s');
PANELFIG.endcalposs = cal(1:10);
calend = findobj(gcf,'tag','tboxendcalposs');
set(calend,'string',PANELFIG.endcalposs);

PANELFIG.begdd = [];
PANELFIG.enddd = [];
PANELFIG.begcal = [];
PANELFIG.endcal = [];
PANELFIG.order{1} = 'pg'; PANELFIG.order{2} = 'pg';
PANELFIG.mixed = 0; % flag for mixed types of panels
PANELFIG.popupvalue = 1;
fn = getname('pg'); % initializes PANELFIG.titlelist
PANELFIG.printfile = 0;
PANELFIG.blkwht = 0;
PANELFIG.printfilefn = 'pg001.ps'; %PANELFIG.title;
pfn = findobj(gcf,'tag','txtboxprintfn');
set(pfn,'string',PANELFIG.printfilefn);
PANELFIG.lock = 0;
PANELFIG.flagged = 0;
fixtitle;

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

function configsetup(filename)

%global UVDATA
global XYDATA
global PANELFIG

fname = char(filename(1)); % cell to str
load(fname)
slashes = findstr(fname, '/');
PANELFIG.loadfn = fname(slashes(end)+1:end);
PANELFIG.loadpath = fname(1:slashes(end));
if PANELFIG.xyselect == 1
   rootname = PANELFIG.rootname;
   inpath = PANELFIG.default_path;
   eval(strcat('XYDATA = load(',char(39),inpath, rootname,'_xy.mat',char(39),');'));
   
   % DO THE PRELIMINARY DATA CRUNCHING
   preparedata
%    XYDATA.x = unwrap_lon(XYDATA.xyt(1,:)');                    %Nx1 vectors
%    XYDATA.y = XYDATA.xyt(2,:)';                    %Nx1 vectors
%    XYDATA.dday = XYDATA.xyt(3,:)';                 %Nx1 vectors
%    
%    badi=find(isnan(XYDATA.x));
%    XYDATA.x(badi)=[];XYDATA.y(badi)=[];XYDATA.dday(badi)=[];
%    % have full data set ready in case sections not selected or reverted
%    XYDATA.X = XYDATA.x;
%    XYDATA.Y = XYDATA.y;
%    XYDATA.Z = XYDATA.z;
%    XYDATA.ZC = XYDATA.zc;
%    XYDATA.DDAY = XYDATA.dday;
end  % otherwise database information is already on hand

gplotpanels

tbt = findobj(gcf,'tag','txtboxtitle');
set(tbt,'string',PANELFIG.title);  % must be before datefix()

ddbeg = findobj(gcf,'tag','tboxbegddposs');
set(ddbeg,'string',PANELFIG.begddposs);
calbeg = findobj(gcf,'tag','tboxbegcalposs');
set(calbeg,'string',PANELFIG.begcalposs);

ddend = findobj(gcf,'tag','tboxendddposs');
set(ddend,'string',PANELFIG.endddposs);
calend = findobj(gcf,'tag','tboxendcalposs');
set(calend,'string',PANELFIG.endcalposs);

ddbeg = findobj(gcf,'tag','tboxbegdduse');
set(ddbeg,'string',PANELFIG.begdd);
calbeg = findobj(gcf,'tag','tboxbegcaluse');
set(calbeg,'string',PANELFIG.begcal);

ddend = findobj(gcf,'tag','tboxenddduse');
set(ddend,'string',PANELFIG.enddd);
calend = findobj(gcf,'tag','tboxendcaluse');
set(calend,'string',PANELFIG.endcal);

fnh = findobj(gcf,'tag','Popuppaneltype');
set(fnh,'string', PANELFIG.pulldownlist); % list defined in gplotpanels.m
set(fnh,'value', PANELFIG.popupvalue);
dpp = findobj(gcf,'tag','txtboxdaysperp');
set(dpp,'string',PANELFIG.daysperpanel);
ppp = findobj(gcf,'tag','txtboxpppg');
set(ppp,'string',PANELFIG.panelsppg);
npp = findobj(gcf,'tag','txtboxnopgs');
set(npp,'string',PANELFIG.nopgs);
lbl = findobj(gcf,'tag','lbldays');
msg = sprintf('4) Total No. of days = %s', PANELFIG.totdays);
set(lbl,'string', msg);

datefix({'3'});

XYDATA.default_path = PANELFIG.default_path;
XYDATA.year_base = PANELFIG.year_base;
XYDATA.dbname = PANELFIG.dbname;

loadnow(1)  % 1=continue loading with new data, skip prelims

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

function loadnow(skip)
% retrieval and re-establishment of previous dataset
global PANELFIG
global XYDATA

if isempty(PANELFIG.ii)
   msg = sprintf('Data set (times) must be \nselected before loading config.');
   noticeOK(msg);
   return
end

if ~skip   % normal load doesn't skip; skip if initializing with config.
   % save some new data vbls before overwritten
   cID = PANELFIG.cruiseID;
   ttl = PANELFIG.title;
   iinow = PANELFIG.ii;
   opath = PANELFIG.outpath;
   dbp = PANELFIG.dbpath;
   mxd = PANELFIG.mixed;
   dfp = PANELFIG.default_path;
   yrb = PANELFIG.year_base;
   dbn = PANELFIG.dbname;
   
   ptx = findobj(gcf,'tag','txtboxloadpath');
   ldpath = get(ptx,'string');
   cnffl = findobj(gcf,'tag','txtboxloadfn');
   fname1 = get(cnffl,'string');
   fname = strcat(ldpath, fname1);
   
   goodload = 1;
   eval('load(fname);', 'goodload = 0;');
   if ~goodload
      noticeOK('Unable to load requested file.  ');
      return
   end
   
   PANELFIG.loadfn = fname1;
   PANELFIG.loadpath = ldpath;
   
   % re-establish current data vbls
   PANELFIG.default_path = dfp;
   PANELFIG.year_base = yrb;
   PANELFIG.dbname = dbn;
   PANELFIG.dbpath = dbp;
   PANELFIG.cruiseID = cID;
   PANELFIG.title = ttl;
   PANELFIG.ii = iinow;
   PANELFIG.outpath = opath;
   PANELFIG.mixed = mxd;
end

% re-establish new layout
cnffs = findobj(gcf,'tag','txtboxsavefn');
set(cnffs,'string', PANELFIG.savefn);
dstr = findobj(gcf,'tag','dynotextoutpath');
if length(PANELFIG.outpath)>36
   set(dstr,'string',PANELFIG.outpath(end-34:end));
else
   set(dstr,'string',PANELFIG.outpath);
end
cnffl = findobj(gcf,'tag','txtboxloadfn');
set(cnffl,'string',PANELFIG.loadfn);
ptx = findobj(gcf,'tag','txtboxloadpath');
set(ptx,'string',PANELFIG.loadpath);
yb = findobj(gcf,'tag','textyear');
set(yb,'string',num2str(PANELFIG.year_base));
df = findobj(gcf,'tag','textdata');
set(df,'string','custom load');
tbl = findobj(gcf,'tag','txtboxdb');
set(tbl,'string',PANELFIG.dbname(end-12:end));
di = findobj(gcf,'tag','chkboxdate');
set(di,'value',PANELFIG.datestamp);
omt = findobj(gcf,'tag','chktitle');
set(omt,'value',PANELFIG.omit_title);
bdh = findobj(gcf,'tag','chkbold');
set(bdh,'value',PANELFIG.bold);
tbc = findobj(gcf,'tag','chkcolor');
set(tbc,'value',PANELFIG.blkwht);
tbr = findobj(gcf,'tag','chkresolution');
set(tbr,'value',PANELFIG.resolution);
pfh = findobj(gcf,'tag','chkboxprint');
set(pfh,'value',PANELFIG.printfile);
crs = findobj(gcf,'tag','txtboxprintfn');
set(crs,'string',PANELFIG.printfilefn);
lbr = findobj(gcf,'tag','chklock');
set(lbr,'value',PANELFIG.lock);
pfh = findobj(gcf,'tag','chkflag');
set(pfh,'value',PANELFIG.flagged);
dpp = findobj(gcf,'tag','txtboxdaysperp');
set(dpp,'string',PANELFIG.daysperpanel);
ppp = findobj(gcf,'tag','txtboxpppg');
set(ppp,'string',PANELFIG.panelsppg);
npp = findobj(gcf,'tag','txtboxnopgs');
set(npp,'string',PANELFIG.nopgs);
lbl = findobj(gcf,'tag','lbldays');
msg = sprintf('4) Total No. of days = %s', PANELFIG.totdays);
set(lbl,'string', msg);

ch = findobj(gcf,'tag','chkcalendar');
dh = findobj(gcf,'tag','chkdecimal');
set(ch,'value',PANELFIG.titlebtnstatus(1));
set(dh,'value',PANELFIG.titlebtnstatus(2));
fixtitle

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

function xytsection
% this simply obtains the new data set from xytzoom

global XYDATA
global PANELFIG

if ~isempty(PANELFIG.ii) & PANELFIG.ii(end) <= length(XYDATA.x) 
   PANELFIG.ii = gxytzoom2(XYDATA.x, XYDATA.y, XYDATA.dday, PANELFIG.ii);
else
   PANELFIG.ii = gxytzoom2(XYDATA.x, XYDATA.y, XYDATA.dday);
end
% Use only the relevant indices
if isempty(PANELFIG.ii), return; end;
XYDATA.DDAY=XYDATA.dday(PANELFIG.ii);

% attempt adjustment of panels and days to match selection
days = round(XYDATA.DDAY(end)-XYDATA.DDAY(1));
if days == 0, days = 1; end;

PANELFIG.nopgs = '1';
if PANELFIG.mixed == 0 % panels/page not fixed
   if days <= 5
      PANELFIG.daysperpanel = '1';
      PANELFIG.panelsppg = num2str(days);
   else
      if mod(days,2) == 1, days = days -1; end % make even
      PANELFIG.daysperpanel = '2';
      PANELFIG.panelsppg = num2str(days/2);
      if days/2 > 5
         PANELFIG.daysperpanel = '1';
         PANELFIG.panelsppg = '4';
         PANELFIG.nopgs = num2str(round(days/4));
      end 
   end
else % panels/page = no. of various types selected
   PANELFIG.daysperpanel = '1';
   PANELFIG.nopgs = num2str(days);
   if days > 5
      if mod(days,2) == 1, days = days -1; end % make even
         PANELFIG.daysperpanel = '2';
         PANELFIG.nopgs = num2str(days/2);
      end
end

dpp = findobj(gcf,'tag','txtboxdaysperp');
set(dpp,'string',PANELFIG.daysperpanel);
ppp = findobj(gcf,'tag','txtboxpppg');
set(ppp,'string',PANELFIG.panelsppg);
npp = findobj(gcf,'tag','txtboxnopgs');
set(npp,'string',PANELFIG.nopgs);

PANELFIG.begdd = num2str(sprintf('%.2f',(XYDATA.DDAY(1))));
ddbg = findobj(gcf,'tag','tboxbegdduse');
set(ddbg,'string', PANELFIG.begdd);
datefix({'1'});

fixtitle;
if days > 10
   msg = sprintf('Days = %d; re-check panels/pgs.  ', days);
   noticeOK(msg); 
end

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

function readfigpanel

notice('Working ...',1.0);
global XYDATA
global PANELFIG

% retreive some of the latest dialog data
yb = findobj(gcf,'tag','textyear');
PANELFIG.year_base = str2num(get(yb,'string'));
tbt = findobj(gcf,'tag','txtboxtitle');
PANELFIG.title = get(tbt,'string');
hst = findobj(gcf,'tag','chktitle');
PANELFIG.omit_title = get(hst,'value');
% olh = findobj(gcf,'tag','txtboxdb');
% PANELFIG.dbname = get(olh,'string');
hbb = findobj(gcf,'tag','chkbold');
PANELFIG.bold = get(hbb,'value');
tbl = findobj(gcf,'tag','chkcolor');
PANELFIG.blkwht = get(tbl,'value');
tbr = findobj(gcf,'tag','chkresolution');
PANELFIG.resolution = get(tbr,'value');
dpp = findobj(gcf,'tag','txtboxdaysperp');
PANELFIG.daysperpanel = get(dpp,'string');
ppp = findobj(gcf,'tag','txtboxpppg');
PANELFIG.panelsppg = get(ppp,'string');
npp = findobj(gcf,'tag','txtboxnopgs');
PANELFIG.nopgs = get(npp,'string');
nb = findobj(gcf,'tag','txtboxbins');
PANELFIG.nbins = get(nb,'string');
ddbeg = findobj(gcf,'tag','tboxbegdduse');
PANELFIG.begdd = get(ddbeg,'string');
ddend = findobj(gcf,'tag','tboxenddduse');
PANELFIG.enddd = get(ddend,'string');
di = findobj(gcf,'tag','chkboxdate');
PANELFIG.datestamp = get(di,'value');
cbp = findobj(gcf,'tag','chkboxprint');
PANELFIG.printfile = get(cbp,'value');
pfn = findobj(gcf,'tag','txtboxprintfn');
PANELFIG.printfilefn = get(pfn,'string');
flg = findobj(gcf,'tag','chkflag');
PANELFIG.flagged = get(flg,'value');

%gpanel; % call to perform actual plotting
%%%%%%%%% NEW SECTION
fnh = findobj(gcf,'tag','Popuppaneltype');
ptval = get(fnh,'value');
PANELFIG.popupvalue = ptval;
if ptval ~= 16
   ptstr = get(fnh,'string');
   ptype = ptstr{ptval};
   fn = getname(ptype); % local function
   PANELFIG.order = [];
   for n = 1:str2num(PANELFIG.panelsppg)
      PANELFIG.order{n} = fn;
   end
end

% %set these parameters in "config"
config.start_dd       =       str2num(PANELFIG.begdd);
config.end_dd        =       str2num(PANELFIG.enddd);
config.ddrange       =      config.end_dd - config.start_dd;
config.yearbase     =       PANELFIG.year_base;
config.comment      =       [] ;
config.comment2    =       [] ;
config.astitle          =       1;
config.dbpath         =       PANELFIG.dbpath; %default_path;
config.dbname       =       PANELFIG.dbname;
config.nbins           =       str2num(PANELFIG.nbins);
config.title              =      PANELFIG.titlelist;
config.flagged        =       PANELFIG.flagged;
config.rl_range       =       [4 12];  % rl = ref layer
config.lon_branch   =       0;   
% split longitude here (eg. 0 if in the Pacific, 180 if in the Atlantic                                     
                                                                                   
%set/verify a few paths
%look for a file in autoedit/
if (exist('alocateit.m') ~= 2)
   aeditpath
end
%look for a file in codas3/
if (exist('smoothr.m') ~= 2)
   adcppath
end
%look for a file in m_map
if (exist('m_plotbndry.m') ~= 2)
   mmappath
end
if PANELFIG.mixed == 1 & length(PANELFIG.order) ~= str2num(PANELFIG.panelsppg)
   noticeOK('Mixed panels not = no. panels/pg ??')
   return
end

% pass on 'config' information to "gpanel" which will then 
% call agetmat and then call aplotit.

gpanel(config);

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

function fixtitle()

global PANELFIG

ch = findobj(gcf,'tag','chkcalendar');
dh = findobj(gcf,'tag','chkdecimal');
tti = findobj(gcf,'tag','txtboxtitle');

calendarflag = get(ch,'value');
decimalflag = get(dh,'value');
PANELFIG.title = get(tti,'string');
PANELFIG.titlebtnstatus = [calendarflag decimalflag];

[cruiseID, rest] = strtok(PANELFIG.title,';');
part1 = cruiseID;
if isempty(part1), part1 = ''; end;

if calendarflag
   cal1 = PANELFIG.begcal;
   cal2 = PANELFIG.endcal;
   if isempty(cal1) | isempty(cal2)
      cal1 = PANELFIG.begcalposs;
      cal2 = PANELFIG.endcalposs;
   end
   cal1 = cal1(6:10); cal2 = cal2(6:10);
   part2 = sprintf('; %s-%s, %d', cal1, cal2, PANELFIG.year_base);
else
   part2 = '';
end
if decimalflag
   if isempty(PANELFIG.begdd) | isempty(PANELFIG.enddd)
      part3 = sprintf('; %s- %s dd', PANELFIG.begddposs, PANELFIG.endddposs);
   else   
      part3 = sprintf('; %s- %s dd', PANELFIG.begdd, PANELFIG.enddd);
   end
else
   part3 = '';
end

PANELFIG.title = strcat(part1, part2, part3);   
set(tti,'string',PANELFIG.title);

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

function datefix(varargbox)

global XYDATA
global PANELFIG

box = char(varargbox(1)); % cell to str

ddbeg = findobj(gcf,'tag','tboxbegdduse');
begdd = get(ddbeg,'string');
bdval = str2num(begdd);
calbeg = findobj(gcf,'tag','tboxbegcaluse');
begcal = get(calbeg,'string');
bcval = str2num(begcal);
ddend = findobj(gcf,'tag','tboxenddduse');
enddd = get(ddend,'string');
edval = str2num(enddd);
calend = findobj(gcf,'tag','tboxendcaluse');
endcal = get(calend,'string');
ecval = str2num(endcal);

dpp = findobj(gcf,'tag','txtboxdaysperp'); % these PANELFIG may not be avail. yet
PANELFIG.daysperpanel = get(dpp,'string');
ppp = findobj(gcf,'tag','txtboxpppg');
PANELFIG.panelsppg = get(ppp,'string');
npp = findobj(gcf,'tag','txtboxnopgs');
PANELFIG.nopgs = get(npp,'string');

if PANELFIG.mixed == 0 % one type only
   totdays = str2num(PANELFIG.daysperpanel) * ...
                 str2num(PANELFIG.panelsppg) * ...
                 str2num(PANELFIG.nopgs);
   PANELFIG.totdays = num2str(totdays);
   msg = sprintf('4) Total No. of days = %s', PANELFIG.totdays);
else
   totdays = str2num(PANELFIG.daysperpanel) * ...
                 str2num(PANELFIG.nopgs);
   PANELFIG.totdays = num2str(totdays);
   msg = sprintf('4) Total No. of days = %s (mixed)', PANELFIG.totdays);
end
lbl = findobj(gcf,'tag','lbldays');
set(lbl,'string', msg);

switch box
   case '1' 
      if isempty(begdd), return, end;
      PANELFIG.begdd = begdd;
      cal = to_date(PANELFIG.year_base, bdval,'s');
      cal = cal(1:10);
      set(calbeg,'string',cal);
      PANELFIG.begcal = cal;
      edval = bdval + totdays;
      set(ddend,'string', num2str(edval));
      PANELFIG.enddd = num2str(edval);
      cal = to_date(PANELFIG.year_base, edval,'s');
      cal = cal(1:10);
      set(calend,'string',cal);
      PANELFIG.endcal = cal;
   case '2' 
      if isempty(begcal), return, end;
      PANELFIG.begcal = begcal;
      yr = begcal(1:4); mo = begcal(6:7); dy = begcal(9:10);
      yr = str2num(yr); mo = str2num(mo); dy = str2num(dy);
      bd = to_day(yr, yr, mo, dy);
      bd = sprintf('%.2f',bd);
      set(ddbeg,'string',num2str(bd));
      PANELFIG.begdd = num2str(bd);
      edval = str2num(bd) + totdays;      
      set(ddend,'string', num2str(edval));
      PANELFIG.enddd = num2str(edval);
      cal = to_date(PANELFIG.year_base, edval,'s');
      cal = cal(1:10);
      set(calend,'string',cal);
      PANELFIG.endcal = cal;
   case '3' 
      if isempty(enddd), return, end;
      PANELFIG.enddd = enddd;
      cal = to_date(PANELFIG.year_base, edval,'s');
      cal = cal(1:10);
      set(calend,'string',cal);
      PANELFIG.endcal = cal;
      bdval = edval - totdays;
      set(ddbeg,'string', num2str(bdval));
      PANELFIG.begdd = num2str(bdval);
      cal = to_date(PANELFIG.year_base, bdval,'s');
      cal = cal(1:10);
      set(calbeg,'string',cal);
      PANELFIG.begcal = cal;
   case '4' 
      if isempty(endcal), return, end;
      PANELFIG.endcal = endcal;
      yr = endcal(1:4); mo = endcal(6:7); dy = endcal(9:10);
      yr = str2num(yr); mo = str2num(mo); dy = str2num(dy);
      ed = to_day(yr, yr, mo, dy);
      ed = sprintf('%.2f',ed);
      set(ddend,'string',num2str(ed));
      PANELFIG.enddd = num2str(ed);
      bdval = str2num(ed) - totdays;      
      set(ddbeg,'string', num2str(bdval));
      PANELFIG.begdd = num2str(bdval);
      cal = to_date(PANELFIG.year_base, bdval,'s');
      cal = cal(1:10);
      set(calbeg,'string',cal);
      PANELFIG.begcal = cal;
   end

earliest = XYDATA.dday(1); latest = XYDATA.dday(end);
if bdval < earliest;
   notice(' Start date preceeds database! ');
   pause(1) % noticeOk() wouldn't pause long enough -> errors
end
if edval > latest
   notice(' End date exceeds database! ');
   pause(1) 
end
 
PANELFIG.ii = find(XYDATA.dday >= str2num(PANELFIG.begdd) & XYDATA.dday <= str2num(PANELFIG.enddd));
fixtitle;

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

function mixedpanelssetup

global PANELFIG

mixedpanelsdlg;
hpn = findobj(gcf,'tag','ppo1');
pstr = get(hpn,'string');

for n =  1: length(PANELFIG.order)
   for m = 1:length(pstr)
      if strcmp(PANELFIG.order{n}, getname(pstr{m}))
         ptag = sprintf('ppo%d', n);
         hpn = findobj(gcf,'tag',ptag);
         set(hpn,'value',m); %re-establish any earlier settings
         break;
      end
   end
end

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

function readfigmixed

global PANELFIG

hpn = findobj(gcf,'tag','ppo1');
pstr = get(hpn,'string');
PANELFIG.order = [];
for n = 1:5
   ptag = sprintf('ppo%d', n);
   hpn = findobj(gcf,'tag',ptag);
   pval = get(hpn,'value');
   ptype = pstr{pval};
   if strcmp(ptype, 'None'), break; end;
   fn = getname(ptype); % local function
   PANELFIG.order{n} = fn;
end

close;

ppp = findobj(gcf,'tag','txtboxpppg');
PANELFIG.panelsppg = get(ppp,'string');
if  str2num(PANELFIG.panelsppg) ~= length(PANELFIG.order)
   set(ppp,'string',num2str(length(PANELFIG.order))); % pages must = 1
   vbox{1} = '1';
   datefix(vbox); % redo the dates on the basis of current start dd
   msg = sprintf('\nNote revised number of panels / page.   \n\n');
   notice(msg);
end

PANELFIG.mixed = 1;
vbox{1} = '1';
datefix(vbox); % redo the dates on the basis of current start dd

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

function fn = getname(ptype)

global PANELFIG

title.pg            = 'PERCENT GOOD';
title.amp           = 'AMPLITUDE';
title.sw            = 'SPECTRAL WIDTH';
title.w             = 'W';
title.e             = 'ERROR VELOCITY';
title.umeas         = 'MEASURED U';         
title.vmeas         = 'MEASURED V';         
title.uabs_sm           = 'UABS  (smoothr)';
title.uabs_g            = 'UABS  (gps nav)';
title.vabs_sm           = 'VABS  (smoothr)';
title.vabs_g            = 'VABS  (gps nav)';
title.fabs_sm         = 'FORE ABS (smoothr)';
title.fabs_g          = 'FORE ABS (gps nav)';
title.pabs_sm         = 'PORT ABS (smoothr)';
title.pabs_g          = 'PORT ABS (gps nav)';

PANELFIG.titlelist = title;
fn = 'null';
fnms = fieldnames(title);
for n = 1:length(fnms)
   %chk = getfield(title, char(fnms(n)));
   chk = getfield(title, fnms{n});
   if strcmp(chk, ptype)
      %fn = char(fnms(n));
      fn = fnms{n};
      break;
   end;
end

%%%%%%%%%%%%%%%%%%%% EOF