function plot_spectra

%Program to plot variable number of spectra saved from the AIRS/MODIS
%collocation GUI program:  you must have saved spectra data from the
%collocation GUI program!

num = inputdlg('Enter number of spectra to be plotted:');
%Error check if they press cancel
while isempty(num)
	uiwait(msgbox('You may not cancel!','Number of Spectra','modal'));
	num = inputdlg('Enter number of spectra to be plotted:');
end
num = cell2mat(num);
num = str2num(num);
%Error check if they enter 0 or less
while num <= 0
	uiwait(msgbox('Invalid Entry Try Again!','Number of Spectra','modal'));
	num = inputdlg('Enter number of spectra to be plotted:');
	num = cell2mat(num);
        num = str2num(num);
end
for j=1:num
[fileName, dataPath]=uigetfile('*.txt','Select Spectra File');
datafile=[dataPath fileName];
%Error check if they hit cancel
while datafile == [0]
	uiwait(msgbox('You may not cancel!','Choose File','modal'));
	[fileName, dataPath]=uigetfile('*.txt','Select Spectra File');
	datafile=[dataPath fileName];
end
fid = fopen(datafile,'r');
if (fid < 0)
	error('Cannot open file');
end
%Reads in data and skips header, same for all files generated from
%AIRS/MODIS collocation
[wavenumber,bt]=textread(datafile,'%f %f','headerlines',9,'commentstyle', 'matlab');
fclose(fid);

%Eliminates bad data points not caught using bad AIRS channel flags
%(eg: the very cold/warm spikes in data) Using a difference of 40K 
%from one wn to the next as threshold
%num_wn=size(wavenumber);
%for i=1:num_wn-1
%diff=abs(bt(i)-bt(i+1));
%	if diff >= 40 && i ~= 1
%		bt(i+1)=NaN;
	%	wavenumber(i+1)=NaN;
%		i=i+1;
%Need to account for possibility that the first element in the bt
%array is out of whack, hoping that both the 2nd and 3rd elements
%are not bad, which should be true, otherwise you might have bigger problems
%	elseif diff >= 40 && i == 1
%		diff=abs(bt(i)-bt(i+2));
%		if diff >=40
%			bt(i)=NaN;
%			wavenumber(i)=NaN;
%		end
%	end
%end
doplot(wavenumber,bt,j);
end

%Code that plots the spectra
function doplot(wavenumber,bt,j)
persistent xlimits
persistent ylimits
color_arr=['r' 'b' 'g' 'k' 'c' 'm'];

hold on
h=plot(wavenumber,bt);
xlabel(['Wavenumber (cm^{-1})']);
ylabel('Brightness Temperature');
%Set X min and max to +/- 15 of actual limit (looks better IMHO)
xmin=(min(wavenumber)-15.0);
xmax=(max(wavenumber)+15.0);
if j == 1
	set(gca,'XLim', [xmin xmax]);
	xlimits=get(gca,'XLim');
end
%Series of statements that makes sure axes for plots contain the smallest min and largest
%max for any of the plots on the figure
if j > 1
	if xmin < xlimits(1) && xmax > xlimits(2)
		set(gca,'XLim', [xmin xmax]);
		 xlimits(1) = xmin;
		 xlimits(2) = xmax;
	elseif xmin < xlimits(1) && xmax <= xlimits(2)
		set(gca,'XLim', [xmin xlimits(2)]);
		xlimits(1) = xmin;
	elseif xmin >= xlimits(1) && xmax > xlimits(2)
		set(gca,'XLim', [xlimits(1) xmax]);
		xlimits(2) = xmax;
	else
		set(gca,'XLim', [xlimits(1) xlimits(2)]);
	end
end
%Set Y min and max to +/- 3% of actual limits (looks better IMHO)
ymin=(min(bt)-(min(bt)*0.03));
ymax=(max(bt)+(max(bt)*0.03));
if j == 1
	set(gca,'YLim', [ymin ymax]);
	ylimits=get(gca,'YLim');
end
%Series of statements that makes sure axes for plots contain the smallest min and largest
%max for any of the plots on the figure
if j > 1
	if ymin < ylimits(1) && ymax > ylimits(2)
		set(gca,'YLim', [ymin ymax]);
		 ylimits(1) = ymin;
		 ylimits(2) = ymax;
	elseif ymin < ylimits(1) && ymax <= ylimits(2)
		set(gca,'YLim', [ymin ylimits(2)]);
		ylimits(1) = ymin;
	elseif ymin >= ylimits(1) && ymax > ylimits(2)
		set(gca,'YLim', [ylimits(1) ymax]);
		ylimits(2) = ymax;
	else
		set(gca,'YLim', [ylimits(1) ylimits(2)]);
	end
end

if j <= 6 
	set(h,'Color',color_arr(j));
elseif j > 6
	new_j=mod(j,6);
	if new_j == 0
		new_j = 6;
	end
	set(h,'Color',color_arr(new_j));
end

