function [DATA,Vx]=plot5(DATA,vdims,iich,iinewfig,psize,instr)
% Call [DATA,Vx]=view5(DATA,vdims,iich,iinewfig,psize,instr)
%
% Generalization of view3 for 5-dimensional data;
% User inputs two dimensions over which to image, one dimension over which to step;
%
% If errors occur for no apparent reason, reset viewing options by executing "view_global_set".
%
% DATA		= structure containing fields .x,.y,.z,.data,.xlab,.ylab,.zlab, OR
%		  	  root of HDF file that contains 2- or 3-D data, OR
%		  	  root of .mat file that contains a structure with same fields as above;
% vdims		= [y_dim,x_dim,fix_dim] dimensions to image and dimension to step through;
% iich 		= 1 to change values, 0 to leave as specified in V;
% iinewfig	= 1 to create new figure, 0 to plot on current figure;
% psize		= image size (default is that in V.size);
% instr     = 1 to print out instructions on key strokes;
%
% V.xlab,ylab,zlab,dlab = 		axis labels for x,y,z, data;
% V.ii_img: 	0=line plot; 1=image; 2=contour; 3=shaded line plot (option removed);
% V.iilog(1:6): 0=linear axis, 1=log axis [choose for each of six axes: y,x,z,z2,z3,data];
% V.iibar: 		0=no colorbar, 1=add colorbar (for images only);
% V.clim = 		min, max value for imaging, plotting, or contouring ([]=use data);
% V.unit,V.size,V.ptsize = 		values for size of plot and lines;
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
view_global
%
if nargin<1, DATA=[]; end
if nargin<2, vdims=[]; end
if nargin<3, iich=1; end
if nargin<4, iinewfig=1; end
if nargin<5, psize=[]; end
if nargin<6, instr=1; end
%
if isempty(V), view_global_set; end
if isempty(iinewfig), iinewfig=1; end
if ~isempty(psize), V.size=psize; end
%
if ~isfield(V,'iilog') | iscell(V.iilog), V.iilog=zeros(1,6); end
if length(V.iilog)<6, V.iilog=zeros(1,6); end
if ~isfield(V,'ii_img'), V.ii_img=1; end
if ~isfield(V,'lwidth'), V.lwidth=1.5; end
if ~isfield(V,'iibold'), V.iibold=1; end
if ~isfield(V,'unit'), V.unit='in'; end
if ~isfield(V,'size'), V.size=[5,3.5]; end
if ~isfield(V,'ptsize'), V.ptsize=14; end
if ~isfield(V,'cmap'), V.cmap=jet; end
if ~isfield(V,'sym'), V.sym={'o'}; end
if ~isfield(V,'sty'), V.sty={'-'}; end
if ~isfield(V,'dot'), V.dot={5}; end
if ~isfield(V,'col'), V.col={'r','b','g','m','c','k'}; end
if ~isfield(V,'fname'), V.fname='Arial'; end
if ~isfield(V,'iivg'), V.iivg=1; end
if ~isfield(V,'iiq'), V.iiq=1; end
%
iihdf=0;
if isempty(DATA),
	disp('No data input to view: choose HDF file');
	[f_file,DATA,iif,path_back]=adi_check_file([],'hdf','HDF');
	DATA=DATA(1:end-1);
end
%
iihdf_fmt=0;
if ischar(DATA),
    if any(DATA=='*'),
	    disp('Choose desired data file or cancel to exit: ');
	    [DATA,f_root,iif,path_back]=adi_check_file([],DATA,[]),
        ii=findstr(DATA,'.');
        if ~isempty(ii), DATA=DATA(1:ii(end)-1), end
    end   
	disp('Checking for HDF or .mat files ...')
	file_root=DATA; clear DATA;
    iihdf1=exist([file_root,'.hdf'],'file')==2;
    iihdf2=exist(file_root,'file')==2 & strcmp(file_root(max(1,end)-3:end),'.hdf');
	if iihdf1 | iihdf2,
        if iihdf2, file_root=file_root(1:end-4); end
		[SD,status]=hdfsd_open([file_root,'.hdf']);
        if status~=0, error(['Error opening HDF file: ',file_root,'.hdf']); end
		if SD.nset>1, 
			disp(['HDF file contains ',int2str(SD.nset),' datasets named: ']);
			disp(SD.data_name)
			jset=input('Enter data number: ');
		else
			jset=1;
		end
		[DATA,status]=hdfsd_read(SD,[],jset);
        if status<0, display('hdfsd_read not successful'); display(status); return; end
        iihdf_fmt=1; DATA.labels{DATA.ndim}
        if strcmp(DATA.labels{DATA.ndim},'Re/Im'),
            disp('forming complex values ...');
            DATA.ndim=DATA.ndim-1;
            npt_tot=prod(DATA.dims(1:DATA.ndim-1)); 
            DATA.data=DATA.data(1:npt_tot) + i*DATA.data(npt_tot+1:2*npt_tot);
            DATA.dims=DATA.dims(1:DATA.ndim);
            DATA.data=reshape(DATA.data,DATA.dims);
            DATA.labels=DATA.labels(1:DATA.ndim);
            DATA.scales=DATA.scales(1:DATA.ndim);
            DATA.iidB=-1;
        end
	else
		disp('Input found to be a string, but no HDF found locally')
		return
	end
end
%
[ny,nx,nz,nz2,nz3]=size(DATA(1).data);
if any([ny,nx,nz,nz2,nz3]==0), disp(DATA); error('DATA is empty'); end
% Check that first argument is a structure containing data, axes, and labels:
if ~isstruct(DATA),
	disp('view5 requires DATA structure or HDF/.mat file root');
	return
end
if ~isfield(DATA,'dims'), error('DATA must have dims as field'); end
nset=length(DATA);
if nset>1,
    disp('Choose which dataset to plot or image: ');
    for jset=1:nset,
        disp(['Set number ',int2str(jset),' = ',DATA(jset).dlab]);
    end
    jset=input('Enter set number: ');
    DATA=DATA(jset);
end

data=DATA.data; ndim_max=7;
%	vars={'y','x','z','z2','z3','z4','z5'};
%	for jdim=1:DATA.ndim,
%		str=[vars{jdim},'=DATA.scales{jdim};']; eval(str);
%		str=['V.',vars{jdim},'lab=DATA.labels{jdim};']; eval(str);
%	end
%	for jdim=DATA.ndim+1:ndim_max,
%		str=[vars{jdim},'=0;']; eval(str);
%		str=['V.',vars{jdim},'lab='' '';']; eval(str);
%	end
%	V.dlab=DATA.dlab;
if isfield(DATA,'clim'), V.clim=DATA.clim; end
if isfield(DATA,'cmap'), V.cmap=DATA.cmap; end
if isfield(DATA,'title'), ident=DATA.title; else, ident=[]; end
if isfield(DATA,'size'), V.size=DATA.size; end
%
if prod(size(V.size))~=2 | any(V.size<=0), V.size=[5,3.5]; disp('Bad plot size fixed'); end
%
if any(~isreal(data(:))), 
	data=dB(data); 'dB done',
end
%
%axlabs={V.ylab; V.xlab; V.zlab; V.z2lab; V.z3lab};
axlabs=DATA.labels;
%%axvals={y; x; z; z2; z3};
axvals=DATA.scales;
%axlims0={[y(1) y(ny)]; [x(1) x(nx)]; [z(1) z(nz)]; [z2(1) z2(nz2)]; [z3(1) z3(nz3)]};
for jdim=1:DATA.ndim,
    axlims0{jdim}=DATA.scales{jdim}(1,end);
    axind{jdim}=[1:DATA.dims(jdim)];
end
axlims=axlims0;
%axind={[1:ny]; [1:nx]; [1:nz]; [1:nz2]; [1:nz3]};
%
[nrow,ncol,nlay,nlay2,nlay3]=size(data);
%
dmin=min(data(:)); dmax=max(data(:));
if isempty(V.clim), V.clim=[dmin,dmax]; end
if dmin>V.clim(2) | dmax<V.clim(1), V.clim=[dmin,dmax]; end
%
%dims=[nrow,ncol,nlay,nlay2,nlay3]; ndim=length(dims);%% disp(dims);
dims=DATA.dims; ndim=DATA.ndim;
%
if prod(dims)==1,
	disp(['Single number in data set: ',num2str(data(1))]);
	return
end
iiok=find(dims>1); dims_ok=dims(iiok); ndim_ok=length(iiok);
ii1v=find(dims==1); dims_1v=dims(ii1v); ndim_1v=length(iiok);
[dims_sort,ii_sort]=sort(dims_ok);
%
% Find time dimension
time_dim=find(strncmp(axlabs,'Time -',6) | strncmp(axlabs,'time -',6));
time_unit=0;
if ~isempty(time_dim),
	iihr1=findstr(axlabs{time_dim},'hour'); iihr2=findstr(axlabs{time_dim},'Hour');
	iimin1=findstr(axlabs{time_dim},'min'); iimin2=findstr(axlabs{time_dim},'Min');
	iisec=findstr(axlabs{time_dim},'- s');
	time_lab{1}='Time - hour'; time_lab{2}='Time - min'; time_lab{3}='Time - s';
	if ~isempty(iihr1) | ~isempty(iihr2),
		time_unit=1;
		time_ax{1}=axvals{time_dim};
		time_ax{2}=60*(time_ax{1} - time_ax{1}(1));
		time_ax{3}=3600*(time_ax{1} - time_ax{1}(1));
		time_lab{1}=axlabs{time_dim};
		hour0=sprintf('%1.3f',time_ax{1}(1));
		time_lab{2}=[time_lab{2},' re ',axlabs{time_dim}(end-2:end),'/',hour0];
		time_lab{3}=[time_lab{3},' re ',axlabs{time_dim}(end-2:end),'/',hour0];
	elseif ~isempty(iimin1) | ~isempty(iimin2),
		time_unit=2; 
		time_ax{2}=axvals{time_dim};
		time_ax{3}=60*(time_ax{2} - time_ax{2}(1));
		time_ax{1}=(1/60)*(time_ax{2} - time_ax{2}(1));
	elseif ~isempty(iisec),
		time_unit=3;
		time_ax{3}=axvals{time_dim};
		time_ax{2}=(1/60)*(time_ax{3} - time_ax{3}(1));
		time_ax{1}=(1/3600)*(time_ax{3} - time_ax{3}(1));
	end
	if time_unit==0,
		disp('Unable to determine if time axis is in minutes or hours');
	end
else
	time_dim=iiok(1);
end
%V.ii_img=1; if ndim_ok==1, V.ii_img=-1; end
%
if isempty(vdims),
	ii_nt=find(iiok~=time_dim);
%    ndim_ok, iiok,time_dim,
	if ndim_ok==1,
		vdims=[ii1v(1) iiok(1) ii1v(2)]; 
	elseif ndim_ok==2,
        ii_td=find(time_dim==iiok);
        if isempty(ii_td), 
            vdims=[iiok,time_dim]; 
        else,
            if V.ii_img>0,
                vdims=[time_dim,iiok(ii_nt),ii1v(1)];
            else
                vdims=[iiok(ii_nt),time_dim,ii1v(1)];
            end
        end
%		vdims=[time_dim iiok(ii_nt(1)) ii1v(1)];
	elseif ndim_ok>=3,
%		vdims=[time_dim iiok(ii_nt(1:end))];
		if V.ii_img>0,
            vdims=[time_dim,iiok(ii_nt(1:end))];
		else
            vdims=[iiok(ii_nt(1)),time_dim,iiok(ii_nt(2:end))];
		end
	end
%	vdims,	keyboard
elseif length(vdims)~=3,
%%	disp('USER ERROR: input vdims must have length 3'); return;
end
%
nvdims=dims(vdims);
if prod(nvdims(1:2))<=10, V.ii_img=-1; end		% Make a line plot if 2 dims are ~1;
%if V.ii_img<=0 & nvdims(2)<nvdims(1),
%	vdims=vdims([2 1 3]);
%end
%
if iinewfig, figure; end
%
if iich & instr,
	disp(['Enter: D-arrow/2 to choose dims imaged, U-arrow/8 to cycle through dims, * to cycle opposite way'])
	disp(['  L/R arrows (4/6) to vary index of fixed dimension, 5 to cycle through fixed dim,'])
	disp(['  7/9 to vary index of second fixed dimension, 1/3 to vary index of third fixed dimension, '])
	disp(['  / to switch x-y axes, c to change clim, s to change size, '])
	disp(['  b to toggle colorbar, B to toggle bold, m to change color map, '])
	disp(['  l to change axis labels, i for axis(''ij''), j for axis(''xy''),'])
	disp(['  x to change x-axis limits, y to change y-axis limits, L/R mouse to zoom in/out on section, '])
	disp(['  L for line plot, M for multi-line plot, I for image, C for contours, S for shaded line,'])
	disp(['  +/- to change line plot index, =/_ to set line plot index(es)/value(s),'])
    disp(['  h to set line colors and styles, H to change symbols and symbol sizes, Y for symbols & no lines, '])
	disp(['  P to pause, p to change point size, V to toggle iivw (VG vs. report) '])
	disp(['  w to change line width, F to spawn new figure window,'])
	disp(['  d to convert to dB (10*log10), D to convert to dB (20*log10), a to toggle dB/(angle/pi) ,'])
	disp(['  e to toggle cliplines call, t to toggle min/HOD'])
	disp(['  (for clipped lines when copied and pasted), return to end'])
end
%
iiij=0;
iigrid=0; iiclip=-1; iiv=[];
%
jfix=1; KV={1 1 1 1 1 1}; jshade=1;
button=-999; climx=[]; ii_ang=0;
if V.ii_img==3, button=83; end
iidB=-1;
if isfield(DATA,'iidB'), iidB=DATA.iidB; end
while ~isempty(button),
%	disp(button),
	switch button,
	case {28,52}, 
		jfix=jfix - 1;
	case {29,54}, 
		jfix=jfix + 1;
	case {55,57},
        if length(vdims)>=4,
            KV{vdims(4)}=KV{vdims(4)} + button-56;
        else
            disp(['Not enough dimensions to flip through 2nd fixed dim.']);
        end
	case {49,51},
        if length(vdims)>=5,
            KV{vdims(5)}=KV{vdims(5)} + button-50;
        else
            disp(['Not enough dimensions to flip through 3rd fixed dim.']);
        end
	case {31,50},
		disp(axlabs); str=sprintf('%i,',dims); disp(['Dimensions = [',str(1:end-1),']']);
		cmm=input(['Enter new y_dim,x_dim,fix_dim (current = [',int2str(vdims(1:3)),']): '],'s'); 
		cmm=str2num(cmm);
		if isempty(cmm), cmm=vdims; end
		if length(cmm)~=3 | any(cmm>5) | any(cmm<1), 
			disp('Invalid dims'); keyboard
		else
			vdims=cmm;
            if length(cmm)<ndim_ok,
                for jdim=1:ndim_ok
                    if ~any(vdims==iiok(jdim)), vdims=[vdims iiok(jdim)]; end
                end
            end
 %           KV={1 1 1 1 1};
		end
	case {30,56},       % 8 or up-arrow to shift through dimensions;
		if ndim_ok<3, 
			disp('Fewer than three dimensions > 1.  Use / to switch hor/ver');
		else
            [dum,iiyy]=min(abs(ypt-yax)); jfix=iiyy;
            vdims=vdims([2:end,1]);
			disp(['vdims = ',int2str(vdims)]);	
		end
	case {42},       % * to shift through dimensions in opposite order;
		if ndim_ok<3, 
			disp('Fewer than three dimensions > 1.  Use / to switch hor/ver');
		else
            [dum,iixx]=min(abs(xpt-xax)); jfix=iixx;
            vdims=vdims([end,1:end-1]);
			disp(['vdims = ',int2str(vdims)]);	
		end
	case 53, % 5 to cycle through fixed dim.
		if ndim_ok<3, 
			disp('Fewer than three dimensions > 1.  Use / to switch hor/ver');
		else
            vdims(3:end)=vdims([4:length(vdims),3]); %KV={1 1 1 1 1};
			disp(['vdims = ',int2str(vdims)]);	
		end
	case 47, % / to switch x,y axes.
		vdims=vdims([2 1 3:end]);
    case 63,    % ? to switch x,y axes and horizontal,vertical sizes;
		vdims=vdims([2 1 3:end]);
        V.size=V.size([2,1]);
	case 43, % + to increment forward on single line plot 
		V.iiq=V.iiq + 1;
	case 45, % - to increment backward on single line plot 
		V.iiq=V.iiq - 1;
	case 61, % = to set single line plot indices
		V.iiq=input('Enter index(es) for line plots ([] for all): ','s'); V.iiq=str2num(V.iiq);
	case 95, % _ to set single line plot index by its value
		iiv=input('Enter axis value(s) for line plots ([] for all):','s'); iiv=str2num(iiv);
        if isempty(iiv), V.iiq=[]; end
	case 99, % Lower case c for clim
		disp(['Min, max of entire data set = ',num2str([dmin,dmax])]);
      	cmm=input(['Enter [cmin,cmax] (or cmax-cmin) (currently: ',num2str(V.clim),'): '],'s');
      	cmm=str2num(cmm);
        if length(cmm)==1, 
            if cmm>0, 
                cmm=[dmax-cmm,dmax];    % Single pos # mean dB dyn range;
            else
                cmm=V.clim + cmm;       % Single negative number means to add
            end
        end
		if isempty(cmm), cmm=[dmin,dmax]; end
		if length(cmm)~=2 | cmm(1)>=cmm(2), disp('Invalid clims.'); else V.clim=cmm; end
	case 115, % Lower case s for size
        disp(['Current plot size = ',num2str(V.size)]);
		cmm=input('Enter plot size [width,height] in inches: ','s'); cmm=str2num(cmm);
        if isempty(cmm), cmm=V.size; end
        if length(cmm)~=2, disp('Illegal plot size: should be [w,h]. '); break; end
		V.size=cmm; V.unit='in'; 
	case 109, % Lower case m for map
		V.cmap=input('Enter color map (no quotes) (jet,1-gray,etc): ');
		if ~isempty(V.cmap), colormap(V.cmap); end
	case 98, % Lower case b to toggle colorbar
		V.iibar=1-V.iibar;
	case 108, % Lower case l to change axis labels
		cmm=input('Enter row (y) axis label (<cr> to keep same): ','s');
		if ~isempty(cmm), V.ylab=cmm; end
		cmm=input('Enter col (x) axis label (<cr> to keep same): ','s');
		if ~isempty(cmm), V.xlab=cmm; end
		cmm=input('Enter data axis label (<cr> to keep same): ','s');
		if ~isempty(cmm), V.dlab=cmm; end
	case 104, % Lower case h to set line colors and syles
        disp('Current line colors: '); disp(V.col);
		cmm=input(['Enter line colors (no quotes): (<cr> to keep same): '],'s');
		if isempty(cmm), cmm='rbgmck'; end
        disp('Current line styles: '); disp(V.sty);
		cmm=input(['Enter line styles (AS CELL ARRAY: {''-''} or {''none''}, e.g.): (<cr> to keep same): ']);
		if ~isempty(cmm), if ~iscell(cmm), cmm=cellstr(cmm); end; V.sty=cmm; end
	case 72, % Upper case h to set line symbols, symbol sizes, and line styles:
        for j=1:length(cmm), V.col{j}=cmm(j); end
        disp('Current symbols: '); disp(V.sym);
		cmm=input(['Enter symbols (AS CELL ARRAY: {''o''} or {''none''}, e.g.): (<cr> to keep same): ']);
		if ~isempty(cmm), if ~iscell(cmm), cmm=cellstr(cmm); end; V.sym=cmm; end
        disp('Current dot sizes: '); disp(V.dot);
		cmm=input(['Enter dot sizes (AS CELL ARRAY: {5} or {5,8}, e.g.): (<cr> to keep same): ']);
		if ~isempty(cmm), if ~iscell(cmm), cmm=cellstr(cmm); end; V.dot=cmm; end
    case 89, %Upper case Y to toggle between symbols and lines:
        if strcmp(V.sym,'none'),
            V.sym={'o'}; V.sty={'none'};
        else
            V.sym={'none'}; V.sty={'-'};
        end
	case 105, % Lower case i to set axis('ij')
		iiij=1;
	case 106, % Lower case j to set axis('xy')
		iiij=-1;
	case 112, % Lower case p to change point size
		V.ptsize=input('Enter point size: ');
	case 103, % Lower case g to toggle grid on and off
		if iigrid==0, iigrid=1; else, iigrid=-iigrid; end
	case 76, % Upper case L for single line plot
		V.ii_img=-1; iiv=ypt;
	case 86, % Upper case V for single line plot
		V.ii_img=0; iiv=xpt;
	case 77, % Upper case M for multiple line plot
		V.ii_img=-1; iiv=ypt;
	case 73, % Upper case I for image plot
		V.ii_img=1;
	case 67, % Upper case C for contour plot
		V.ii_img=2;
	case 83, % Upper case S for shaded line plot
		V.ii_img=3;
		disp(axlabs); str=sprintf('%i,',dims); disp(['Dimensions = [',str(1:end-1),']']);
        cmm=input(['Enter 2 axis dims and plot/shade dim, followed by plot index & shade index: '],'s');
        cmm=str2num(cmm); if length(cmm)<5, 'Must be 5 long'; break; end
        vdims=cmm(1:3); jfix=cmm(4); jshade=cmm(5);
        for jdim=1:ndim_ok,
            if ~any(vdims==iiok(jdim)), vdims=[vdims iiok(jdim)]; end
        end        
	case 80, % Upper case P to pause so that figure may be copied
		disp('Pausing so that figure may be copied.  Press return to continue.');
		pause;
	case 101, % Lower case e to toggle cliplines call (makes copied plot's lines clipped)
		iiclip=-iiclip,
	case 66, % Upper case B to toggle bold lettering 
		V.iibold=1-V.iibold;
	case 119, % Lower case s for size
		cmm=input(['Enter line width in points: (current value = ', ...
			num2str(V.lwidth),'): ']);
		if isempty(cmm), cmm=1.5; end
		V.lwidth=cmm;
    case 1,     % Left mouse button to zoom in on section of image
        disp('Click on opposite corner of region to zoom in on (<CR> to abort):');
        [xpt2,ypt2,button2]=ginput(1);
        if isempty(button2), disp('Aborting image zoom-in'); return; end;
        axlims{vdims(1)}=[min(ypt,ypt2),max(ypt,ypt2)];
        axlims{vdims(2)}=[min(xpt,xpt2),max(xpt,xpt2)];
		xvals=axvals{vdims(2)};
		axind{vdims(2)}=find(xvals>=axlims{vdims(2)}(1) & xvals<=axlims{vdims(2)}(2));
		yvals=axvals{vdims(1)};
		axind{vdims(1)}=find(yvals>=axlims{vdims(1)}(1) & yvals<=axlims{vdims(1)}(2));
    case 3,     % Right mouse button to zoom out
        axlims{vdims(1)}=axlims0{vdims(1)};
        axlims{vdims(2)}=axlims0{vdims(2)};
		axind{vdims(1)}=[1:dims(vdims(1))];
 		axind{vdims(2)}=[1:dims(vdims(2))];
    case 120, % Lower case x to change x-axis limits
		xlims0=axlims0{vdims(2)}; xlims=axlims{vdims(2)};
		disp(['Data horizontal axis values   : ',num2str(xlims0)]);
		disp(['Current horizontal axis values: ',num2str(xlims)]);
		cmm=input('Enter new  horizontal axis limits xmin,xmax (<CR>=keep same): ','s');
		cmm=str2num(cmm);
		if length(cmm)==2,
			axlims{vdims(2)}=cmm;
        elseif length(cmm)==1,
            axlims{vdims(2)}=xlims0;
        else
            disp(['Keeping same']);
		end
		xvals=axvals{vdims(2)};
		axind{vdims(2)}=find(xvals>=axlims{vdims(2)}(1) & xvals<=axlims{vdims(2)}(2));
	case 121, % Lower case y to change y-axis limits
		ylims0=axlims0{vdims(1)}; ylims=axlims{vdims(1)}; 
		disp(['Data vertical axis values   : ',num2str(ylims0)]);
		disp(['Current vertical axis values: ',num2str(ylims)]);
		cmm=input('Enter new  horizontal axis limits ymin,ymax: ','s'); cmm=str2num(cmm);
		if length(cmm)==2,
			axlims{vdims(1)}=cmm;
        elseif length(cmm)==1,
            axlims{vdims(1)}=ylims0;
        else
            disp('Keeping same'); 
		end
		yvals=axvals{vdims(1)};
		axind{vdims(1)}=find(yvals>=axlims{vdims(1)}(1) & yvals<=axlims{vdims(1)}(2));
	case 116,
		if time_unit==0, 
			disp('No Time label found in data or unable to figure out h/m/s'); axlabs; return; 
		end
		time_unit=mod(time_unit,3) + 1;
		axlabs{time_dim}=time_lab{time_unit}; axvals{time_dim}=time_ax{time_unit};
	case 86,
		V.iivg=1-V.iivg;
		if V.iivg,
			V.fname='Arial'; V.ptsize=14; V.iibold=1;
		else
			V.fname='Times'; V.ptsize=11; V.iibold=0;
		end
	case 70,	% Upper case F to spawn new figure;
		figure; zoom on;
	case {100,68}	% Lower/upper case d to convert to dB using 10*log10 or 20*log10:
		if button==100, fac=10, else, fac=20; end
		if iidB~=1,
			data=fac*log10(abs(data));
			iidB=1; dmin=fac*log10(abs(dmin)); dmax=fac*log10(abs(dmax)); V.dlab=[V.dlab,' dB'];
			V.clim=fac*log10(max(V.clim,1e-30));
		else
			data=10.^(abs(data)/fac);
			iidB=0; dmin=10.^(dmin/fac); dmax=10.^(dmax/fac); V.dlab=V.dlab(1:end-3);
			V.clim=10.^(V.clim/fac);
		end
		disp(V.clim),
    case {97}
        if DATA.iidB~=-1, 
            disp('DATA.iidB must be -1, indicating complex numbers, to toggle between dB/angle.')    
        elseif ii_ang==1,
            data=dB(DATA.data); ii_ang=0; if ~isempty(climx), V.clim=climx; end
        else,
            data=angle(DATA.data)/pi; ii_ang=1; climx=V.clim; V.clim=[-1,1];
        end
	case -999,
	otherwise
		disp('Unknown button pushed...');
	end
%
    if jfix>dims(vdims(3)), jfix=1; elseif jfix<1, jfix=dims(vdims(3)); end
%
    KV{vdims(3)}=jfix;
% Guard against not plotting anything and getting nyax=0 or nxax=0:
    if isempty(axind{vdims(1)}), axind{vdims(1)}=[1:dims(vdims(1))]; end
    if isempty(axind{vdims(2)}), axind{vdims(2)}=[1:dims(vdims(2))]; end
	KV{vdims(1)}=axind{vdims(1)}; KV{vdims(2)}=axind{vdims(2)};
    for jdim=4:length(vdims),
        jd=vdims(jdim);
        if length(KV{jd})>1, KV{jd}=1; end
        if KV{jd}>dims(jd), KV{jd}=1; end
        if KV{jd}<1, KV{jd}=dims(jd); end
    end
	nytake=length(axind{vdims(1)}); nxtake=length(axind{vdims(2)});
	if vdims(1)<vdims(2),
		datax=reshape(data(KV{1},KV{2},KV{3},KV{4},KV{5}),nytake,nxtake);
	else
% Must transpose:
		datax=reshape(data(KV{1},KV{2},KV{3},KV{4},KV{5}),nxtake,nytake).';
	end
    if V.ii_img==3,
        KS=KV; KS{vdims(3)}=jshade;
% Read in values with which to shade plot:
		if vdims(1)<vdims(2),
			shadex=reshape(data(KS{1},KS{2},KS{3},KS{4},KS{5}),nytake,nxtake);
		else
	% Must transpose:
			shadex=reshape(data(KS{1},KS{2},KS{3},KS{4},KS{5}),nxtake,nytake).';
		end
        Slim=[min(shadex(:)),max(shadex(:))];
    end
	yax=axvals{vdims(1)}(KV{vdims(1)});
	xax=axvals{vdims(2)}(KV{vdims(2)});
	nyax=length(yax); nxax=length(xax);
	ylab=axlabs{vdims(1)}; xlab=axlabs{vdims(2)};

% Only auto-set vertical "Depth" axis if iiij hasn't been set by hand:
	if ~isempty(findstr('Depth',ylab)) & iiij==0, iiij=1; end
	iilog=V.iilog([vdims(1) vdims(2) 6]);
% Construct title:
    tlab=[];
    for jd=length(vdims):-1:3,
        jdim=vdims(jd);
        if ~isempty(tlab), tlab=[tlab,'; ']; end
        tlab=[tlab,axlabs{jdim},' = ',num2str(axvals{jdim}(KV{jdim}))];
    end
    if ndim_ok<=2, tlab=[]; end
%
    if V.ii_img==-1,
        jdim=vdims(1); ny=size(datax,1);%% if V.iiq<1, V.iiq=ny; elseif V.iiq>ny, V.iiq=1; end
        if isempty(V.iiq), V.iiq=[1:ny]; end
        ii=find(V.iiq<1); V.iiq(ii)=ny; ii=find(V.iiq>ny); V.iiq(ii)=1;
        if length(V.iiq)>10,
            str=sprintf('[%1.1f,%1.1f]',axvals{jdim}(V.iiq([1,end])));
        else
            str=sprintf('%1.1f ',axvals{jdim}(V.iiq));
        end
        if ~isempty(tlab), tlab=[tlab,'; ']; end
        tlab=[tlab,axlabs{jdim},' = ',str];
    elseif V.ii_img==0,
        jdim=vdims(2); nx=size(datax,2);%% if V.iiq<1, V.iiq=ny; elseif V.iiq>ny, V.iiq=1; end
        if isempty(V.iiq), V.iiq=[1:nx]; end
        ii=find(V.iiq<1); V.iiq(ii)=nx; ii=find(V.iiq>nx); V.iiq(ii)=1;
        if length(V.iiq)>10,
            str=sprintf('[%1.1f,%1.1f]',axvals{jdim}(V.iiq([1,end])));
        else
            str=sprintf('%1.1f ',axvals{jdim}(V.iiq));
        end
        if ~isempty(tlab), tlab=[tlab,'; ']; end
        tlab=[tlab,axlabs{jdim},' = ',str];
    end    
	if isempty(V.clim),
		V.clim=dbdyn_calc2([],[],datax);
	else
		V.clim=dbdyn_calc2(V.clim(1),V.clim(2),datax);
	end
%
	if V.ii_img==1,
		imagesc(xax,yax,datax,V.clim); xlabel(xlab); ylabel(ylab);
		colormap(V.cmap);
	elseif V.ii_img==2,
		cinc=round(V.clim(2)-V.clim(1))/10;
		cvals=[V.clim(1):cinc:V.clim(2)];
		[c,h]=contour(xax,yax,datax,cvals); xlabel(xlab); ylabel(ylab);
		clabel(c,h); grid on;
		axis('ij');
	elseif V.ii_img==-1,
		if ~isempty(iiv),
			nv=length(iiv); vmat=abs(ones(nv,1)*yax(:)' - iiv'*ones(1,length(yax)));
			[dum,V.iiq]=min(vmat,[],2); V.iiq=V.iiq'; iiv=[];
		end
		if isempty(V.iiq), 
			V.iiq=[1:size(datax,1)]';
		else
			if V.iiq<1, V.iiq=size(datax,1); elseif V.iiq>size(datax,1), V.iiq=1; end
		end;
		xx=[min(xax),max(xax)]; if xx(1)==xx(end) & length(xx)>1, xx(end)=xx(1) + 1; end
		plotll(xax,datax(V.iiq,:),iilog([1 3]),V.sym,V.col,V.dot,V.sty,0); 
		xlabel(xlab); ylabel(V.dlab); grid on;
		axis([xx V.clim]);
	elseif V.ii_img==0,
		if ~isempty(iiv),
			nv=length(iiv); vmat=abs(ones(nv,1)*xax(:)' - iiv'*ones(1,length(xax)));
			[dum,V.iiq]=min(vmat,[],2); V.iiq=V.iiq'; iiv=[];
		end
		if isempty(V.iiq), 
			V.iiq=[1:size(datax,2)]';
		else
			if V.iiq<1, V.iiq=size(datax,2); elseif V.iiq>size(datax,2), V.iiq=1; end
		end;
		yy=[min(yax),max(yax)]; if yy(1)==yy(end) & length(yy)>1, yy(end)=yy(1) + 1; end
		plotll(yax,datax(:,V.iiq),iilog([3 2]),V.sym,V.col,V.dot,V.sty,1); 
		xlabel(V.dlab); ylabel(ylab); grid on;
		axis([V.clim yy]);
	elseif V.ii_img==3,
		if ~isempty(iiv),
			nv=length(iiv); vmat=abs(ones(nv,1)*xax(:)' - iiv'*ones(1,length(xax)));
			[dum,V.iiq]=min(vmat,[],2); V.iiq=V.iiq'; iiv=[];
		end
		if isempty(V.iiq), 
			V.iiq=[1:size(datax,2)]';
		else
			if V.iiq<1, V.iiq=size(datax,2); elseif V.iiq>size(datax,2), V.iiq=1; end
		end;
		yy=[min(yax),max(yax)]; if yy(1)==yy(end) & length(yy)>1, yy(end)=yy(1) + 1; end
%		plotll(yax,datax(:,V.iiq),iilog([3 2]),V.sym,V.col,V.dot,V.sty,1);
        y_mat=repmat(yax(:),[1,length(V.iiq)]); size(y_mat),size(datax(:,V.iiq)),
        h=plot_cfill(datax(:,V.iiq),y_mat,'o',shadex(:,V.iiq),5,'none',1.5,0,Slim(1),Slim(2),V.cmap);
		xlabel(V.dlab); ylabel(ylab); grid on;
        V.clim=[min(datax(:)),max(datax(:))];
		axis([V.clim yy]);
	end
	niiq=length(V.iiq);
% If time is not on vertical axis, use axis('xy'):
	if V.ii_img==1 | V.ii_img==2,
		if ~strncmp(ylab,'Time',4) & ~strncmp(ylab,'Sequ',4), axis('xy'); end
	end
%
	if iiij==1, axis('ij'); elseif iiij==-1, axis('xy'); end
	if iigrid==1; grid on; elseif iigrid==-1; grid off; end
	if iiclip==1; clipline(gca); end
%
	if ~isempty(tlab), mytitle([tlab]); end
	plot_set3(gca,V.lwidth,V.fname,V.ptsize,V.iibold);
	if ~isempty(V.unit),
		plot_size(gca,V.unit,V.size(1),V.size(2));
	end
	if (V.ii_img==1 | V.ii_img==2) & V.iibar==1, hc=mycolorbar2('vert',0,V.dlab); end;
	if ~isempty(ident) & V.ident,
        str=strrep(ident,'\','/');      % ????
		str=strrep(ident,'_','\_'); %%xax([1,end]),yax([1,end]),nxax,nyax,iiij
        iixy=strcmp(get(gca,'Ydir'),'normal');      % Place label at top of figure
%        if V.ii_img==-1,
%			if iixy, yy=V.clim(2); else, yy=V.clim(1); end
%        else
%			if iixy, yy=yax(nyax); else, yy=yax(1); end
%        end
        Ylim=get(gca,'Ylim'); 
        if iixy, yy=Ylim(2); else, yy=Ylim(1); end
        h=text(xax(nxax),yy,str);
		set(h,'FontName','Arial','Fontsize',9,'HorizontalAl','Center','VerticalAl','Bottom'); 
	end
% Break if no changes desired:
	if ~iich, break; end
%%keyboard
% Otherwise get input key:
	[xpt,ypt,button]=ginput(1);
end
zoom on;
DATA.clim=V.clim;
if nargout>1, Vx.vdims=vdims; Vx.ii_img=V.ii_img; Vx.iiq=V.iiq; Vx.KV=KV; end
% Save plotting defaults in file view_def
expr=['save view_def V']; eval(expr);
%
%%%
%
function plotll(xax,datax,iilog,sym,col,dot,sty,iiv); 
%
[nr,nc]=size(datax);
nx=length(xax);
if nr~=nx & nc==nx, 
	datax=datax.'; 
	[nr,nc]=size(datax);
end
%
nlog=iilog(1)+2*iilog(2);
ncol=length(col); nsty=length(sty); nsym=length(sym); ndot=length(dot);
for jplot=1:nc,
	jcol=mod(jplot-1,ncol) + 1; jsty=mod(jplot-1,nsty) + 1;
    jsym=mod(jplot-1,nsym) + 1; jdot=mod(jplot-1,ndot) + 1;
    if iiv==0,
        plot_cfill(xax,datax(:,jplot),sym{jsym},col{jcol},dot{jdot},sty{jsty},1.5,nlog);
    else
        plot_cfill(datax(:,jplot),xax,sym{jsym},col{jcol},dot{jdot},sty{jsty},1.5,nlog);
    end
    hold on;
end
hold off;
return
%
if iilog(1)==0 & iilog(2)==0,
	for jplot=1:nc,
		jcol=mod(jplot-1,ncol) + 1;
		plot(xax,datax(:,jplot),col(jcol)); hold on;
	end
elseif iilog(1)==1 & iilog(2)==0,
	for jplot=1:nc,
		jcol=mod(jplot-1,ncol) + 1;
		semilogx(xax,datax(:,jplot),col(jcol))
	end
elseif iilog(1)==0 & iilog(2)==1,
	for jplot=1:nc,
		jcol=mod(jplot-1,ncol) + 1;
		semilogy(xax,datax(:,jplot),col(jcol))
	end
elseif iilog(1)==1 & iilog(2)==1,
	for jplot=1:nc,
		jcol=mod(jplot-1,ncol) + 1;
		loglog(xax,datax(:,jplot),col(jcol))
	end
end
