function y=metplot(timev,ivar,yy,mid,varnum,flag);
% y=metplot(timev,ivar,yy,mid,varnum,flag);
% timev=time vector
% ivar=vector of variable data to plot
% yy=year
% mid=mooring id
% varnum= variable numer
% flag= flag array relevant to variable to plot
%!!!!!!Note code will require a file time.dat for current
% GMT time to plot backwards from.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%# Need to have an array of min and max y limits for plotting.
%# This is especially true since we can nolonger use that values
%# sent to bound the plotting as bad data will be included.
%###################
%# Insert plotting limits here
%###################
%#
fpath=['/data/span9/bahr/perlprogs/'];
yylim=[  8   18
        20  100
       980 1040
         0   50
         0  365
	 0  365
         0  365];
maxylim=yylim(varnum-1,2);
minylim=yylim(varnum-1,1);
ylab=['Air Temperature (^o C)',
      'Relative Humidity (%) ',
      '   Air Pressure (mb)  ',
      '   Wind Speed (m/s)   ',
      ' Wind Direction (Deg) ',
      ' Buoy Heading (Deg)   ',
      ' Rel. Wind Dir. (Deg) '];
	cla reset;
	figure('Visible','off','Position',[200 200 600 270]);
	set(gcf,'Units','inches');
	set(gcf,'PaperPosition',[1.5 1.5 8 6]);
eval(['load ',fpath,'time.dat;']);
eval(['gtime=time;']);
% Datetick won't run under CRON for matlab 5.3 Use Year Day
%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);
%# First we see if we have any values that are considered good.
%# Define flags 0=good, 1=bad out of bounds, 2=bad filter 3=nodata
	ll=flag==0;
	lq=flag==3;
	if size(timev,1)==0,
		xtc=[];
	else,
%		xtc=timev;
		xtc=ydtostr(yy,timev);
	end
%# Now we have to decide what the ICON partners want.  Do they
%# want to have plots of all bad data or just to say that no
%# data there is no data.
	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,
%		xtc=timev;
		xtc=ydtostr(yy,timev);
%# Now we need to put code here to fill in the gaps in time.
% We have data...
% Add blanks to the array to plot data gaps as gaps...
		lx=size(xtc,1);
		zc=[xtc(1)];
		iz=[ivar(1)];
		fg=[flag(1)];
		for i=1:lx-1,
			cx=xtc(i+1)-xtc(i);
% gap greater than 10 min put in a NaN
			if (cx > 0.008),
				zc=[zc
			    	    xtc(i+1)
			            xtc(i+1)];
%# we add a duplicate to the time.  We also add
%# a blank to the data.  We will add a bad flag also
%# so that all arrays have the same length.
				iz=[iz 
			            NaN
			            ivar(i+1)];
				fg=[fg
                                    1
                                    flag(i+1)];
			else,
				zc=[zc
			            xtc(i+1)];
				iz=[iz
			    	    ivar(i+1)];
				fg=[fg
				    flag(i+1)];
			end
		end
		xtc=zc;
		ivar=iz;
		flag=fg;
		clear ll;
		ygood=ivar;
		ybad=ivar;
		yoff=ivar;
		ll=flag==0;
%# Set all of the non good values in array ygood to NaN so
%# that they don't plot.
		ygood(~ll)=NaN;
%# Set all of the good values in the array ybad to NaN so
%# that they don't plot.
		ybad(ll)=NaN;
%# Find out which bad values fall outside of the plotting bounds.
		lb=((ybad > maxylim)|(ybad < minylim));
%# First get all of the bad values
		yoff(ll)=NaN;
		ybad(lb)=NaN;
%# Next remove the values that still plot
		yoff(~lb)=NaN;
%# Now set all the other values (those that plot out of range) to
%# the top value of the y-axis for plotting as symbols there.
		switch(varnum)
			case 2,
				yoff(lb)=maxylim-0.5;
			case 3,
				yoff(lb)=maxylim-1;
			case 4,
				yoff(lb)=maxylim-2;
			case 5,
				yoff(lb)=maxylim-1;
			otherwise,
				yoff(lb)=maxylim-2;
		end 
		
% If we have no data plot an empty plot with the words no data in it.
		xmi=min(xtc);
		xma=end_time;
		dxt=xma-xmi;
		set(gca,'XLimMode','manual');
%# Really need realistic limits for y axis....
		set(gca,'YLim',[minylim maxylim]);
		hold on
		if varnum >=6,
			plot(xtc,ygood,'b*');
			hold on
			plot(xtc,ybad,'r*');
			plot(xtc,yoff,'r^');	
			if dxt < 7.0,
				xmi=xma-7;
			end
			tiz=[floor(xmi):ceil(xma)];
			set(gca,'XTick',tiz);	
			hold on
			datetick('x',6);
			set(gca,'XLim',[xmi xma]);
		else,
			handl=plot(xtc,ygood,'b');
			set(handl,'clipping','off');
			hold on
			plot(xtc,ygood,'b.');
			plot(xtc,ybad,'ro-');
			plot(xtc,yoff,'r^');
			if dxt < 7.0,
				xmi=xma-7;
			end
			tiz=[floor(xmi):ceil(xma)];
			set(gca,'XTick',tiz);	
			hold on
			datetick('x',6);
			set(gca,'XLim',[xmi xma]);
		end
		grid on;
	end
	xlabel(['Date (',num2str(yy),')'],'FontWeight','bold','FontSize',20);
	ylabel(ylab(varnum-1,:),'FontWeight','bold','FontSize',20);
		set(gca,'FontWeight','bold','FontSize',20);
		set(gca,'Box','on');
		pltnm=['m',num2str(mid),'_metvar',num2str(varnum),'.ppm'];
		eval(['print -dppm ',fpath,pltnm,';']);
		clf;
		close all;
		close all hidden;
		return
