function vizu(action)
% %W% %D%
% Main file to plot GUISDAP results
% Please specify the data directory and the plotting options in
% the configuration file config.m
%
% To get informations about the data:
% >> vizu('test')
% To open a new graphic window:
% >> vizu('newfig')
% To send the figure to the default greyscale printer:
% >> vizu('print') 
% To reload and plot the data:
% >> vizu('load')
% Note that vizu will automatically reload the data if the source
% directory or the GAP_FILL variable is modified
% To save the current figure in .ps format:
% >> vizu('save')
%
% Philippe Gamet - EISCAT Group, Uppsala, 96/10/07
% Various modifications, etc TvE


if strcmp(action,'load') 
  clear all
end

global Time
global RAN
global ALT
global Ne
global Te
global Ti
global Vi
global Neerr
global Teerr
global Tierr
global Vierr
global Status
global AZ
global EL
global max_az
global min_az
global max_el
global min_el
global name_expr
global DATA_PATH
global PLOT_STATUS
global LOCATION
global auto_print
global print_name
global background
global limit_az_el

set(0,'DefaultAxesFontName','default');
set(0,'DefaultAxesFontWeight','default');

set(0,'DefaultTextFontName','default');
set(0,'DefaultTextFontWeight','default');

% Save previous values (if any) and (re)load variables from config file

if ~isempty(PLOT_STATUS)
	OLD_STATUS = PLOT_STATUS;
else
	OLD_STATUS = -99;
end;
if ~isempty(DATA_PATH)
	OLD_PATH = DATA_PATH;
else
	OLD_PATH='none';
end;
config_vizu;

% Check for missing location entry in config.m

if isempty(LOCATION)
	LOCATION = 'EISCAT';
end;

if ~exist('action')
	if size(Time) == 0 | ~strcmp(OLD_PATH,DATA_PATH) | OLD_STATUS~=PLOT_STATUS
		[Time,RAN,ALT,Ne,Te,Ti,Vi,Neerr,Teerr,Tierr,Vierr,Status,name_expr] = load_param(START_TIME,END_TIME,DATA_PATH,limit_az_el);
		figure('Position',[400 170 650 800],'Name',['GUISDAP results from ',DATA_PATH]);
		set(gcf,'NumberTitle','off')
		set(gcf,'PaperType','a4');

	end;

else
	if strcmp(action,'load')
		[Time,RAN,ALT,Ne,Te,Ti,Vi,Neerr,Teerr,Tierr,Vierr,Status,name_expr] = load_param(START_TIME,END_TIME,DATA_PATH,limit_az_el);
		figure('Position',[400 170 650 800],'Name',['GUISDAP results from ',DATA_PATH]);

		set(gcf,'NumberTitle','off')
	elseif strcmp(action,'newfig')
		figure('Position',[400 170 650 800],'Name',['GUISDAP results from ',DATA_PATH]);
		set(gcf,'NumberTitle','off')
	elseif strcmp(action,'test')
		test(DATA_PATH);
		return;	
	elseif strcmp(action,'print')
		orient tall;
		disp('Sending current figure to default printer ...');
		print
		disp('Done.');
		return;
	elseif strcmp(action,'save')
		set(gcf,'PaperType','a4');
%		Start = fromepoch(Time(1,1));
                Start = START_TIME;
		if (Start(1:1) > 80)
		  year = ['19',num2str(Start(1:1))];
		else
  year = [num2str(2000 + Start(1:1))];

%		  year = ['20',num2str(Start(1:1))];
		end  
		month = num2str(sprintf('%0.2d',Start(1,2)));
		day   = num2str(sprintf('%0.2d',Start(1,3)));
		name_fig = [year,'-',month,'-',day,'_',name_expr];
		orient tall;
		
% modify to reduce size of PostScript produced
		
		set(gcf,'RendererMode','manual');
		set(gcf,'Renderer','painters');

		disp(['Creating file ',pwd,'/',name_fig,'.ps ...']);
		eval(['print ',name_fig,' -dpsc2']);
		%disp('Adding logo ...')
		%eval(['!addlogo.exp ',name_fig,'.ps'])
		disp('Done.')
		return;
	end;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
option = zeros(4,1);
for i=1:length(WHICH_PARAM)-1
	str = [WHICH_PARAM(i),WHICH_PARAM(i+1)];
	if strcmp(str,'Ne')
		option(1) = 1;
	elseif strcmp(str,'Te')
		option(2)	=	1;
	elseif strcmp(str,'Ti')
		option(3)	=	1;
	elseif strcmp(str,'Vi')
		option(4)	=	1;
	end;
end;
n_tot = nnz(option(1:4));

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

%Start = fromepoch(Time(1,1));
%End   = fromepoch(Time(length(Time),1));
% to label plot to match window selected
Time=Time/3600.0;
Start = START_TIME;
End   = END_TIME;
STime=[toepoch(START_TIME):60:toepoch(END_TIME)]/3600.0;

if (Start(1:1) > 80)
  year = ['19',num2str(Start(1:1))];
else
  year = [num2str(2000 + Start(1:1))];
%  year = ['20',num2str(Start(1:1))];

end
month_ix =['  January February    March    April      May     June',...
					'     July   AugustSeptember  October November December'];
month = month_ix(9*Start(1,2)-8:9*Start(1,2));
if Start(1,3) == End(1,3)
	day   = num2str(sprintf('%0.2d',Start(1,3)));
else
	day   = [num2str(sprintf('%0.2d',Start(1,3))),'-',num2str(sprintf('%0.2d',End(1,3)))];
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ti = subplot(n_tot+1,1,1);
%set(ti,'Position',[0 0.92 0.95 0.08]);
set(ti,'Position',[0.0 0.87 0.95 0.08]);%vizu_www
%set(ti,'Position',[0.0 0.03 0.95 0.92]);
set(ti,'Visible','off');
set(ti,'Clipping','off');
text('Position',[0.5,0.25],'HorizontalAlignment','center', ...
		 'VerticalAlignment','top', ...
		 'String',MESSAGE1,'FontSize',12);
text('Position',[0.10,0.15],'HorizontalAlignment','left', ...
		 'VerticalAlignment','top', ...
		 'String',MESSAGE2,'FontSize',8);
text('Position',[0.10,-10.9],'HorizontalAlignment','left', ...
		 'VerticalAlignment','bottom', ...
		 'String',MESSAGE3,'FontSize',8);
text('Position',[0.95,-10.9],'HorizontalAlignment','right', ...
		 'VerticalAlignment','bottom', ...
		 'String',MESSAGE4,'FontSize',9);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determine the Y-Axis parameter

	if upper(Y_PARAM(1)) == 'R'
		Y = 'RAN';
		YTitle = 'RAN (km)';
	else
		Y = 'ALT';
		YTitle = 'ALT (km)';
	end;

if ~exist('GATES')
	GATES = [1:length(ALT(:,1))];
end;
y_param = eval(Y);
add_plot = 0;
height = (0.80 - (n_tot-1)*0.03)/n_tot;
colormap('jet');

if PLOT_STATUS == 3 
	PLOT_STATUS = 0.5;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot Ne

if upper(NE_SCALE2(2)) == 'I' % 'lInear' 
	Ne2=Ne;
	Ne=(Ne*0+10).^Ne;
	Ne=Ne./(10^10);
end

if option(1)
	add_plot = add_plot+1;
	axeNe = subplot(n_tot+1,1,add_plot+1);
	set(axeNe,'Position',[0.1 0.05+(n_tot-add_plot)*(height+0.03) 0.8 height]);
	set(axeNe,'FontSize',8);
	set(axeNe,'Box','on');
	grid on;

        s=size(Status);
	if OLD_STATUS ~= PLOT_STATUS
	  for i=1:s(1)
			for j=1:s(2)
				if Status(i,j) > PLOT_STATUS
					Ne(i,j) = nan;
				end;
				if PLOT_STATUS == 0.5 
                                if ~(Neerr(i,j) >= NEERR_SCALE(1) &...
                                     Neerr(i,j) <= NEERR_SCALE(2))
					Ne(i,j) = nan;
				end;
				end;
			end;
		end;
	end;
	for i=1:s(2)/2
          j = (i-1)*2 + 1;
	  hndl = surface(Time(j:j+1),y_param(GATES,j:j+1),Ne(GATES,j:j+1));
          set(hndl,'EdgeColor','none');
        end;
	if exist('NE_SCALE')
		set(gca,'CLim',NE_SCALE);
	end;
	if (exist('START_TIME') & exist('END_TIME'))
                START_TIME
                toepoch(START_TIME)/3600.0
	        set(gca,'XLim',[toepoch(START_TIME) toepoch(END_TIME)]/3600.0);
	end;
	if exist([Y,'_SCALE'])
		set(gca,'YLim',eval([Y,'_SCALE']));
	end;		
	hmaxis(STime);
	if add_plot == n_tot
		xlabel('UNIVERSAL TIME');
	end;
	ylabel(YTitle);
	my_colorbar;

if upper(NE_SCALE2(2)) == 'I' % 'lInear' 
	text('String','Electron Density (/10^1^0)','Units','Normalized','Position',[1.16 0.5],...
			 'HorizontalAlignment','Center','Rotation',-90,'FontSize',8);
end
if upper(NE_SCALE2(2)) == 'O' % 'lOg' 
	text('String','Electron Density (Log10)','Units','Normalized','Position',[1.16 0.5],...
			 'HorizontalAlignment','Center','Rotation',-90,'FontSize',8);
end;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot Te

if option(2)
	add_plot = add_plot+1;
	axeTe = subplot(n_tot+1,1,add_plot+1);
	set(axeTe,'Position',[0.1 0.05+(n_tot-add_plot)*(height+0.03) 0.8 height]);
	set(axeTe,'FontSize',8);
	set(axeTe,'Box','on');
	grid on;
           
	s = size(Status); 
	
	if OLD_STATUS ~= PLOT_STATUS
		for i=1:s(1)
			for j=1:s(2)
				if Status(i,j) > PLOT_STATUS
					Te(i,j) = nan;
				end;
				if PLOT_STATUS == 0.5 
                                if ~(Teerr(i,j) >= TEERR_SCALE(1) &...
                                     Teerr(i,j) <= TEERR_SCALE(2))
					Te(i,j) = nan;
				end;
			end;
			end;
		end;
	end;

	for i=1:s(2)/2
          j = (i-1)*2 + 1;
	  hndl = surface(Time(j:j+1),y_param(GATES,j:j+1),Te(GATES,j:j+1));
          set(hndl,'EdgeColor','none');
        end;

	if exist('TE_SCALE')
		set(gca,'CLim',TE_SCALE);
	end;
	if (exist('START_TIME') & exist('END_TIME'))
		set(gca,'XLim',[toepoch(START_TIME) toepoch(END_TIME)]/3600.0);
	end;
	if exist([Y,'_SCALE'])
		set(gca,'YLim',eval([Y,'_SCALE']));
	end;		
	hmaxis(STime);
	if add_plot == n_tot
		xlabel('UNIVERSAL TIME');
	end;
	ylabel(YTitle);
	my_colorbar;
	text('String','Electron Temperature (K)','Units','Normalized','Position',[1.16 0.5],...
			 'HorizontalAlignment','Center','Rotation',-90,'FontSize',8);
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot Ti

if option(3)
	add_plot = add_plot+1;
	axeTi = subplot(n_tot+1,1,add_plot+1);
	set(axeTi,'Position',[0.1 0.05+(n_tot-add_plot)*(height+0.03) 0.8 height]);
	set(axeTi,'FontSize',8);
	set(axeTi,'Box','on');
	grid on;
           
	s = size(Status);
	
	if OLD_STATUS ~= PLOT_STATUS
		for i=1:s(1)
			for j=1:s(2)
				if Status(i,j) > PLOT_STATUS
					Ti(i,j) = nan;
				end;
				if PLOT_STATUS == 0.5 
                                if ~(Tierr(i,j) >= TIERR_SCALE(1) &...
                                     Tierr(i,j) <= TIERR_SCALE(2))
					Ti(i,j) = nan;
				end;
			end;
			end;
		end;
	end;

	for i=1:s(2)/2
          j = (i-1)*2 + 1;
	  hndl = surface(Time(j:j+1),y_param(GATES,j:j+1),Ti(GATES,j:j+1));
          set(hndl,'EdgeColor','none');
        end;

	if exist('TI_SCALE')
		set(gca,'CLim',TI_SCALE);
	end;
	if (exist('START_TIME') & exist('END_TIME'))
		set(gca,'XLim',[toepoch(START_TIME) toepoch(END_TIME)]/3600.0);
	end;
	if exist([Y,'_SCALE'])
		set(gca,'YLim',eval([Y,'_SCALE']));
	end;		
	hmaxis(STime);
	if add_plot == n_tot
		xlabel('UNIVERSAL TIME');
	end;
	ylabel(YTitle);
	my_colorbar;
	text('String','Ion Temperature (K)','Units','Normalized','Position',[1.16 0.5],...
			 'HorizontalAlignment','Center','Rotation',-90,'FontSize',8);
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot Vi

if option(4)
	add_plot = add_plot+1;
	axeVi = subplot(n_tot+1,1,add_plot+1);
	set(axeVi,'Position',[0.1 0.05+(n_tot-add_plot)*(height+0.03) 0.8 height]);
	set(axeVi,'FontSize',8);
	set(axeVi,'Box','on');
	grid on;
           
	s = size(Status);
	
	if OLD_STATUS ~= PLOT_STATUS
		for i=1:s(1)
			for j=1:s(2)
				if Status(i,j) > PLOT_STATUS
					Vi(i,j) = nan;
				end;
				if PLOT_STATUS == 0.5 
                                if ~(Vierr(i,j) >= VIERR_SCALE(1) &...
                                     Vierr(i,j) <= VIERR_SCALE(2))
					Vi(i,j) = nan;
				end;
				end;
			end;
		end;
	end;

	for i=1:s(2)/2
          j = (i-1)*2 + 1;
	  hndl = surface(Time(j:j+1),y_param(GATES,j:j+1),Vi(GATES,j:j+1));
          set(hndl,'EdgeColor','none');
        end;

	if exist('VI_SCALE')
		set(gca,'CLim',VI_SCALE);
	end;
	if (exist('START_TIME') & exist('END_TIME'))
		set(gca,'XLim',[toepoch(START_TIME) toepoch(END_TIME)]/3600.0);
	end;
	if exist([Y,'_SCALE'])
		set(gca,'YLim',eval([Y,'_SCALE']));
	end;		
        hmaxis(STime);
	if add_plot == n_tot
		xlabel('UNIVERSAL TIME');
	end;
	ylabel(YTitle);
	my_colorbar;
	text('String','Ion Velocity (m/s)','Units','Normalized','Position',[1.16 0.5],...
			 'HorizontalAlignment','Center','Rotation',-90,'FontSize',8);
	text('String','(positive away)','Units','Normalized','Position',[1.14 0.5],...
                         'HorizontalAlignment','Center','Rotation',-90,'FontSize',8);

end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if length(WHICH_PARAM) > 2
	set(gcf,'PaperPosition',[1.3254 3.5533 18.331 22.561]);
end;
if length(WHICH_PARAM) == 2
	set(gcf,'PaperOrientation','landscape');
	set(gcf,'PaperPosition',[0.63452 0.79315 26.65 19.987]);
end;
if background ==1
	set(gcf,'Visible','off');
end
if auto_print == 1
	eval(['print ',print_name,' -dpsc2']);
end
