%function y=windplot4
% This m-file creates a plotplus script to plot all 4
% mooring winds as a single image.
fpath=['/data/span9/bahr/perlprogs/'];
eval(['load ',fpath,'time.dat']);
year=time(1);
mond=[0,32,60,91,121,152,182,213,244,274,305,335,366];
if(rem(year,4)==0),
	leap=[0 0 1 1 1 1 1 1 1 1 1 1 1];
	ileap=1;
else,
	leap=[0 0 0 0 0 0 0 0 0 0 0 0 0];
	ileap=0;
end
mond=mond+leap;
%eval(['load ',fpath,'mooringid.dat']);
%mid=mooringid;
fid=fopen([fpath,'wind4.ppl'],'w');
fprintf(fid,'pltype,0\n');
fprintf(fid,'pltnme,%swind4.meta\n',fpath(:));
fprintf(fid,'multplt,1,2\n');
fprintf(fid,'6.2\n');
fprintf(fid,'1.5,1.5\n');
fprintf(fid,'1.5\n');
fprintf(fid,'0.25,1\n');
fprintf(fid,'dfltfnt,@tr\n');
fprintf(fid,'window,off\n');
fprintf(fid,'size,11.0,11.0\n');
fprintf(fid,'limits,-99,XLE,on\n');
fprintf(fid,'limits,-99,YLE,on\n');
amax=10;
amin=-10;
for k=[1 2],
	fname=[fpath,'wind1',num2str(k),'.dat'];
	flid(k)=fopen(fname,'r');
	if flid(k) > 0,
		eval(['load wind1',num2str(k),'.dat']);
		eval(['jj=wind1',num2str(k),';']);
		if isempty(jj)==0,
			eval(['ut=wind1',num2str(k),'(:,2);']);
			eval(['vt=wind1',num2str(k),'(:,3);']);
			mau=max(ut); mav=max(vt);
			miu=min(ut); miv=min(vt);
			maxvel=sqrt(mau^2+mav^2);
			minvel=sqrt(miu^2+miv^2);
			if maxvel > amax,
				amax=maxvel;
			end
			if minvel < amin,
				amin=minvel;
			end
			ad=rem(amax,5);
			if ad ~=0,
				amax=amax+(5-ad);
			end
			ad=rem(amin,5);
			if ad ~=0,
				amin=amin-(5+ad);
			end
			eval(['tss(k)=min(wind1',num2str(k),'(:,1));']);
		else,
			tss(k)=1;
		end
	else,
		tss(k)=1;
	end %if
end
for i=[1 2],
	if i < 2,
		fprintf(fid,'axlabp,0,-1\n');
	else,
		fprintf(fid,'axlabp,-1,-1\n');
	end
	if i==1,
		fprintf(fid,'txlabp,1\n');
		fprintf(fid,'txlsze,0.09\n');
	else
		if i < 2,
			fprintf(fid,'txlabp,0\n');
		else,	
			fprintf(fid,'txlabp,-1\n');
			fprintf(fid,'txlsze,0.09\n');
		end
	end
	fprintf(fid,'axlint,1,1\n');
	fprintf(fid,'axnmtc,1,1\n');
	fprintf(fid,'axset,1,1,1,1\n');
	fprintf(fid,'yaxis,%f,%f,5\n',amin,amax);
	fprintf(fid,'taxis,10,on\n');
	fprintf(fid,'txnmtc,-1\n');
	fprintf(fid,'txlint,24,2\n');
	if i==2,
		fprintf(fid,'rlabs,1,90\n');
		fprintf(fid,'labs:nouser,1,-1.2,3.5,0,Wind Speed (m/s)\n');
	end
	fprintf(fid,'txtype,DAYS,HR\n');
% Now compute the start and stop time limits for the plot
%	yy=rem(year,100);
	yy=year;
	thig=time(6)+1+(time(4)+(time(5)/60))/24;
	tlow=thig-2;
%tlow=min(tc);
%thig=max(tc);
%if(thig-tlow) < 7,
%	thig=tlow+7;
%end
	if tlow < 0,
		if ileap==1,
			tlow=tlow+367;
		else,
			tlow=tlow+366;
		end
	end
	if tss(i) < 0,
		if ileap==1,
			tss(i)=tss(i)+367;
		else,
			tss(i)=tss(i)+366;
		end
	end

	for j=1:12,
		if (thig >mond(j))&(thig <= mond(j+1)),
			moh=j;
			if j==1,
				dah=fix(thig-mond(j));
			else,
				dah=fix(thig-mond(j))+1;
			end
			hoh=rem(thig,1);
			hoh=fix(hoh*24);
		end
		if (tlow > mond(j))&(tlow <= mond(j+1)),
			mol=j;
			if j==1,
			 dal=fix(tlow-mond(j));
			else,
			 dal=fix(tlow-mond(j))+1;
			end
			hol=rem(tlow,1);
			mil=hol;
			hol=fix(hol*24);
			mil=mil*24-hol;
			mil=fix(mil*60);
		end
		if (tss(i) > mond(j))&(tss(i) <= mond(j+1)),
			mos=j;
			if j==1,
				das=fix(tss(i)-mond(j));
			else,
				das=fix(tss(i)-mond(j))+1;
			end
			hos=rem(tss(i),1);
			mis=hos;
			hos=fix(hos*24);
			mis=mis*24-hos;
			mis=fix(mis*60);
		end
	end
	if(moh==1)&(dah < 2),
		yo=year-1;
%		yo=rem(yo,100);
	else,
		yo=yy;
	end
	if mol < 10,
		mol=['0',num2str(mol)];
	else,
		mol=num2str(mol);
	end
	if mos < 10,
		mos=['0',num2str(mos)];
	else,
		mos=num2str(mos);
	end
	if moh < 10,
		moh=['0',num2str(moh)];
	else,
		moh=num2str(moh);
	end
	if dal < 10,
		dal=['0',num2str(dal)];
	else,
		dal=num2str(dal);
	end
	if das < 10,
		das=['0',num2str(das)];
	else,
		das=num2str(das);
	end
	if dah < 10,
		dah=['0',num2str(dah)];
	else,
		dah=num2str(dah);
	end
	if hol < 10,
		hol=['0',num2str(hol)];
	else,
		hol=num2str(hol);
	end
	if hos < 10,
		hos=['0',num2str(hos)];
	else,
		hos=num2str(hos);
	end
	if hoh < 10,
		hoh=['0',num2str(hoh)];
	else,
		hoh=num2str(hoh);
	end
	if mil < 10,
		mil=['0',num2str(mil)];
	else,
		mil=num2str(mil);
	end
	if mis < 10,
		mis=['0',num2str(mis)];
	else,
		mis=num2str(mis);
	end
	tstar=['W',num2str(yo),mol,dal,hol,'00'];
	tstop=['W',num2str(yy),moh,dah,hoh,'00'];
%	tstart=['W',num2str(yo),mol,dal,hol,mil];
	tstart=['W',num2str(yo),mos,das,hos,mis];
	fprintf(fid,'time,%s,%s,%s\n',tstar(:),tstop(:),tstart(:));
	fprintf(fid,'format,free\n');
	fprintf(fid,'ylab,@tr M%d\n',i);
%	if i==4,
%		fprintf(fid,'title,@tr Wind Speed (m/s)\n');
%	end
	if flid(i) > 0,
		eval(['jj=wind1',num2str(i),';']);
	else,
		jj=[];
	end
	if ((flid(i) > 0)&(isempty(jj)==0)),
		fprintf(fid,'skp,0,%swind1%d.dat\n',fpath(:),i);
	else,
		fprintf(fid,'skp,0,%sdummywind.dat\n',fpath(:));
	end
	fprintf(fid,'vars,1,,1,2\n');
	fprintf(fid,'rd\n');
	fprintf(fid,'plotv\n');
end
fprintf(fid,'exit\n');
fclose(fid);
quit;
