%function PlotVPData(HoursPast,varargin)

clear all
close all

%if (nargin ~= 1)
%   HoursPast = 4;
%end

current_time = clock;


outDir = 'SeaHorseFiles';
files = dir([outDir '/*.cnv']);


num_hours_back = 6*24;  %1 Week
  
%cmd = ['!perl getSeaHorse.pl 144'];
%eval(cmd);

cmd = ['cd ',outDir]
eval(cmd);
cmd = '/usr/bin/make -f makefile'
eval(cmd);
cmd = 'cd ..'
eval(cmd);


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

data
%Strip Bogus Datapoints
idx = find(data.depth > 55);
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
[C,h] = contourf(XI,YI,TempI,40);
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
[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
%[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
[C,h] = contourf(XI,YI,Aux1I,20);
set(h,'LineStyle','none')
datetick('x','keeplimits');
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
[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)]);