 	function OUT = plotcalhistory(fname)
% PLOTCALHISTORY - Create plots showing the changes in calibration history for a given instrument
%
% WARNING: This function uses a hard wired path for convience. Changes to the path
%          \\tsunami.shore.mbari.org\oasis\cfg must be changed in this funcion
%
% Use as: plotcalhistory(n)
%
% Inputs: n = the instrument type and number 
%             For Example: 'satlant.012'  for Satlantic sn.12
%                          'specprr.19615' for the Spectorreadiometer sn 19615

% Brian Schlining
% 25 Aug 1999

calpath = '\\tsunami.shore.mbari.org\oasis\cfg';
files   = sortrows(getfname([calpath filesep fname '*']));
[r c]   = size(files);

value = 100;

for j = 1:r
   
   file        = deblank(files(j,:));		% Get the file name
   [p, n, ext] = fileparts(file);			% The extension contains the date
   OUT.date(j,1) = datenum(sscanf(ext(2:5),'%f'), sscanf(ext(6:7),'%f'), sscanf(ext(8:9),'%f'));
   
   IN = readcal([calpath filesep file]);
   
   for i = 1:length(IN)
      
      
      a = IN(i).coef(1);
      b = IN(i).coef(2);
      c = IN(i).coef(3);
      
      switch IN(i).type
      case 0
         volts = NaN;
      case 1
         volts = (value - c) / b;
      case 2
         volts = (value - c) / b;
      case 3
         volts = (value - c) / b;
      case 4
         volts = (value - c) / b;
      case 5
         volts = NaN;
      case 6
         volts = value / a * b + c;
      case 7
         volts = value / a / b + c;
      end
      
      if j == 1 									% Create structure out on first pass	
         OUT = setfield(OUT,IN(i).name,volts);
      else							
         m = strmatch(IN(i).name, fieldnames(OUT)); % See if the field exists on the second pass
         if isempty(m)										 % If not create it and fill it with NaN's
            [row col] = size(OUT.date);
            OUT = setfield(OUT,IN(i).name,[ones(r-1,c)*NaN; volts]);
         else
            dat = getfield(OUT,IN(i).name);
            OUT = setfield(OUT,IN(i).name,[dat; volts]);
         end
         
      end
      
      
   end  
   
end

fn = fieldnames(OUT);
row = length(fn);
Color = jet(row-1);
n = 0;
for i = 2:row
   cfn = char(fn(i));
   dat = getfield(OUT,cfn);
   pct = (dat - dat(1))./dat(1).*100;
   %fprintf(1,'%s %4i %4i\n',cfn,length(dat),length(pct));
   if length(dat) == length(OUT.date)
      n = n + 1;
      legendS{n} = cfn;
      pH(n) = plot(OUT.date,pct,'Color',Color(i-1,:));
      if i == 2
         hold on
      end
      plot(OUT.date,pct,'o','Color',Color(i-1,:),'markerSize',2);
      drawnow
   end
   
end
legend(pH,legendS,-1);
hold off
datetick('x')
xlabel('Date')
ylabel('% change from original calibration');
title(fname)
grid


