%function PlotVPData(HoursPast,varargin)

clear all
close all


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



ndays = 8;
profperday = 24;

nprof = ndays*profperday;
nfiles = length(files);

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



for n = 1:nprof
  n
  filenum =  nfiles-(nprof-n)
  files(filenum).name
  
  fid = fopen([outDir '/' files(filenum).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