% Matlab plot rountine to monitor the
% athena sensors. It will plot imet and
% other logged data fields. Use data files 
% like kn01min.* and knyymmdd.* or oc01min.* 
% and ocyymmdd.* or at01min.* and atyymmdd.*

% Make sure you change the logfile home 
% directory setting according to the ship.
%
% dir = '/home/data/athena/asciilog/' ???
%		
% 		 	April 14, 1998
%

clear all;
close all;
%warning off;

% set up the athena asciilog home directory here
dir = '/home/data/athena/asciilog/';

% Enter filename by user 
finp=input('Enter asciilog file name ', 's');
filena =[dir, finp];
disp(['load file ',filena]);
fid = fopen(filena,'r');
if fid == -1 
    disp(['No such file - ',filena,'. Check the dir setting.']);
    break;
end

k= 0;
m= 0;
l = 0;
first_row = 0;
time_flag = 0;
imet_flag = 0;
true_wind = 0;
index = 0;
xstart = 0.0;
xtmp = 0.0;
shr=0.0;
smn = 0.0;
ssc = 0.0;
hcnt = 0;


% scan through the headers for variable names
while 1
    hcnt = hcnt + 1;
    line = fgetl(fid);
    if hcnt == 3
	[tmp,count] = sscanf(line,'%*s %d:%d:%d');
	if count == 3
		shr = tmp(1);
		smn = tmp(2);
		ssc = tmp(3);
	else
		shr = 0.0;
		smn = 0.0;
		ssc = 0.0;
	end
    end
    p = findstr(line,'|');
    if isempty(p) ~= 1 & p == 4
	index = sscanf(line, '%d|', 1);	
	name = sscanf(line, '%*d| %s %*s');
	% make the name 8 characters long by 
	% adding character ' ' at the end
	addnum= 8-length(name);
	if length(name) < 8
	    T(1:length(name))=name(1:length(name));
	    for j = length(name)+1:8
		T(j)=' ';
	    end
	    tname(index,:) = T;
	else
	    tname(index,:)=name(1:8);
	end
	% replace '_' character by '-'
	for j =1:8
	    if tname(index,j) == '_'
		tname(index,j) = '-';
	    end
	end
    end
    %  end of header and break out
    if isempty(line) == 1 & hcnt > 8  
        break;
    end
end

% read data lines
while  1
	line = fgetl(fid);
	% the end of file
	if line == -1
		break;
	end
	p = findstr(line,'|');
	if isempty(p) ~= 1 & p == 4
		index = sscanf(line, '%d|', 1);	
	
	 	if index == 3 
		     % get the logging time
			[tmp,count] = sscanf(line,'%*s %d:%d:%d');
			if count == 3
				time_flag = 1;
				shr=tmp(1);
				smn=tmp(2);
				ssc=tmp(3);
			else
				time_flag = 0;
			end
		elseif index == 20 
		     % for one line IMET data [wnd bar hum swr prc}
			imet_flag  = 1;
			[tmp,count]=sscanf(line,'%*s %*f %*f %f %*f %*f %f %*f %*f %*f; %f; %f %*f; %f; %*f %*f %f;');
			% set bad data points equal to value -99.9
			if count ~= 6
				tmp=[-99.9,-99.9,-99.9,-99.9,-99.9,-99.9];
			end
			for i=1:6
				val(m+i) = tmp(i);
			end
			varname(m+1,:)='IMET-WSP';
			varname(m+2,:)='IMET-WDR';
			varname(m+3,:)='IMET-BPR';
			varname(m+4,:)='IMET-HUM';
			varname(m+5,:)='IMET-SWR';
			varname(m+6,:)='IMET-PRC';
			m = m+6;
		elseif (index == 21 | index == 22 | ...
			index == 23 | index == 24 | ...
			index == 25 | index == 26 | index == 27 ...
			| index == 28 ) & imet_flag == 0
		     % if line 20 did not get logged then use
		     % separated IMET data field imet_pwx, imet_pwy
		     % imet_swx, imet_swy, imet_bpr
		     % imet_hum, imet_swr, imet_prc
			m = m+1;
			temp = sscanf(line,'%*s %f');
			if isempty(temp) == 1
				val(m) = -99.9;
			else
				val(m) = temp;
			end
			varname(m,:)= tname(index,:);
		elseif index == 4 | index == 5 ...
			| index == 29 | index == 30 ...
 			| index == 31 ...
 			| index == 34 | index == 35 
		     % all other single field data for gyro, spdlog
		     % imet_air, sstmp, sscnd, Fluord, PCO2 
			m = m+1;
			temp = sscanf(line,'%*s %f');
			if isempty(temp) == 1
				val(m) = -99.9;
			else
				val(m) = temp;
			end
			varname(m,:)= tname(index,:);
	        elseif index == 32
		     % for sea surface salinity
			m = m+1;
			temp = sscanf(line,'%*s %f, %*s');
			if isempty(temp) == 1
				val(m) = -99.9;
			else
				val(m) = temp;
			end
			varname(m,:)= tname(index,:);
		elseif index == 40
		     % for one line true_wind field (wind speed & dir)
			true_wind = 1;
			[tmp,count] = sscanf(line,'%*s %f, %f');
			if count ~= 2
				tmp = [-99.9,-99.9];
			end
			for i=1:2
				val(m+i) = tmp(i);
			end
			varname(m+1,:) = 'TRUE-WSP';
			varname(m+2,:) = 'TRUE-WDR';
			m = m + 2;
		elseif (index == 41 | index == 42) & true_wind == 0 
		    % if index 40 didn't get logged use
		    % seperated wind speed and wind dir fields
			m = m+1;
			temp = sscanf(line,'%*s %f');
			if temp == ''
				val(m) = -99.9;
			else
				val(m) = temp;
			end
			varname(m,:)= tname(index,:);
		end
	elseif isempty(line)  == 1  
		tnumrow = m;
		if first_row == 0
			data = val;
			xstart = shr + smn/60.0 + ssc/3600.0;
			x = xstart;
			xtmp = xstart;
			first_row = 1;
		else
			if time_flag == 1
			    	% use recorded time
			    	xtmp = shr + smn/60.0 + ssc/3600.0;
			elseif time_flag == 0
				% add 1 minute to the time
				xtmp = xtmp + 1/60.0;
			end
			x = [x, xtmp]; 
			data = [data; val];
		end 
		m = 0;
	end	
end
fclose(fid);


% if there is more than 8 variables, plot them with 2 figures

nplot = 1;
nrows(1) = tnumrow;
st(1) = 0;
if  tnumrow >= 5 
	nplot = 2;
	nrows(1) = ceil(tnumrow/2);
	nrows(2) = tnumrow - nrows(1);
	st(1)=0;
	st(2) = nrows(1);
end

if max(size(x)) >= 800
	msize=1;
else
	msize=2;
end

help1='Click the left mouse button to zoom in on the point under the mouse. ';
help2='Click the right mouse button to zoom out. ';
help3='You can also click and drag to zoom into an area.';

helpstr=[help1,help2,help3];

for i = 1:nplot
	figure(i);
	zoom('XON');
        top=uimenu(i,'label','Zoom');
        uimenu(top,'label','Help','callback','helpdlg(helpstr,''Help'')');
	if i == 1
		set(1,'Position',[15 20 480 680],'color','w');
	else
		set(2,'Position',[500 20 480 680],'color','w');
	end
	for j = 1:nrows(i) 
		subplot(nrows(i),1,j), plot(x,data(:,st(i)+j),'c-', ...
				x,data(:,st(i)+j),'b*','Markersize',msize);
		ymin=min(data(find(data(:,st(i)+j) > -99.9),st(i)+j)); 
		ymax=max(data(find(data(:,st(i)+j) < 2000.0),st(i)+j)); 
		dy = (ymax -ymin)/10.0;
                if isempty(ymin) == 1 & ymax == -99.9
                        % for all -99.9 bad data case
                        ymin = ymax;
                end
                if ymin == ymax
                        dy = 0.05;
                end

		set(gca, 'Ylim',[ymin-dy ymax+dy],'Xlim', ...
				[floor(min(x)) ceil(max(x))], ...
				'xcolor','k','Ycolor','k' );

                if ymin == ymax
			ymin= ymin-dy;
			ymax= ymax+dy;
                end

		if (ymax-ymin) < 0.15
			format='%6.2f';
		else
			format='%6.1f';
		end
		sy=sprintf(format,ymin);
		slab=sprintf(format, ymin + (ymax-ymin)/2);
		sy= [sy;slab];
		slab=sprintf(format,ymax);
		sy= [sy;slab];

		set(gca,'Ytick',[ymin,ymin+(ymax-ymin)/2,ymax]);
		set(gca, 'Yticklabels', sy);

		if nrows(i) <= 7
		    ylabel(varname(st(i)+j,:),'Fontsize',13, ...
				'FontName','times','color','k');
		else
		    ylabel(varname(st(i)+j,:),'Fontsize', 10, ...
				'FontName','times','color','k');
		end
			
		if j == 1
		    title(finp, 'Fontsize',20,'FontName','times','color','k');
		end
		if j == nrows(i)
		    xlabel('Hours (GMT)','Fontsize',15,'FontName', ...
					'times','color','k');
		end
	end
end
