function y=ctdplot(timeu,ivar,yy,mid,varnum,fl);
% y=ctdplot(time,ivar,yy,mid,varnum,fl)
% time=time vector
% ivar=data vector
% yy = year
% mid = mooring id number
% varnum= variable number (3=temp, 5=salt)
% fl= flag vector for data variable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function routine to try and get around an
% annoying bug in the batch processing of
% plots from a scripts.  The xaxis keeps
% changing from plot to plot.  Try to 
% isolate things in this file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%# Add flag as last parameter for plotting.
%# Need a bounds for the plotting.
%# Only plot no data if we really have no data.
%#
fpath=['/data/span9/bahr/perlprogs/'];
yylim=[5 20
       0  0
       32.5 34.5];
maxylim=yylim(varnum-2,2);
minylim=yylim(varnum-2,1);
% load the time limit file
eval([' load ',fpath,'time.dat;']);
eval([' gtime=time;']);
% Because datetick doesn't work correctly with CRON will change from
% matlab datenum format to year day format for plots
%end_time=cal2yearday(gtime(1),gtime(2),gtime(3),gtime(4),gtime(5),0);
end_time=datenum(gtime(1),gtime(2),gtime(3),gtime(4),gtime(5),0);
ylab=['Sea Surface Temperature (^o C)',
      '                              ',
      '    Sea Surface Salinity      ',];
%	cla reset;
	figure('Visible','off','Position',[200 200 600 270]);
	set(gcf,'Units','inches');
	set(gcf,'PaperPosition',[1.5 1.5 8 6]);
	clear ll;
%# Now we add the flag stuff here
%# We define the flags here to be 0=good, 1=out of bounds
%# 2=filtered bad, 3=no data.
	ll=fl==0;
	lq=fl==3;
% datenum vs year day change
%	xtc=timeu(~lq,:);
	xtc=ydtostr(yy,timeu(~lq,:));
	if isempty(xtc)==1,
		xfil=[0 1 1 0];
		yfil=[0 0 1 1];
		plot(xfil,yfil);
		axis([0 1 0 1]);
		text(0.4,0.5,'NO DATA','FontWeight','bold','FontSize',20);
	else,
%# We want all times.
% datenum vs year day change
%	xtc=timeu;
	xtc=ydtostr(yy,timeu);
% Add code to plot gaps as gaps
	lx=size(xtc,1);
	zc=[xtc(1)];
	iz=[ivar(1)];
	fg=[fl(1)];
	for i=1:lx-1,
		cx=xtc(i+1)-xtc(i);
		if cx > 0.008,
			zc=[zc
			    xtc(i+1)
			    xtc(i+1)];
			fg=[fg
    			    1
  			    fl(i+1)];
			iz=[iz
			    NaN
			    ivar(i+1)];
		else,
			zc=[zc
			    xtc(i+1)];
			iz=[iz
			    ivar(i+1)];
			fg=[fg
			    fl(i+1)];
		end
	end
	xtc=zc;
	ivar=iz;
	fl=fg;
	ygood=ivar;
	ybad=ivar;
	yoff=ivar;
	ll=fl==0;
%# Turn all bad values to NaN's in ygood
	ygood(~ll)=NaN;
%# Turn all good values in ybad and yoff to NaN's
	ybad(ll)=NaN;
	yoff(ll)=NaN;
%# Find which values fall outside of the plotting bounds
	lb=(ybad > maxylim)|(ybad < minylim);
%# Next remove bad values which still plot
	yoff(~lb)=NaN;
	ybad(lb)=NaN;
%# Now set all off plot values as maxylimit values
	if(varnum==3),
		yoff(lb)=maxylim-0.1;
	end
	if(varnum==5),
		yoff(lb)=maxylim-0.05;
	end
	xmi=min(xtc);
	xma=end_time;
	dxt=xma-xmi;
	set(gca,'XLimMode','manual');
	plot(xtc,ygood,'b');
	hold on;
	plot(xtc,ygood,'b.');
	plot(xtc,ybad,'ro-');
	plot(xtc,yoff,'r^');
	if dxt < 7.0,
		xmi=xma-7;
	end
% Datenum vs year day changes
	tiz=[floor(xmi):ceil(xma)];
	set(gca,'XTick',tiz);
	hold on	
	datetick('x',6);
	set(gca,'XLim',[xmi xma]);
	set(gca,'Ylim',[minylim maxylim]);
	grid on;
	end
	xlabel(['Date (',num2str(yy),')'],'FontWeight','bold','FontSize',20);
	ylabel(ylab(varnum-2,:),'FontWeight','bold','FontSize',20);
	set(gca,'FontWeight','bold','FontSize',20);
	set(gca,'Box','on');
	pltnm=[fpath,'m',num2str(mid),'_ctdvar',num2str(varnum),'.ppm'];
	eval(['print -dppm ',pltnm,';']);
	hold off
	clf;
	clear;
	close all hidden;
	close all;
