%function PlotVPData(HoursPast,varargin)

clear all
close all

%if (nargin ~= 1)
%   HoursPast = 4;
%end

current_time = clock;
outDir = 'SeaHorseFilesFirstDeploy';
files = dir([outDir '/*.cnv']);



nprof = length(files);


n = 0;
bsize = 0.25;
bufr= bsize/2;
bZ = [1:bsize:54];
BSal = repmat(NaN,length(bZ),nprof);
BTemp = BSal;
BFluor = BSal;
BOxygen = BSal;
BTrans = BSal;

for n = 1:50
    files(n).name
  fid = fopen([outDir '/' files(n).name]);
  
  %Determine appropriate info from text of each file.
  first_sample_time = [];
  while(isempty(first_sample_time))
     tline = fgets(fid);
     k = strfind(tline,'First Sample Time:');
     if(k>0)
         first_sample_time = tline(k+19:k+39);
     end
  end
 
  %Read Data
  RawData = textscan(fid, '%f %f %f %f %f %f %f %f %f','commentStyle','*');
  fclose(fid);
  
  %Bin Data
  time(n)=datenum(first_sample_time);
  
  %Remove points with bad pressure data
  cp=RawData{:,1}; idx=find(cp<0 | cp>60); cp(idx)=NaN;
  ct=RawData{:,2}; ct(idx)=NaN;
  cs=RawData{:,3}; cs(idx)=NaN;
  co=RawData{:,5}; co(idx)=NaN;
  cf=RawData{:,6}; cf(idx)=NaN;
  cb=RawData{:,7}; cb(idx)=NaN;
  
  
  
       for B=1:length(bZ);
           idx=find(cp>=bZ(B)-bufr & cp<bZ(B)+bufr);
            if ~isempty(idx)
                cdat=ct(idx); cdat(isnan(cdat))=[];
                if ~isempty(cdat); BTemp(B,n)=median(cdat); end
                cdat=cs(idx); cdat(isnan(cdat))=[];
                if ~isempty(cdat); BSal(B,n)=median(cdat); end
                cdat=cf(idx); cdat(isnan(cdat))=[];
                if ~isempty(cdat); BFluor(B,n)=median(cdat); end
                cdat=co(idx); cdat(isnan(cdat))=[];
                if ~isempty(cdat); BOxygen(B,n)=median(cdat); end
                cdat=cb(idx); cdat(isnan(cdat))=[];
                if ~isempty(cdat); BTrans(B,n)=median(cdat); end
            end
       end
end

%end  
  
f = figure('Visible','on');
[C,h] = contourf(time,bZ,BTemp,20);
set(h,'LineStyle','none')
foo = axis;
foo(3) = 0;
axis(foo);
set(gca,'xtick',[time(1):2:time(end)])
datetick('x','keeplimits','keepticks');

set(gca,'YDir','reverse');
colorbar;
ylabel('depth (m)');
xlabel('GMT');
title('Temperature (deg C)');

set(f,'Units','pixels');
rect = get(f,'Position');
rect(3) = 550;
rect(4) = 200;
set(f,'Position',rect);
set(f,'PaperPositionMode','auto');

print -dpng Temp
print -depsc2 Temp

f = figure('Visible','on');
[C,h] = contourf(time,bZ,BSal,20);
set(h,'LineStyle','none')
foo = axis;
foo(3) = 0;
axis(foo);
set(gca,'xtick',[time(1):2:time(end)])
datetick('x','keeplimits','keepticks');
set(gca,'YDir','reverse') ;
colorbar;
ylabel('depth (m)');
xlabel('GMT');
title('Salinity (PSU)');

set(f,'Units','pixels');
rect = get(f,'Position');
rect(3) = 550;
rect(4) = 200;
set(f,'Position',rect);
set(f,'PaperPositionMode','auto');

print -dpng Sal
print -depsc2 Sal

f = figure('Visible','on');
[C,h] = contourf(time,bZ,BFluor,20);
set(h,'LineStyle','none')
foo = axis;foo(3) = 0;axis(foo);
caxis([0 .75]);
set(gca,'xtick',[time(1):2:time(end)])
datetick('x','keeplimits','keepticks');
set(gca,'YDir','reverse') ;
colorbar;

ylabel('depth (m)');
xlabel('GMT');
title('Fluorometer (0-5V)');

set(f,'Units','pixels');
rect = get(f,'Position');
rect(3) = 550;
rect(4) = 200;
set(f,'Position',rect);
set(f,'PaperPositionMode','auto');

print -dpng Fluor
print -depsc2 Fluor


f = figure('Visible','on');
[C,h] = contourf(time,bZ,BOxygen,20);
set(h,'LineStyle','none')
foo = axis; foo(3) = 0; axis(foo);
caxis([0 .75]);
set(gca,'xtick',[time(1):2:time(end)])
datetick('x','keeplimits','keepticks');
set(gca,'YDir','reverse') ;
colorbar;

ylabel('depth (m)');
xlabel('GMT');
title('Oxygen (0-5V)');

set(f,'Units','pixels');
rect = get(f,'Position');
rect(3) = 550;
rect(4) = 200;
set(f,'Position',rect);
set(f,'PaperPositionMode','auto');

print -dpng Oxygen
print -depsc2 Oxygen

f = figure('Visible','on');
[C,h] = contourf(time,bZ,BTrans,20);
set(h,'LineStyle','none')
foo = axis; foo(3) = 0; axis(foo);
set(gca,'xtick',[time(1):2:time(end)])
datetick('x','keeplimits','keepticks');
set(gca,'YDir','reverse') ;
colorbar;

ylabel('depth (m)');
xlabel('GMT');
title('Transmissometer (0-5V)');

set(f,'Units','pixels');
rect = get(f,'Position');
rect(3) = 550;
rect(4) = 200;
set(f,'Position',rect);
set(f,'PaperPositionMode','auto');

print -dpng Transmissometer
print -depsc2 Transmissometer



disp(['Done at ' datestr(clock)]);
