function y=tdplot(y1,y2,y3,y4,y5,yy,mid,oi,varnum,fl1,fl2,fl3,fl4,fl5);
% y=tdplot(y1-y5,yy,mid,oi,varnum,fl1-fl5);
% call requires:
% y1-y5   = Variable arrays
% yy      = current year YYYY
% mid     = mooring id number
% oi      = upper(1) or lower(2);
% varnum  = temp(1),salt(2);
% fl1-fl5 = flag arrays
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fpath=['/data/span9/bahr/perlprogs/'];
% car = color array [ blue, red, green, cyan, magenta]
car=['b','r','g','c','m'];
% sym = symbol array [ circle, square, diamond, ex, plus]
sym=['o','s','d','x','+'];
yylim=[ 8 18    5 15
       32 34.2 32 34.2];
eval(['load ',fpath,'time.dat']);
eval(['gtime=time;']);
%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);
if varnum==1,
	ik=4;
	maxylim=yylim(varnum,(oi-1)*2+2);
	minylim=yylim(varnum,(oi-1)*2+1);
else,
	ik=6;
	maxylim=yylim(varnum,(oi-1)*2+2);
	minylim=yylim(varnum,(oi-1)*2+1);
end
%if mid==4,
%	maxylim=30;
%	minylim=0;
%end
xma=-99;
xmi=1e32;
ylab=['Temperature (^o C)',
      '   Salinity       '];
cla reset;
figure('Visible','off','Position',[200 200 600 270]);
set(gcf,'Units','inches');
set(gcf,'PaperPosition',[1.5 1.5 8 6]);
hold on;
zid=0;
% time difference to check for was 10 min (0.008) now
% it is 1 hour! So set check to 1hr 10 minutes.
ddt=0.0486;
for j=1:5,		
	clear ll; clear xtc; clear zzva;
	clear zc; clear iz; clear lq;
%# Set ll to good data
	eval(['ll=fl',num2str(j),'(:,ik-1)==0;']);
%# Set lq to missing data.
	eval(['lq=fl',num2str(j),'(:,ik-1)==3;']);
%	eval(['xtc=y',num2str(j),'(:,1);']);
	eval(['xtc=ydtostr(yy,y',num2str(j),'(:,1));']);
	if (size(xtc(~lq),1) > 0),
		zid=1;
	end
	clear xtc;
%# compute the matlab datestring.
%	eval(['xtc=y',num2str(j),'(:,1);']);
	eval(['xtc=ydtostr(yy,y',num2str(j),'(:,1));']);
%# create temporary arrays to fill up...
	eval(['zzva=y',num2str(j),'(:,ik);']);
	eval(['fc=fl',num2str(j),'(:,ik-1);']);
	lx=size(xtc,1);
%# assume we have some data.
	if (lx > 0)&(zid > 0),
%# set up the initial values of the temporary arrays.
		zc=[xtc(1)];
		iz=[zzva(1)];
		fz=[fc(1)];
%# loop through the data looking for gaps.
		for i=1:lx-1,
			cx=xtc(i+1)-xtc(i);
			if cx > ddt,
				zc=[zc
			    	xtc(i+1)
			    	xtc(i+1)];
				iz=[iz
			    	NaN
			    	zzva(i+1)];
				fz=[fz
				    1
				    fc(i+1)];
			else,
				zc=[zc
			    	xtc(i+1)];
				iz=[iz
			    	zzva(i+1)];
				fz=[fz
				    fc(i+1)];
			end
		end
%# Overwrite the arrays and fill them with "gap filled data."
		xtc=zc;
		zzva=iz;
		fc=fz;
%# Set up the sepcial plotting arrays
		ygood=zzva;
		ybad=zzva;
		yoff=zzva;
%# Define good of gappy points
		ll=(fc==0);
%		if mid==4,
%			ll=ones(size(fc,1));
%		end
%# Set all bad points in ygood to NaN
		ygood(~ll)=NaN;
%# Set all good points to NaN in ybad and yoff
		ybad(ll)=NaN;
		yoff(ll)=NaN;
%# Now find all points outside of plotting area
		lb=((ybad > maxylim)|(ybad < minylim))&(fc~=3);
%# Remove outside points from ybad and set those in yoff to max
%# ylimint.
		ybad(lb)=NaN;
		yoff(~lb)=NaN;
%# Find flags set to 3 missing values and set to NaN
		lmis=(fc==3);
		ybad(lmis)=NaN;
		yoff(lmis)=NaN;
%# Set nonplotting vlaues to maxylimit.
		if(varnum==1),
			yoff(lb)=maxylim-0.5+(j-1)*0.1;
		end
		if(varnum==2),
			if(oi==2),
				yoff(lb)=minylim+0.05-(j-1)*0.01;
			else,
				yoff(lb)=maxylim-0.05+(j-1)*0.01;
			end
		end
		w1=min(xtc);
		w2=max(xtc);
		if xma < w2,
			xma=w2;
		end
		if xmi > w1,
			xmi=w1;
		end
		csym=[ car(j) sym(j)];
% plot good data
		plot(xtc,ygood,car(j));
		plot(xtc,ygood,[car(j) '.']);
% plot bad data in black
		plot(xtc,ybad,'ko-');
		plot(xtc,ybad,[car(j) '.']);
% plot data out of bounds as colored symbol
		plot(xtc,yoff,csym);
	
	end
end
if zid==0,
	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,
% add in gap plotting...
%# Note: xmi and xma are determined above.
	xma=end_time;
	dxt=xma-xmi;
	set(gca,'XLimMode','manual');
	hold on;
	if dxt < 7.0,
		xmi=xma-7;
	end
	tiz=[floor(xmi):ceil(xma)];
	set(gca,'XTick',tiz);	
	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,:),'FontWeight','bold','FontSize',20);
set(gca,'FontWeight','bold','FontSize',20);
set(gca,'Box','on');
	pltnm=['m',num2str(mid),'_tup',num2str(oi),'_var',num2str(varnum),'.ppm'];
eval(['print -dppm ',fpath,pltnm,';']);
clf;
close all;
