%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modification to become more general% 
% for loading indeterminate number of% 
% microcats. Start July 8 1999 %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% M-file to process buoy m"x" metsys data.
% Simple bounds checks have already been
% applied to the data which have been output
% from the perl script.  This step is
% to concattenate the data and to create a
% much large files and do more sophisticated (sp?)
% processing of the data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define an array
fpath=['/data/span9/bahr/perlprogs/'];
mond=[0,31,59,90,120,151,181,212,243,273,304,334];
monn=[31,28,31,30,31,30,31,31,30,31,30,31];
monc=['jan',
      'feb',
      'mar',
      'apr',
      'may',
      'jun',
      'jul',
      'aug',
      'sep',
      'oct',
      'nov',
      'dec'];
dtt=10.0/60.0/24.0;
% open file with mooring number which we will be working on
eval(['load ',fpath,'mooringid.dat']);
eval(['mid=mooringid;']);
% grab time
eval(['load ',fpath,'time.dat']);
eval(['gtime=time;']);
year=gtime(1);
month=gtime(2);
day=gtime(3);
hour=gtime(4);
minu=gtime(5);
sec=0;
yday=gtime(6);
yearo=year;
% create filename for the current month
pathm1=['/data/span9/bahr/m',num2str(mid),'/tstring/',num2str(year),'/'];
patho=pathm1;
fnm1=['m',num2str(mid),'_tstring_',monc(month,:),'_',num2str(year)];
% see if the file exists
fid=fopen([pathm1,fnm1,'.mat'],'r+');
% if the file exists then fid will be greater than 2.  If the file
% doesn't exist we want to create it but we also want to load
% the previous months file.  We also have to careful around the year
% change.
% set up seperate arrays for each t string sensor so we can hang onto
% time changes....
if fid > 2,
	fclose(fid);
	rw1=size(who,1);
	eval(['load ',pathm1,fnm1,'.mat']);
	rw2=size(who,1);
	nvn=(rw2-rw1-2)/2;
	for in=1:nvn,
		eval(['b',num2str(in),'=t',num2str(in),'n;']);
		eval(['fb',num2str(in),'=f',num2str(in),'n;']);
		eval(['clear t',num2str(in),'n;']);
		eval(['clear f',num2str(in),'n;']);
	end
	eval(['bt=timen;']);
end
% Check to see if we want to load the old file.  We assume that data
% from the oasis mooring will not be older than 7 days.
if day < 8,
	if month==1,
		yearo=year-1;
		patho=['/data/span9/bahr/m',num2str(mid),'/tstring/',num2str(yearo),'/'];
		fnm1o=['m',num2str(mid),'_tstring_dec_',num2str(yearo)];
	else,
		yearo=year;
		patho=pathm1;
		fnm1o=['m',num2str(mid),'_tstring_',monc(month-1,:),'_',num2str(year)];
	end
% check to see if old file is there.  If it is load it.
%# Add code to load the flag arrays.
	fido=fopen([patho,fnm1o,'.mat'],'r+');	
	if fido > 2,
		fclose(fido);
		rw3=size(who,1);
		eval(['load ',patho,fnm1o,'.mat']);
		rw4=size(who,1);
		nvo=(rw4-rw3-2)/2;
		nvo=round(nvo);
		for in=1:nvo,
			eval(['a',num2str(in),'=t',num2str(in),'n;']);
			eval(['fa',num2str(in),'=f',num2str(in),'n;']);
			eval(['clear t',num2str(in),'n;']);
			eval(['clear f',num2str(in),'n;']);
		end
		eval(['at=timen;']);
	end
end
% create one array of data.  We have to be careful doing this at the
% year change.  Since the year is not explicitely stated in the file.
% Do a trick to handle year changes.  If we have a year change yearo!=year
% then we subtract the max days from the previous year?
maxd=366;
if yearo~=year,
	maxd=366;
	if rem(yearo,4)==0,
		maxd=maxd+1;
	end
% Check to see if we have a variable "at"
% if we do then compute the times as follows for the number of t arrays...
	if exist('at','var')==1,
		if isempty(at)==0,
			for in=1:nvo,
			  eval(['a',num2str(in),'(:,1)=a',num2str(in),'(:,1)-maxd;']);
			end
			at=at-maxd;
		end
	end
end
% concatenate the arrays together
%# Add line to handle flags
%# Now we need to add arrays to handle if "a arrays" or "b arrays" don't exist
nubbin=0;
if exist('nvn','var')==1,
	nub=1;
	nubbin=nvn;
else,
	nub=0;
end
if exist('nvo','var')==1,
	nub=nub+2;
	if nvo > nubbin,
		nubbin=nvo;
	end
end
switch nub
	case 1,
		for i=1:nubbin,
			eval(['t',num2str(i),'=[b',num2str(i),'];']);
			eval(['f',num2str(i),'=[fb',num2str(i),'];']);
		end
		timet=[bt];
	case 2,
		for i=1:nubbin,
			eval(['t',num2str(i),'=[a',num2str(i),'];']);
			eval(['f',num2str(i),'=[fa',num2str(i),'];']);
		end
		timet=[at];
	case 3,
		timet=[at;bt];
		for i=1:nubbin,
			eval(['if exist(''a',num2str(i),''',''var'')==1,',...
			't',num2str(i),'=[a',num2str(i),'];',...
			'f',num2str(i),'=[fa',num2str(i),'];',...
			'else,',...
			't',num2str(i),'=[];',...
			'f',num2str(i),'=[];',...
			'end;']);
			eval(['if exist(''b',num2str(i),''',''var'')==1,',...
		         't',num2str(i),'=[t',num2str(i),';b',num2str(i),'];',...
			 'f',num2str(i),'=[f',num2str(i),';fb',num2str(i),'];',...
			'end']);
		end
	otherwise,
		timet=[];
		t1=[];
		t2=[];
end
% we should now have the old matlab process tstring files loaded.
% The next step is to load the new files with data to be processed.
ff1=['m',num2str(mid),'_tstring.dat'];
ff2=['m',num2str(mid),'_tstringo.dat'];
if (month==1)&(day >=2),
	fod1=fopen([pathm1,ff1],'r');
	fod2=fopen([pathm1,ff2],'r');
else,
	fod1=fopen([pathm1,ff1],'r');
	fod2=fopen([patho,ff2],'r');
end
if fod1 > 2,
	fclose(fod1);
	eval(['load ',pathm1,'m',num2str(mid),'_tstring.dat']);
	eval(['m1=m',num2str(mid),'_tstring;']);
	if((mid==4)&(year==1999)),
		ll=m1(:,1) > 236.90;
		m1=m1(ll,:);
	end
else
	m1=[];
end
if fod2 > 2,
	fclose(fod2);
	if(month==1)&(day >=2),
	eval(['load ',pathm1,'m',num2str(mid),'_tstringo.dat']);
	else,
	eval(['load ',patho,'m',num2str(mid),'_tstringo.dat']);
	end
	eval(['m1o=m',num2str(mid),'_tstringo;']);
	if((mid==4)&(year==1999)),
		ll=m1o(:,1) > 236.90;
		m1o=m1o(ll,:);
	end
else,
	m1o=[];
end
% we only have to worry about day ==1 on this part.  But that doesn't
% cover the posibility that data from before may be in one of the old
% files?
if ((day==1)&(month==1)&(size(m1o,1) > 0)),
	m1o(:,1)=m1o(:,1)-maxd;
end
% we have a problem here at the year change.  We need to worry about.
% since the files only look at year day and not have the year in it.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%!!!!!!!!!!!!! Before we do this we need to check if both arrays are the same 
% size!
[ll1 lw1]=size(m1);
[ll2 lw2]=size(m1o);
if lw1 < lw2,
	mm=lw2-lw1;
	mtt=-99.*ones(ll1,mm);
	m1=[m1 mtt];
end
if lw1 > lw2,
	mm=lw1-lw2;
	mtt=-99.*ones(ll2,mm);
	m1o=[m1o mtt];
end
% now we have them the same size but one is filled with blanks.	
m1=[m1o
    m1];
% we now have the data.  Now we need to check times and sort times?
% the format for tstring files will be time, id, depth, temp, cond
% for the 10 sensors.  We have individual times for each microcat.
% do a loop for the sensors and sort each loop of data.
% only do the following if we have nonzero m1
if (isempty(m1) == 0),
% sort oasis time first
	dt=diff(m1(:,1));
	[ineg]=find(dt < 0);
	if isempty(ineg)==0,
		[newtt index]=sort(m1(:,1));
		m1=[newtt m1(index,[2:size(m1,2)])];
	end
	dt=diff(m1(:,1));
	idi=find(dt==0);
	m1(idi,:)=[];
% At this point we have sorted the whole mess by the oasis times.
% Now since at each oasis time microcats transmit latest value
% we might assume that the for microcats are sorted except for 
% bad values.
% now check times for each microcat and against the oasis time.
%%%%%%%%%%%%%
% Here we need to know how many microcats we have!
% but we do know that because the stripped file will have 
% the format oasist,m1time,m1id,m1p,m1T,m1c,m2time,m2id,m2T,....
% so we can compute the number from the width of the file.
%%%%%%%%%%%%end edit July 8 1999
	nmc=(size(m1,2)-1)/5;
	nmc=round(nmc);
	for muk=1:nmc,
		dot=abs(m1(:,1)-m1(:,(2+(muk-1)*5)));
%# allow a period of twenty minutes to be between microcat and oasis time.
		idot=find(dot > 0.014);
		m1(idot,(2+(muk-1)*5))=m1(idot,1);
	end
	for itc=1:nmc,
		clear dt; clear ineg;
		ict=2+(itc-1)*5;
		dt=diff(m1(:,ict));
%check for neg values to see if we have times out of order.
		[ineg]=find(dt < 0);
% we want to do all the time check stuff as vetor/matrix checks and
% try to avoid costly loops and redoing stuff that we don't need to
% do more than once.
		if isempty(ineg)==0,
% we need to do a sort of the file
% just do this for the time being.  If things are the way we think
% they are this should nolonger be here.....
			[newt1 idex1]=sort(m1(:,ict));
			m1(:,[ict:ict+4])=[newt1 m1(idex1,(ict+1):(ict+4))];
		end
	end
% again below we have to worry about the year change.  This could
% be a recipe for disaster.  But try to get something that runs now
% and patch up the year bug later.
% data is now sorted.  Now we need to find which values are in the
% cleaned data file already and remove get send them to the nowhere.
% we only want to process what we have to.  If I can think up a vectorized
% method for doing this, then this should be done before the sort to make
% the sort faster (fewer points sort).
% compute current year day time
	ydnow=mond(month)+day+hour/24+minu/(24*60);
	if (rem(year,4)==0)&(month > 2),
		ydnow=ydnow+1;
	end
	ydnowl=ydnow-1;
% find the minimum time in the currently captured files
% Now we have to loop for each microcat...
% No! We only do it to remove the oasis times....
	itc=1;
	tmin=min(m1(:,1));
	lt=length(timet);
	if lt > 0,
		tmax=max(timet);
	else,
		tmax=0;
	end
	lm1=size(m1(:,1),1);
	im=zeros(lm1,1);
	lt=size(timet,1);
	if lt > 0,
	        for i=1:lm1,
			if m1(i,1) > tmax,
				i=lm1;
			end
			id=find(timet==m1(i,1));
			if isempty(id)==0,
				im(i)=1;
			end
		end
	end
	m1(logical(im),:)=[];
end
%# After we change the size of m1 we need to check if it is empty for
%# continuation.
if (isempty(m1)==0),
%# Now that we have removed all the surpuflus points we go about setting up
%# the new flag arrays.  We also do a bounds check on the data.
	tft=zeros(size(m1));	
% Now set the flags to 1 for out of bounds data.;
% currently only checking temperatures
	for ib=1:nmc,
%# First define out of bounds data and flag that data.
		ll=((m1(:,(5+(ib-1)*5)) > 22)|(m1(:,(5+(ib-1)*5)) < 5));
		tft(ll,(5+(ib-1)*5))=1;
%# Then define data which is missing (Temperature).
		ll=(m1(:,(5+(ib-1)*5)) == -99);
		tft(ll,(5+(ib-1)*5))=3;
%# Then define data which is missing (conductivity).
		ll=((m1(:,(6+(ib-1)*5)) > 6)|(m1(:,(6+(ib-1)*5)) < 3));
		tft(ll,(6+(ib-1)*5))=1;
		ll=(m1(:,(6+(ib-1)*5)) == -99);
		tft(ll,(6+(ib-1)*5))=3;
%# Second define out of bounds data.
	end
		
% create an array without points we already have.....
% We need to worry that for times we may have blanks in the original but we want
% to replace them with new data (Do we store *.mat files without blanks?).
% we can't do the next step for the t-string we must also worry if the
% above eval's will work.
% Now we need to stuff our values in.  We also want to keep a "binary"
% mask of where the values are put in for computing our filters later.
% compare against current day -1 and see how much of old file
% we need to retain before stuffing in our values in a slow loop.
% NOTE: ABOVE needs to be modified for the transition that occurs at
% the new year.
% now we have paired down the file somewhat.  The next step is to
% actually loop and insert the data.  We set up some indicies first
% before actually doing the loop.
	i=0; j=0;
% all the t'x' array will be the same size so we only need one to check...
	lt1=size(t2,1);
	bmask=zeros(lt1,1);
	lnew=size(m1,1);
	k=1;
	while(k <= lnew),
		lt1=size(t2,1);
		if lt1==0,
			timet=m1(:,1);
			k=lnew+1;
			for mm=1:nmc,
				st=2+(mm-1)*5;
				sp=6+(mm-1)*5;
				eval(['t',num2str(mm),'=[m1(:,[st:sp])',...
					' -99.*ones(size(m1,1),1)];']);
%# Add in flags.  Note: set salt flag to temp flag for new data at this
%# point.  Also, no flag for time.
				eval(['f',num2str(mm),'=[tft(:,[st+1:sp])',...
				      ' tft(:,st+3)];']);
			end
			bmask=[bmask
		               ones(lnew,1)];
		else,
			[index]=find(timet(:,1) < m1(k,1));
			idmz=max(index);
			if idmz==lt1,
				timet=[timet
                                       m1([k:lnew],1)];
				for mm=1:nmc,
					st=2+(mm-1)*5;
					sp=6+(mm-1)*5;
				eval(['t',num2str(mm),'=[t',num2str(mm),';',...
 				      ' m1([k:lnew],[st:sp]) ',... 
				      ' -99.*ones(length(k:lnew),1)];']);
				eval(['f',num2str(mm),'=[f',num2str(mm),';',...
				      ' tft([k:lnew],[st+1:sp]) ',...
				      ' tft([k:lnew],[st+3])];']); 
				end
				bmask=[bmask
			       	       ones(length(k:lnew),1)];
				k=lnew;
			else,
				timet=[timet([1:idmz])
				      m1(k,1)
				      timet([idmz+1:lt1])];
				for mm=1:nmc,
					st=2+(mm-1)*5;
					sp=6+(mm-1)*5;
			eval(['t',num2str(mm),'=[t',num2str(mm),...
			      '([1:idmz],:); m1(k,[st:sp]) -99;',... 
			      ' t',num2str(mm),'([idmz+1:lt1],:)];']);
			eval(['f',num2str(mm),'=[f',num2str(mm),...
			      '([1:idmz],:); tft(k,[st+1:sp]) tft(k,st+3);',...
			      ' f',num2str(mm),'([idmz+1:lt1],:)];']);
				end
				bmask=[bmask([1:idmz])
				       1
			               bmask([idmz+1:lt1])];
		 	end
		end
		k=k+1;
	end
end
% If the above works, bmask should be a file with 1's where
% we have inserted data and should be the same length as
% the array met with the inserted data.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% We have a much bigger problem...How to coordinate all of the
% data.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now we can apply our gradient filter to the data (?)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get the indecies of the insertion points
if isempty(m1)==0,
	for i=1:nmc,
		eval(['ck=t',num2str(i),';']);
		eval(['cf=f',num2str(i),';']);
		nwhat=sigfilt(ck(:,4),ck(:,1),cf(:,3),1,2.25,9,6,0);
		eval(['f',num2str(i),'(:,3)=nwhat(:);']);
	end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Need to add a calculation of salinity from the conductivity 
% here after we filter the conductivity.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%==>ADD SALT CALC HERE<==
c35=4.2914;	% S/m
% Why compute salt here?  Why not compute it earlier and then put the
% values in?  Want to filter before we compute salt?
%##########################################################
%# Only do the following if there is something to plot...
%# So, we need to check the size of t1 all arrays are the
%# defined to be the same size even if they are empty or full
%# of missing data.
%##########################################################
if(isempty(t2)==0),
	for mm=1:nmc,
		clear crat; clear ccf;
		eval(['crat=t',num2str(mm),'(:,5)./c35;']);
%# grab the flag for salts (same as temp at this point)
		eval(['ccf=f',num2str(mm),'(:,4);']);
%# set flags and watch for computing bounds
		ll=(ccf == 0); %# bad values are flagged.
		if isempty(crat(ll))==1,
			eval(['t',num2str(mm),'(:,6)=-99.*ones(size(crat,1),1);']);
		else,	
		  eval(['t',num2str(mm),'(ll,6)=sw_salt(crat(ll),t',num2str(mm),...
                        '(ll,4),t',num2str(mm),'(ll,3));']);
		end
%# Now we want to check the values and change bound flags accordingly.
       	  eval(['ls=((t',num2str(mm),'(:,6) > 32)&(t',num2str(mm),'(:,6) < 35));']);
		eval(['f',num2str(mm),'(~ls,5)=1;']);
%# Add a gradient filter check on the salt values.  
%# Say values can not change by 0.25
		clear kko; clear kkf; clear jjf;
		eval(['kko=t',num2str(mm),';']);
		eval(['jjf=f',num2str(mm),';']);
		kkf=sigfilt(kko(:,6),kko(:,1),jjf(:,5),1,2.25,9,6,0);
		eval(['f',num2str(mm),'(:,5)=kkf(:);']);
%# Flag ones where we have no data as missing not out of bounds.
	    eval(['ls=((f',num2str(mm),'(:,3)==3)|(f',num2str(mm),'(:,4)==3));']);
		eval(['f',num2str(mm),'(ls,5)=3;']);
	end	%# end of for loop
end 	%# end of if isempty(t1)
% Need to save the appropriate data here!
% we need to be aware of year and month breaks
% in our data if we want to save it correctly.
if isempty(t2)==0,
	if day > 8,
		for mm=1:nmc,
			eval(['t',num2str(mm),'n=t',num2str(mm),';']);
			eval(['f',num2str(mm),'n=f',num2str(mm),';']);
		end
		timen=timet;
		outarr=['save ',pathm1,fnm1,' timen '];
		for mm=1:nmc,
			outarr=[outarr,' t',num2str(mm),'n f',num2str(mm),'n '];
		end
		eval(outarr);
	else
		if month==1,
			ll=timet < 0;
			timen=timet(ll);
			timen=timen+maxd;
			for mm=1:nmc,
			  eval(['t',num2str(mm),'n=t',num2str(mm),'(ll,:);']);
			  eval(['t',num2str(mm),'n(:,1)=t',...
			        num2str(mm),'n(:,1)+maxd;']);
			  eval(['f',num2str(mm),'n=f',num2str(mm),'(ll,:);']);
			end
			outarr=['save ',patho,fnm1o,' timen '];
			for mm=1:nmc,
			outarr=[outarr,' t',num2str(mm),'n f',num2str(mm),'n '];
			end
			eval(outarr);
			clear timen;
			timen=timet(~ll);
			for mm=1:nmc,
				eval(['clear t',num2str(mm),'n;']);
				eval(['clear f',num2str(mm),'n;']);
			eval(['t',num2str(mm),'n=t',num2str(mm),'(~ll,:);']);
			eval(['f',num2str(mm),'n=f',num2str(mm),'(~ll,:);']);
			end
			outarr=['save ',pathm1,fnm1,' timen '];
			for mm=1:nmc,
			outarr=[outarr,' t',num2str(mm),'n f',num2str(mm),'n '];
			end
			eval(outarr);
		else
			dmax=mond(month)+1;
			if (month > 2)&(rem(year,4)==0),
				dmax=dmax+1;
			end
			ll=timet(:,1) < dmax;
			timen=timet(ll,:);
			for mm=1:nmc,
			eval(['t',num2str(mm),'n=t',num2str(mm),'(ll,:);']);
			eval(['f',num2str(mm),'n=f',num2str(mm),'(ll,:);']);
			end
			outarr=['save ',patho,fnm1o,' timen '];
			for mm=1:nmc,
			outarr=[outarr,' t',num2str(mm),'n f',num2str(mm),'n '];
			end
			eval(outarr);
			clear timen;
			timen=timet(~ll);
			for mm=1:nmc,
				eval(['clear t',num2str(mm),'n;']);
				eval(['clear f',num2str(mm),'n;']);
			eval(['t',num2str(mm),'n=t',num2str(mm),'(~ll,:);']);
			eval(['f',num2str(mm),'n=f',num2str(mm),'(~ll,:);']);
			end
			outarr=['save ',pathm1,fnm1,' timen '];
			for mm=1:nmc,
			outarr=[outarr,' t',num2str(mm),'n f',num2str(mm),'n '];
			end
			eval(outarr);
		end
	end
end %# end of if isempty(t1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now process the last days worth of data for display
% by various web pages.
% 1) compute 24 hour means and deviations along with
% current values.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ytval=[];
ysval=[];
dval=[];
ufp=[];
if(isempty(t2)== 0),
	ydp=ydnow-7;
	if ydp < 0,
		ydp=ydp-1;
	end
	for mi=1:nmc,
		eval(['ll=(t',num2str(mi),'(:,1) > ydnowl)&(t',...
                      num2str(mi),'(:,1) <= ydnow);']);
		eval(['lp=(t',num2str(mi),'(:,1) > ydp)&(t',...
                      num2str(mi),'(:,1) <= ydnow);']);
		eval(['t',num2str(mi),'d=t',num2str(mi),'(ll,:);']);
		eval(['f',num2str(mi),'d=f',num2str(mi),'(ll,:);']);
		eval(['t',num2str(mi),'p=t',num2str(mi),'(lp,:);']);
		eval(['f',num2str(mi),'p=f',num2str(mi),'(lp,:);']);
	end
% now to write out a simple table of numbers for a perl
% script to load and create an htlm table from
	bad=-99;
	ylab=['Temperature (^o C)',
      	      '   Salinity       '];
	if isempty(t2p)==0,
	for i=1:nmc,
		eval(['time=t',num2str(i),'p(:,1);']);
		eval(['tval=t',num2str(i),'p(:,4);']);
		eval(['sval=t',num2str(i),'p(:,6);']);
		eval(['ytval=[ytval; t',num2str(i),'p(size(t',num2str(i),'p(:,1),1),4)];']);
		eval(['dval=[dval; t',num2str(i),'p(size(t',num2str(i),'p(:,1),1),3)];']);
		eval(['ysval=[ysval; t',num2str(i),'p(size(t',num2str(i),'p(:,1),1),6)];']);
		eval(['ufp=[ufp; f',num2str(i),'p(size(t',num2str(i),'p(:,1),1),:)];']);
	end
	tprof(ytval,ysval,dval,mid,ufp);
	tdplot(t1p,t2p,t3p,t4p,t5p,year,mid,1,1,f1p,f2p,f3p,f4p,f5p);
	tdplot(t1p,t2p,t3p,t4p,t5p,year,mid,1,2,f1p,f2p,f3p,f4p,f5p);
	if mid < 4,
	tdplot(t6p,t7p,t8p,t9p,t10p,year,mid,2,1,f6p,f7p,f8p,f9p,f10p);
	tdplot(t6p,t7p,t8p,t9p,t10p,year,mid,2,2,f6p,f7p,f8p,f9p,f10p);
	else,
	tdplot4(t6p,t7p,t8p,t9p,t10p,t11p,year,mid,2,1,f6p,f7p,f8p,f9p,f10p,f11p);
	tdplot4(t6p,t7p,t8p,t9p,t10p,t11p,year,mid,2,2,f6p,f7p,f8p,f9p,f10p,f11p);
	end
	dtplot(t1p(:,1),year,mid,3);
	end
%end
%%%%%%%%%%%
% Insert writeout data for contours here
tout=[];
ffut=[];
for ik=1:nmc,
	eval(['tout=[tout; t',num2str(ik),'p];']);
	eval(['ffut=[ffut; f',num2str(ik),'p];']);
end
%# Define the good data with flags set to 0.
if isempty(tout)==0,
	lt=(ffut(:,3) ==0);
	if isempty(tout(lt,1))==0,
		y=contourt(tout(lt,1),tout(lt,3),tout(lt,4),year);
	end
%# Define the good salt data with flags set to 0.
	ll=(ffut(:,5)==0);
	if isempty(tout(ll,1))==0,
		y=contours(tout(ll,1),tout(ll,3),tout(ll,6),year);
	end
	cchar=[fpath,'mt.dat'];
	ttt=tout(lt,[1 3 4]);
	writedata(ttt,cchar);
%end
	cchar=[fpath,'ms.dat'];
	ttt=tout(ll,[1 3 6]);
	writedata(ttt,cchar);
end
%%%%%%%%%%%
ffn=[fpath,'m',num2str(mid),'_nowts.dat'];
% write out data for time
nodat=3;
if isempty(t2d)==0,
	zt=t2d(size(t2d,1),1);
else,
	zt=day;
end
	zt=ydtostr(year,zt);
	zts=datestr(zt,0);
	fprintf(ffn','%s\n',zts(1,:));
	for i=1:nmc,
		clear tempt; clear temps;
		eval(['ln=(size(t',num2str(i),'d,1));']);
		eval(['lt=(f',num2str(i),'d(:,3)==0);']);
		eval(['ls=(f',num2str(i),'d(:,5)==0);']);
		eval(['tempt=t',num2str(i),'d(lt,4);']);
		eval(['temps=t',num2str(i),'d(ls,6);']);
	if isempty(tempt)==0,
		eval(['fprintf(ffn,''%9.4f %3d %9.4f ''',...
		      ',t',num2str(i),'d(ln,4),f',...
		      num2str(i),'d(ln,3),mean(tempt));']);
		fprintf(ffn,'%9.4f %9.4f ',std(tempt),min(tempt));
		fprintf(ffn,'%9.4f %3d ',max(tempt),length(tempt));
	else,
		eval(['fprintf(ffn,''%9.4f %3d %9.4f ''',...
	        ',bad,nodat,bad);']);
		fprintf(ffn,'%9.4f %9.4f ',bad,bad);
		fprintf(ffn,'%9.4f %3d ',bad,bad);
	end
	if isempty(temps)==0,
		eval(['fprintf(ffn,''%9.4f %3d %9.4f ''',...
		      ',t',num2str(i),'d(ln,6),f',...
		      num2str(i),'d(ln,5),mean(temps));']);
		fprintf(ffn,'%9.4f %9.4f ',std(temps),min(temps));
		fprintf(ffn,'%9.4f %3d \n',max(temps),length(temps));
	else,
		eval(['fprintf(ffn,''%9.4f %3d %9.4f ''',...
		      ',bad,nodat,bad);']);
		fprintf(ffn,'%9.4f %9.4f ',bad,bad);
		fprintf(ffn,'%9.4f %3d \n',bad,bad);
	end
end
end %# end of if t1 is empty
quit
