%function PlotVPData(HoursPast,varargin)

clear all
close all

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

current_time = clock;


outDir = 'SeaHorseFiles';

  
cmd = ['!perl getSeaHorse.pl 4'];  %Just get the last few hours, this runs hourly, so that should do it.
eval(cmd);


%Convert .HEX files to .cnv files (makefile in outDir does that).
cmd = ['cd ',outDir]
eval(cmd);
cmd = ['!make']
eval(cmd);
cmd = ['cd ..']
eval(cmd);


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

n = 0;
for n = 1:length(files)
  fid = fopen([outDir '/' files(n).name]);
  
  %Determine appropriate info from text of each file.
  first_sample_time{n,1} = [];
  while(isempty(first_sample_time{n,1}))
     tline = fgets(fid);
     k = strfind(tline,'First Sample Time:');
     if(k>0)
         first_sample_time{n,1} = tline(k+19:k+39);
     end
  end
 
  %Read Data
  C{n,1} = textscan(fid, '%f %f %f %f %f %f %f %f %f','commentStyle','*');
  fclose(fid);
end


%Create timestamp estimate of each measurement, based on sample rate and
%start time.  Place result in last C field, it's unused anyway.
freq = 4;  %hz
for n = 1:length(files)
nn = datenum(first_sample_time{n,1});  %,'dd mmm yyyy HH:MM:SS');
for m = 1:length(C{n,1}{1})
C{n,1}{end}(m) = nn+m/(freq*60*60*24);
end
end


%Put data into arrays for plotting, etc.
data.time = []; data.press = []; data.temp = []; data.sal=[];
data.depth = []; data.pottemp = [];
data.aux0 = []; data.aux1 = []; data.aux2=[];
for n = 1:length(files)
 data.press = [data.press; cell2mat(C{n,1}(1))];
 data.temp = [data.temp; cell2mat(C{n,1}(2))];
 data.sal =  [data.sal; cell2mat(C{n,1}(3))];
 data.depth =  [data.depth; cell2mat(C{n,1}(4))];
 data.pottemp =  [data.pottemp; cell2mat(C{n,1}(5))];
 data.aux0 = [data.aux0; cell2mat(C{n,1}(6))];
 data.aux1 = [data.aux1; cell2mat(C{n,1}(7))];
 data.aux2 = [data.aux2; cell2mat(C{n,1}(8))];
 data.time = [data.time; cell2mat(C{n,1}(end))];
end


%Strip Bogus Datapoints
idx = find(data.depth > 55);
idx
data.time(idx) = [];
data.press(idx) = [];
data.temp(idx) = [];
data.sal(idx) = [];
data.depth(idx) = [];
data.pottemp(idx) = [];
data.aux0(idx) = [];
data.aux1(idx) = [];
data.aux2(idx) = [];

%Sort all into order of increasing time, in case file's were read in out of
%order.
[data.time,IX] = sort(data.time);
data.press = data.press(IX);
data.temp = data.temp(IX);
data.sal = data.sal(IX);
data.depth = data.depth(IX);
data.pottemp = data.pottemp(IX);
data.aux0 = data.aux0(IX);
data.aux1 = data.aux1(IX);
data.aux2 = data.aux2(IX);
clear IX;


%Grid and Plot
dt = 40;  %minutes
t0 = data.time(1);
tf = data.time(end);
depthMin = min(data.depth);
depthMax = max(data.depth);
dh = 0.5;  %meters

[XI,YI] = meshgrid(t0:dt/(60*24):tf,depthMin:dh:depthMax);

TempI = griddata(data.time,data.press,data.temp,XI,YI);
SalI = griddata(data.time,data.press,data.sal,XI,YI);
PotTempI = griddata(data.time,data.press,data.pottemp,XI,YI);
Aux0I = griddata(data.time,data.press,data.aux0,XI,YI);
Aux1I = griddata(data.time,data.press,data.aux1,XI,YI);
Aux2I = griddata(data.time,data.press,data.aux2,XI,YI);

f = figure('Visible','on')
[C,h] = contourf(XI,YI,TempI,20);
set(h,'LineStyle','none')
datetick('x','keeplimits');
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(XI,YI,SalI,20);
set(h,'LineStyle','none')
datetick('x','keeplimits');
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(XI,YI,PotTempI,20);
%set(h,'LineStyle','none')
%datetick('x','keeplimits');
%set(gca,'YDir','reverse') ;
%colorbar;

%ylabel('depth (m)');
%xlabel('GMT');
%title('Potential 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 PotTemp
%print -depsc2 PotTemp


f = figure('Visible','on');
[C,h] = contourf(XI,YI,Aux0I,20);
set(h,'LineStyle','none')
datetick('x','keeplimits');
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(XI,YI,Aux2I,20);
set(h,'LineStyle','none')
datetick('x','keeplimits');
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


!cp Temp.png W:/MOOSVP/.
!cp Sal.png W:/MOOSVP/.
!cp PotTemp.png W:/MOOSVP/.
!cp Fluor.png W:/MOOSVP/.
!cp Oxygen.png W:/MOOSVP/.
!cp Transmissometer.png W:/MOOSVP/.

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